11"""
2- Graph Coloring also called "m coloring problem"
3- consists of coloring a given graph with at most m colors
4- such that no adjacent vertices are assigned the same color
2+ Graph Coloring ( also called the "m coloring problem") is the problem of
3+ assigning at most 'm' colors to the vertices of a graph such that
4+ no two adjacent vertices share the same color.
55
66Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
77"""
@@ -11,22 +11,46 @@ def valid_coloring(
1111 neighbours : list [int ], colored_vertices : list [int ], color : int
1212) -> bool :
1313 """
14+ Check if a given vertex can be assigned the specified color
15+ without violating the graph coloring constraints (i.e., no two adjacent vertices
16+ have the same color).
17+
18+ Procedure:
1419 For each neighbour check if the coloring constraint is satisfied
1520 If any of the neighbours fail the constraint return False
1621 If all neighbours validate the constraint return True
1722
18- >>> neighbours = [0,1,0,1,0]
23+ Parameters:
24+ neighbours: The list representing which vertices
25+ are adjacent to the current vertex.
26+ 1 indicates an edge between the current vertex
27+ and the neighbour.
28+ colored_vertices: List of current color assignments for all vertices
29+ (-1 means uncolored).
30+ color: The color we are trying to assign to the current vertex.
31+
32+ Returns:
33+ True if the vertex can be safely colored with the given color,
34+ otherwise False.
35+
36+ Examples:
37+ >>> neighbours = [0, 1, 0, 1, 0]
1938 >>> colored_vertices = [0, 2, 1, 2, 0]
20-
2139 >>> color = 1
2240 >>> valid_coloring(neighbours, colored_vertices, color)
2341 True
2442
2543 >>> color = 2
2644 >>> valid_coloring(neighbours, colored_vertices, color)
2745 False
46+
47+ >>> neighbors = [1, 0, 1, 0]
48+ >>> colored_vertices = [-1, -1, -1, -1]
49+ >>> color = 0
50+ >>> valid_coloring(neighbors, colored_vertices, color)
51+ True
2852 """
29- # Does any neighbour not satisfy the constraints
53+ # Check if any adjacent vertex has already been colored with the same color
3054 return not any (
3155 neighbour == 1 and colored_vertices [i ] == color
3256 for i , neighbour in enumerate (neighbours )
@@ -37,7 +61,7 @@ def util_color(
3761 graph : list [list [int ]], max_colors : int , colored_vertices : list [int ], index : int
3862) -> bool :
3963 """
40- Pseudo-Code
64+ Recursive function to try and color the graph using backtracking.
4165
4266 Base Case:
4367 1. Check if coloring is complete
@@ -51,6 +75,20 @@ def util_color(
5175 2.4. if current coloring leads to a solution return
5276 2.5. Uncolor given vertex
5377
78+ Parameters:
79+ graph: Adjacency matrix representing the graph.
80+ graph[i][j] is 1 if there is an edge
81+ between vertex i and j.
82+ max_colors: Maximum number of colors allowed (m in the m-coloring problem).
83+ colored_vertices: Current color assignments for each vertex.
84+ -1 indicates that the vertex has not been colored
85+ yet.
86+ index: The current vertex index being processed.
87+
88+ Returns:
89+ True if the graph can be colored using at most max_colors, otherwise False.
90+
91+ Examples:
5492 >>> graph = [[0, 1, 0, 0, 0],
5593 ... [1, 0, 1, 0, 1],
5694 ... [0, 1, 0, 1, 0],
@@ -67,43 +105,68 @@ def util_color(
67105 >>> util_color(graph, max_colors, colored_vertices, index)
68106 False
69107 """
70-
71- # Base Case
108+ # Base Case: If all vertices have been assigned a color, we have a valid solution
72109 if index == len (graph ):
73110 return True
74111
75- # Recursive Step
76- for i in range (max_colors ):
77- if valid_coloring ( graph [ index ], colored_vertices , i ):
78- # Color current vertex
79- colored_vertices [index ] = i
80- # Validate coloring
112+ # Try each color for the current vertex
113+ for color in range (max_colors ):
114+ # Check if it's valid to color the current vertex with 'color'
115+ if valid_coloring ( graph [ index ], colored_vertices , color ):
116+ colored_vertices [index ] = color # Assign color
117+ # Recur to color the rest of the vertices
81118 if util_color (graph , max_colors , colored_vertices , index + 1 ):
82119 return True
83- # Backtrack
120+ # Backtrack if no solution found with the current assignment
84121 colored_vertices [index ] = - 1
85- return False
122+
123+ return False # Return False if no valid coloring is possible
86124
87125
88126def color (graph : list [list [int ]], max_colors : int ) -> list [int ]:
89127 """
90- Wrapper function to call subroutine called util_color
91- which will either return True or False.
92- If True is returned colored_vertices list is filled with correct colorings
93-
128+ Attempt to color the graph with at most max_colors colors such that no two adjacent
129+ vertices have the same color.
130+ If it is possible, returns the list of color assignments;
131+ otherwise, returns an empty list.
132+
133+ Parameters:
134+ graph: Adjacency matrix representing the graph.
135+ max_colors: Maximum number of colors allowed.
136+
137+ Returns:
138+ List of color assignments if the graph can be colored using max_colors.
139+ Each index in the list represents the color assigned
140+ to the corresponding vertex.
141+ If coloring is not possible, returns an empty list.
142+
143+ Examples:
94144 >>> graph = [[0, 1, 0, 0, 0],
95145 ... [1, 0, 1, 0, 1],
96146 ... [0, 1, 0, 1, 0],
97147 ... [0, 1, 1, 0, 0],
98148 ... [0, 1, 0, 0, 0]]
99-
100149 >>> max_colors = 3
101150 >>> color(graph, max_colors)
102151 [0, 1, 0, 2, 0]
103152
104153 >>> max_colors = 2
105154 >>> color(graph, max_colors)
106155 []
156+
157+ >>> graph = [[0, 1], [1, 0]] # Simple 2-node graph
158+ >>> max_colors = 2
159+ >>> color(graph, max_colors)
160+ [0, 1]
161+
162+ >>> graph = [[0, 1, 1], [1, 0, 1], [1, 1, 0]] # Complete graph of 3 vertices
163+ >>> max_colors = 2
164+ >>> color(graph, max_colors)
165+ []
166+
167+ >>> max_colors = 3
168+ >>> color(graph, max_colors)
169+ [0, 1, 2]
107170 >>> color([], 2) # empty graph
108171 []
109172 >>> color([[0]], 1) # single node, 1 color
@@ -113,9 +176,11 @@ def color(graph: list[list[int]], max_colors: int) -> list[int]:
113176 >>> color([[0, 1], [1, 0]], 2) # 2 nodes, 2 colors (possible)
114177 [0, 1]
115178 """
179+ # Initialize all vertices as uncolored (-1)
116180 colored_vertices = [- 1 ] * len (graph )
117181
182+ # Use the utility function to try and color the graph starting from vertex 0
118183 if util_color (graph , max_colors , colored_vertices , 0 ):
119- return colored_vertices
184+ return colored_vertices # The successful color assignment
120185
121- return []
186+ return [] # No valid coloring is possible
0 commit comments