Kruskal's algorithm
1. Original procedure of the algorithm
Kruskal's algorithm is used to find the minimal spanning tree of a graph. We work with a weighted undirected graph G =(V, E, w) where V is a set of nodes, E is a set of undirected edges and w is a positive weight of any edge.
- At the beginning of the algorithm initialize a subgraph T =(V, \infty) containing all the nodes of the graph G and an empty set of edges (At the termination of the algorithm the subgraph T becomes a minimum spanning tree of the graph G.).
- Sort the edges of the graph G according to their weight and process them consecutively from the shortest one.
- Check if addition of the current edge to the subgraph T establishes a cycle.
- If not, add the edge to the subgraph T.
- Otherwise, do not add the edge to the subgraph T.
- After processing the last edge the subgraph T becomes the minimum spanning tree of the graph G.
When processing the algorithm in the standard way we work with a visual representation of a graph. When adding an edge to the subgraph T we highlight it directly in the graph (using a different color or any other means of highlighting). There is a graph H with seven nodes in Image 1 demonstrating the situation before processing the fourth edge between the nodes D and G. After the first three steps of the algorithm the edges CF, DE, CD were added to the subgraph T. The next one (DG) does not establish a cycle and therefore we can add it to the subgraph T as well.
Example 1: Weighted undirected graph H
We cannot add the next two edges of the weight 4 to the subgraph T because they would both establish a cycle of the size 3 (tripple of the nodes C, D, F or D, E, G).
To demonstrate Kruskal's algorithm we come with an Animation 2 of the computation and use a concrete weighted undirected graph.
Example 2: Weighted undirected graph H
-
- Start of the algorithm - undirected graph H with seven nodes A, B, C, D, E, F, G
Edges of the spanning tree: - Still not processed edges: C–2–F, D–2–E, C–3–D, D–3–G, D–4–F, E–4–G, A–5–C, B–5–D, A–6–D, B–6–E, A–7–B, F–7–G
Do not display this label