import sys
from sage.all import *
G = Graph([('a', 'b'),('b', 'c'), ('b','d')])
H = Graph([(1, 2), (2, 3) ])
print("\n\n Graph G \n")
G.show()
print("\n\n Graph H \n\n")
H.show()
Pg = Set (G.vertices() + G.edges(labels=False));
Ph = Set (H.vertices() + H.edges(labels=False));
X = FiniteSetMaps(Pg, Ph);
Z = Set(X)
HomS = Set([]);
for f in Z:
s = True
for j in G.vertices():
if f(j)in H.vertices():
s=s and True
else:
s=s and False
for i in G.edges(labels=False):
u=i[0];
v=i[1];
if f(u)==f(v) and f(u)==f(i):
s=s and True
elif f(i)in H.edges(labels=False):
z=f(i);
if (z[0]==f(u) and z[1]==f(v)) or (z[1]==f(u) and z[0]==f(v)):
s=s and True
else:
s=s and False
else: s=s and False
if s==True:
HomS = HomS + Set([f])
Homfg = FiniteEnumeratedSet(HomS)
f = Homfg.random_element()
print("\n\n Morphism f \n\n")
f
g = Homfg.random_element()
print("\n\n Morphism g \n\n")
g
Eq = Graph()
for j in G.vertices():
if f(j)==g(j):
Eq.add_vertex(j)
for j in G.edges(labels=False):
u=j[0]
v=j[1]
if f(j)==g(j) and j[0] in Eq.vertices() and j[1] in Eq.vertices():
Eq.add_edge(j)
print("\n\n Graph Eq \n\n")
Eq.show()
Coeq = Graph(multiedges=True, loops=True)
Gdone = Set([])
Hdone = Set([])
for p in H.vertices():
if p not in Hdone:
Gtemp=Set([])
Htemp=Set([p])
s = False
while s == False:
Gcheck=Gtemp
Hcheck=Htemp
for x in Pg:
if f(x) in Htemp:
Gtemp = Gtemp + Set([x])
if g(x) in Htemp:
Gtemp = Gtemp + Set([x])
for p in Gtemp:
Htemp = Htemp + Set([f(p), g(p)])
if (Gtemp == Gcheck) and (Htemp == Hcheck):
s = True
Gdone = Gdone + Gtemp
Hdone = Hdone + Htemp
Coeq.add_vertex(Htemp)
for p in H.edges(labels=False):
if p not in Hdone:
Gtemp=Set([])
Htemp=Set([p])
s = False
while s == False:
Gcheck=Gtemp
Hcheck=Htemp
for x in Pg:
if f(x) in Htemp:
Gtemp = Gtemp + Set([x])
if g(x) in Htemp:
Gtemp = Gtemp + Set([x])
for p in Gtemp:
Htemp = Htemp + Set([f(p), g(p)])
if (Gtemp == Gcheck) and (Htemp == Hcheck):
s = True
Gdone = Gdone + Gtemp
Hdone = Hdone + Htemp
e=Htemp[0]
for v in Coeq.vertices():
if e[0] in v:
a = v
if e[1] in v:
b = v
Coeq.add_edge(a, b, Htemp)
print("\n\n Graph Coeq \n\n")
Coeq.show()