Minimum Spanning Tree

Overview

Let G=V,E be a connected, undirected graph. Let w(u,v) denote the weight of each edge {u,v}E.

A spanning tree T is an acyclic subset TE that connects all the vertices of G. A minimum spanning tree is a spanning tree with w(T), defined as follows, minimized:

w(T)={u,v}Tw(u,v)

Kruskal's Algorithm

Let G=V,E be an undirected graph and w:ER be a weight function. Kruskal's algorithm is a greedy algorithm for finding an MST in G. Assuming G is represented as an adjacency-list:

  1. Set A=.
  2. For each vertex vV, make a disjoint-set {v}.
  3. Create a sorted list of the edges in E in increasing order.
  4. For each edge {u,v} in the sorted list, check if the sets containing u and v are disjoint. If so, join the disjoint sets and set A=A{{u,v}}.

Upon termination, A is the desired MST.

kruskals-algorithm.gif

Prim's Algorithm

Let G=V,E be an undirected graph and w:ER be a weight function. Prim's algorithm is a greedy algorithm for finding an MST in G. Assume G is represented as an adjacency-list. Given an empty min priority queue Q and an arbitrary vertex sV:

  1. For each vV, set v.key= and v.π=NIL.
  2. Set r.key=0.
  3. Insert all vertices into Q.
  4. While Q:
  5. Let m be the extracted minimum of Q.
  6. For each vertex vQ adjacent to u such that w(u,v)<v.key:
    1. Set v.π=u and v.key=w(u,v).
    2. Decrease the key of v in Q.

Upon termination, {v,v.πvV{r}} is the desired MST.

prims-algorithm.gif

Powered by Forestry.md