You are on page 1of 8

Date: -

 ASSIGNMENT NO: -
Write a program in c to find the minimum spanning tree for an undirected graph using kruskal’s algo.

 THEORY: -
Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and
connects all the vertices together. A single graph can have many different spanning trees. A minimum
spanning tree (MST) or minimum weight spanning tree for a weighted, connected and undirected graph
is a spanning tree with weight less than or equal to the weight of every other spanning tree. The weight
of a spanning tree is the sum of weights given to each edge of the spanning tree.

Below are the steps for finding MST using Kruskal’s algorithm
1.  Sort all the edges in non-decreasing order of their weight.
2.  Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If cycle is not
formed, include this edge. Else, discard it.
3.  Repeat step#2 until there are (V-1) edges in the spanning tree.

 ALGORITHM: -

Variable Listing: -

Variable Name Type Purpose

cost[12][12] integer It input the cost matrix

parent[12] integer It is used to indicate the parent


node of the graph in matrix
form.
mincost integer It indicates the minimum cost
possible from one to another
node
min integer It indicates the minimum path
required from one node to
another node
i,j integer It is used to execute the looping
statement
a,b,u,v integer Performs under the loop
condition to assign the variable
n integer It indicates the no of vertices

STEP 1: - Input i,j,a,b,u,v,n,ne=1

STEP 2: - Input min,mincost=0,cost[12][12],parent[12]

STEP 3: - Define a function- int find(int)

Page No.
Date: -

STEP 4: - Define a function- int uni(int,int)

Start of main() function: -

STEP 1: - Display “Implementation of kruskal’s algorithm”

STEP 2: - Input “enter the no of vertices”

STEP 3: - Read n

STEP 4: - Input “enter the cost adjacency matrix”

STEP 5: - Repeat through step 6 to step for i=1 to n do

STEP 6: - Repeat through step 6 to step for j=1 to n do

STEP 7: - Input “enter the value of cost[%d][%d]”i,j

STEP 8: - Read cost[i][j]

STEP 9: - If(cost[i][j]=0) then

STEP 10: - Set cost[i][j]=9999

[End of inner for loop]

[End of outer for loop]

STEP 11: - Display “ the given cost matrix is->\n”

STEP 12: - Repeat through step 13 to step for i=1 to n do

STEP 13: - Repeat through step 14 to step for j=1 to n do

STEP 14: - Print “%d”,cost[i][j]

[End of inner for loop]

STEP 15: - Print “\n”

[End of outer for loop]

STEP 16: - Print “\nthe edges of minimum cost spanning trees are \n”

STEP 17: - Repeat through step 18 to step while(ne<n) do

STEP 18: - Repeat through step 19 to step for i=1 to n & min=9999 do

STEP 19: - Repeat through step 20 to step for j=1 to n do

STEP 20: - If(cost[i][j]<min) do

STEP 21: - Set min=cost[i][j]

STEP 22: - set a=u=i

STEP 23: - set b=v=j

Page No.
Date: -

[End of if]

[End of inner for loop]

[End of inner for loop]

STEP 24: - Set u=find(u)

STEP 25: - Set v=find(v)

STEP 26: - If(uni(u,v)) then

STEP 27: - Print “\n %d edge(%d %d)=%d”,ne++,a,b,min

STEP 28: - Set mincost+=min

[End of if]

STEP 29: - Set cost[a][b]=cost[b][a]=999

[End of while loop]

STEP 30: - Print “\n minimum cost=%d\n”,mincost

STEP 31: - Stop

Method for calling the function- int find(int i)

STEP 1: - Repeat through step 1 while(parent[i]) do

STEP 2: - Set i= parent[i]

STEP 3: - Return i

STEP 4: - Stop

Method for calling the function- int uni(int i,int j)

STEP 1: - If(i!=j) then

STEP 2: - Set parent[j]=i

STEP 3: - return 1

[End of if]

STEP 4: - stop

 SOURCE CODE: -
#include<stdio.h>

#include<stdlib.h>

int i,j,k,a,b,u,v,n,ne=1;

