Ask Your Question
0

Graph polynomial construction

asked 2023-03-14 11:59:36 +0100

vidyarthi gravatar image

updated 2023-03-14 12:16:42 +0100

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-03-14 12:54:42 +0100

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)
edit flag offensive delete link more

Comments

1

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

Max Alekseyev gravatar imageMax Alekseyev ( 2023-03-14 16:57:38 +0100 )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().

David Coudert gravatar imageDavid Coudert ( 2023-03-14 18:47:45 +0100 )edit

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: 2023-03-14 11:59:36 +0100

Seen: 175 times

Last updated: Mar 14 '23