n
as the number of nodes, mstCost
as 0, edgesUsed
as 0, inMST
as a boolean array, and minDist
with infinity values except for the first node (0).minDist[0]
to 0 to start from the first node.edgesUsed
is less than n
:
a. Find the node u
not in inMST
with the smallest value in minDist
.
b. Add minDist[u]
to mstCost
and increment edgesUsed
.
c. Mark u
as included in inMST
.
d. Update minDist
for all neighbors of u
if the edge to the neighbor is smaller than the current value in minDist
.mstCost
as the total cost of the MST.