Go to content messages.homepage.accessibility

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.

  1. 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.).
  2. Sort the edges of the graph G according to their weight and process them consecutively from the shortest one.
  3. Check if addition of the current edge to the subgraph T establishes a cycle.
    1. If not, add the edge to the subgraph T.
    2. Otherwise, do not add the edge to the subgraph T.
  4. 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
Image 1: Weighted undirected graph H, highlighting edges of the subgraph T and a currently processed edge

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
  • https://www.teiresias.muni.cz/amalg/www/images/animation/Kruskal/Minimum_spanning_tree_norm14_1.jpg
  • 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: C2F, D2E, C3D, D3G, D4F, E4G, A5C, B5D, A6D, B6E, A7B, F7G
    Do not display this label
Animation 1: finding a minimum spanning tree with Kruskal's algorithm

2. Proposals of adaptation

3. Discussion of pros and cons