KEMBAR78
Part4 graph algorithms | PDF
Part IV
                           Graph Algorithms


Saturday, April 30, 2011                      1
1




                                          Undirected Graphs


                  Undirected graph. G = (V, E)
                    V = nodes.
                    !



                    E = edges between pairs of nodes.
                    !



                    Captures pairwise relationship between objects.
                    !



                    Graph size parameters: n = |V|, m = |E|.
                    !




                                                  V={1,2,3,4,5,6,7,8}
                                             V = { 1, 2, 3, 4, 5, 6, 7, 8 }
                                             E = {E={(1,2),(1,3),(2,3),...,(7,8)}3-8, 4-5, 5-6 }
                                                  1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7,
                                             n=8
                                                  n=8
                                             m = 11
                                                  m=11



Saturday, April 30, 2011                                                                           3
                                                                                                       2
Directed Graphs


hs                          Directed graph. G = (V, E)
                             ! Edge (u, v) goes from node u to node v.




                            Ex. Web graph - hyperlink points from one web page to a
                             ! Directedness of graph is crucial.
                             ! Modern web search engines exploit hyperlink structur
                               pages by importance.
 Saturday, April 30, 2011                                                       3
Graph Representation: Adjacency Matrix


                     Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge.
                       !Two representations of each edge.
                       !Space proportional to n2.
                       !Checking if (u, v) is an edge takes !(1) time.
                       !Identifying all edges takes !(n2) time.             
                                                                                           1   if (i, j) ∈ E,
                                        A = (aij ), where aij =
                                                                                           0   otherwise.
                                                               1   2   3   4   5   6   7   8
                                                           1   0   1   1   0   0   0   0   0
                                                           2   1   0   1   1   1   0   0   0
                                                           3   1   1   0   0   1   0   1   1
                                                           4   0   1   0   1   1   0   0   0
                                                           5   0   1   1   1   0   1   0   0
                                                           6   0   0   0   0   1   0   0   0
                                                           7   0   0   1   0   0   0   0   1
                                                           8   0   0   1   0   0   0   1   0


 7                                                                                                        8




Saturday, April 30, 2011                                                                                        4
Saturday, April 30, 2011   5
Saturday, April 30, 2011   6
Saturday, April 30, 2011   7
Paths and Connectivity


Def. A path in an undirected graph G = (V, E) is a sequence P of nodes
v1, v2, …, vk-1, vk with the property that each consecutive pair vi, vi+1 is
joined by an edge in E.

Def. A path is simple if all nodes are distinct.

Def. An undirected graph is connected if for every pair of nodes u and
v, there is a path between u and v.




                                                                               10



Saturday, April 30, 2011                                                            8
Saturday, April 30, 2011   9
Breadth First Search


tion. Explore outward from s in all possible directions, adding
 layer at a time.


                            s    L1      L2               L
 ithm.                                                      n-1


s }.
  neighbors of L0.
   nodes that do not belong to L0 or L1, and that have an edge
  de in L1.
 ll nodes that do not belong to an earlier layer, and that have
 e to a node in Li.



 For each i, Li consists of all nodes at distance exactly i
here is a path from s to t iff t appears in some layer.


 Saturday, April 30, 2011                                         18   10
Breadth First Search


t T be a BFS tree of G = (V, E), and let (x, y) be an edge of
evel of x and y differ by at most 1.
                           s=1
                                 √   1. insert the start node into queue




                                                       a queue
                                          1


                                                                 L0


                                                                 L1


                                                                 L2
Saturday, April 30, 2011                                                   11
Breadth First Search


t T be a BFS tree of G = (V, E), and let (x, y) be an edge of
evel of x and y differ by at mostremove a node from queue
                               2. 1.
                           s=1
                                 √       3. insert neighbors of it into the queue

                       √             √




                                                    1



                                                2    3
                                                                         L0


                                                                         L1


                                                                         L2
Saturday, April 30, 2011                                                            12
Breadth First Search


t T be a BFS tree of G = (V, E), and let (x, y) be an edge of
evel of x and y differ by at mostremove a node from queue
                               2. 1.
                           s=1
                                   √       3. insert neighbors of it into the queue

                           √           √

                                                   3
                 √             √
                                                       2



                                                   3       4   5
                                                                           L0


                                                                           L1


                                                                           L2
