Professional Documents
Culture Documents
EXP4 Rishabh 2019130024 Se Comps
EXP4 Rishabh 2019130024 Se Comps
UID: 2019130024
EXPERIMENT 04
NO :
DATE OF 01/03/2021
PERFORMANC
E
DATE OF 14/03/2021
SUBMISSION
AIM: Experiment based on Greedy Strategy.
Program:
Kruskal's algorithm to find the minimum cost spanning tree uses the greedy
approach. This algorithm treats the graph as a forest and every node it has as an
individual tree. A tree connects to another only and only if, it has the least cost
among all available options and does not violate MST properties.
Below are the steps for finding MST using Kruskal’s algorithm
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree
formed so far. If cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
ALGORITHM 1. Start
2. In main():
-go to kruskal() function.
3. In kruskal():
- Take input of total vertices, edges and cost, and declare int ‘cost’ to store the
total minimum cost.
- Sort the edges in ascending order of the cost.
- In a for loop for all edges, check if the edge makes a cycle or not (ie: if it is
present in the same set or not), if it does not, then add it to the final ans and add
its cost to the variable ‘cost’.
4. Stop.
PROGRAM: #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
void kruskals();
void sorting(int value[], char edge[30][2], int n);
void swap1(int *a, int *b);
void swap2(char *a, char *b);
int main()
{
kruskals();
return 0;
}
void kruskals()
{
int m = 0, n = 0, value[30], temp[30], cost = 0;
char edge[30][2];
int dd = 0;
printf("\nEnter number of vertexes: ");
scanf("%d", &m);
printf("Enter number of edges: ");
scanf("%d", &n);
printf("\nEnter edges and its value:\n");
for (int i = 0; i < n; i++)
scanf(" %c %c %d", &edge[i][0], &edge[i][1], &value[i]);
for (int i = 0; i < m; i++)
temp[i] = 0;
sorting(value, edge, n);
printf("\n\n--------\nOutput:\n--------\n");
int w = 0;
for (int i = 0; i < n; i++)
{
int a = edge[i][0] - 97;
int b = edge[i][1] - 97;
if (temp[a] == 0 && temp[b] == 0 || temp[a] != temp[b])
{
if (temp[a] == 0 && temp[b] == 0)
{
w++;
temp[a] = w;
temp[b] = w;
}
else if (temp[a] != 0 && temp[b] == 0)
temp[b] = temp[a];
else if (temp[a] == 0 && temp[b] != 0)
temp[a] = temp[b];
else
{
int p = temp[a];
int q = temp[b];
for (int j = 0; j < m; j++)
{
if (temp[j] == q)
temp[j] = p;
}
}
printf("\n%c --> %c (cost: %d)", edge[i][0], edge[i][1],
value[i]);
cost += value[i];
dd++;
}
}
printf("\n-------------------------\n");
printf("Total minimum cost: %d", cost);
printf("\n-------------------------\n");
}
void sorting(int value[], char edge[30][2], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (value[j] < value[i])
{
swap1(&value[i], &value[j]);
swap2(&edge[i][0], &edge[j][0]);
swap2(&edge[i][1], &edge[j][1]);
}
}
}
}
void swap1(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void swap2(char *a, char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}
RESULT ( SNAPSHOT)
(input taken)
(output)
CONCLUSION: I learnt how to carry out programs using greedy method for the Kruskal’s
Algorithm, how to check if the vertex belong to the same set of not by assigning
different temporary numbers to the vertexes (ie if both vertexes are new, then
some different value, else the value of its adjacent vertex), and if we have to
make an edge, then we have to make both of the temporary numbers same
(indicating that both vertexes belong to the same set).