First time here? Check out the FAQ!

Ask Your Question
0

Graph polynomial construction

asked 2 years ago

vidyarthi gravatar image

updated 2 years ago

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 2 years ago

vidyarthi gravatar image

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)
Preview: (hide)
link

Comments

1

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

Max Alekseyev gravatar imageMax Alekseyev ( 2 years ago )
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().

David Coudert gravatar imageDavid Coudert ( 2 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2 years ago

Seen: 215 times

Last updated: Mar 14 '23