Discrete Structure L6
Discrete Structure L6
SPD3255
                           Discrete Structures
      Topic 6
      ~ Graphs (Part I)
Lecture notes are based on textbook and various resources over the Internet
The use of this lecture note is solely for academic purpose and is subject to the approval of course instructor
                         SPD3255 Discrete Structures
2   Graph Fundamentals
        Graphs
3
                          a                                  a
                                5.2                11              20
                 10
                          3.1                                7      d
                                  d
      20         b                          21      b
                           -5         1.7                    2.9        -1
                      9                     c      4.2
      c                           f                                 f
                      e                                  e
           2.7                                   61.2
b a b a b a
b a b a b a
b a a b a
           a                 b
     Graph Representations
10
a b c d e f a b d NULL
     a   1    1   0   1   0   0   b   a   d   f      e      c   NULL
     b   1    1   1   1   1   1   c   b   e   NULL
     c   0    1   1   0   1   0
 M                                d   a   b   f      NULL
     d   1    1   0   1   0   1
     e   0    1   1   0   1   0   e   c   b   NULL
     f   0    1   0   1   0   1   f   b   d   NULL
Graph Representations – Adjacency Matrix
 Suppose we have a graph
 G = (V,E) with n or |V|
 vertices, then an                                     a                               a
 adjacency matrix M is an n
                                                               d                               d
 by n matrix defined as                        b                               b
 follows:
                                   c                                   c                       f
                                                               f
      1 if (i, j) is an edge                       e                               e
mij
      0      otherwise
                                           a b c d e f                         a b c d e f
                                       a   1   1   0   1   0       0       a   0   0   0   0   0   0
                                       b   1   1   1   1   1       1       b   1   0   1   0   1   1
                                       c   0   1   1   0   1       0       c   0   0   0   0   0   0
                               M                                       M
                                       d   1   1   0   1   0       1       d   1   1   0   0   0   0
                                       e   0   1   1   0   1       0       e   0   0   1   0   0   0
                                       f   0   1   0   1   0       1       f   0   0   0   1   0   0
     Adjacency Matrix
12
      Pros:
         Easy to determine whether an edge
         exist between any two nodes
         Easy to add or remove an edge
      Cons:
         Require |V|2 or n2 space and memory
         space is wasted if the graph is not
         complete                                            a b c d e f
         Require |V| or n time to check whether          a   1   1   0   1   0   0
         all nodes could be reached by a specific        b   1   1   1   1   1   1
         node                                            c   0   1   1   0   1   0
         Difficult to add or remove a node           M
                                                         d   1   1   0   1   0   1
         If the graph is undirected, the matrix is       e   0   1   1   0   1   0
         symmetric which is again a waste of
         memory                                          f   0   1   0   1   0   1
     Graph Representations
     – Adjacency List
13
         An adjacency list is an array of linked lists
         showing the adjacent nodes of every node
         The following is an example of undirected
         graph and its corresponding adjacency list:
a b d NULL
a b a d f e c NULL
                       d     c                  e
           b                            b                NULL
                             d          a       b        f      NULL
     c                 f
               e             e          c       b        NULL
                             f          b       d        NULL
     Graph Representations – Adjacency List
14
         The following is an example of directed graph and its corresponding
         adjacency list:
a NULL
a b a f e c NULL
                      d    c         NULL
          b
                           d          a       b      NULL
     c                f
              e            e          c      NULL
                           f          d      NULL
     Adjacency List
15
      Pros:
         Only require |V|+|E| or
         n+e space
         Require |V| or n for
         finding all neighboring
         nodes of a specific node
      Cons:
         Require |V| or n time to
         determine if there a node
         is connected to another
         node
         Require |V| or n time to
         add or remove an edge
         Difficult to add or remove
         a node
     Graph Traversal
16
      Idea:
      Starting from a node S, visit the other
      nodes / vertices at increasing distances
      (i.e. number of edges) from S
      Algorithm:
         For each node / vertex v in graph, mark
         every vertex as unvisited
         Mark the start node S as visited
         enqueue S to a queue Q
         while(Q is NOT empty)
          v = dequeue Q
          for each w adjacent to v
             if w is not visited
                mark it to visited & enqueue it to Q
     Breadth-First Search - Example
