Tuesday 11 March 2014

Graph Traversal

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

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

Representations of Graphs

Adjacency Matrices

Graphs = (V, E) can be represented by adjacency matrices G[v1..v||, v1..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
A0111
B1001
C1001
D1110
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 1041
B oo  oo  oo 15
C oo  oo  oo 9
D oo  oo  oo  oo
Adjacency matrices require O(||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(||E|) space.

No comments:

Post a Comment

 

FACEBOOK PAGE

SKETCHES & PAINTINGS