-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoloring.py
More file actions
52 lines (44 loc) · 1.21 KB
/
coloring.py
File metadata and controls
52 lines (44 loc) · 1.21 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
"""Graph coloring."""
from collections import namedtuple as T
Graph = T("Graph", "v e")
def greedy_clique(G):
degdist = sorted([(len(G.e[v]), v) for v in G.v])
clique = set()
for deg, v in degdist:
if not clique:
clique.add(v)
continue
for c in clique:
if c not in G.e[v]:
break
else:
clique.add(v)
return clique
def find_best_vertex(G, k, coloring):
best = None
used = []
for v in G.v:
if v in coloring:
continue
c_used = set([coloring[u] for u in G.e[v] if u in coloring])
if len(c_used) == k:
return v, c_used # early abort ; used all colors
if best is None or len(c_used) > len(used):
best = v
used = c_used
return best, used
def colorable(G, k, coloring):
if len(coloring) == len(G.v):
return True
# find a vertex
v, used = find_best_vertex(G, k, coloring)
if len(used) == k:
return False
for color in range(k):
if color in used:
continue
coloring[v] = color
if colorable(G, k, coloring):
return True
del coloring[v]
return False