Chapter 14
Graph Traversal

14.1 Depth-First Traversal

algorithm  dft(x) 
   visit(x) 
   FOR each y such that (x,y) is an edge DO 
     IF y was not visited yet THEN 
        dft(y) 
A recursive algorithm implicitly recording a “backtracking” path from the root to the node currently under consideration

                    ---aoo
              --------||||
         --------- ||  | ||
boo--- oco --- odo----eoo    foo |||
      |     |     |    |    ||
      ogo --- oho --- oio--- joo  --oko
         ------      ------
              --- olo---                     ---aoo
               -------||||
         --------- ||  | ||
boo--- oco --- odo----eoo    foo ||
      |     |     |    |    ||
      ogo --- oho --- oio--- joo  --oko
         ------       ------
              --- olo---                     ---aoo
               -------||||
          ---------|||  |||
boo--- coo--- odo---- oeo|   foo |||
      |     |     |     |   ||
      goo--- oho --- oio--- joo  --oko
         ------       ------
              --- oo----
                  l                      --aoo
               -------||||
          ---------||| | ||
boo--- oco --- odo----eoo|   foo ||
      |     |     |    |   ||
      ogo --- oho --- oio--- joo  --oko
        -------       ------
              --- oo----
                  l                      --aoo
               -------||||
          ---------||| | ||
boo--- oco --- odo----eoo|   foo ||
      |     |     |    |   ||
      ogo --- oo --- oo--- joo   -oo
        ----h-    i   ------ k
              --- oo----
                  l                     ---aoo
              --------||||
         --------- ||  | ||
boo--- oco --- odo----eoo    foo |||
      |     |     |    |    ||
      ogo --- oho --- oio--- joo  --oko
         ------      ------
              --- olo---                      --oao
               -------||||
          ---------||| | ||
boo--- oco ---doo--- eoo|   ofo  ||
      |     |     |    |   ||
      ogo --- oo---  oo--- ojo   - oo
        ---h--   i    ------ k
             ---- oo----
                 l

                    ---aoo
              --------||||
         --------- ||  | ||
boo--- oco --- odo----eoo    foo |||
      |     |     |    |    ||
      ogo --- oho --- oio--- joo  --oko
         ------      ------
              --- olo---                      --aoo
               -------||||
          ---------||| | ||
boo--- oco --- odo----eoo|   foo ||
      |     |     |    |   ||
      ogo --- oo --- oo--- joo   -oo
        ----h-    i   ------ k
              --- oo----
                  l

                    ---aoo
              --------||||
         --------- ||  | ||
boo--- oco --- odo----eoo    foo |||
      |     |     |    |    ||
      ogo --- oho --- oio--- joo  --oko
         ------      ------
              --- olo---                     ---aoo
               -------||||
         --------- ||  | ||
boo--- oco --- odo----eoo    foo ||
      |     |     |    |    ||
      ogo --- oho --- oio ---joo  --oko
         ------       ------
              --- olo---                     ---aoo
               -------||||
         ----------||  | ||
boo--- oco --- odo----eoo|   foo ||
      |     |     |    |    |
      ogo --- oho --- oio ---joo  --oko
         ------       ------
              --- oo----
                  l                      --aoo
               -------||||
          ---------||| | ||
boo--- oco --- odo----eoo|   foo ||
      |     |     |    |   ||
      ogo --- oho --- oio ---joo  --oko
        -------       ------
              --- oo----
                  l                      --aoo
               -------||||
          ---------||| | ||
boo--- oco --- odo----eoo|   foo ||
      |     |     |    |   ||
      ogo --- oo --- oo ---joo   -oo
        ----h-    i   ------ k
              --- oo----
                  l                     ---aoo
              --------||||
         --------- ||  | ||
boo--- oco --- odo----eoo    foo |||
      |     |     |    |    ||
      ogo --- oho --- oio ---joo  --oko
         ------      ------
              --- olo---                     ---aoo
               -------||||
         --------- ||  | ||
