| Download
Graph to ideal modified for two colour
Project: Algebraic Optimization
Path: graph_to_ideal_2col.sage
Views: 94# 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_e], a polynomial ring over F2 with one variable per edge13# 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();43#print 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]^2-x[i])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]+x[j]-1)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(CC, g.order(), 'x', order="lex") # over the rationals59x = P.gens();60#print str(type(P))61I = list(); # create an empty list62#print "Constructing boolean polynomials, one per edge"63# for each edge, create the polynomial eij^2 - eij64for i in range(0, g.order()): # for each vertex in the graph65I.append(x[i]^2-x[i])66for j in range(i+1, g.order()):67if A[i,j] == 1:68I.append(x[i]+x[j]-1)69#70#endif71#end for ee72#endif73#end for edges74#end for vertices7576if o==1 and r==0:77# establish the ring containing the ideal (this will be returned)78# number of variables is number of edges79P = PolynomialRing(FiniteField(2), g.order(), 'x', order="deglex") # over F280x = P.gens();81#print str(type(P))82I = list(); # create an empty list83#print "Constructing boolean polynomials, one per edge"84# for each edge, create the polynomial eij^2 - eij85for i in range(0, g.order()): # for each vertex in the graph86I.append(x[i]^2-x[i])87for j in range(i+1, g.order()):88if A[i,j] == 1:89I.append(x[i]+x[j]-1)90#91#endif92#end for ee93#endif94#end for edges95#end for vertices9697if o==1 and r==1:98# establish the ring containing the ideal (this will be returned)99P = PolynomialRing(CC, g.order(), 'x', order="deglex") # over the rationals100x = P.gens();101#print str(type(P))102I = list(); # create an empty list103#print "Constructing boolean polynomials, one per edge"104# for each edge, create the polynomial eij^2 - eij105for i in range(0, g.order()): # for each vertex in the graph106I.append(x[i]^2-x[i])107for j in range(i+1, g.order()):108if A[i,j] == 1:109I.append(x[i]+x[j]-1)110#111#endif112#end for ee113#endif114#end for edges115#end for vertices116117if o==2 and r==0:118# establish the ring containing the ideal (this will be returned)119# number of variables is number of edges120P = PolynomialRing(FiniteField(2), g.order(), 'x', order="degrevlex") # over F2121x = P.gens();122#print str(type(P))123I = list(); # create an empty list124#print "Constructing boolean polynomials, one per edge"125# for each edge, create the polynomial eij^2 - eij126for i in range(0, g.order()): # for each vertex in the graph127I.append(x[i]^2-x[i])128for j in range(i+1, g.order()):129if A[i,j] == 1:130I.append(x[i]+x[j]-1)131#132#endif133#end for ee134#endif135#end for edges136#end for vertices137138if o==2 and r==1:139P = PolynomialRing(CC, g.order(), 'x', order="degrevlex")140x = P.gens();141print str(type(P))142I = list(); # create an empty list143print("ideal")144#print "Constructing boolean polynomials, one per edge"145# for each edge, create the polynomial eij^2 - eij146for i in range(0, g.order()): # for each vertex in the graph147I.append(x[i]^2-x[i])148for j in range(i+1, g.order()):149if A[i,j] == 1:150I.append(x[i]+x[j]-1)151#152#endif153#end for ee154#endif155#end for edges156#end for vertices157158159# use the "dictionary" method return the variables160return {'P':P, 'I':I}161#enddef graph_to_ideal162163