int min,mincost=0,cost[12][12],parent[12];

Page No.
Date: -

int find(int);

int uni(int,int);

int main()

printf("\n\tImplementation of Kruskal's algorithm:\n");

printf("\nEnter the no. of vertices:");

scanf("%d",&n);

printf("\nEnter the cost adjacency matrix-->\n");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

printf("\n\t enter the value of cost[%d][%d]:",i,j);

scanf("%d",&cost[i][j]);

if(cost[i][j]==0)

cost[i][j]=999;

printf("\n\t the given cost matrix is-->\n");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

printf("%d ",cost[i][j]);

printf("\n");

printf("\nThe edges of Minimum Cost Spanning Tree are:\n");

while(ne<n)

Page No.
Date: -

for(i=1,min=999;i<=n;i++)

for(j=1;j<=n;j++)

if(cost[i][j]<min)

min=cost[i][j];

a=u=i;

b=v=j;

u=find(u);

v=find(v);

if(uni(u,v))

printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min);

mincost +=min;

cost[a][b]=cost[b][a]=999;

printf("\n\tMinimum cost = %d\n",mincost);

int find(int i)

while(parent[i])

i=parent[i];

return i;

Page No.
Date: -

int uni(int i,int j)

if(i!=j)

parent[j]=i;

return 1;

return 0;

 INPUT & OUPUT: -


Set 1: - Implementation of Kruskal's algorithm:
Enter the no. of vertices: 4

Enter the cost adjacency matrix-->

enter the value of cost[1][1]: 2

enter the value of cost[1][2]: 6

enter the value of cost[1][3]: 4

enter the value of cost[1][4]: 1

enter the value of cost[2][1]: 9

enter the value of cost[2][2]: 5

enter the value of cost[2][3]: 8

enter the value of cost[2][4]: 7

enter the value of cost[3][1]: 4

enter the value of cost[3][2]: 2

enter the value of cost[3][3]: 1

enter the value of cost[3][4]: 3

enter the value of cost[4][1]: 5

enter the value of cost[4][2]: 8

enter the value of cost[4][3]: 2

Page No.
Date: -

enter the value of cost[4][4]: 4

the given cost matrix is-->

2641

9587

4213

5824

The edges of Minimum Cost Spanning Tree are:

1 edge (1,4) =1

2 edge (3,2) =2

3 edge (4,3) =2

Minimum cost = 5

--------------------------------

Process exited after 44.79 seconds with return value 0

Press any key to continue . . .

Set 2: - Implementation of Kruskal's algorithm:

Enter the no. of vertices: 3

Enter the cost adjacency matrix-->

enter the value of cost[1][1]: 4

enter the value of cost[1][2]: 1

enter the value of cost[1][3]: 8

enter the value of cost[2][1]: 5

enter the value of cost[2][2]: 2

enter the value of cost[2][3]: 9

enter the value of cost[3][1]: 7

enter the value of cost[3][2]: 6

enter the value of cost[3][3]: 4

Page No.
Date: -

the given cost matrix is-->

418

529

764

The edges of Minimum Cost Spanning Tree are:

1 edge (1,2) =1

2 edge (3,2) =6

Minimum cost = 7

--------------------------------

Process exited after 19.62 seconds with return value 0

Press any key to continue . . .

 DISCUSSION: -
1. The algorithm is a Greedy Algorithm. The Greedy Choice is to pick the smallest weight edge that
does not cause a cycle in the MST constructed so far. Let us understand it with an example:
Consider the below input graph.
2. Time Complexity is O(ElogE) or O(ElogV). Sorting of edges takes O(ELogE) time. After sorting, we
iterate through all edges and apply find-union algorithm. The find and union operations can take
atmost O(LogV) time. So overall complexity is O(ELogE + ELogV) time. The value of E can be
atmost O(V2), so O(LogV) are O(LogE) same. Therefore, overall time complexity is O(ElogE) or
O(ElogV).
3. This algorithm requires listing of all edges in the increasing order of weight or checking each
step to find whether a newly selected edge forms a cycle or not.

Page No.

You might also like