23.2 The algorithms of Kruskal and Prim
23.2-1
Kruskal's algorithm can return different spanning trees for the same input graph , depending on how it breaks ties when the edges are sorted into order. Show that for each minimum spanning tree of , there is a way to sort the edges of in Kruskal's algorithm so that the algorithm returns .
Suppose that we wanted to pick as our minimum spanning tree. Then, to obtain this tree with Kruskal's algorithm, we will order the edges first by their weight, but then will resolve ties in edge weights by picking an edge first if it is contained in the minimum spanning tree, and treating all the edges that aren't in as being slightly larger, even though they have the same actual weight.
With this ordering, we will still be finding a tree of the same weight as all the minimum spanning trees . However, since we prioritize the edges in , we have that we will pick them over any other edges that may be in other minimum spanning trees.
23.2-2
Suppose that we represent the graph as an adjacency matrix. Give a simple implementation of Prim's algorithm for this case that runs in time.
At each step of the algorithm we will add an edge from a vertex in the tree created so far to a vertex not in the tree, such that this edge has minimum weight. Thus, it will be useful to know, for each vertex not in the tree, the edge from that vertex to some vertex in the tree of minimal weight. We will store this information in an array , where if is the weight of and is minimal among the weights of edges from to some vertex in the tree built so far. We'll use to access and to access .
PRIM-ADJ(G, w, r)
initialize A with every entry = (NIL, ∞)
T = {r}
for i = 1 to V
if Adj[r, i] != 0
A[i] = (r, w(r, i))
for each u in V - T
k = min(A[i].2)
T = T ∪ {k}
k.π = A[k].1
for i = 1 to V
if Adf[k, i] != 0 and Adj[k, i] < A[i].2
A[i] = (k, Adj[k, i])
23.2-3
For a sparse graph , where , is the implementation of Prim's algorithm with a Fibonacci heap asymptotically faster than the binary-heap implementation? What about for a dense graph, where ? How must the sizes and be related for the Fibonacci-heap implementation to be asymptotically faster than the binary-heap implementation?
Prim's algorithm implemented with a Binary heap has runtime , which in the sparse case, is just . The implementation with Fibonacci heaps is
- In the sparse case, the two algorithms have the same asymptotic runtimes.
-
In the dense case.
-
The binary heap implementation has a runtime of
-
The Fibonacci heap implementation has a runtime of
So, in the dense case, we have that the Fibonacci heap implementation is asymptotically faster.
-
-
The Fibonacci heap implementation will be asymptotically faster so long as . Suppose that we have some function that grows more quickly than linear, say , and .
-
The binary heap implementation will have runtime of
However, we have that the runtime of the Fibonacci heap implementation will have runtime of
This runtime is either or depending on if grows more or less quickly than respectively.
In either case, we have that the runtime is faster than .
23.2-4
Suppose that all edge weights in a graph are integers in the range from to . How fast can you make Kruskal's algorithm run? What if the edge weights are integers in the range from to for some constant ?
(Removed)
23.2-5
Suppose that all edge weights in a graph are integers in the range from to . How fast can you make Prim's algorithm run? What if the edge weights are integers in the range from to for some constant ?
For the first case, we can use a van Emde Boas tree to improve the time bound to . Comparing to the Fibonacci heap implementation, this improves the asymptotic running time only for sparse graphs, and it cannot improve the running time polynomially. An advantage of this implementation is that it may have a lower overhead.
For the second case, we can use a collection of doubly linked lists, each corresponding to an edge weight. This improves the bound to .
23.2-6
Suppose that the edge weights in a graph are uniformly distributed over the halfopen interval . Which algorithm, Kruskal's or Prim's, can you make run faster?
For input drawn from a uniform distribution I would use bucket sort with Kruskal's algorithm, for expected linear time sorting of edges by weight. This would achieve expected runtime .
23.2-7
Suppose that a graph has a minimum spanning tree already computed. How quickly can we update the minimum spanning tree if we add a new vertex and incident edges to ?
(Removed)
23.2-8
Professor Borden proposes a new divide-and-conquer algorithm for computing minimum spanning trees, which goes as follows. Given a graph , partition the set of vertices into two sets and such that and differ by at most . Let be the set of edges that are incident only on vertices in , and let be the set of edges that are incident only on vertices in . Recursively solve a minimum-spanning-tree problem on each of the two subgraphs and . Finally, select the minimum-weight edge in that crosses the cut , and use this edge to unite the resulting two minimum spanning trees into a single spanning tree.
Either argue that the algorithm correctly computes a minimum spanning tree of , or provide an example for which the algorithm fails.
The algorithm fails. Suppose , the weight of and is , and the weight of is , partition the set into two sets and .