19
         Assume
              Start from node a
              Use adjacency list as
              graph representation
                             a        b   d   NULL
                   a         b        a   d   f      e      c   NULL
                       d     c
          b                           b   e   NULL
                             d        a   b   f      NULL
     c                 f
               e             e        c   b   NULL
                             f        b   d   NULL
     Breadth-First Search
     – Example (Initial)
20
                     a
                             d          Q={}
             b
                                        Order of visit:
         c                   f
                 e
                                        Q={a}
                             d
             b
                                        Order of visit: a
         c                   f
                 e
                                        Q = { b, d }
                             d
             b
                                        Order of visit: a
         c                   f
                 e
                                        Q = { d, f, e, c }
                             d
             b
                                        Order of visit: a, b
         c                   f
                 e
                                        Q = { f, e, c }
                             d
             b
                                        Order of visit: a, b, d
         c                   f
                 e
                                        Q = { e, c }
                             d
             b
                                        Order of visit: a, b, d, f
         c                   f
                 e
                                        Q={c }
                             d
             b
                                        Order of visit: a, b, d, f, e
         c                   f
                 e
                                        Q={ }
                             d
             b
                                        Order of visit: a, b, d, f, e, c
         c                   f
                 e
         Assume
              Start from node a
              Use adjacency list as graph representation
a b d NULL
a b a d f e c NULL
                       d      c         b        e         NULL
          b
                              d         a        b         f      NULL
     c                 f
               e              e         c        b         NULL
                              f         b        d         NULL
         Breadth-First Search
         – Example (Initial)
30
                                          Q={}
                               d
                   b
                                          Order of visit:
           c                   f
                       e
                                                                              Predecessor and
                                                              Visited Table   distance table
     a         b           d       NULL
                                                                    a   F      a   NULL   ∞
     b         a           d       f      e        c        NULL
                                                                    b   F      b   NULL   ∞
     c         b           e       NULL                             c   F      c   NULL   ∞
d a b f NULL d F d NULL ∞
     e                                                              e   F      e   NULL   ∞
               c           b       NULL
                                                                    f   F      f   NULL   ∞
     f         b           d       NULL
         Breadth-First Search
         – Example (Step 1)
31
                                          Q={a}
                               d
                   b
                                          Order of visit: a
           c                   f
                       e
                                                                                Predecessor and
                                                                Visited Table   distance table
     a         b           d       NULL
                                                                      a   T      a   NULL   0
     b         a           d       f      e        c          NULL
                                                                      b   F      b   NULL   ∞
     c         b           e       NULL                               c   F      c   NULL   ∞
d a b f NULL d F d NULL ∞
     e                                                                e   F      e   NULL   ∞
               c           b       NULL
                                                                      f   F      f   NULL   ∞
     f         b           d       NULL
         Breadth-First Search
         – Example (Step 2)
32
                                              Q = { b, d }
                                   d
                   b
                                              Order of visit: a
           c                       f
                       e
                                                                                    Predecessor and
                                                                    Visited Table   distance table
     a         b               d       NULL
                                                                          a   T      a   NULL   0
     b         a               d       f      e        c          NULL
                                                                          b   T      b     a    1
     c         b               e       NULL                               c   F      c   NULL   ∞
d a b f NULL d T d a 1
     e                                                                    e   F      e   NULL   ∞
               c               b       NULL
                                                                          f   F      f   NULL   ∞
     f         b               d       NULL
         Breadth-First Search
         – Example (Step 3)
33
                                              Q = { d, f, e, c }
                                   d
                   b
                                              Order of visit: a, b
           c                       f
                       e
                                                                                     Predecessor and
                                                                     Visited Table   distance table
     a         b               d       NULL
                                                                           a   T      a   NULL   0
     b         a               d       f      e         c          NULL
                                                                           b   T      b     a    1
     c         b               e       NULL                                c   T      c     b    2
