-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathgraph_coloring.py
48 lines (34 loc) · 982 Bytes
/
graph_coloring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import doctest
def graph_coloring(graph: list[list[int]]) -> list[int]:
"""
Graph coloring algorithm using a greedy approach.
Parameters:
- graph (List[List[int]])
Returns:
- list[int]
Example:
>>> graph_coloring([[1, 2, 3], [0, 2], [0, 1, 3], [0, 2]])
[0, 1, 2, 1]
>>> graph_coloring([[1], [0, 2], [1]])
[0, 1, 0]
"""
num_vertices = len(graph)
result = [-1] * num_vertices
result[0] = 0
available_colors = [True] * num_vertices
for u in range(1, num_vertices):
for i in graph[u]:
if result[i] != -1:
available_colors[result[i]] = False
cr = 0
while cr < num_vertices:
if available_colors[cr]:
break
cr += 1
result[u] = cr
for i in graph[u]:
if result[i] != -1:
available_colors[result[i]] = True
return result
if __name__ == "__main__":
doctest.testmod()