Minimum Spanning Tree (Kruskal's Algorithm)

jjb

JB

Posted on April 12, 2020

Minimum Spanning Tree (Kruskal's Algorithm)

Resources:

This post requires knowledge of graphs and union-find (covered in earlier posts).

  1. Kruskal's algorithm video explanation
  2. Another video explanation
  3. OO implementation of Kruskal's
  4. Wikipedia article on Minimum Spanning Tree

Takeaways:

  • A Minimum Spanning Tree (MST) is a subset of edges of an undirected, connected, weighted graph.
    • This means a MST connects all vertices together in a path that has the smallest total edge weight.
  • One algorithm for finding the MST of a graph is Kruskal's Algorithm.
  • Kruskal's algorithm is a greedy algorithm - this means it will make locally optimum choices, with an intent of finding the overall optimal solution.
  • Kruskal's algorithm relies on the union-find data structure.
    • First the algorithm sorts the graph's edges in ascending order (by weight).
    • Then for every edge, if it's vertices have different root vertices (determined by union-find's Find()), it will add the edge to a list & Union() it's vertices within the union-find data structure.
    • If roots are the same, it will skip the edge.
    • The final list represents the MST of the graph.
  • Another common algorithm for finding the MST of a graph is Prim's Algorithm. Commonly, Prim's uses a heap or priority queue in it's implementation.
  • Time complexity for Kruskal's algorithm is O(e log v) where e is the number of edges and v is the number of vertices in the graph. Space is O(e + v).

Below is my implementation of Kruskal's algorithm:

As always, if you found any errors in this post please let me know!

💖 💪 🙅 🚩
jjb
JB

Posted on April 12, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Traveling Salesman Problem
computerscience Traveling Salesman Problem

June 13, 2020

Fenwick Tree (Binary Indexed Tree)
computerscience Fenwick Tree (Binary Indexed Tree)

May 15, 2020

String Searching Using Rabin-Karp
computerscience String Searching Using Rabin-Karp

May 5, 2020

Sliding Window Technique
computerscience Sliding Window Technique

April 20, 2020

Minimum Spanning Tree (Kruskal's Algorithm)
computerscience Minimum Spanning Tree (Kruskal's Algorithm)

April 12, 2020