# Graph polynomial construction

I wish to construct the graph polynomial (as given here) of any given graph. To do so, I wrote the following code. But, the output was null. What needs to be modified in the following code to get the appropriate graph polynomial (the product of binomials corresponding each edge):

 def grappoly(G):
R=PolynomialRing(ZZ,['x_'+str(k) for k in G.vertices()])
R.inject_variables()
for i in G.vertices():
for j in G.vertices():
P=1
if set((i,j)).intersection(set(G.edges()))==(i,j):
P=('x_'+str(i)-'x_'+str(j))*P
return P
X=graphs.CompleteBipartiteGraph(5,5)
grappoly(X)


Thanks beforehand.

edit retag close merge delete

Sort by ยป oldest newest most voted

The code required the following modifications after which it worked smoothly:

def grappoly(G):
R=PolynomialRing(ZZ,['x_'+str(k) for k in G.vertices()])
R.inject_variables()
P=1
for i,j in G.edges(labels=false):
P=(R('x_'+str(i))-R('x_'+str(j)))*P
return P
X=graphs.CompleteBipartiteGraph(5,5)
grappoly(X)

more

1

f'x_{k}' looks a bit nicer than 'x_'+str(k)

( 2023-03-14 16:57:38 +0200 )edit
1

Use G.edges(labels=False, sort=False) to avoid deprecation warning. You can also use for k in G instead of for k in G.vertices().

( 2023-03-14 18:47:45 +0200 )edit