Algorithm of spanning tree 1. Implement the Kruskal's algorithm of spanning tree 2. Write comments in each line as you understand 3. Screenshots of three times output, for 5 verteces, 7 verteces, 8 verteces ============================================================= /// Implementation of Kruskal"s algorithm on Spanning Tree #include #define max 99 int k; int array[8]; int u,v; void main() { int vertex, edge =1; /// ***We initialized but didn't increment no where int i,j, p,q; int edgecost[8][8]; int min = max, totalmincost = 0; /// To find out the minimum and total of those printf("\nEnter the quantity of Verteces: "); scanf("%d", &vertex); /// 1,2 -> 0 printf("\nEnter the cost values in matrix:\n"); for(i=1; i<=vertex; i++) { printf("\n"); for(j=1; j<=vertex; j++) { printf("cost[%d][%d]: ", i,j); scanf("%d", &edgecost[i][j]); if(edgecost[i][j] == 0) edgecost[i][j] = max; } } printf("\nEdge(s) with minimum cost:"); while(edge
1. Implement the
Kruskal's algorithm of spanning tree
2. Write comments in each line as you understand
3. Screenshots of three times output, for 5 verteces, 7 verteces, 8 verteces
=============================================================
/// Implementation of Kruskal"s algorithm on Spanning Tree
#include<stdio.h>
#define max 99
int k;
int array[8];
int u,v;
void main()
{
int vertex, edge =1; /// ***We initialized but didn't increment no where
int i,j, p,q;
int edgecost[8][8];
int min = max, totalmincost = 0; /// To find out the minimum and total of those
printf("\nEnter the quantity of Verteces: ");
scanf("%d", &vertex);
/// 1,2 -> 0
printf("\nEnter the cost values in matrix:\n");
for(i=1; i<=vertex; i++)
{
printf("\n");
for(j=1; j<=vertex; j++)
{
printf("cost[%d][%d]: ", i,j);
scanf("%d", &edgecost[i][j]);
if(edgecost[i][j] == 0)
edgecost[i][j] = max;
}
}
printf("\nEdge(s) with minimum cost:");
while(edge<vertex)
{
for(i=1, min = max; i<=vertex; i++)
{
for(j=1; j<=vertex; j++)
{
if(edgecost[i][j] < min)
{
min = edgecost[i][j];
u=i;
v=j;
p=i;
q=j;
}
}
}
u = search(u);
v = search(v);
if(combination(u,v))
{
printf("\nEdge(%d,%d) with minimum cost of %d", p,q,min);
totalmincost = totalmincost + min;
///========================================================
edge = edge +1; /// We initialized but didn't increment, was need to do it
///========================================================
}
edgecost[p][q] = edgecost[q][p] = max;
}
printf("\n\nTotal of minimum cost: %d", totalmincost);
printf("\n\n\n");
return 0;
}
/// 0,1,2,0,0,3,0.......
int search(int k)
{
while(array[k])
k = array[k];
return k;
}
/// comparison between vertex, edge
int combination(int u, int v)
{
if(u != v)
{
array[v] = u;
return 1;
}
return 0;
}
Step by step
Solved in 2 steps with 6 images