-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph_coloring.py
More file actions
81 lines (59 loc) · 2.14 KB
/
graph_coloring.py
File metadata and controls
81 lines (59 loc) · 2.14 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Graph Coloring Algorithm
Graph coloring is the problem of assigning colors to vertices of a graph
such that no two adjacent vertices have the same color. This implementation
uses a greedy approach.
Time Complexity: O(V² + E) where V is vertices and E is edges
Space Complexity: O(V)
The minimum number of colors needed is called the chromatic number.
"""
def graph_coloring(graph):
"""
Colors a graph using greedy algorithm.
Args:
graph: Dictionary representing adjacency list {node: [neighbors]}
Returns:
Dictionary mapping vertices to colors {vertex: color}
"""
colors = {}
# Sort vertices by degree (descending) for better coloring
vertices = sorted(graph.keys(), key=lambda v: len(graph.get(v, [])), reverse=True)
for vertex in vertices:
# Find colors used by adjacent vertices
used_colors = {colors[neighbor] for neighbor in graph.get(vertex, [])
if neighbor in colors}
# Find the smallest color not used by neighbors
color = 0
while color in used_colors:
color += 1
colors[vertex] = color
return colors
def is_valid_coloring(graph, colors):
"""
Checks if a coloring is valid (no two adjacent vertices have same color).
Args:
graph: Dictionary representing adjacency list
colors: Dictionary mapping vertices to colors
Returns:
True if coloring is valid, False otherwise
"""
for vertex in graph:
for neighbor in graph.get(vertex, []):
if colors.get(vertex) == colors.get(neighbor):
return False
return True
# Example usage
if __name__ == "__main__":
# Example graph
graph = {
'A': ['B', 'C', 'D'],
'B': ['A', 'C'],
'C': ['A', 'B', 'D'],
'D': ['A', 'C']
}
print("Graph coloring:")
colors = graph_coloring(graph)
for vertex, color in colors.items():
print(f" {vertex}: Color {color}")
print(f"\nValid coloring: {is_valid_coloring(graph, colors)}")
print(f"Number of colors used: {max(colors.values()) + 1}")