CoCalc Shared FilesRandom(Co)EqPublic.sagews
Author: Tien Chih
Views : 6
import sys
from sage.all import *

#Graphs G and H  G is the Domain, H is the coDomain.
G = Graph([('a', 'b'),('b', 'c'), ('b','d')])
H = Graph([(1, 2), (2, 3) ])

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#PLEASE DO NOT EDIT ANYTHING AFTER THIS LINE!
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

print("\n\n Graph G \n")
G.show()

print("\n\n Graph H \n\n")
H.show()

#Creating the Part Sets for each graph

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

#Checking whether f is a graph morphism (vertices)

for j in G.vertices():
if f(j)in H.vertices():
s=s and True
else:
s=s and False

#Checking whether f is a graph morphism (edges)

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

#Adds f to HomS if f is a morphism
if s==True:
HomS = HomS + Set([f])

#Picking Random Morphisms

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 is the equalizer graph

Eq = Graph()

#Creating the equalizer

for j in G.vertices():
if f(j)==g(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():

print("\n\n Graph Eq \n\n")

Eq.show()

#Coeq is the co-equalizer graph

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

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

print("\n\n Graph Coeq \n\n")

Coeq.show()