-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_clique_cover_backtracking.py
44 lines (40 loc) · 1.31 KB
/
min_clique_cover_backtracking.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
# Minimum clique cover
def bt(cl, v, z):
if v < n:
for i in range(1, v+2):
cl[v] = i; d[i].append(v); ok = 1
for j in range(len(d[i])-1):
if am[d[i][j]][v] < 1: ok = 0; break
if ok and max(cl) < z: z = bt(cl, v+1, z)
cl[v] = 0; d[i].pop()
elif (m:=max(cl)) < z: z = m
return z
def bt_show_clique(cl, v, z):
if v < n:
for i in range(1, v+2):
cl[v] = i; d[i].append(v); ok = 1
for j in range(len(d[i])-1):
if am[d[i][j]][v] < 1: ok = 0; break
if ok and max(cl) < z[0]: z = bt_show_clique(cl, v+1, z)
cl[v] = 0; d[i].pop()
elif (m:=max(cl)) < z[0]: z = (m, [*cl])
return z
if __name__ == '__main__':
am = [
[1, 0, 1, 1, 1, 1],
[0, 1, 0, 1, 1, 1],
[1, 0, 1, 0, 1, 1],
[1, 1, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1],
]
# Initial assignment of cliques - 0 means unassigned
n = len(am)
d = {i:[] for i in range(1, n+1)}; d[1].append(0)
cl = [1]+[0]*(n-1)
print(bt(cl, 1, n+1))
# If you need the cliques
n = len(am)
d = {i:[] for i in range(1, n+1)}; d[1].append(0)
cl = [1]+[0]*(n-1)
print(bt_show_clique(cl, 1, (n+1, None)))