Saturday, April 30, 2011                                                              13
Breadth First Search


t T be a BFS tree of G = (V, E), and let (x, y) be an edge of
evel of x and y differ by at mostremove a node from queue
                               2. 1.
                           s=1
                                   √       3. insert neighbors of it into the queue

                           √           √

                                                   4       5
                 √             √
                                                       3
                               √


                                                   4       5   6
                                                                           L0


                                                                           L1


                                                                           L2
Saturday, April 30, 2011                                                              14
Saturday, April 30, 2011   15
Saturday, April 30, 2011   16
Saturday, April 30, 2011   17
Saturday, April 30, 2011   18
Saturday, April 30, 2011   19
94
                                              96

               Figure 3.2 Exploring a graph is rather like navigating themaze. Figure 3.2.
                          begin here     Figure 3.4 The result of explore(A) on a graph of


                                                                              L            A
                                                                              K
                           D       A      B             E                         B                D   F
                                                                                               B

                                                                              E        F           G

                G              H   C      F         I           J                                          C
                                                                              I
                                                                              J        C E         H


                                                                              J
                                   K      L

                                              Figure 3.5 Depth-first search.   I                            A
                                              procedure dfs(G)

                                              for all v ∈ V :
                                                 visited(v) = false

                       What parts of the graph are reachable from a given vertex?
                                            for all v ∈ V :
Saturday, April 30, 2011                           if not visited(v):     explore(v)                           20
Saturday, April 30, 2011   21
Saturday, April 30, 2011   22
Saturday, April 30, 2011   23
Saturday, April 30, 2011   24
Saturday, April 30, 2011   25
➙

                           ➙   ➙




Saturday, April 30, 2011               26
Def. A graph is strongly connected if every pair of nodes i
            reachable.

            Lemma. Let s be any node. G is strongly connected iff eve
            reachable from s, and s is reachable from every node.

            Pf. ! Follows from definition.
            Pf.  Path from u to v: concatenate u-s path with s-v path
                  Path from v to u: concatenate v-s path with s-u pat

                                                            ok if paths over

                               s              u


                                         v




Saturday, April 30, 2011                                                27
Strong Connectivity: Algorithm


                      Theorem. Can determine if G is strongly connected in O(m + n) time.
                      Pf.
                       !  Pick any node s.
                       !  Run BFS from s in G.      reverse orientation of every edge in G

                       !  Run BFS from s in Grev.
                       !  Return true iff all nodes reached in both BFS executions.
                       !  Correctness follows immediately from previous lemma. !




                             strongly connected                     not strongly connected




                                                                                             37



Saturday, April 30, 2011                                                                          28
strongly connected                       not strongly connected




                                                                                                              37




                                                      Directed Acyclic Graphs


                       Def. An DAG is a directed graph that contains no directed cycles.                            Pr

                       Ex. Precedence constraints: edge (vi, vj) means vi must precede vj.                          Ap
                                                                                                                        !



                       Def. A topological order of a directed graph G = (V, E) is an ordering                           !



                       of its nodes as v1, v2, …, vn so that for every edge (vi, vj) we have i  j.


                                v2           v3




                           v6         v5               v4       v1   v2   v3      v4       v5       v6   v7




                                v7           v1



                                     a DAG                                 a topological ordering


                                                                                                              39


Saturday, April 30, 2011                                                                                           29
O(n+m) with adjacent list

Saturday, April 30, 2011                               30
Saturday, April 30, 2011   31
b
                           b       ee

                     a                      SCC
                               d
                               d        g
                           c
                           c   h
                               h   ff




Saturday, April 30, 2011                          32
b
                                 b            ee

                           a
                                        d
                                        d          g
                                c
                                c        h
                                         h    ff




                               more than one SCC

Saturday, April 30, 2011                               33
Saturday, April 30, 2011   34
Saturday, April 30, 2011   35
a SCC
                                                       another SCC




                                         YES !!

                           The SCCs partition nodes of graph.

Saturday, April 30, 2011                                             36
b
                               b       ee

                           a
                                   d
                                   d        g
                               c
                               c   h
                                   h   ff