d a b f NULL d T d a 1
     e                                                                     e   T      e     b    2
               c               b       NULL
                                                                           f   T      f     b    2
     f         b               d       NULL
         Breadth-First Search
         – Example (Step 4)
34
                                          Q = { f, e, c }
                               d
                   b
                                          Order of visit: a, b, d
           c                   f
                       e
                                                                                Predecessor and
                                                                Visited Table   distance table
     a         b           d       NULL
                                                                      a   T      a   NULL   0
     b         a           d       f      e         c        NULL
                                                                      b   T      b     a    1
     c         b           e       NULL                               c   T      c     b    2
d a b f NULL d T d a 1
     e                                                                e   T      e     b    2
               c           b       NULL
                                                                      f   T      f     b    2
     f         b           d       NULL
         Breadth-First Search
         – Example (Step 5)
35
                                          Q = { e, c }
                               d
                   b
                                          Order of visit: a, b, d, f
           c                   f
                       e
                                                                                Predecessor and
                                                                Visited Table   distance table
     a         b           d       NULL
                                                                       a   T     a   NULL   0
     b         a           d       f      e         c         NULL
                                                                       b   T     b     a    1
     c         b           e       NULL                                c   T     c     b    2
d a b f NULL d T d a 1
     e                                                                 e   T     e     b    2
               c           b       NULL
                                                                       f   T     f     b    2
     f         b           d       NULL
         Breadth-First Search
         – Example (Step 6)
36
                                          Q={c }
                               d
                   b
                                          Order of visit: a, b, d, f, e
           c                   f
                       e
                                                                                  Predecessor and
                                                                 Visited Table    distance table
     a         b           d       NULL
                                                                          a   T    a   NULL   0
     b         a           d       f      e         c         NULL
                                                                          b   T    b     a    1
     c         b           e       NULL                                   c   T    c     b    2
d a b f NULL d T d a 1
     e                                                                    e   T    e     b    2
               c           b       NULL
                                                                          f   T    f     b    2
     f         b           d       NULL
         Breadth-First Search
         – Example (Step 7)
37
                                          Q={ }
                               d
                   b
                                          Order of visit: a, b, d, f, e, c
           c                   f
                       e
                                                                                 Predecessor and
                                                                 Visited Table   distance table
     a         b           d       NULL
                                                                        a    T    a   NULL   0
     b         a           d       f      e         c         NULL
                                                                        b    T    b     a    1
     c         b           e       NULL                                 c    T    c     b    2
d a b f NULL d T d a 1
     e                                                                  e    T    e     b    2
               c           b       NULL
                                                                         f   T    f     b    2
     f         b           d       NULL
     Paths from Source to Each Node
38
      Algorithm:
      Path(w)                                                    a
e b a 1
                                                                                      c     b    2
                                                         a
                                                                                      d     a    1
                                                1                                1    e     b    2
                                                    b                    d
                                                                                      f     b    2
                                        2   c                f
                                                                     2
                                                    e
                                                2
                                                    BFS Tree
                           SPD3255 Discrete Structures
      Idea:
         Similar to pre-order traversal of tree in which visit children node first
         Continue visit neighbors in a recursive manner,
         i.e. if we visit v from u, we recursively visit all unvisited neighbors of v and then
         return back to u
      Algorithm:
         DFS(s)
          for each node v
              set it to unvisited
          RDFS(s);
         RDFS(v)
          mark v as visited
          for each neighbor node w of v
             if w is unvisited, RDFS(w)
     Depth-First Search - Example
41
         Assume
          Start from node a
          Use adjacency list as
          graph representation
                          a       b   d   NULL
                  a
                          b       a   d   f      e      c   NULL
                      d
          b               c       b   e   NULL
     c                    d       a   b   f      NULL
                      f
              e           e       c   b   NULL
                          f       b   d   NULL
         Depth-First Search
         – Example (Initial)
42
                               d
                   b
                                          Order of visit:
           c                   f
                       e
                                                                            Predecessor
                                                              Visited Table table
     a         b           d       NULL
                                                                    a   F      a   NULL
     b         a           d       f      e        c        NULL
                                                                    b   F      b   NULL
