Posts

Showing posts from June, 2023

Python Project: Havel-Hakimi Algorithm

Image
 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)

Python Project: Finding Adjacency Matrix from a Graphic Sequence

Image
 Hey everyone. In this blog I would like to share a very small project in python. In this project we can calculate the adjacency matrix of a graphic sequence in a simple algorithm. The code is here: #Enter the sequence in the array gseq. It has to be a graphic sequence gseq = [4,4,2,2,2,1,1,0] n = len(gseq) #Next we define the number of rows and columns of the adjacency matrix. rows, cols = (n, n) arr = [[0 for i in range(cols)] for j in range(rows)] #starting algorithm for i in range(n): for j in range(n): if i==j: arr[i][j]=0 else: if gseq[i]>0 and gseq[j]>0: arr[i][j] = 1 arr[j][i] = 1 gseq[i] -= 1 gseq[j] -= 1 print(arr) ================================= I hope you will understand the program. If there is any error let me know in the comments.