Repository navigation
Expand file tree
/
Copy pathDsatur.py
More file actions
38 lines (30 loc) · 1.41 KB
/
Copy pathDsatur.py
File metadata and controls
38 lines (30 loc) · 1.41 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
def dsatur(graph):
"""
DSATUR algorithm for graph coloring.
Parameters:
graph (dict): The adjacency list of the graph.
Returns:
dict: A dictionary mapping vertices to their assigned color.
"""
colors = {v: -1 for v in graph} # -1 means uncolored
saturation_degree = {v: 0 for v in graph} # Tracks saturation degree
degrees = {v: len(neighbors) for v, neighbors in graph.items()} # Vertex degrees
def update_saturation(vertex):
adjacent_colors = {colors[neighbor] for neighbor in graph[vertex] if colors[neighbor] != -1}
saturation_degree[vertex] = len(adjacent_colors)
uncolored_vertices = set(graph.keys())
while uncolored_vertices:
# Select the vertex with the highest saturation degree, breaking ties with degree
vertex = max(uncolored_vertices, key=lambda v: (saturation_degree[v], degrees[v]))
uncolored_vertices.remove(vertex)
# Update saturation degree of its neighbors
for neighbor in graph[vertex]:
if neighbor in uncolored_vertices:
update_saturation(neighbor)
# Assign the smallest available color
neighbor_colors = {colors[neighbor] for neighbor in graph[vertex] if colors[neighbor] != -1}
color = 0
while color in neighbor_colors:
color += 1
colors[vertex] = color
return colors