0% found this document useful (0 votes)
24 views14 pages

9117lab Manual Expt No. 5 AOA - MST Using Greedy Method

Uploaded by

danish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

9117lab Manual Expt No. 5 AOA - MST Using Greedy Method

Uploaded by

danish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PART A

(PART A: TO BE REFFERED BY STUDENTS)

Experiment No.05
A.1 Aim:
Write a program to implement minimum cost spanning trees using Greedy Method
Approach – Kruskal Algorithm and Prim’s Algorithm.

A.2 Prerequisite:

A.3 Outcome:

After successful completion of this experiment, students will be able to apply greedy
method approach to different problems to find optimal solution and analyze the
complexity of the problem.

A.4 Theory:

Kruskal's algorithm :

Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge of the


least possible weight that connects any two trees in the forest. It is a greedy
algorithm in graph theory as it finds a minimum spanning tree for a connected graph
adding increasing cost arcs at each step. This means it finds a subset of the edges that
forms a tree that includes every vertex, where the total weight of all the edges in the tree
is minimized. If the graph is not connected, then it finds a minimum spanning forest.

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.
Example:

Time Complexity:

Time complexity is large as order of O(V+E) where V is the number of vertices, E is the
number of edges.

Prim’s algorithm:

Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for
a weighted undirected graph. This means it finds a subset of the edges that forms
a tree that includes every vertex, where the total weight of all the edges in the tree is
minimized. The algorithm operates by building this tree one vertex at a time, from an
arbitrary starting vertex, at each step adding the cheapest possible connection from the
tree to another vertex.

Algorithm:

1. Maintain two disjoint sets of vertices. One containing vertices that are in the growing
spanning tree and other that are not in the growing spanning tree.
2. Select the cheapest vertex that is connected to the growing spanning tree and is not
in the growing spanning tree and add it into the growing spanning tree. This can be
done using Priority Queues. Insert the vertices, that are connected to growing
spanning tree, into the Priority Queue.
3. Check for cycles. To do that, mark the nodes which have been already selected and
insert only those nodes in the Priority Queue that are not marked.

Example:

Time Complexity:

The time complexity of the Prim’s Algorithm is O((V+E)logV) because each vertex is
inserted in the priority queue only once and insertion in priority queue take logarithmic
time.
PART B

Roll No.: A05 Name: PRASHANTH NAIDU


Class: SE – A (COMPS) Batch: A1
Date of Experiment: Date of Submission:
04/03/24 10/03/24
Grade:

B.1 Software Code written by student:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_VERTICES 100


#define INF 999999

struct Edge {
int src, dest, weight;
};

struct Graph {
int V, E;
struct Edge* edges;
};

void kruskalMST(struct Graph* graph);


void primMST(struct Graph* graph);

int main() {
struct Graph* graph = (struct
Graph*)malloc(sizeof(struct Graph));
graph->V = 4; // Number of vertices
graph->E = 5; // Number of edges

graph->edges = (struct Edge*)malloc(graph->E *


sizeof(struct Edge));
graph->edges[0] = (struct Edge){0, 1, 10};
graph->edges[1] = (struct Edge){0, 2, 6};
graph->edges[2] = (struct Edge){0, 3, 5};
graph->edges[3] = (struct Edge){1, 3, 15};
graph->edges[4] = (struct Edge){2, 3, 4};

printf("Kruskal's Algorithm:\n");
kruskalMST(graph);

printf("\nPrim's Algorithm:\n");
primMST(graph);

free(graph->edges);
free(graph);
return 0;
}

int find(int parent[], int i) {


if (parent[i] == -1)
return i;
return find(parent, parent[i]);
}

void Union(int parent[], int x, int y) {


int xset = find(parent, x);
int yset = find(parent, y);
parent[xset] = yset;
}
int compare(const void* a, const void* b) {
struct Edge* edge1 = (struct Edge*)a;
struct Edge* edge2 = (struct Edge*)b;
return edge1->weight - edge2->weight;
}

void kruskalMST(struct Graph* graph) {


int V = graph->V;
struct Edge result[V];
int e = 0;
int i = 0;
int parent[V];

qsort(graph->edges, graph->E, sizeof(graph-


>edges[0]), compare);

for (int v = 0; v < V; ++v)


parent[v] = -1;

while (e < V - 1 && i < graph->E) {


struct Edge next_edge = graph->edges[i++];
int x = find(parent, next_edge.src);
int y = find(parent, next_edge.dest);

if (x != y) {
result[e++] = next_edge;
Union(parent, x, y);
}
}

for (i = 0; i < e; ++i)


printf("%d - %d : %d\n", result[i].src,
result[i].dest, result[i].weight);
}

void primMST(struct Graph* graph) {


int V = graph->V;
int parent[V];
int key[V];
bool mstSet[V];

for (int i = 0; i < V; ++i) {


key[i] = INF;
mstSet[i] = false;
}

key[0] = 0;
parent[0] = -1;

for (int count = 0; count < V - 1; ++count) {


int u, min = INF;
for (int v = 0; v < V; ++v)
if (!mstSet[v] && key[v] < min) {
min = key[v];
u = v;
}

mstSet[u] = true;

for (int v = 0; v < V; ++v)


if (graph->edges[u].weight && !mstSet[v] &&
graph->edges[u].weight < key[v]) {
parent[v] = u;
key[v] = graph->edges[u].weight;
}
}
for (int i = 1; i < V; ++i)
printf("%d - %d : %d\n", parent[i], i, key[i]);
}

B.2 Input and Output:


B.3 Conclusion:

Both Kruskal's and Prim's algorithms efficiently find


Minimum Cost Spanning Trees. Kruskal's algorithm
iteratively adds the lowest-weight edges, while Prim's
algorithm starts from a vertex and expands the tree by
adding the nearest vertex. Depending on the problem and
graph characteristics, one algorithm may be more suitable
than the other.

B.4 Question of Curiosity


Q1: What are the applications of Minimum Spanning
Tree?
Q2: How many edges do a minimum spanning tree has?

Q3: Consider an undirected graph G with unique positive


weights. Suppose it has a minimum spanning tree T. If
we square all the edge weights and compute the MST
again, do we still get the same tree structure? Explain
briefly.

You might also like