Chapter 15
Least Cost Spanning Trees

15.1 Prim’s Algorithm

At each stage, include the least cost edge whose addition to the selected edges forms a tree.

 aoo----1----boo----7-----oco
7|   -----   |3  -----  |9
  oo---2------oo-----6--- oo
 d ----8     e    4     f
3|    5----- |5         |8
 goo---------hoo--------- oio
       8          1   oo----1---- oo----7---- oo
 a ----     b|      ----c
7|    2----- |3----6    |9
 doo----------oeo--------- ofo
 |  ---8-    |    4     |
3|oo---5------oo5-------- oo8
 g     8    h     1     i  aoo----1----boo----7-----oco
 | ------    |    ----- |
7|    2 -----|3----6    |9
 doo----8---- oeo----4---- ofo
3|    -----  |5         |8
 goo---5-----hoo--------- oio
       8          1  aoo----1----boo----7-----oco
7|   -----   |3  -----  |9
  oo---2------oeo-----6--- oo
 d ----8     |    4     f
3|    5------|5         |8
 goo---------hoo--------- oio
       8          1   oo----1---- oo----7---- oo
 a ----     b|     -----c
7|    2----- |3----6    |9
 doo----------oeo--------- ofo
3|  ---8-    |5   4     |8
 |oo---5------oo--------- oo
 g     8    h     1     i  aoo----1----boo----7-----oco
 |  -----    |    ----- |
7|    2 -----|3----6    |9
 doo----8---- oeo----4---- ofo
3|    -----  |5         |8
 goo---5-----hoo--------- oio
       8          1  aoo----1----boo----7-----oco
7|   -----   |3 ------  |9
  oo---2------oeo-----6--- oo
 d ----8     |    4     f
3|    5------|5         |8
 goo---------hoo--------- oio
       8          1  aoo----1----boo----7-----oco
7|   -----   |3  -----  |9
  oo---2------oo-----6--- oo
 d ----8     e    4     f
3|    5----- |5         |8
 goo---------hoo--------- oio
       8          1

The approach takes O(|E| log |V |) time for a graph G = (V, E). The time requirement is driven by the algorithm used for selecting the edges to be added in each stage.

  1. Assume a priority queue Q for the nodes of the graph that have not been chosen yet.
  2. For the priority evaluation, assign each node in Q the least cost of adding it into V - Q (i.e., the nodes already selected for inclusion the spanning tree T).
  3. Initially, the priority queue is a complete tree, and each node carries a priority value  oo larger than the cost of all the edges.
  4. Upon removing a node u from the queue Q, and adding it into the spanning tree T, the priority value is modified for each node v in Q that is adjacent to u. The changing of the priority value of a node may require the shifting of the up or down in the tree.
The initialization of the the priority queue takes O(|V |) time. Each deletion of a node from the queue takes O(log |V |) time, and so O(|V | log |V |) time takes to empty the queue. A modification of a priority value of a node in the queue takes O(log |V |) time, and |E| such changes are needed.

Consequently, the algorithm takes O(|V |) + O(|V | log |V |) + O(|E| log |V |) = O(|E| log |V |) time.

                                      e
 oao ---1----- oo----7---- oco            |2o|
 |------    b|    ------|           ||  |||
7|    2----- 3-----6    |9         ||     ||
 odo----------eoo--------- ofo        c7o         oo fo
3|   -8---   5    4     |8      ||||       | |
 ogo ---5----- oo--------- oo       |  ||     |   |
      8     h     1     i     do    oo go   ho    io
                               7          oo     oo

 oao ---1----- oo----7---- oco            ||o|
 |------    b|    ------|           ||  |||
7|    2----- 3-----6    |9         ||     ||
 odo----------eoo--------- ofo        c7o         oo fo
3|   -8---   5    4     |8      ||||       | |
 ogo ---5----- oo--------- oo       |  ||     |   |
      8     h     1     i     do    oo go   ho    io
                               7          oo     oo         io
      | oo ||
    |||   ||
  c |       |f
  7o         oo o
 || |      || ||
 |  |g     |   |
d7o    oo o   h oo o    o        io
       oo ||
    |||  ||
   co|     |fo
  |7|      oo 
 || ||     |
do   |go    ho
7     oo     oo        c7o
     || ||
    ||   ||
   io       fo
   oo |      oo 
 ||  |     |
