algorithm - KeyError in Python Program but not in interpreter? -
here program trying implement prims algorithm minimum spanning tree. problem when execute program whole shows keyerror : 7 :
but when execute error causing portion in interpreter shows no error ?
my code :
edges = { 'a':{'d':1,'b':3}, 'b':{'a':3,'c':4}, 'c':{'b':4,'d':5,'a':2,'e':7}, 'd':{'a':1,'c':5,'e':6}, 'e':{'d':6,'c':7} } # here null represented -1 , infinity represented 1000 vertices = { 'a':[1000,-1], 'b':[1000,-1], 'c':[1000,-1], 'd':[1000,-1], 'e':[1000,-1] } def extractmin(): low = 'a' key in vertices.keys(): if vertices[low][0]>vertices[key][0]: low=key del vertices[low] return low vertices['a'][0]=0 while not(vertices=={}): u=extractmin() print(str(u)+" ") v in [x x in edges[u].keys()]: if v in vertices , edges[u][v]<vertices[v][0]: vertices[v][0]=edges[u][v]
well, problem in function extractmin
first delete key a
vertices
because a
minimum.
then, when calling again function extractmin
try delete again (since vertices[low][0]>vertices[key][0]
false
) delete a
, not exist in vertices
.
you not putting in while loop becuase of condition v in vertices
, know a
not there
Comments
Post a Comment