Prims
Prims
ENROLLMENT NUMBER:220280152057
TOPIC:IMPLEMENTATION OF PRIMS ALGORITHM
ALGORITHM:
1)Initialize
-Choose a starting vertex (can be arbitrary) and add it to the minimum
spanning tree.
-Initialize a priority queue (min-heap) to store edges based on their weights.
EXAMPLE:
Step 2 - Now, we have to choose and add the shortest edge from vertex B.
There are two edges from vertex B that are B to C with weight 10 and edge B
to D with weight 4. Among the edges, the edge BD has the minimum weight.
So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the
other edges. In this case, the edges DE and CD are such edges. Add them to
MST and explore the adjacent of C, i.e., E and A. So, select the edge DE and
add it to the MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it
would create a cycle to the graph. So, choose the edge CA and add it to the
MST.
So, the graph produced in step 5 is the minimum spanning tree of the given
graph. The cost of the MST is 10 units