boo--- oco --- odo----eoo    foo ||
      |     |     |    |    ||
      ogo --- oho --- oio ---joo  --oko
         ------       ------
              --- olo---                     ---aoo
               -------||||
         ----------||| | ||
boo--- oco --- odo ---eoo    foo ||
      |     |     |    |    |
      ogo --- oho --- oio ---joo  --oko
         ------       ------
              --- oo----
                  l                      --aoo
               -------||||
          ---------||| | ||
boo--- oco --- odo----eoo    foo ||
      |     |     |    |   ||
      ogo --- oho --- oio ---joo  --oko
        -------       ------
              --- oo----
                  l                      --aoo
               -------||||
          ---------||| | ||
boo--- oco --- odo----eoo|   foo ||
      |     |     |    |   ||
      ogo --- oo --- oo ---joo   -oo
        ----h-    i   ------ k
              --- oo----
                  l                     ---aoo
              --------||||
         --------- ||  | ||
boo--- oco --- odo----eoo    foo |||
      |     |     |    |    ||
      ogo --- oho --- oio--- joo  --oko
         ------      ------
              --- olo---                     ---aoo
               -------||||
         --------- ||  | ||
boo--- oco --- odo----eoo    foo ||
      |     |     |    |    ||
      ogo --- oho --- oio--- joo  --oko
         ------       ------
              --- olo---                     ---aoo
               -------||||
         ----------||  | ||
boo--- oco --- odo----eoo|   foo ||
      |     |     |    |    |
      ogo --- oho --- oio--- joo  --oko
         ------       ------
              --- oo----
                  l                      --aoo
               -------||||
          ---------||| | ||
boo--- oco --- odo----eoo|   foo ||
      |     |     |    |   ||
      ogo --- oho --- oio--- joo  --oko
        -------       ------
              --- oo----
                  l                      --aoo
               -------||||
          ---------||| | ||
boo--- oco --- odo----eoo|   foo ||
      |     |     |    |   ||
      ogo --- oo --- oo--- joo   -oo
        ----h-    i   ------ k
              --- oo----
                  l

14.2 Breadth-First Traversal

Visit the nodes at level i before the nodes of level i+1.

                   ---0oo
             --------||||
        --------- ||    ||
oo--- oo --- oo---- oo     oo |||
     |     |     |    |    ||
     oo --- oo --- oo---  oo  --oo
        ------      ------
             --- oo---

                   ---0oo
             --------||||
        --------- ||    ||
oo--- o1o --- o1o----1oo    1oo |||
     |     |     |    |    ||
     oo --- oo --- oo---  oo  --o1o
        ------      ------
             --- oo---                     ---0oo
               -------||||
         --------- ||| | ||
2oo--- o1o --- o1o----1oo    1oo ||
      |     |     |    |    ||
      o2o --- o2o ---2oo--- 2oo  --o1o
         ------       ------
              ---2oo---

visit(start node) 
queue <- start node 
WHILE queue is nor empty DO 
  x <- queue 
  FOR each y such that (x,y) is an edge 
                  and y has not been visited yet DO 
    visit(y) 
    queue <- y 
  END 
END 
                   ---1oo
             --------||||
        --------- ||  | ||
oo--- oo --- oo---- oo     oo |||
     |     |     |    |    ||
     oo --- oo --- oo---  oo  --oo
        ------      ------
             --- oo---                    ---o1o|
              -------||||
        --------- ||  | ||
oo--- o2o --- o3o----4oo    o5o  ||
     |     |     |    |   ||
     oo --- oo---  oo--- oo   - o6o
        ------      -------
             --- oo---                     ---1oo
               ------|||||
         ----------||| | ||
7oo--- o2o --- o3o ---4oo    5oo ||
      |     |     |    |    |
      o8o --- oo --- oo---  oo  --o6o
         ------       ------
              --- oo----                      --1oo
               ------|||||
          ---------||| | ||
7oo--- o2o --- o3o----4oo    5oo ||
      |     |     |    |   ||
      o8o --- o9o --- oo---  oo  --o6o
        -------       ------
              --- oo----                      --1oo
               ------|||||
          ---------||| | ||
