Python Project: Havel-Hakimi Algorithm
Hey guys, this project in on Havel-Hakimi theorem. I have written a simple program to check whether a given degree sequence is graphical or not. Here's the code:
# First we take the length of the initial sequence
n = int(input("Enter length of the sequence: "))
# Next we enter the elements of the sequence
print("Enter the sequence d_n: ")
d = []
for i in range(n):
v = int(input())
d.append(v)
# next we run a loop, which is basically the algorithm.
while True:
d.sort(reverse=True)
m = d[0]
if m==0 and d[len(d)-1] == 0:
print("The sequence is graphical.")
break
elif m>len(d)-1:
print("The sequence is not graphical")
else:
for x in d:
if x<0:
print("The sequence is not graphical.")
break
d.pop(0)
for i in range(m):
d[i] = d[i]-1
print(d)
Comments
Post a Comment