0% found this document useful (0 votes)
10 views5 pages

Solution To Code

The document contains Python functions for analyzing graphs represented as adjacency matrices and lists. It includes functions to calculate maximum degree, maximum weight, and convert directed graphs to undirected graphs. The main function demonstrates the usage of these functions with example graphs.

Uploaded by

Tasnim Faizah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Solution To Code

The document contains Python functions for analyzing graphs represented as adjacency matrices and lists. It includes functions to calculate maximum degree, maximum weight, and convert directed graphs to undirected graphs. The main function demonstrates the usage of these functions with example graphs.

Uploaded by

Tasnim Faizah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

def adj_mtx_max_deg(mat):

n=len(mat)
max_deg=0
for i in range(n):
deg=0
for j in range(n):
if mat[i][j] != 0:
deg+=1
if deg > max_deg:
max_deg=deg
return max_deg
def main():
# --- Adjacency Matrix Tests ---
print("Max Degree:")
mat = [[0 for _ in range(5)] for _ in range(5)]
mat[0][1] = mat[1][0] = 1
mat[0][2] = mat[2][0] = 1
mat[1][2] = mat[2][1] = 1
mat[1][3] = mat[3][1] = 1
mat[2][4] = mat[4][2] = 1
mat[3][4] = mat[4][3] = 1
print(adj_mtx_max_deg(mat)) # Output: 3
main()

def adj_mtx_max_wt(mat):
n=len(mat)
max_wt=0
for i in range(n):
total=0
for j in range(n):
total+=mat[i][j]
if total > max_wt:
max_wt=total
return max_wt
def main():
print("Max Weight:")
wmat = [[0 for _ in range(5)] for _ in range(5)]
wmat[0][1] = wmat[1][0] = 3
wmat[0][2] = wmat[2][0] = 2
wmat[1][2] = wmat[2][1] = 4
wmat[1][3] = wmat[3][1] = 5
wmat[2][4] = wmat[4][2] = 1
wmat[3][4] = wmat[4][3] = 6
print(adj_mtx_max_wt(wmat)) # Output: 12
main()

def adj_mtx_dir_wt(mat):
n=len(mat)
max_wt=0
for i in range(n):
total=0
for j in range(n):
total+=mat[i][j]
if total>max_wt:
max_wt=total
return max_wt
def main():
print("Directed Max Outgoing Weight:")
dmat = [[0 for _ in range(5)] for _ in range(5)]
dmat[0][1] = 3
dmat[0][2] = 2
dmat[1][2] = 4
dmat[1][3] = 5
dmat[2][4] = 1
dmat[3][4] = 6
print(adj_mtx_dir_wt(dmat)) #Output 9
main()

def adj_mtx_dirToUndir(mat):
n=len(mat)
new_mat=[]
i=0
while i<n:
row=[]
j=0
while j<n:
row+=[0]
j+=1
new_mat+=[row]
i+=1
i=0
while i<n:
j=0
while j < n:
if mat[i][j] != 0 or mat[j][i] != 0:
if mat[i][j] != 0:
new_mat[i][j] = mat[i][j]
else:
new_mat[i][j] = mat[j][i]
new_mat[j][i] = new_mat[i][j]
j+=1
i+=1
return new_mat
def main():
dmat = [[0 for _ in range(5)] for _ in range(5)]
dmat[0][1] = 3
dmat[0][2] = 2
dmat[1][2] = 4
dmat[1][3] = 5
dmat[2][4] = 1
dmat[3][4] = 6

print("Directed to Undirected:")
undirected = adj_mtx_dirToUndir(dmat)
for row in undirected:
print(row)

main()

# ADJACENCY LIST FUNCTIONS

def adj_list_max_deg(l1):
n=len(l1)
max_deg=0
for i in range(n):
deg=0
for j in range(len(l1[i])):
deg+=1
if deg > max_deg:
max_deg=deg
return max_deg
def main():
ulist = [[0 for _ in range(2)] for _ in range(5)]
ulist[0] = [1,2]
ulist[1] = [2,3]
ulist[2] = [4, -1]
ulist[3] = [4, -1]
ulist[4] = [-1, -1]
print(adj_mtx_max_deg(ulist)) #output 2
main()

def agj_list_weight(l2):
n=len(l2)
max_wt=0
for i in range(n):
total=0
for j in range(len(l2[i])):
total+=l2[i][j][1]
if total > max_wt:
max_wt = total
return max_wt
def main():
print("Max Weight:")
l2 = [
[[1, 3], [2, 2]],
[[2, 4], [3, 5]],
[[4, 1]],
[[4, 6]],
[]
]
print(agj_list_weight(l2)) # Output: 9
main()

def adj_list_dir_weight(l3):
n=len(l3)
max_wt=0
for i in range(n):
total=0
for j in range(len(l3[i])):
total+=l3[i][j][1]
if total > max_wt:
max_wt=total
return max_wt
def main():
print("Directed Max Outgoing Weight:")
l3 = [
[[1, 3], [2, 2]],
[[2, 4], [3, 5]],
[[4, 1]],
[[4, 6]],
[]
]
print(adj_list_dir_weight(l3)) # Output: 9
main()
def dir_to_undir(l4):
n = len(l4)
new=[]
i = 0
while i<n:
l5=[]
j=0
while j<n * 2:
l5+=[[0, 0]]
j+=1
new+=[l5]
i += 1
size=[]
i=0
while i<n:
size+=[0]
i+=1
i=0
while i<n:
j=0
while j<len(l4[i]):
dir = l4[i][j][0]
wt = l4[i][j][1]

temp=0
k=0
while k < size[i]:
if new[i][k][0] == dir:
temp=1
break
k+=1
if temp==0:
new[i][size[i]][0] = dir
new[i][size[i]][1] = wt
size[i] += 1

temp=0
k=0
while k < size[dir]:
if new[dir][k][0] == i:
temp=1
break
k += 1
if temp==0:
new[dir][size[dir]][0] = i
new[dir][size[dir]][1] = wt
size[dir]+=1
j+=1
i+=1

l6=[]
i=0
while i<n:
row=[]
j=0
while j < size[i]:
row += [[new[i][j][0], new[i][j][1]]]
j += 1
l6+=[row]
i+=1
return l6
def main():
print("Directed to Undirected:")
l4 = [
[[1, 3], [2, 2]],
[[2, 4], [3, 5]],
[[4, 1]],
[[4, 6]],
[]
]
new = dir_to_undir(l4)
for i in range(len(new)):
print("Node", i, "->", new[i])
main()

You might also like