0% found this document useful (0 votes)
203 views

Write A Program To Implement Kruskal's Algorithm

The document describes Kruskal's algorithm for finding the minimum spanning tree of a connected weighted graph. It provides pseudocode for the algorithm, which takes a graph G and weight function w as input. It initializes an empty set A and sorts the edges by weight. It then iterates through the edges, adding each edge to A if it does not create a cycle. After iterating through all edges, A will contain the minimum spanning tree. Source code in C is provided to implement Kruskal's algorithm on a sample graph.

Uploaded by

SayanMaiti
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
203 views

Write A Program To Implement Kruskal's Algorithm

The document describes Kruskal's algorithm for finding the minimum spanning tree of a connected weighted graph. It provides pseudocode for the algorithm, which takes a graph G and weight function w as input. It initializes an empty set A and sorts the edges by weight. It then iterates through the edges, adding each edge to A if it does not create a cycle. After iterating through all edges, A will contain the minimum spanning tree. Source code in C is provided to implement Kruskal's algorithm on a sample graph.

Uploaded by

SayanMaiti
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Write a program to implement Kruskal’s Algorithm.

Introduction: Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge of the


least possible weight that connects any two trees in the forest. It is a greedy algorithm in graph
theory as it finds a minimum spanning tree for a connected weighted graph adding increasing cost
arcs at each step.

Algorithm:
MST_Kruskal (G,w)
1. A ←Q
2. For each vertex v ∈ V[G]
3. Do Make_Set(v)
4. Sort the edges of E into non-decreasing order by weight w
5. For each edge (u,v) ∈ E taken in non-decreasing order by weight
6. Do if Find_Set(u) ≠ Find_Set(v)
7. Then A←A U {u,v}
8. Union(u,v)
9. Return A

Source Code:
#include <stdio.h>
int main()
{
int n,i,j,k,e,t,sum,u,v;
int adj[10][10];
int a[10][3];
int b[2][10];

printf("Enter the number of vertices:");


scanf("%d",&n);

printf("Enter the adjacency matrix:\n");


for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&adj[i][j]);

//Edge Counter
e=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(adj[i][j]!=0)
e++;

e/=2;

/* //Matrix Display
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",adj[i][j]);
}
printf("\n");
}
*/

printf("\nEdge Counted = %d\n",e);

//Storing the edges unsorted and printing


k=0;
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(adj[i][j]!=0)
{
a[k][0]=adj[i][j];
a[k][1]=i;
a[k][2]=j;
k++;
}
}
}

printf("Unsorted Edge Array:\n");


for(i=0;i<e;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}

printf("\n");
}

//Sorting the edge array


for(i=0;i<e-1;i++)
for(j=0;j<=n-i-1;j++)
if(a[j][0]>a[j+1][0])
{
t=a[j][0];
a[j][0]=a[j+1][0];
a[j+1][0]=t;

t=a[j][1];
a[j][1]=a[j+1][1];
a[j+1][1]=t;

t=a[j][2];
a[j][2]=a[j+1][2];
a[j+1][2]=t;
}

printf("Sorted Edge Array:\n");


for(i=0;i<e;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}

printf("\n");
}

//Storing the representative graph


for(i=0;i<n;i++)
{
b[0][i]=i;
b[1][i]=i;
}
printf("Before Union:\n");
for(i=0;i<2;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}

//MST Cost Calculation


sum=0;
for(i=0;i<e;i++)
{
u=a[i][1];
v=a[i][2];

if(b[1][u]!=b[1][v])
{
sum+=a[i][0];
b[1][u]=b[1][v]=100;
}
}
printf("After Union:\n");
for(i=0;i<2;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}

printf("Cost=%d\n",sum);
return 0;
}

Output:
Enter the number of vertices:4
Enter the adjacency matrix:
0121
1030
2302
1020

Edge Counted = 5

Unsorted Edge Array:


1 0 1
2 0 2
1 0 3
3 1 2
2 2 3
Sorted Edge Array:
1 0 1
1 0 3
2 0 2
2 2 3
3 1 2
Before Union:
0 1 2 3
0 1 2 3
After Union:
0 1 2 3
100 100 100 100

Cost=4

You might also like