Saturday, April 30, 2011                        37
bb
                                        ee                    C2
                                                    C1

                           a
                           a

                                    d
                                    d           g
                                                g
                                                                   C4
                               c
                               c
                                    h                    C3
                                    h
                                        f   f




Saturday, April 30, 2011                                                38
b
                           b       ee                 C2
                                            C1

               a
                               d
                               d        g                  C4
                                                 C3
                           c
                           c   h
                               h   ff




Saturday, April 30, 2011                                        39
Saturday, April 30, 2011   40
Saturday, April 30, 2011   41
Saturday, April 30, 2011   42
13/14       11/16   1/10   8/9




               12/15        3/4    2/7    5/6




Saturday, April 30, 2011                        43
13/14       11/16          1/10   8/9



                             start here


               12/15        3/4           2/7    5/6




Saturday, April 30, 2011                               44
13/14       11/16          1/10   8/9



                             start here


               12/15        3/4           2/7    5/6




Saturday, April 30, 2011                               45
13/14       11/16          1/10   8/9



                             start here


               12/15        3/4           2/7    5/6




Saturday, April 30, 2011                               46
13/14       11/16   1/10   8/9




               12/15        3/4    2/7    5/6




                                          start here


Saturday, April 30, 2011                               47
13/14       11/16   1/10   8/9




               12/15        3/4    2/7    5/6




Saturday, April 30, 2011                        48
Saturday, April 30, 2011   49
Saturday, April 30, 2011   50
Saturday, April 30, 2011   51

