Python Project: Finding Adjacency Matrix from a Graphic Sequence
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.
Comments
Post a Comment