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

Program To Implement Krushkal

This document contains code to implement Kruskal's algorithm for finding the minimum spanning tree of a graph. It includes functions to input the graph, initialize each vertex as its own set, find the minimum weight edge, determine what set a vertex belongs to, unite sets, and output the minimum spanning tree. The main function calls these functions, first calling makeset to initialize the sets, then repeatedly calling findmin and unionset until all vertices are in one set, outputting the minimum spanning tree.

Uploaded by

sameer106
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Program To Implement Krushkal

This document contains code to implement Kruskal's algorithm for finding the minimum spanning tree of a graph. It includes functions to input the graph, initialize each vertex as its own set, find the minimum weight edge, determine what set a vertex belongs to, unite sets, and output the minimum spanning tree. The main function calls these functions, first calling makeset to initialize the sets, then repeatedly calling findmin and unionset until all vertices are in one set, outputting the minimum spanning tree.

Uploaded by

sameer106
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PRACTICAL:- 8

Program to implement Krushkal’s Algorithm

#include<stdio.h>
#define MAX 50
#define INFINITY 4000
void inputgraph(int);
void makeset(int);
void findmin(int);
int findset(int);
void unionset(int,int);
int wt[MAX][MAX];
int edge[MAX][MAX];
int edge1,edge2;
int c = INFINITY;
int mstree[ 2*MAX];
int n;
int set[MAX];
int flag;
int tedge = 0;
int main()
{
int i,j;
int k=0;
printf("\n Enter No of Vertex");
scanf("%d",&n);
for (i=0; i<n;++i)
for (j=0; j<n;++j)
{
printf("Enter weight of edge (%d,%d) ",i+1,j+1);
scanf("%d",&wt[i][j]);
if(wt[i][j] == 0)
{
edge[i][j] = 0;
edge[j][i] = 0;
}
if(wt[i][j] != 0)
{
edge[i][j] = 1;
edge[j][i] = 1;
tedge = tedge + 2;
MADE BY:-
WASEEM
PRACTICAL:- 8
}
}
makeset(n);
for(i = 1;i<=tedge;i++)
{
findmin(n);
if(findset(edge1) != findset(edge2))
{
mstree[k++] = edge1;
mstree[k++] = edge2;
edge[edge1][edge2] = 0;
unionset(edge1,edge2);
}
}
k=0;
flag = 1;
for(i = 0;i<n;i++)
{
printf("Edge no. %d of minimum spanning tree hv vertices %d and %d
\n",flag,mstree[k],mstree[k+1]);
flag++;
k = k+2;
}
}
void makeset(int n)
{
int i;
for (i=1;i<=n;i++)
{
set[i] = i;
}
}
void findmin(int n)
{
int i,j;
for (i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(edge[i][j] == 1)
MADE BY:-
WASEEM
PRACTICAL:- 8
{
if(wt[i][j] <= c)
{
c = wt[i][j];
edge1 = i;
edge2 = j;
}
}
}
}
edge[edge1][edge2]= 0;
c = INFINITY;
}
int findset( int a)
{
return set[a];
}
void unionset(int a,int b)
{
int z;
int temp;
temp = set[b];
set[b] = set[a];
for(z = 0;z<n;z++)
{
if(set[z] == temp)
{
set[z] = set[a];
}
}
}

MADE BY:-
WASEEM

You might also like