# this file ONLY contains graph_to_ideal, and is meant to be1# loaded from other files2# via the command load('func_graph_to_ideal.sage')3# see the file experiments.sagews for an example4#5# DESC: graph_to_ideal takes as input a graph (and related information)6# and returns as output the associated perfect matching ideal7#8# INPUT:9# g: a object of sage graph class10#11# OUTPUT:12# P: a F_2[x_1,...x_v], a polynomial ring over F2 with one variable per vertex13# I: a list of the polynomials in the perfect matching ideal14#15def graph_to_ideal(g,o,r):16print "Entering graph_to_ideal... "17#print "G: " + str(G)18print "num_vertices = " + str(g.order())19print "num_edges = " + str(g.size())2021P = PolynomialRing(FiniteField(2), g.order(), 'x', order="lex")22#I = list(); # create an empty list2324# the incidence matrix is a (# of vertices) x (# of edges matrix)25# where A(i,j) = 1 if vertex i is incident on edge j, and 0 otherwise26A = g.adjacency_matrix()27#print str(A)28# this is just for debugging29debug = 130if debug == 1:31count = 0;32for e in g.edge_iterator():33#print "x[" + str(count) + "]: " + str(e)34count = count + 1;35#end for36#end if debug3738if o==0 and r==0:39# establish the ring containing the ideal (this will be returned)40# number of variables is number of edges41P = PolynomialRing(FiniteField(2), g.order(), 'x', order="lex") # over F242x = P.gens();43print str(type(P))44I = list(); # create an empty list45# for each vertice, create polynomial x^3-146for i in range(0, g.order()): # for each vertex in the graph, cr47I.append(x[i]^3+1)48#print("I: ") + str(I)49#print "Constructing polynomials, one per vertice" + str(x[i]^3-1)50for j in range(i+1, g.order()):51if A[i,j] == 1:52I.append(x[i]^2+x[i]*x[j]+x[j]^2)53#print("I: ") + str(I)5455if o==0 and r==1:56# establish the ring containing the ideal (this will be returned)57# number of variables is number of edges58P = PolynomialRing(QQ, g.order(), 'x', order="lex") # over the rationals59x = P.gens();60#print str(type(P))61I = list(); # create an empty list62for i in range(0, g.order()): # for each vertex in the graph63I.append(x[i]^3-1)64for j in range(i+1, g.order()):65if A[i,j] == 1:66I.append(x[i]^2+x[i]*x[j]+x[j]^2)67#68#endif69#end for ee70#endif71#end for edges72#end for vertices7374if o==1 and r==0:75# establish the ring containing the ideal (this will be returned)76# number of variables is number of edges77P = PolynomialRing(FiniteField(2), g.order(), 'x', order="deglex") # over F278x = P.gens();79#print str(type(P))80I = list(); # create an empty list81for i in range(0, g.order()): # for each vertex in the graph82I.append(x[i]^3+1)83for j in range(i+1, g.order()):84if A[i,j] == 1:85I.append(x[i]^2+x[i]*x[j]+x[j]^2)86#87#endif88#end for ee89#endif90#end for edges91#end for vertices9293if o==1 and r==1:94# establish the ring containing the ideal (this will be returned)95P = PolynomialRing(QQ, g.order(), 'x', order="deglex") # over the rationals96x = P.gens();97#print str(type(P))98I = list(); # create an empty list99for i in range(0, g.order()): # for each vertex in the graph100I.append(x[i]^3-1)101for j in range(i+1, g.order()):102if A[i,j] == 1:103I.append(x[i]^2+x[i]*x[j]+x[j]^2)104#105#endif106#end for ee107#endif108#end for edges109#end for vertices110111if o==2 and r==0:112# establish the ring containing the ideal (this will be returned)113# number of variables is number of edges114P = PolynomialRing(FiniteField(2), g.order(), 'x', order="degrevlex") # over F2115x = P.gens();116#print str(type(P))117I = list(); # create an empty list118for i in range(0, g.order()): # for each vertex in the graph119I.append(x[i]^3+1)120for j in range(i+1, g.order()):121if A[i,j] == 1:122I.append(x[i]^2+x[i]*x[j]+x[j]^2)123#print("ideal: ") + str(I)124#end for ee125#endif126#end for edges127#end for vertices128129if o==2 and r==1:130P = PolynomialRing(QQ, g.order(), 'x', order="degrevlex")131x = P.gens();132print str(type(P))133I = list(); # create an empty list134for i in range(0, g.order()): # for each vertex in the graph135I.append(x[i]^3-1)136for j in range(i+1, g.order()):137if A[i,j] == 1:138I.append(x[i]^2+x[i]*x[j]+x[j]^2)139#print("ideal: ") + str(I) #140#endif141#end for ee142#endif143#end for edges144#end for vertices145146147# use the "dictionary" method return the variables148return {'P':P, 'I':I}149#enddef graph_to_ideal150151