Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

NAME: Rishabh Jain

UID: 2019130024

SUBJECT DAA (Lab)

EXPERIMENT 04
NO :
DATE OF 01/03/2021
PERFORMANC
E
DATE OF 14/03/2021
SUBMISSION
AIM: Experiment based on Greedy Strategy.

PROBLEM Kruskal’s Algorithm:


STATEMENT: Given the tree with the vertices and edges, find the minimum spanning tree by
the Kruskal’s algorithm, hence calculate the total minimum cost.
THEORY Greedy Strategy:
Among all the algorithmic approaches, the simplest and straightforward
approach is the Greedy method. In this approach, the decision is taken on the
basis of current available information without worrying about the effect of the
current decision in future.
Greedy algorithms build a solution part by part, choosing the next part in such a
way, that it gives an immediate benefit. This approach never reconsiders the
choices taken previously. This approach is mainly used to solve optimization
problems. Greedy method is easy to implement and quite efficient in most of
the cases.
In many problems, it does not produce an optimal solution though it gives an
approximate (near optimal) solution in a reasonable time.

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.

(Minimum Spanning Tree)


Given a connected and undirected graph, a spanning tree of that graph is a
subgraph that is a tree and connects all the vertices together. A single graph can
have many different spanning trees. A minimum spanning tree (MST) or
minimum weight spanning tree for a weighted, connected and undirected graph
is a spanning tree with weight less than or equal to the weight of every other
spanning tree. The weight of a spanning tree is the sum of weights given to each
edge of the spanning tree.
A minimum spanning tree has (V – 1) edges where V is the number of vertices in
the given graph. 

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).

You might also like