Kru Skal
Kru Skal
Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Kruskal's algorithm.
/*************************************************************
*****************
*File : 06Kruskal.cpp
*Description : Program to find Minimum Cost Spanning Tree of a
given
* undirected graph using Kruskal's algorithm.
*
**************************************************************
****************/
#include <iostream.h>
//using namespace std;
#include<conio.h>
const int MAXNODES = 10;
const int INF = 9999;
// Structure to represent an edge
struct edge
{
int u, v, cost;
};
int fnFindParent(int v, int parent[]);
void fnUnion_ij(int i, int j, int parent[]);
void fnInputGraph(int m, edge e[]);
int fnGetMinEdge(edge e[], int n);
void fnKruskal(int n, edge e[], int m);
/*************************************************************
*****************
*Function : main
*Input parameters:
* int argc - no of commamd line arguments
*
char **argv - vector to store command line argumennts
*RETURNS : 0 on success
**************************************************************
****************/
int main( int argc, char **argv)
{
int n = 6, m = 20;
edge e[2*MAXNODES] =
{{0,1,6},{1,4,4},{4,5,6},{5,3,2},{3,0,5},{0,2,1},
{1,2,5},{3,2,2},{4,2,6},{5,2,3} };
cout << "Enter the number of nodes : ";
cin >> n;
cout << "Enter the number of edges : ";
cin >> m;
fnInputGraph(m, e);
fnKruskal(n, e, m);
return 0;
}
/*************************************************************
*****************
*Function : fnFindParent
*Description : Function to find parent of a given vertex
*Input parameters:
* int v - vertex for whom parent has to be found
* int parent[] - parent vector
*RETURNS : parent vertex
**************************************************************
****************/
int fnFindParent(int v, int parent[])
{
while (parent[v] != v)
v = parent[v];
return v;
}
/*************************************************************
*****************
*Function : fnUnion_ij
*Description : Function to merge two trees
*Input parameters:
* int i, j - vertices to be merged
* int parent[] - parent vector
*RETURNS : no value
**************************************************************
****************/
void fnUnion_ij(int i, int j, int parent[])
{
if(i < j)
parent[j] = i;
else
parent[i] = j;
}
/*************************************************************
*****************
*Function : fnInputGraph
*Description : Function to read a graph
*Input parameters:
* int m - no of edges in the graph
* edge e[] - set of edges in the graph
*RETURNS : no value
**************************************************************
****************/
void fnInputGraph(int m, edge e[])
{
int i, j, k, cost;
for(k=0; k<m; k++)
{
cout << "Enter edge and cost in the form u, v, w : \n";
cin >> i >> j >> cost;
}
e[k].u = i;
e[k].v = j;
e[k].cost = cost;
}
/*************************************************************
*****************
*Function : fnGetMinEdge
*Description : Function to find the least cost edge in the edge
set
*Input parameters:
* edge e[] - set of edges in the graph
* int n - no of vertices in the graph
*RETURNS : index of least cost edge in the edge set
**************************************************************
****************/
int fnGetMinEdge(edge e[], int n)
{
int i, small, pos;
small = INF;
pos = -1;
for(i=0; i<n; i++)
{
if(e[i].cost < small)
{
small = e[i].cost;
pos = i;
}
}
return pos;
}
/*************************************************************
*****************
*Function : fnKruskal
*Description : Function to find MST of a graph
*Input parameters:
* int n - no of vertices in the graph
* int m - no of edges in the graph
* edge e[] - set of edges in the graph
*RETURNS : no value
**************************************************************
****************/
if(count == n-1)
{
cout << "\nSpanning tree exists";
cout << "\nThe Spanning tree is shown below\n";
for(i=0; i<n-1; i++)
cout << t[i][0] << " " << t[i][1] << endl;
cout << "\nCost of the spanning tree : " << sum <<
endl;
}
else
cout << "\nThe spanning tree does not exist" << endl;
}
Input Graph
Output
Enter the number of nodes : 6
Enter the number of edges : 10
Enter edge and cost in the form u, v, w :
0 1 6
Enter edge and cost in the form u, v, w :
1 4 4
Enter edge and cost in the form u, v, w :
4 5 6
Enter edge and cost in the form u, v, w :
5 3 2
Enter edge and cost in the form u, v, w :
3 0 5
Enter edge and cost in the form u, v, w :
0 2 1
Enter edge and cost in the form u, v, w :
1 2 5
Enter edge and cost in the form u, v, w :
3 2 2
Enter edge and cost in the form u, v, w :
4 2 6
Enter edge and cost in the form u, v, w :
5 2 3
#include <iostream.h>
#include<conio.h>
using namespace std;
const int MAXNODES = 10;
void fnPrims(int n, int cost[MAXNODES][MAXNODES]);
void fnGetMatrix(int n,int a[MAXNODES][MAXNODES]);
/*************************************************************************
*****
*Function : main
*Input parameters:
* int argc - no of commamd line arguments
* char **argv - vector to store command line argumennts
*RETURNS : 0 on success
**************************************************************************
****/
int main( int argc, char **argv)
{
int a[MAXNODES][MAXNODES] = {
{0, 3, 9999, 7, 9}, {3, 0, 4, 2, 9999}, {9999, 4, 0, 5, 6},
{7, 2, 5, 0, 4}, {9, 9999, 6, 4, 0}};
int n = 5;
cout << "Enter the number of vertices : ";
cin >> n;
fnGetMatrix(n,a);
fnPrims(n,a);
return 0;
}
/*************************************************************************
*****
*Function : fnPrims
*Description : Function to find Minimum Cost Spanning Tree of a given
* undirected graph using Prims algorithm.
*Input parameters:
* int n - no of vertices in the graph
* int cost[][] - cost adjacency matrix of the graph
*RETURNS : no value
**************************************************************************
****/
void fnPrims(int n, int cost[MAXNODES][MAXNODES])
{
int i, j, u, v, min;
int sum, k, t[MAXNODES][2], p[MAXNODES], d[MAXNODES], s[MAXNODES];
int source, count;
min = 9999;
source = 0;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(cost[i][j] != 0 && cost[i][j] <= min)
{
min = cost[i][j];
source = i;
}
}
}
for(i=0; i<n; i++)
{
d[i] = cost[source][i];
s[i] = 0;
p[i] = source;
}
s[source] = 1;
sum = 0;
k = 0;
count = 0;
while (count != n-1)
{
min = 9999;
u = -1;
for(j=0; j<n; j++)
{
if(s[j] == 0)
{
if(d[j] <= min)
{
min = d[j];
u = j;
}
}
}
t[k][0] = u;
t[k][1] = p[u];
k++;
count++;
sum += cost[u][p[u]];
s[u] = 1;
for(v=0; v<n; v++)
{
if(s[v]==0 && cost[u][v]<d[v])
{
d[v] = cost[u][v];
p[v] = u;
}
}
}
if(sum >= 9999)
cout << "\nSpanning tree does not exist\n";
else
{
cout << "\nThe spanning tree exists and minimum cost spanning tree
is \n";
for(i=0; i<k; i++)
cout << t[i][1] << " " << t[i][0] << endl;
cout << "\nThe cost of the minimum cost spanning tree is " << sum <<
endl;
}
}
/*************************************************************************
*****
*Function : fnGetMatrix
*Description : Function to read cost adjacency matrix of the graph
*Input parameters:
* int n - no of vertices in the graph
* int a[][] - cost adjacency matrix of the graph
*RETURNS : no value
**************************************************************************
****/
void fnGetMatrix(int n,int a[MAXNODES][MAXNODES])
{
int i, j;
cout << "Enter the Cost Adjacency Matrix" << endl;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
cin >> a[i][j];
}
Input Graph