Kruskal's Algorithm
Kruskal's Algorithm
ALGORITHM
Kruskal’s algorithm creates a minimum spanning tree from a weighted undirected
graph by adding edges in increasing order of weights.
Kruskal’s algorithm is greedy in nature as the edges are chosen in the increasing order
of their weights.
The algorithm makes sure that the addition of new edges to the spanning tree does not
create a cycle within it.
A union-by-rank & path compression technique used for merging two disjoint subsets
could be used to efficiently add edges to the minimum spanning tree.
PythonC++Java
import java.util.ArrayList;
import java.util.List;
class Edge {
int node_start;
int node_end;
int weight;
Edge (int node1, int node2, int wt) {
node_start = node1;
node_end = node2;
weight = wt;
}
class Graph {
return parent.get(node);
return FindParent(parent.get(node)); */
}
g1.AddEdge(e1);
g1.AddEdge(e2);
g1.AddEdge(e3);
g1.AddEdge(e4);
g1.AddEdge(e5);
g1.AddEdge(e6);
g1.AddEdge(e7);
g1.AddEdge(e8);
g1.AddEdge(e9);
g1.AddEdge(e10);
num_nodes = 7;
mst.clear();
g2.KruskalMST(mst);
g2.DisplayEdges(mst);
}
}
Output