You are on page 1of 6

Date:-

 ASSIGNMENT NO.-

 PROBLEM STATEMENT:-
Program in C to implement Prim’s algorithm for finding Minimum Spanning Tree for an
undirected graph.

 ALGORITHM:-
INPUT:
i. G, the pointer to the graph is in adjacency matrix form, whose maximum size is defined as
MAX.
ii. Vertices of the graph are labeled as 0,1,2,3,……..,n-1 where n be the total number of ver-
tices.

OUTPUT:
i. Spanning, the pointer to the Minimum Spanning Tree in adjacency matrix form of order
n*n.
ii. Total cost of the spanning tree.

DATA STRUCTURE:
An Array, visited[0,1,2,3,…..,n-1], of Boolean values.

 The steps for main() are-


STEP 1:
Display “Enter no. of vertices: ”
STEP 2:
Read n
STEP 3:
Display “Enter the adjacency matrix: ”
STEP 4:
Repeat through step 5 to step 8 for i= 0 to n-1
STEP 5:
Repeat through step 6 and step 7 for j= 0 to n-1
STEP 6:
Read G[i][j]
STEP 7:
Increment the value of j by 1
[End of inner for loop]
STEP 8:
Increment the value of i by 1
[End of outer for loop]
STEP 9:
Method prims() is called and return value is stored in total_cost
STEP 10:
Display “Spanning Tree Matrix:”
STEP 11:
Repeat through step 12 to step 16 for i= 0 to n-1
STEP 12:
Display a new line
STEP 13:
Repeat through step 14 and step 15 for j= 0 to n-1
STEP 14:

Page No.-
Date:-

Display spanning[i][j]
STEP 15:
Increment the value of j by 1
[End of inner for loop]
STEP 16:
Increment the value of i by 1
[End of outer for loop]
STEP 17:
Display “Total cost of spanning tree=”,total_cost
STEP 18:
End.

 The steps for prims() are-


STEP 1:
Repeat through step 2 to step 9 for i= 0 to n-1
STEP 2:
Repeat through step 3 to step 8 for j= 0 to n-1
STEP 3:
Check if G[i][j]=0 then continue
else go to step 5
STEP 4:
Assign infinity to the variable cost[i][j]
STEP 5:
else
STEP 6:
Assign the value of G[i][j] to cost[i][j]
[End of if-else structure]
STEP 7:
Initialize spanning[i][j] with 0
STEP 8:
Increment the value of j by 1
[End of inner for loop]
STEP 9:
Increment the value of i by 1
[End of outer for loop]
STEP 10:
i. Initialize distance[0] with 0
ii. Initialize visited[0] with 1
STEP 11:
Repeat through step 12 and step 13 for i= 1 to n-1
STEP 12:
i. Assign the value of cost[0][i] to distance[i]
ii. Initialize from[i] with 0
iii. Initialize visited[i] with 0
STEP 13:
Increment the value of i by 1
[End of for loop]
STEP 14:
i. Initialize min_cost with 0
ii. Assign n-1 to no_of_edges

STEP 15:
Repeat through step 16 to step 26 while no_of_edges greater than 0 do
STEP 16:
Page No.-
Date:-

Assign infinity to min_distance


STEP 17:
Repeat through step 18 to step 20 for i= 1 to n-1
STEP 18:
Check if visited[i] =0 AND distance[i] less than min_distance then continue
STEP 19:
i. Assign the value o i to v
ii. Assign the value of distance[i] to min_distance
STEP 20:
Increment the value of i by 1
[End of for loop]
STEP 21:
i. Assign the value of from[v] to u
ii. Assign the value of distance[v] to spanning[u][v]
iii. Assign the value of distance[v] to spanning[v][u]
iv. Decrement the value of no_of_edges by 1
v. Initialize visited[v] with 1
STEP 22:
Repeat through step 23 to step 25 for i=1 to n-1
STEP 23:
Check if visited[i]=0 AND cost[i][v] less than distance[i] then continue
STEP 24:
i. Assign the value of cost[i][v] to distance[i]
ii. Assign the value of v to from[i]
[End of if structure]
STEP 25:
Increment the value of i by 1
[End of for loop]
STEP 26:
The value of cost[u][v] is added with min_cost and stored in min_cost
[End of while loop]
STEP 27:
The value of min_cost is returned
STEP 28:
End.

 SOURCE CODE:-
//To implement Prim’s algorithm for finding MST
#include<stdio.h>
#include<stdlib.h>
#define infinity 9999
#define MAX 20
int G[MAX][MAX],spanning[MAX][MAX],n;
int prims();
int main()
{
int i,j,total_cost;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);

Page No.-
Date:-

total_cost=prims();
printf("\nSpanning Tree Matrix:\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",spanning[i][j]);
}
printf("\n\nTotal cost of spanning tree=%d",total_cost);
return 0;
}
int prims()
{
int cost[MAX][MAX];
int u,v,min_distance,distance[MAX],from[MAX];
int visited[MAX],no_of_edges,i,min_cost,j;
for(i=0;i<n;i++) //create cost[][] matrix,spanning[][]
for(j=0;j<n;j++)
{
if(G[i][j]==0)
cost[i][j]=infinity;
else
cost[i][j]=G[i][j];
spanning[i][j]=0;
}
distance[0]=0; //initialise visited[],distance[] and from[]
visited[0]=1;
for(i=1;i<n;i++)
{
distance[i]=cost[0][i];
from[i]=0;
visited[i]=0;
}
min_cost=0; //cost of spanning tree
no_of_edges=n-1; //no. of edges to be added
while(no_of_edges>0)
{
min_distance=infinity; //find the vertex at minimum distance from the tree
for(i=1;i<n;i++)
if(visited[i]==0&&distance[i]<min_distance)
{
v=i;
min_distance=distance[i];
}
u=from[v];
spanning[u][v]=distance[v]; //insert the edge in spanning tree
spanning[v][u]=distance[v];
no_of_edges--;
visited[v]=1;
for(i=1;i<n;i++)
if(visited[i]==0&&cost[i][v]<distance[i])
{
distance[i]=cost[i][v]; //updated the distance[] array
from[i]=v;

Page No.-
Date:-

}
min_cost=min_cost+cost[u][v];
}
return(min_cost);
}

 INPUT OUTPUT:-
Set 1: Enter no. of vertices:7

Enter the adjacency matrix:

9999 4 5 2 9999 9999 9999


4 9999 9999 1 2 9999 9999
5 9999 9999 8 9999 1 9999
2 1 8 9999 3 4 7
9999 2 9999 3 9999 9999 9
9999 9999 1 4 9999 9999 6
9999 9999 9999 7 9 6 9999

Spanning Tree Matrix:


0 0 0 2 0 0 0
0 0 0 1 2 0 0
0 0 0 0 0 1 0
2 1 0 0 0 4 0
0 2 0 0 0 0 0
0 0 1 4 0 0 6
0 0 0 0 0 6 0
Total cost of spanning tree=16

Set 2: - Enter no. of vertices:7

Enter the adjacency matrix:

9999 28 9999 9999 9999 10 9999


28 9999 16 9999 9999 9999 14
9999 16 9999 12 9999 9999 9999
9999 9999 12 9999 22 9999 18
9999 9999 9999 22 9999 25 24
10 9999 9999 9999 25 9999 9999
9999 14 9999 18 24 9999 9999

Spanning Tree Matrix:


0 0 0 0 0 10 0
0 0 16 0 0 0 14
0 16 0 12 0 0 0
0 0 12 0 22 0 0
0 0 0 22 0 25 0
10 0 0 0 25 0 0
0 14 0 0 0 0 0
Total cost of spanning tree=99

Page No.-
Date:-

 DISCUSSION:-

1. The above program obtains the minimum spanning tree of a given graph using PRIM’s algorithm.
2. This approach doesnot require listing of all the edges in the increasing order of weights, neither does
it check if the new vertex forms a cycle or not.
3. The PRIM’s algorithm can be easily implemented using the adjacency matrix form of a graph.

(Signature of the Teacher)

Page No.-

You might also like