-
Notifications
You must be signed in to change notification settings - Fork 28
/
FindHomomorphisms.py
executable file
·71 lines (62 loc) · 2.03 KB
/
FindHomomorphisms.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import more_itertools as miter
import itertools as iter
import networkx as nx
import string
# import matplotlib.pyplot as plt
G = nx.read_graph6('G.g6')
H = nx.read_graph6('H.g6')
# G = nx.petersen_graph()
# H = nx.complete_graph(3)
G_nodes = G.nodes()
H_nodes = H.nodes()
iterable = string.ascii_lowercase[0:len(G_nodes)]
is_homomorphism_found = False
for part in miter.set_partitions(iterable, len(H_nodes)):
is_correct_homomorphism = True
if is_homomorphism_found:
break
for p in part:
if not is_correct_homomorphism:
break
if len(p) == 1:
continue
all_combs = iter.combinations(p, 2)
for a_comb in all_combs:
v1 = ord(a_comb[0]) - 97
v2 = ord(a_comb[1]) - 97
if G.has_edge(v1, v2):
is_correct_homomorphism = False
break
if is_correct_homomorphism:
all_combs = iter.combinations(range(0,len(part)), 2)
for a_comb in all_combs:
if is_correct_homomorphism:
v1H = a_comb[0]
v2H = a_comb[1]
if not H.has_edge(v1H, v1H):
for p1 in part[a_comb[0]]:
if is_correct_homomorphism:
for p2 in part[a_comb[1]]:
v1G = ord(p1) - 97
v2G = ord(p2) - 97
if G.has_edge(v1G,v1G):
is_correct_homomorphism = False
break
if is_correct_homomorphism:
is_homomorphism_found = True
with open("homomorphism_G_H",'w') as f:
for p in part:
for a in p:
f.write(a)
f.write(' ')
f.flush()
# print([''.join(p) for p in part])
break
if not is_homomorphism_found:
with open("homomorphism_G_H",'w') as f:
f.write("not found")
f.flush()
f.close()
# nx.draw(G, with_labels=True)
# plt.draw()
# plt.show()