0% found this document useful (0 votes)
11 views5 pages

Exp Minimum Spanning Tree

The document outlines a program to find the Minimum Spanning Tree (MST) of a weighted, undirected graph using Kruskal's and Prim's algorithms. It includes a detailed explanation of the theory behind MST, its applications, and the steps involved in both algorithms, along with a C code implementation of Kruskal's algorithm. The program demonstrates how to sort edges, check for cycles, and calculate the total weight of the MST.

Uploaded by

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

Exp Minimum Spanning Tree

The document outlines a program to find the Minimum Spanning Tree (MST) of a weighted, undirected graph using Kruskal's and Prim's algorithms. It includes a detailed explanation of the theory behind MST, its applications, and the steps involved in both algorithms, along with a C code implementation of Kruskal's algorithm. The program demonstrates how to sort edges, check for cycles, and calculate the total weight of the MST.

Uploaded by

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

EXPERIMENT NO.

-15

Aim: Write a program to find minimum spanning tree.


Theory:
A Minimum Spanning Tree (MST) is a subgraph of a weighted, undirected graph that connects
all the vertices together, without any cycles, and with the minimum possible total edge weight.

Applications of MST:

 Network design (e.g., telecommunications, electrical grids)


 Cluster analysis
 Approximation algorithms for NP-hard problems

Given a connected, undirected graph G(V,E)G(V, E)G(V,E), a spanning tree is a subset of edges
that connects all the vertices in the graph, and the sum of the weights of the edges is as small as
possible. The Minimum Spanning Tree of a graph is the spanning tree with the smallest
possible total edge weight.

Algorithms for Finding MST

Two of the most commonly used algorithms to find an MST are:

1. Kruskal's Algorithm
2. Prim's Algorithm
Algorithm:

1. Kruskal's Algorithm

Kruskal's algorithm is a greedy algorithm that finds the MST by sorting all edges in increasing
order of weight and adding them one by one to the MST if they do not form a cycle.

Steps of Kruskal’s Algorithm:

1. Sort all the edges in the graph by increasing weight.


2. Start with an empty MST.
3. Pick the smallest edge. If it does not form a cycle with the already included edges, add it
to the MST.
4. Repeat step 3 until you have added V−1V-1V−1 edges to the MST, where VVV is the
number of vertices.

2. Prim's Algorithm

Prim's algorithm is also a greedy algorithm, but it builds the MST one vertex at a time, starting
from an arbitrary vertex.

Steps of Prim’s Algorithm:

1. Start with an arbitrary vertex.


2. Add the smallest edge that connects a vertex in the MST to a vertex outside the MST.
3. Repeat the above step until all vertices are included in the MST.
Source Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct {
int u, v, weight;
} Edge;

typedef struct {
int parent;
int rank;
} Subset;

int find(Subset subsets[], int i) {


if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}

void unionSets(Subset subsets[], int x, int y) {


int xroot = find(subsets, x);
int yroot = find(subsets, y);

if (subsets[xroot].rank < subsets[yroot].rank)


subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}

int compareEdges(const void *a, const void *b) {


return ((Edge*)a)->weight - ((Edge*)b)->weight;
}

void kruskal(int V, Edge edges[], int E) {

Edge result[V];
int e = 0;
int i = 0;
int subsetCount = 0;

// Step 1: Sort all the edges in increasing order of their weight


qsort(edges, E, sizeof(edges[0]), compareEdges);
// Allocate memory for subsets
Subset subsets[V];
for (int v = 0; v < V; v++) {
subsets[v].parent = v;
subsets[v].rank = 0;
}

// Step 2: Pick the smallest edge. Check if it forms a cycle with the MST.
while (e < V - 1 && i < E) {
Edge nextEdge = edges[i++];

int x = find(subsets, nextEdge.u);


int y = find(subsets, nextEdge.v);

// If including this edge does not form a cycle, include it in the result
if (x != y) {
result[e++] = nextEdge;
unionSets(subsets, x, y);
}
}

// Step 3: Print the result


printf("Edges in the Minimum Spanning Tree:\n");
int totalWeight = 0;
for (int i = 0; i < e; i++) {
printf("%d -- %d == %d\n", result[i].u, result[i].v, result[i].weight);
totalWeight += result[i].weight;
}
printf("Total weight of MST: %d\n", totalWeight);
}

int main() {
int V = 4; // Number of vertices
int E = 5; // Number of edges

// Create an array of edges (u, v, weight)


Edge edges[] = {
{0, 1, 10},
{0, 2, 6},
{0, 3, 5},
{1, 3, 15},
{2, 3, 4}
};

kruskal(V, edges, E);

return 0;
}
Output:

You might also like