Prim’s algorithm

The cut property naturally extends to an algorithm for finding the MST.

  1. Initialize cut for any arbitrary and the tree to begin with.
  2. Find the min-weight edge such that and it crosses over all the vertices in .
  3. Update , and add to .
  4. When the cut includes all the vertices in the graph , return as the MST for .

Finding the min-weight edge

This is not a trivial task and we need to use a min-heap in order to minimize the time we take to find the min-weight edge in .

  1. In addition to inserting into , we insert all edges into our min-heap, with their weights as the key.
  2. To get the min-weight edge, we simply extract the minimum.
  3. When we add vertex to , we remove all edges adjacent to that lead to a vertex back in from the min-heap, and we add all outgoing edges from to the min-heap.

Runtime

We run the primary loop once for every vertex, where . Here, we extract the minimum edge, which takes .

We also insert and remove edges for every vertex that we add to the cut, which depends on the degree of the vertex. However, we do this once for every vertex, so this value is just equal to , where .

Then runtime is given by .

CIS 1600 showed another way of using min-heaps that allows us to collapse the runtime down to … instead of storing the edges in the heap, store the vertices.

Kruskal’s algorithm

  1. Initialize empty set , and have each vertex be in its own connected component.
  2. Sort the edges according to their weight.
  3. Iterate through the edges in order. Given edge , we check if vertices and are already connected in our connected component. If not, then we include in , and merge the connected components that and belong to. Otherwise, we don’t add it.

Proof of correctness: using the cut property

When we iterate through the edges and we’re currently on edge , we can treat the set that belongs to as the cut , and the other vertices belong in . Then we can argue with the cut property that the MST must include , because we’ve sorted the edges by weight, so is the current minimum-weight edge.

Usage of union-find

How do we efficiently check if and are already connected? How do we efficiently merge connected components?

Storing our vertices in a disjoint-set data structure allows us to perform queries on two vertices in an efficient manner, and merge two connected components in a trivial manner. This allows us to perform queries in at most time, meaning that sorting the edges will dominate the overall time complexity: .