# On finding and updating spanning trees and shortest paths

### On finding and updating spanning trees and shortest paths - how long were nick jonas and miley cyrus dating

KRUSKAL'S MINIMUM SPANNING TREE GREEDY ALGORITHM Given a cost-weighted (connected) graph, recall that the greedy algorithm due to Kruskal for constructing a minimum cost spanning tree adds edges one a time, where the next edge to be added is the smallest cost edge amongst those not already added such that adding the edge does not create a cycle.We now prove that this greedy stategy actually builds a minimum cost spanning tree.

We assume that the spanning tree grown by Kruskal's algorithm does not have minimum cost, and then we reach a contradiction.

Proof of Kruskal's algorithm Let K be the tree grown by Kruskal's algorithm, and suppose there are spanning trees having smaller cost than K.

Let e'), so that the cost of T' is no more than the cost of T (since we have replaced an edge of T with an edge of no more cost).

Hence, the cost of T' is also less than the cost of K.

But T' agrees with K in the first k+1 edges, a contradiction.

PRIM'S MINIMUM SPANNING TREE ALGORITHM Kruskal's minimum spanning tree algorithm built the final tree by adding edges (in increasing order of weights) to a growing forest of trees, until one finally arrives at a spanning tree when the (n-1)st edge is added.

If the graph G has m edges, and we sort the edges in increasing order, then Kruskal's algorithm has complexity O(mlog m), since we can sort the edges using an optimal algorithm such as Mergesort having O(mlog m) complexity, and we can check for cycles with complexity O(mlog m) if we use the union and find algorithms discussed earlier.Some improvement on average results by using a (min) heap to store the edges (since the tree might be constructed long before all the m edges are considered), but the worst case remains mlog m.PRIM'S ALGORITHM FOR A MINIMUM COST SPANNING TREE We now consider another greedy algorithm for constructing a minimum cost spanning tree due to Prim.Prim's algorithm grows a spanning tree starting from a given root vertex r (any vertex will do), and adds edges in such a way as to maintain a growing tree at each stage (as opposed to a forest as grown by Kruskal's algorithm).In order to give a high-level description of Prim's algorithm, we need to define Cut(T) where T is any tree in G.Cut(T) is the collection of all edges in G which have exactly one endpoint in T.