Exp Minimum Spanning Tree
Exp Minimum Spanning Tree
-15
Applications of MST:
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.
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.
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.
typedef struct {
int u, v, weight;
} Edge;
typedef struct {
int parent;
int rank;
} Subset;
Edge result[V];
int e = 0;
int i = 0;
int subsetCount = 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++];
// If including this edge does not form a cycle, include it in the result
if (x != y) {
result[e++] = nextEdge;
unionSets(subsets, x, y);
}
}
int main() {
int V = 4; // Number of vertices
int E = 5; // Number of edges
return 0;
}
Output: