1.0 - Minimum Spanning Trees

Inputs

Output

1.1 - Generic Construction of a MST

💡 Incrementally construct T which is a set of edges, and which will eventually become a MST of G

genericMST(G, w):
  T = Graph() # Empty
  while T is not a spanning tree
		// invariant: T is a subset of some MST of G
    find an edge (u, v) that is safe for T // new edge added forms a new T that
                                           // is also a subset of a MST for the graph
    T = T ∪ {(u, v)}
  return T

1.1 - Prim’s Algorithm

🌱 Guaranteed to form a MST at the end of the algorithm if we choose least-weighted edges connected to our graph.

T is always a tree (a connected acyclic sub-graph of G):

1.1.1 - Priority Queues

A priority queue Q maintains a set S of elements, each associated with a key, denoting its priority

Operations are available to:

We have the possible implementations of priority queues, and their associated time complexities:

Unsorted List PQ

Sorted List PQ

Heap (Complete Tree)

As it is a complete tree, the height of the tree Θ(logn)\Theta(\log n)

1.1.2 - Implementation of Prim’s Algorithm Using PQ

MST_Prim(G, w, r)
	for each v ∈ G.V
			// v.key: least edge weight connecting v to T
			v.key = infinity 
			//vertex adjacent to v in T on least edge
      v.pi = NULL
	r.key = 0 // Initially set to highest priority in PQ
  while Q ≠ ∅
			// invariant: T is a subset of some MST of G
			//                where T = {(v, v.pi) : v \in V-{r}-Q
      u = extract_min(Q)
			// Need to re-compute the priority of edges in sub-MST
			// as we have changed the minimum spanning tree -priorities
			// might have changed
      for each v ∈ G.Adj[i] // "Edge relaxation process"
          if v ∈ Q and w(u, v) < v.key
							v.key = w(u, v) // Decrease the key
              v.pi = u 

1.1.3 - Prim’s Algorithm in Practice

1.1.4 - Performance of Prim’s Algorithm

Q <- V 
key[v] <- ∞ for all v ∈ V
key[s] <- 0 for some arbitrary s ∈ V
while Q ≠ ∅:
	do u <- extract_min(Q)
		for each v ∈ Adj[u]
			do if v ∈ Q and w(u, v) < key[v]
				then key[v] <- w(u, v)
					π[v] <- u
Θ(V)×Textract-min+Θ(E)×Tdecrease-key \Theta(V)\times T_{\text{extract-min}} +\Theta(E)\times T_{\text{decrease-key}}