1086 Assignement-4 (ADS)
1086 Assignement-4 (ADS)
ASSIGNMENT – 4
AIM : You are given an undirected weighted graph with nodes and edges. The nodes are numbered
from and to. Find the total weight of the minimum spanning tree, as well as one specific minimum
spanning tree using Krushkal’s algorithm. Note that there may be multiple different minimum
spanning trees. You need to construct any one of them.
THEORY :
Kruskal's algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a
connected, undirected graph with weighted edges. Here's a theoretical overview of Kruskal's
algorithm:
1. Initialization:
Begin with an empty set of edges, which will eventually form the minimum spanning
tree.
Sort all the edges of the graph in non-decreasing order of their weights.
2. Iterative Process:
Iterate through the sorted edges:
Select the edge with the smallest weight.
Check if adding this edge to the set of selected edges forms a cycle in the
current set. This can be efficiently done using a disjoint-set data structure.
If adding the edge does not create a cycle (i.e., the edge connects two disjoint
components of the spanning tree):
Include this edge in the set of selected edges, as it contributes to the
minimum spanning tree.
Union the two components connected by the selected edge.
3. Termination:
Continue this process until there are V - 1 edges in the minimum spanning tree, where
V is the number of vertices in the graph.
4. Output:
The output is the set of edges that form the minimum spanning tree.
Key Points:
Kruskal's algorithm grows the minimum spanning tree by iteratively adding the smallest edge
that does not form a cycle.
It ensures that the selected edges always form a tree and eventually cover all vertices of the
original graph.
Kruskal's algorithm is efficient for sparse graphs, where the number of edges is significantly
less than the number of vertices.
Complexity:
Time complexity: O(E log E) or O(E log V), depending on the sorting algorithm used, where
E is the number of edges and V is the number of vertices.
Space complexity: O(V + E), primarily for storing the edges and maintaining the disjoint-set
data structure.
Applications:
Network design, such as laying telecommunication cables.
Cluster analysis.
Image segmentation.
Kruskal's algorithm is widely used due to its simplicity and efficiency, especially for graphs with a
large number of vertices and relatively few edges. It guarantees the construction of a minimum
spanning tree while being easy to implement.
ALGORITHM:
1. Input:
Input the number of vertices V.
Input the list of edges, each containing source vertex, destination vertex, and weight.
2. Graph Initialization:
Initialize an empty list to store the edges of the minimum spanning tree (MST).
Initialize a disjoint set data structure to keep track of the connected components.
3. Sort Edges:
Sort the edges in non-decreasing order of their weights.
4. Kruskal's Algorithm:
Iterate through the sorted edges:
For each edge (u, v):
Check if adding this edge creates a cycle in the MST. This can be done
by checking if u and v belong to the same connected component in the
disjoint set.
If adding the edge (u, v) does not create a cycle:
Add the edge (u, v) to the MST.
Merge the connected components containing u and v by union
operation in the disjoint set.
5. Output:
Output the total weight of the minimum spanning tree.
Optionally, output the edges of the minimum spanning tree.
PROGRAM CODE:
import java.util.*;
@Override
public int compareTo(Edge other) {
return Integer.compare(this.weight, other.weight);
}
}
class DisjointSet {
int[] parent;
public DisjointSet(int n) {
parent = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
return minCost;
}
OUTPUT: