Hi! I am currently developing a custom algorithm that implements the Havel-Hakimi algorithm choosing a node randomly. Unfortunately I have run into a logical error that is hard for me to trace (since I am a beginner in programming).I would appreciate it if someone could help me spot the issue. What happens is I get the correct sequence returned sometimes, but other times not
import networkx as nx
import matplotlib.pyplot as plt
import random
def nodes_connected(G,u,v):
return u in G.neighbors(v)
def hh_graph(seq):
if not(nx.is_graphical(seq)):
print("Sequence is not graphical")
else:
random.shuffle(seq)
G = nx.Graph()
G.add_nodes_from(range(1,len(seq)+1))
for i in range(0,len(seq)):
j=seq[i]
seq[i]=0
while j>0:
k=0
done=False
while k<len(seq) and done==False :
if seq[k]==max(seq) and nodes_connected(G,i+1,k+1)==False:
G.add_edges_from([[i+1,k+1]])
seq[k]-=1
done=True
k+=1
j-=1
degree_sequence = [d for n, d in G.degree()]
print(f"Graph degree sequence {degree_sequence}")
A=[5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3]
B=[6, 3, 3, 3, 3, 2, 2, 2, 2, 2,1,1]
C=[4,3,2,1]
hh_graph(A)
hh_graph(B)
hh_graph(C)
What I have tried:
I have tried to focus on the loops where I think there might be an issue.I have tested different if cases and implementations,but still cant understand how it works half of the time and other times not