2020-04-30 21:23:52 +00:00
|
|
|
"""
|
|
|
|
Graph Coloring also called "m coloring problem"
|
|
|
|
consists of coloring given graph with at most m colors
|
|
|
|
such that no adjacent vertices are assigned same color
|
|
|
|
|
|
|
|
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
|
|
|
|
"""
|
2020-09-23 11:30:13 +00:00
|
|
|
from __future__ import annotations
|
2020-04-30 21:23:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def valid_coloring(
|
2020-09-23 11:30:13 +00:00
|
|
|
neighbours: list[int], colored_vertices: list[int], color: int
|
2020-04-30 21:23:52 +00:00
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
For each neighbour check if coloring constraint is satisfied
|
|
|
|
If any of the neighbours fail the constraint return False
|
|
|
|
If all neighbours validate constraint return True
|
|
|
|
|
|
|
|
>>> neighbours = [0,1,0,1,0]
|
|
|
|
>>> colored_vertices = [0, 2, 1, 2, 0]
|
2020-05-22 06:10:11 +00:00
|
|
|
|
2020-04-30 21:23:52 +00:00
|
|
|
>>> color = 1
|
|
|
|
>>> valid_coloring(neighbours, colored_vertices, color)
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> color = 2
|
|
|
|
>>> valid_coloring(neighbours, colored_vertices, color)
|
|
|
|
False
|
|
|
|
"""
|
|
|
|
# Does any neighbour not satisfy the constraints
|
|
|
|
return not any(
|
|
|
|
neighbour == 1 and colored_vertices[i] == color
|
|
|
|
for i, neighbour in enumerate(neighbours)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def util_color(
|
2020-09-23 11:30:13 +00:00
|
|
|
graph: list[list[int]], max_colors: int, colored_vertices: list[int], index: int
|
2020-04-30 21:23:52 +00:00
|
|
|
) -> bool:
|
2020-05-22 06:10:11 +00:00
|
|
|
"""
|
2020-04-30 21:23:52 +00:00
|
|
|
Pseudo-Code
|
|
|
|
|
|
|
|
Base Case:
|
2020-05-22 06:10:11 +00:00
|
|
|
1. Check if coloring is complete
|
2020-04-30 21:23:52 +00:00
|
|
|
1.1 If complete return True (meaning that we successfully colored graph)
|
|
|
|
|
|
|
|
Recursive Step:
|
|
|
|
2. Itterates over each color:
|
|
|
|
Check if current coloring is valid:
|
|
|
|
2.1. Color given vertex
|
|
|
|
2.2. Do recursive call check if this coloring leads to solving problem
|
|
|
|
2.4. if current coloring leads to solution return
|
|
|
|
2.5. Uncolor given vertex
|
|
|
|
|
|
|
|
>>> graph = [[0, 1, 0, 0, 0],
|
|
|
|
... [1, 0, 1, 0, 1],
|
|
|
|
... [0, 1, 0, 1, 0],
|
|
|
|
... [0, 1, 1, 0, 0],
|
|
|
|
... [0, 1, 0, 0, 0]]
|
|
|
|
>>> max_colors = 3
|
|
|
|
>>> colored_vertices = [0, 1, 0, 0, 0]
|
|
|
|
>>> index = 3
|
2020-05-22 06:10:11 +00:00
|
|
|
|
2020-04-30 21:23:52 +00:00
|
|
|
>>> util_color(graph, max_colors, colored_vertices, index)
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> max_colors = 2
|
|
|
|
>>> util_color(graph, max_colors, colored_vertices, index)
|
|
|
|
False
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Base Case
|
|
|
|
if index == len(graph):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Recursive Step
|
|
|
|
for i in range(max_colors):
|
|
|
|
if valid_coloring(graph[index], colored_vertices, i):
|
|
|
|
# Color current vertex
|
|
|
|
colored_vertices[index] = i
|
|
|
|
# Validate coloring
|
|
|
|
if util_color(graph, max_colors, colored_vertices, index + 1):
|
|
|
|
return True
|
|
|
|
# Backtrack
|
|
|
|
colored_vertices[index] = -1
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-09-23 11:30:13 +00:00
|
|
|
def color(graph: list[list[int]], max_colors: int) -> list[int]:
|
2020-05-22 06:10:11 +00:00
|
|
|
"""
|
2020-04-30 21:23:52 +00:00
|
|
|
Wrapper function to call subroutine called util_color
|
|
|
|
which will either return True or False.
|
|
|
|
If True is returned colored_vertices list is filled with correct colorings
|
2020-05-22 06:10:11 +00:00
|
|
|
|
2020-04-30 21:23:52 +00:00
|
|
|
>>> graph = [[0, 1, 0, 0, 0],
|
|
|
|
... [1, 0, 1, 0, 1],
|
|
|
|
... [0, 1, 0, 1, 0],
|
|
|
|
... [0, 1, 1, 0, 0],
|
|
|
|
... [0, 1, 0, 0, 0]]
|
|
|
|
|
|
|
|
>>> max_colors = 3
|
|
|
|
>>> color(graph, max_colors)
|
|
|
|
[0, 1, 0, 2, 0]
|
|
|
|
|
|
|
|
>>> max_colors = 2
|
|
|
|
>>> color(graph, max_colors)
|
|
|
|
[]
|
|
|
|
"""
|
|
|
|
colored_vertices = [-1] * len(graph)
|
|
|
|
|
|
|
|
if util_color(graph, max_colors, colored_vertices, 0):
|
|
|
|
return colored_vertices
|
|
|
|
|
|
|
|
return []
|