OneCompiler

Prims Algo

172

import numpy as np
vertices = int(input("Please enter the number of vertices"))
adjacency_matrix = {}
char_val = 65
global shortest
shortest = []
for i in range(vertices):
print("Please enter the row values")

adjacency_matrix[chr(char_val)] = np.asarray(list(map(int,input().split())))
char_val += 1

print(adjacency_matrix)

def minvaloflst(lst):
global shortest
minval = -1
while(1):
minval = np.min(i[np.nonzero(i)])
position = np.where(i==minval)[0][0]
if chr((position+97)) in shortest:
lst[position] = 0
elif np.max(lst)==0:
break
else:
return(minval, chr((np.where(i==minval)[0][0])+97))

def shortestpath(adjacency_matrix, vertices):
global shortest
while(len(shortest) <= vertices):
minvaloflst()