Primâ - S Minimal Spanning Tree - Tutorialspoint - 230718 - 192027
Primâ - S Minimal Spanning Tree - Tutorialspoint - 230718 - 192027
Prim’s minimal spanning tree algorithm is one of the efficient methods to find the minimum
spanning tree of a graph. A minimum spanning tree is a subgraph that connects all the vertices
present in the main graph with the least possible edges and minimum cost (sum of the weights
assigned to each edge).
The algorithm, similar to any shortest path algorithm, begins from a vertex that is set as a root
and walks through all the vertices in the graph by determining the least cost adjacent edges.
Prim’s Algorithm
To execute the prim’s algorithm, the inputs taken by the algorithm are the graph G {V, E}, where
V is the set of vertices and E is the set of edges, and the source vertex S. A minimum spanning
tree of graph G is obtained as an output.
Algorithm
Declare an array visited[] to store the visited vertices and firstly, add the arbitrary root, say
S, to the visited array.
Check whether the adjacent vertices of the last visited vertex are present in the visited[]
array or not.
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_prims_minimal_spanning_tree.htm 1/6
7/18/23, 10:46 PM Primâs Minimal Spanning Tree | Tutorialspoint
If the vertices are not in the visited[] array, compare the cost of edges and add the least
cost edge to the output spanning tree.
The adjacent unvisited vertex with the least cost edge is added into the visited[] array and
the least cost edge is added to the minimum spanning tree output.
Steps 2 and 4 are repeated for all the unvisited vertices in the graph to obtain the full
minimum spanning tree output for the given graph.
Examples
Find the minimum spanning tree using prim’s method (greedy approach) for the graph
given below with S as the arbitrary root.
Solution
Step 1
Create a visited array to store all the visited vertices into it.
V = { }
The arbitrary root is mentioned to be S, so among all the edges that are connected to S we
need to find the least cost edge.
S → B = 8
V = {S, B}
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_prims_minimal_spanning_tree.htm 2/6
7/18/23, 10:46 PM Primâs Minimal Spanning Tree | Tutorialspoint
Step 2
Since B is the last visited, check for the least cost edge that is connected to the vertex B.
B → A = 9
B → C = 16
B → E = 14
V = {S, B, A}
Step 3
Since A is the last visited, check for the least cost edge that is connected to the vertex A.
A → C = 22
A → B = 9
A → E = 11
But A → B is already in the spanning tree, check for the next least cost edge. Hence, A → E is
added to the spanning tree.
V = {S, B, A, E}
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_prims_minimal_spanning_tree.htm 3/6
7/18/23, 10:46 PM Primâs Minimal Spanning Tree | Tutorialspoint
Step 4
Since E is the last visited, check for the least cost edge that is connected to the vertex E.
E → C = 18
E → D = 3
V = {S, B, A, E, D}
Step 5
Since D is the last visited, check for the least cost edge that is connected to the vertex D.
D → C = 15
E → D = 3
V = {S, B, A, E, D, C}
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_prims_minimal_spanning_tree.htm 4/6
7/18/23, 10:46 PM Primâs Minimal Spanning Tree | Tutorialspoint
Example
The final program implements Prim’s minimum spanning tree problem that takes the cost
adjacency matrix as the input and prints the spanning tree as the output along with the
minimum cost.
#include<stdio.h>
#include<stdlib.h>
#define inf 99999
#define MAX 10
int G[MAX][MAX] = {
{0, 19, 8},
{21, 0, 13},
{15, 18, 0}
};
int S[MAX][MAX], n;
int prims();
int main(){
int i, j, cost;
n = 3;
cost=prims();
printf("\nSpanning tree:\n");
for(i=0; i<n; i++) {
printf("\n");
for(j=0; j<n; j++)
printf("%d\t",S[i][j]);
}
printf("\n\nMinimum cost = %d", cost);
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_prims_minimal_spanning_tree.htm 5/6
7/18/23, 10:46 PM Primâs Minimal Spanning Tree | Tutorialspoint
return 0;
}
int prims(){
int C[MAX][MAX];
int u, v, min_dist, dist[MAX], from[MAX];
int visited[MAX],ne,i,min_cost,j;
Output
Spanning tree:
0 0 8
0 0 13
8 13 0
Minimum cost = 26
https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithms_prims_minimal_spanning_tree.htm 6/6