How Does The Prim's Algorithm Work?
How Does The Prim's Algorithm Work?
In this article, we will discuss the prim's algorithm. Along with the algorithm, we will also see
the complexity, working, example, and implementation of prim's algorithm.
Spanning tree - A spanning tree is the subgraph of an undirected connected graph.
Minimum Spanning tree - Minimum spanning tree can be defined as the spanning tree in which
the sum of the weights of the edge is minimum. The weight of the spanning tree is the sum of the
weights given to the edges of the spanning tree.
Prim's Algorithm is a greedy algorithm that is used to find the minimum spanning tree from a
graph. Prim's algorithm finds the subset of edges that includes every vertex of the graph such that
the sum of the weights of the edges can be minimized.
Prim's algorithm starts with the single node and explores all the adjacent nodes with all the
connecting edges at every step. The edges with the minimal weights causing no cycles in the
graph got selected.
Prim's algorithm is a greedy algorithm that starts from one vertex and continue to add
the edges with the smallest weight until the goal is reached. The steps to implement the
prim's algorithm are given as follows -
Step 1
Step 2
Step 3
Step 4
Step 5
So, the graph produced in step 5 is the minimum spanning tree of the given graph. The
cost of the MST is given below -
Algorithm
Step 1: Select a starting vertex
Step 2: Repeat Steps 3 and 4 until there are fringe vertices
Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum we
ight
Step 4: Add the selected edge and the vertex to the minimum spanning tree T
[END OF LOOP]
Step 5: EXIT
Data structure used for the minimum edge weight Time Complexity