We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
for i, j, w in edges[:]:
if i in uncovered and j in covered:
distance[i] = min(distance[i], w)
if j in uncovered and i in covered:
distance[j] = min(distance[j], w)
weight = min([i for i in distance.values()])
for k in distance.keys():
if distance[k] == weight:
next_vertex = k
break
covered.add(next_vertex)
uncovered.remove(next_vertex)
del distance[next_vertex]
if len(distance) == 0:
return weight
else:
return weight + add_a_vertex(covered, uncovered, edges, distance)
def prims(n, edges, start):
# Write your code here
Prim's (MST) : Special Subtree
You are viewing a single comment's thread. Return to all comments →
My naive implementation without using heap. It is still good to try this way at least once I guess :)
import sys sys.setrecursionlimit(3500)
def add_a_vertex(covered, uncovered, edges, distance):
def prims(n, edges, start): # Write your code here