Part4 graph algorithms

  • 1.
    Part IV Graph Algorithms Saturday, April 30, 2011 1
  • 2.
    1 Undirected Graphs Undirected graph. G = (V, E) V = nodes. ! E = edges between pairs of nodes. ! Captures pairwise relationship between objects. ! Graph size parameters: n = |V|, m = |E|. ! V={1,2,3,4,5,6,7,8} V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = {E={(1,2),(1,3),(2,3),...,(7,8)}3-8, 4-5, 5-6 } 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, n=8 n=8 m = 11 m=11 Saturday, April 30, 2011 3 2
  • 3.
    Directed Graphs hs Directed graph. G = (V, E) ! Edge (u, v) goes from node u to node v. Ex. Web graph - hyperlink points from one web page to a ! Directedness of graph is crucial. ! Modern web search engines exploit hyperlink structur pages by importance. Saturday, April 30, 2011 3
  • 4.
    Graph Representation: AdjacencyMatrix Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge. !Two representations of each edge. !Space proportional to n2. !Checking if (u, v) is an edge takes !(1) time. !Identifying all edges takes !(n2) time. 1 if (i, j) ∈ E, A = (aij ), where aij = 0 otherwise. 1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 1 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0 7 8 Saturday, April 30, 2011 4
  • 5.
  • 6.
  • 7.
  • 8.
    Paths and Connectivity Def.A path in an undirected graph G = (V, E) is a sequence P of nodes v1, v2, …, vk-1, vk with the property that each consecutive pair vi, vi+1 is joined by an edge in E. Def. A path is simple if all nodes are distinct. Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v. 10 Saturday, April 30, 2011 8
  • 9.
  • 10.
    Breadth First Search tion.Explore outward from s in all possible directions, adding layer at a time. s L1 L2 L ithm. n-1 s }. neighbors of L0. nodes that do not belong to L0 or L1, and that have an edge de in L1. ll nodes that do not belong to an earlier layer, and that have e to a node in Li. For each i, Li consists of all nodes at distance exactly i here is a path from s to t iff t appears in some layer. Saturday, April 30, 2011 18 10
  • 11.
    Breadth First Search tT be a BFS tree of G = (V, E), and let (x, y) be an edge of evel of x and y differ by at most 1. s=1 √ 1. insert the start node into queue a queue 1 L0 L1 L2 Saturday, April 30, 2011 11
  • 12.
    Breadth First Search tT be a BFS tree of G = (V, E), and let (x, y) be an edge of evel of x and y differ by at mostremove a node from queue 2. 1. s=1 √ 3. insert neighbors of it into the queue √ √ 1 2 3 L0 L1 L2 Saturday, April 30, 2011 12
  • 13.
    Breadth First Search tT be a BFS tree of G = (V, E), and let (x, y) be an edge of evel of x and y differ by at mostremove a node from queue 2. 1. s=1 √ 3. insert neighbors of it into the queue √ √ 3 √ √ 2 3 4 5 L0 L1 L2 Saturday, April 30, 2011 13
  • 14.
    Breadth First Search tT be a BFS tree of G = (V, E), and let (x, y) be an edge of evel of x and y differ by at mostremove a node from queue 2. 1. s=1 √ 3. insert neighbors of it into the queue √ √ 4 5 √ √ 3 √ 4 5 6 L0 L1 L2 Saturday, April 30, 2011 14
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
    94 96 Figure 3.2 Exploring a graph is rather like navigating themaze. Figure 3.2. begin here Figure 3.4 The result of explore(A) on a graph of L A K D A B E B D F B E F G G H C F I J C I J C E H J K L Figure 3.5 Depth-first search. I A procedure dfs(G) for all v ∈ V : visited(v) = false What parts of the graph are reachable from a given vertex? for all v ∈ V : Saturday, April 30, 2011 if not visited(v): explore(v) 20
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
    ➙ ➙ Saturday, April 30, 2011 26
  • 27.
    Def. A graphis strongly connected if every pair of nodes i reachable. Lemma. Let s be any node. G is strongly connected iff eve reachable from s, and s is reachable from every node. Pf. ! Follows from definition. Pf. Path from u to v: concatenate u-s path with s-v path Path from v to u: concatenate v-s path with s-u pat ok if paths over s u v Saturday, April 30, 2011 27
  • 28.
    Strong Connectivity: Algorithm Theorem. Can determine if G is strongly connected in O(m + n) time. Pf. ! Pick any node s. ! Run BFS from s in G. reverse orientation of every edge in G ! Run BFS from s in Grev. ! Return true iff all nodes reached in both BFS executions. ! Correctness follows immediately from previous lemma. ! strongly connected not strongly connected 37 Saturday, April 30, 2011 28
  • 29.
    strongly connected not strongly connected 37 Directed Acyclic Graphs Def. An DAG is a directed graph that contains no directed cycles. Pr Ex. Precedence constraints: edge (vi, vj) means vi must precede vj. Ap ! Def. A topological order of a directed graph G = (V, E) is an ordering ! of its nodes as v1, v2, …, vn so that for every edge (vi, vj) we have i j. v2 v3 v6 v5 v4 v1 v2 v3 v4 v5 v6 v7 v7 v1 a DAG a topological ordering 39 Saturday, April 30, 2011 29
  • 30.
    O(n+m) with adjacentlist Saturday, April 30, 2011 30
  • 31.
  • 32.
    b b ee a SCC d d g c c h h ff Saturday, April 30, 2011 32
  • 33.
    b b ee a d d g c c h h ff more than one SCC Saturday, April 30, 2011 33
  • 34.
  • 35.
  • 36.
    a SCC another SCC YES !! The SCCs partition nodes of graph. Saturday, April 30, 2011 36
  • 37.
    b b ee a d d g c c h h ff Saturday, April 30, 2011 37
  • 38.
    bb ee C2 C1 a a d d g g C4 c c h C3 h f f Saturday, April 30, 2011 38
  • 39.
    b b ee C2 C1 a d d g C4 C3 c c h h ff Saturday, April 30, 2011 39
  • 40.
  • 41.
  • 42.
  • 43.
    13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 Saturday, April 30, 2011 43
  • 44.
    13/14 11/16 1/10 8/9 start here 12/15 3/4 2/7 5/6 Saturday, April 30, 2011 44
  • 45.
    13/14 11/16 1/10 8/9 start here 12/15 3/4 2/7 5/6 Saturday, April 30, 2011 45
  • 46.
    13/14 11/16 1/10 8/9 start here 12/15 3/4 2/7 5/6 Saturday, April 30, 2011 46
  • 47.
    13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 start here Saturday, April 30, 2011 47
  • 48.
    13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 Saturday, April 30, 2011 48
  • 49.
  • 50.
  • 51.