Tuesday 11 March 2014

Least Cost Spanning Trees


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 ||) time for a graph = (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 for the nodes of the graph that have not been chosen yet.
  2. For the priority evaluation, assign each node in the least cost of adding it into (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 from the queue Q, and adding it into the spanning tree T, the priority value is modified for each node in 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(||) time. Each deletion of a node from the queue takes O(log ||) time, and so O(|| log ||) time takes to empty the queue. A modification of a priority value of a node in the queue takes O(log ||) time, and |Esuch changes are needed.
Consequently, the algorithm takes O(||) + O(|| log ||) + O(|E| log ||) = O(|E| log ||) 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

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(|| log ||) time for checking the existence of cycles (employing the union-find algorithm below). Hence, the algorithm is of time complexity O(|E| log |E|).

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.
  • Components are represented by linked lists, with each element carrying the name of its component. Initially, each of the components consists of a single node.
    --|
A -||| aO
---    A
---||| bO
B--    B
--|
C -||| cO
---    C
---||| dO
D--    D
--|
E -||| eO
---    E
---||| fO
F--    F
--|
G -||| gO
---    G
---||| hO
H--    H
--|
|I-||| iO
--     I
  • Merging of components is accomplished by moving the elements of the smaller set into the bigger one. To that end, the shorter linked list is traversed to change the name of the set recorded in the elements, and the the last element in the shorter linked list is set to point to the first element of the longer list.
     U  B--|
|A|-|| bO -||| aO
---    A       A
--|-|| cO
-C-    C
--|
|D|-|| dO
---    D
---||| eO
-E-    E
--|
|F-||| fO
---    F
--|-|| gO
-G-    G
--|
|H|-|| hO
---    H
---||| iO
-I|    I
     U  I--|
|A|-|| bO -||| aO
---    A       A
--|-|| cO
-C-    C
--|
|D|-|| dO
---    D
---||| eO
-E-    E
--|
|F-||| fO
---    F
--|-|| gO
-G-    G
--|
|H|-||  iO -||| hO
---    H       H
     U  C---|
|A -||  eO -||| bO -||- aO
---    A       A      A
----||  cO
-C-|   C
---|
|D -||-dO
---    D
--|-||  fO
-F-    F
---|
|G -||- gO
---    G
----||  iO -||| hO
-H-|   H       H
     U  G---|
|A -||  eO -||| bO -||- aO
---    A       A      A
----||  cO
-C-|   C
---|
|D -||- gO -||| dO
---    D       D
--|-||  fO
-F-    F
---|
|H -||  iO -||| hO
---    H       H
     U  F--|
A -||- fO  -|| eO -||| bO  -||  aO
---   A       A       A      A
---||- cO
C--   C
--|
D -||- gO  -|| dO
---   D       D
---||- iO  -|| hO
H--   H       H
     U  H--|
A -||| iO -||- hO  -|| fO -||| eO  -|| bO -||| aO
---    A      A       A       A      A       A
---||| cO
C--    C
--|
D -||| gO -||- dO
---    D      D
     U  A|--|
|A -||- gO -||| dO -||- iO  -|| hO -||| fO  -||  eO -||| bO -||- aO
 ---   A       A      A       A       A      A       A      A
|---||  cO
|C--   C
     U  A--|
A -||- cO  -|| gO -||| dO  -||  iO -||| hO -||- fO  -|| eO -||| bO  -|| aO
---   A       A       A      A       A      A       A       A      A
  • The time to merge two components is linear in the number of elements in the shorter list
  • Each node can be visited at most O(log ||) times, since each merge at least doubles the cardinality of the shorter list. Hence, the algorithm is of time complexity O(|| log ||)

No comments:

Post a Comment

 

FACEBOOK PAGE

SKETCHES & PAINTINGS