Kruskal’s Algorithm for Minimum Spanning Tree
/************************************************************
-> This Program is to implement Kruskal algorithm.
-> This program is to find minimum spanning tree
for undirected weighted graphs
-> Data Structers used:
Graph:Adjacency Matrix
-> This program works in microsoft vc++ 6.0 environment.
**************************************************************/
#include
class kruskal
{
private:
int n; //no of nodes
int noe; //no edges in the graph
int graph_edge[100][4];
int tree[10][10];
int sets[100][10];
int top[100];
public:
void read_graph();
void initialize_span_t();
void sort_edges();
void algorithm();
int find_node(int );
void print_min_span_t();
};
void kruskal::read_graph()
{
cout<<”*************************************************\n”
<<”This program implements the kruskal algorithm\n”
<<”*************************************************\n”;
cout<<”Enter the no. of nodes in the undirected weighted graph ::”;
cin>>n;
noe=0;
cout<<”Enter the weights for the following edges ::\n”;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
cout<<” < “< ::”;
int w;
cin>>w;
if(w!=0)
{
noe++;
graph_edge[noe][1]=i;
graph_edge[noe][2]=j;
graph_edge[noe][3]=w;
}
}
}
// print the graph edges
cout<<”\n\nThe edges in the given graph are::\n”;
} void kruskal::sort_edges()
for(int i=1;i<=noe-1;i++)
t=graph_edge[j][2];
t=graph_edge[j][3];
// print the graph edges cout<<”\n\nAfter sorting the edges in the given graph are::\n”;
void kruskal::algorithm()
cout<<”\nThe algorithm starts ::\n\n”; for(i=1;i<=noe;i++)
if(p1!=p2)
tree[graph_edge[i][1]][graph_edge[i][2]]=graph_edge[i][3];
// Mix the two sets for(int j=1;j<=top[p2];j++)
top[p2]=0;
int kruskal::find_node(int n)
int main()
for(i=1;i<=noe;i++)
cout<<” < “<
{
/**** Sort the edges using bubble sort in increasing order**************/
{
for(int j=1;j<=noe-i;j++)
{
if(graph_edge[j][3]>graph_edge[j+1][3])
{
int t=graph_edge[j][1];
graph_edge[j][1]=graph_edge[j+1][1];
graph_edge[j+1][1]=t;
graph_edge[j][2]=graph_edge[j+1][2];
graph_edge[j+1][2]=t;
graph_edge[j][3]=graph_edge[j+1][3];
graph_edge[j+1][3]=t;
}
}
}
for(i=1;i<=noe;i++)
cout<<” < “<
{
// ->make a set for each node
for(int i=1;i<=n;i++)
{
sets[i][1]=i;
top[i]=1;
}
{
int p1=find_node(graph_edge[i][1]);
int p2=find_node(graph_edge[i][2]);
{
cout<<”The edge included in the tree is ::”
<<” < “<
tree[graph_edge[i][2]][graph_edge[i][1]]=graph_edge[i][3];
{
top[p1]++;
sets[p1][top[p1]]=sets[p2][j];
}
}
else
{
cout<<”Inclusion of the edge “
<<” < “<
}
}
}
{
for(int i=1;i<=noe;i++)
{
for(int j=1;j<=top[i];j++)
{
if(n==sets[i][j])
return i;
}
}
return -1;
}
{
kruskal obj;
obj.read_graph();
obj.sort_edges();
obj.algorithm();
return 0;
}
0 comments:
Post a Comment