7oo--- o2o --- o3o----4oo|   5oo ||
      |     |     |    |   ||
      oo --- oo --- oo --- oo   -oo
      8 ----9-   10   ------ 6
              --- oo----                     --|1oo
              -------|||||
         --------- ||    ||
7oo--- o2o --- o3o----4oo    5oo |||
      |     |     |    |    |
      o8o --- o9o ---1o0o ---1o1o  --o6o
         ------      ------
              --- oo---                     ---1oo
               ------|| ||
         ----------|||  |||
7oo--- 2oo--- o3o----4oo    5oo  ||
      |     |     |     |   ||
      8oo--- o9o ---1o0o ---1o1o  --o6o
         ------       ------
              ---1o2o---                      --1oo
                -----|||||
          ---------||| | ||
7oo--- o2o --- o3o----4oo|   5oo ||
      |     |     |    |   ||
      oo --- oo --- oo --- oo   -oo
      8 ----9-   10   -11----6
             ---- oo----
                 12                       -1oo
                -----|||||
           --------||| | ||
 oo--- oo ----oo---- oo|    oo ||
7     2     3    4|    5   ||
      oo --- oo --- oo --- oo   |oo
      8 ----9    10    11----6
             ---- oo-----
                 12                         oo
                 ----||1||
            --------|| | ||
 oo--- oo ----oo---- oo|    oo ||
7     2     3    4|    5   ||
                            ||
      o8o ----o9o ---1o0o ---1o1o----o6o
            -----  ------
                 1o2o                     --|1oo
              -------|||||
         --------- ||    ||
7oo--- o2o --- o3o----4oo    5oo |||
      |     |     |    |    ||
      o8o --- o9o ---1o0o--- 1o1o  --o6o
         ------      ------
              ---1o2o---                      --1oo
               ------|||||
          ---------||| | ||
7oo--- o2o --- o3o----4oo|   5oo ||
      |     |     |    |   ||
      o8o --- o9o ---1o0o--- 1o1o   -o6o
        ------        ------
              --- oo----
                 12                      --1oo
                -----|||||
           --------||| | ||
 oo--- oo --- oo---- oo|    oo ||
7     2     3    4|    5   ||
      oo --- oo --- oo---  oo   |oo
      8 ----9-   10    11----6
             ---- oo-----
                 12

14.3 Representations of Graphs

Adjacency Matrices

Graphs G = (V, E) can be represented by adjacency matrices G[v1..v|V |, v1..v|V |], where the rows and columns are indexed by the nodes, and the entries G[vi, vj] represent the edges. In the case of unlabeled graphs, the entries are just boolean values.

Ao---- Bo
 |---  |
 |  ---|
Co-----Do
ABCD
A0 1 1 1
B1 0 0 1
C1 0 0 1
D1 1 1 0

In case of labeled graphs, the labels themselves may be introduced into the entries.

Ao---10---||Bo
 |----       |
     ---
4|    1---  15
||       ---|||
 o---- ---||-o
C     9     D
ABCD
A oo 10 4 1
B oo  oo  oo 15
C oo  oo  oo 9
D oo  oo  oo  oo

Adjacency matrices require O(|V |2) space, and so they are space-efficient only when they are dense (that is, when the graphs have many edges). Time-wise, the adjacency matrices allow easy addition and deletion of edges.

Adjacency Lists

A representation of the graph consisting of a list of nodes, with each node containing a list of its neighboring nodes.

Ao----------Bo
 |---        |
 |  ----     |
 |     ---   |
 |       --- |
 o-----------o
C           D  |---|||--|-| ||--|-- | |-|--
A|----||B-|---||C-|---||D-|-|
 |----||--|---||--|--
B|---  |A-|-|  |D-|--
C| ---||A |---||D | |
 |---  |--|-|  |--|--   |-|--
D| ---||A-|---||B-|---||C-|-|
  ---

This representation takes O(|V | + |E|) space.

14.4 Demo Applets