i tried to make a 1 function prims algorithm in python but it doesn't seem to be working
def prim(edges):
inGraph = ['A']
discovered = []
results = []
counter = 0
while True:
tmplist = []
for i in edges:
if inGraph[counter] in i:
discovered.append(i)
tmplist.append(i)
for i in tmplist:
edges.remove(i)
tmp = []
for i in discovered:
tmp.append(i[2])
mini = tmp.index(min(tmp))
alpha = discovered[mini][0]
beta = discovered[mini][1]
if alpha not in inGraph or beta not in inGraph:
if alpha in inGraph:
inGraph.append(beta)
elif beta in inGraph:
inGraph.append(alpha)
results.append(discovered[mini])
discovered.pop(mini)
if discovered == []:
break
counter+=1
return (inGraph, results)
print [['A', 'B', 1], ['A', 'C', 102], ['B', 'C', 4], ['C', 'F', 2], ['B', 'F', 1], ['F', 'E', 1]] print prim([['A', 'B', 1], ['A', 'C', 102], ['B', 'C', 4], ['C', 'F', 2], ['B', 'F', 1], ['F', 'E', 1]])
the problem lies somewhere with discovered, either the if statement checking to see if it's empty or removing an element from it. I'm just not sure where to put it