do    go    ho
7     oo     oo       |c7o|
     || ||
    ||    |
   d7o       oo fo
  ||||     |
 ||  |     |
 io    oo go   ho
 oo          oo        co
      |6|
     ||  ||
    ||    |
   d7o|      oo fo
  | ||     |
 ||  |
 oo io   oo go   oo ho       |c6o|
     || ||
    ||    |
   d7o       f4o
  ||||     |
 ||  |     |
 io    oo go   ho
 oo          oo        fo
      |4|
     ||  ||
    ||    |c
   d7o|      6o
  | ||     |
 ||  |
 oo io   oo go   oo ho        fo
      |4||
    |||  ||
   do|     |co
  |7|      6
  | ||     |
 i   |g    h
 oo o   oo o   5o        fo
     ||4||
    ||   ||
   do       ho
  |7|      5
 || ||     |
 io    go    co
 oo     oo     6

15.2 Kruskal’s Algorithm

A greedy algorithm: Visit the edges in order of increasing cost. Include just those edges that don’t create cycles.

 aoo----1----boo----7-----oco
7|   -----   |3  -----  |9
  oo---2------oo-----6--- oo
 d ----8     e    4     f
3|    5----- |5         |8
 goo---------hoo--------- oio
       8          1   oo----1---- oo----7---- oo
 a ----     b|      ----c
7|    2----- |3----6    |9
 doo----------oeo--------- ofo
 |  ---8-    |    4     |
3|oo---5------oo5-------- oo8
 g     8    h     1     i

 aoo----1----boo----7-----oco
7|   -----   |3  -----  |9
  oo---2------oo-----6--- oo
 d ----8     e    4     f
3|    5----- |5         |8
 goo---------hoo--------- oio
       8          1   oo----1---- oo----7---- oo
 a ----     b|      ----c
7|    2----- |3----6    |9
 doo----------oeo--------- ofo
 |  ---8-    |    4     |
3|oo---5------oo5-------- oo8
 g     8    h     1     i

 aoo----1----boo----7-----oco
7|   -----   |3  -----  |9
  oo---2------oo-----6--- oo
 d ----8     e    4     f
3|    5----- |5         |8
 goo---------hoo--------- oio
       8          1   oo----1---- oo----7---- oo
 a ----     b|      ----c
7|    2----- |3----6    |9
 doo----------oeo--------- ofo
 |  ---8-    |    4     |
3|oo---5------oo5-------- oo8
 g     8    h     1     i

 aoo----1----boo----7-----oco
7|   -----   |3  -----  |9
  oo---2------oo-----6--- oo
 d ----8     e    4     f
3|    5----- |5         |8
 goo---------hoo--------- oio
       8          1   oo----1---- oo----7---- oo
 a ----     b|      ----c
7|    2----- |3----6    |9
 doo----------oeo--------- ofo
 |  ---8-    |    4     |
3|oo---5------oo5-------- oo8
 g     8    h     1     i

Kruskal’s algorithm requires O(|E| log |E|) time for sorting the edges, O(|E|) time to traversing them, and O(|V | log |V |) time for checking the existence of cycles (employing the union-find algorithm below). Hence, the algorithm is of time complexity O(|E| log |E|).

15.3 Union-Find Algorithm

Kruskal’s algorithm relies on a union-find algorithm for checking cycles. The “union” operation unites equivalent sets of elements, and the “find” operation determines the sets in which the elements reside.

15.4 Demo Applets and Resources

15.5 Assignment #7 (due We, Nov 10)

Write a (real) program that reads the edges of a directed graph, internally stores the graph using the adjacency list approach, and then prints

  1. The list of nodes, and the list of nodes that are adjacent to each of these node.
  2. An adjacency matrix of the graph.
  3. A topologically sorted list of the nodes of the graph, if the graph is acyclic.

    A list of nodes is said to be topologically sorted, if node ‘A’ precedes node ‘B’ in the listing whenever there exists an edge from node ‘A’ to node ‘B’.

    The lists of nodes ‘A,B,C’ and ‘A,C,B’ are the only topologically sorted lists for the the following graph. The list ‘B,A,C’ is an example of one which is not topologically ordered.

         ----Bo
  ----
Ao---
    -----
        -Co

    Hint: Assume an indegree field in each node.

Your program should be well documented, and accept files of pairs of integers for input. Each pair represents an edge.

Submit a printout of the program, of an input file, and of the corresponding output.