c b e NULL c F c NULL
d a b f NULL d F d NULL
     e                                                              e   F      e   NULL
               c           b       NULL
                                                                    f   F      f   NULL
     f         b           d       NULL
         Depth-First Search
         – Example (Step 1)
43
                           (1
                           a
                                d
                   b
                                           Order of visit:
           c                    f
                       e
                                                                             Predecessor
                                                               Visited Table table
     a         b           d        NULL
                                                                     a   T      a   NULL
     b         a           d        f      e        c        NULL
                                                                     b   F      b   NULL
c b e NULL c F c NULL
d a b f NULL d F d NULL
     e                                                               e   F      e   NULL
               c           b        NULL
                                                                     f   F      f   NULL
     f         b           d        NULL
         Depth-First Search
         – Example (Step 2)
44
                            (1
                            a
           (2
                                 d
                    b
                                            Order of visit:
           c                     f
                        e
                                                                              Predecessor
                                                                Visited Table table
     a          b           d        NULL
                                                                      a   T      a   NULL
     b          a           d        f      e        c        NULL
                                                                      b   T      b     a
c b e NULL c F c NULL
d a b f NULL d F d NULL
     e                                                                e   F      e   NULL
                c           b        NULL
                                                                      f   F      f   NULL
     f          b           d        NULL
         Depth-First Search
         – Example (Step 3)
45
                           (1
                           a
                                    (3
            (2
                                d
                   b
                                           Order of visit:
           c                    f
                       e
                                                                             Predecessor
                                                               Visited Table table
     a         b           d        NULL
                                                                     a   T      a   NULL
     b         a           d         f     e        c        NULL
                                                                     b   T      b     a
c b e NULL c F c NULL
d a b f NULL d T d b
     e                                                               e   F      e   NULL
               c           b        NULL
                                                                     f   F      f   NULL
     f         b           d        NULL
         Depth-First Search
         – Example (Step 4)
46
                            (1
                            a
                                     (3
           (2
                                 d
                    b
                                              Order of visit:
                                     (4
           c                     f
                        e
                                                                                Predecessor
                                                                  Visited Table table
     a          b           d         NULL
                                                                        a   T      a   NULL
     b          a           d             f   e        c        NULL
                                                                        b   T      b     a
c b e NULL c F c NULL
d a b f NULL d T d b
     e                                                                  e   F      e   NULL
                c           b         NULL
                                                                        f   T      f     d
     f          b           d         NULL
         Depth-First Search
         – Example (Step 5)
47
                            (1
                            a
                                     (3
           (2
                                 d
                    b
                                              Order of visit: f
                                      (4,5)
           c                     f
                        e
                                                                                  Predecessor
                                                                    Visited Table table
     a          b           d         NULL
                                                                          a   T      a   NULL
     b          a           d             f   e        c          NULL
                                                                          b   T      b     a
c b e NULL c F c NULL
d a b f NULL d T d b
     e                                                                    e   F      e   NULL
                c           b         NULL
                                                                          f   T      f     d
     f          b           d         NULL
         Depth-First Search
         – Example (Step 6)
48
                            (1
                            a
                                     (3,6)
           (2
                                 d
                    b
                                              Order of visit: f, d
                                      (4,5)
           c                     f
                        e
                                                                                   Predecessor
                                                                     Visited Table table
     a          b           d         NULL
                                                                           a   T      a   NULL
     b          a           d          f      e         c        NULL
                                                                           b   T      b     a
c b e NULL c F c NULL
d a b f NULL d T d b
     e                                                                     e   F      e   NULL
                c           b         NULL
                                                                           f   T      f     d
     f          b           d         NULL
         Depth-First Search
         – Example (Step 7)
49
                        (1
                             a
                                      (3,6)
           (2
                                  d
                    b
                                               Order of visit: f, d
                                       (4,5)
           c                      f
                        e
                             (7                                                     Predecessor
                                                                      Visited Table table
     a          b            d         NULL
                                                                            a   T      a   NULL
     b          a            d          f      e         c        NULL
                                                                            b   T      b     a
c b e NULL c F c NULL
d a b f NULL d T d b
     e                                                                      e   T      e     b
                c            b         NULL
                                                                            f   T      f     d
     f          b            d         NULL
          Depth-First Search
          – Example (Step 8)
50
                               (1
                               a
                                        (3,6)
              (2
                                    d
                       b
                                                 Order of visit: f, d
                                         (4,5)
              c                     f
         (8                e
                               (7                                                     Predecessor
                                                                        Visited Table table
     a             b           d         NULL
                                                                              a   T      a   NULL
     b             a           d          f      e         c        NULL
                                                                              b   T      b     a
c b e NULL c T c e
d a b f NULL d T d b
     e                                                                        e   T      e     b
                   c           b         NULL
                                                                              f   T      f     d
     f             b           d         NULL
          Depth-First Search
          – Example (Step 9)
51
                                  (1
                                  a
                                           (3,6)
                 (2
                                       d
                          b
                                                    Order of visit: f, d, c
                                            (4,5)
                 c                     f
         (8,9)                e
                                  (7                                                        Predecessor
                                                                              Visited Table table
     a                b           d         NULL
                                                                                    a   T      a   NULL
     b                a           d          f      e         c         NULL
                                                                                    b   T      b     a
c b e NULL c T c e
d a b f NULL d T d b
     e                                                                              e   T      e     b
                      c           b         NULL
                                                                                    f   T      f     d
     f                b           d         NULL
          Depth-First Search
          – Example (Step 10)
52
                                  (1
                                  a
                                           (3,6)
                 (2
                                       d
                          b
                                                    Order of visit: f, d, c, e
                                            (4,5)
                 c                     f
         (8,9)                e
                                  (7,10)                                                 Predecessor
                                                                           Visited Table table
     a                b           d         NULL
                                                                                 a   T      a   NULL
     b                a           d          f      e         c         NULL
                                                                                 b   T      b     a
c b e NULL c T c e
d a b f NULL d T d b
     e                                                                           e   T      e     b
                      c           b         NULL
                                                                                 f   T      f     d
     f                b           d         NULL
          Depth-First Search
          – Example (Step 11)
53
                                  (1
                                  a
                                           (3,6)
                 (2,11)
                                       d
                          b
                                                    Order of visit: f, d, c, e, b
                                            (4,5)
                 c                     f
         (8,9)                e
                                  (7,10)                                                   Predecessor
                                                                             Visited Table table
     a               b            d         NULL
                                                                                    a   T     a   NULL
     b               a            d          f      e         c         NULL
                                                                                    b   T     b     a
c b e NULL c T c e
d a b f NULL d T d b
     e                                                                              e   T     e     b
                     c            b         NULL
                                                                                    f   T     f     d
     f               b            d         NULL
          Depth-First Search
          – Example (Step 12)
54
                                  (1,12)
                                  a
                                           (3,6)
                 (2,11)
                                       d
                          b
                                            (4,5)
                 c                     f            Order of visit: f, d, c, e, b, a
         (8,9)                e
                                  (7,10)                                                   Predecessor
                                                                             Visited Table table
     a               b            d         NULL
                                                                                       a   T   a   NULL
     b               a            d          f      e         c         NULL
                                                                                       b   T   b    a
c b e NULL c T c e
d a b f NULL d T d b
     e                                                                                 e   T   e    b
                     c            b         NULL
                                                                                       f   T   f    d
     f               b            d         NULL
     Paths from Source to Each Node
55
      Algorithm:
      Path(w)                                               a
        If predecessor[w] is not NULL
                                                                    d
        Path(predecessor[w])                    b
        Output w                                                        Predecessor
                                        c                           f   table
                                                        e
                                                                          a   NULL
                                                                          b    a
                                                            a
                                                                          c    e
b d d b
                                                                          e    b
                                            c                   f
                                                    e                     f    d
                                                DFS Tree
     Final Note about BFS and DFS
56