the decomposition of graphs
play

The Decomposition of Graphs DPV Chapter 3 Jim Royer EECS February - PowerPoint PPT Presentation

The Decomposition of Graphs DPV Chapter 3 Jim Royer EECS February 6, 2019 Royer (EECS) Graph Decomposition February 6, 2019 1 / 32 Graph basics, 1 Definition An undirected graph consists of a set of vertices V and a set of edges E between


  1. The Decomposition of Graphs DPV Chapter 3 Jim Royer EECS February 6, 2019 Royer (EECS) Graph Decomposition February 6, 2019 1 / 32

  2. Graph basics, 1 Definition An undirected graph consists of a set of vertices V and a set of edges E between vertices. See: http://en.wikipedia.org/wiki/Seven_bridges_of_konigsberg For a Google-map view, click here—they seem to have lost a few bridges. Royer (EECS) Graph Decomposition February 6, 2019 2 / 32

  3. Graph basics, 2 Definition main An directed graph consists of: V , a set of vertices and E , a set of (directed) edges between vertices. parse cleanup (So, E ⊆ { ( u , v ) u , v ∈ V & u � = v } .) init execute Note: In this course, (almost) all graphs will be finite . make_string compare printf Royer (EECS) Graph Decomposition February 6, 2019 3 / 32

  4. Graph basics, 3 Adjacency Matrix Representation Let V = { 1, . . . , n } and a ij = true ⇐ ⇒ ( i , j ) ∈ E . 1 2 3 4 5 6 1 F T F F T F 2 T F T F T F 3 F T F T F F 4 F F F F T T 5 T T F T F F F F F T F F 6 Testing if ( i , j ) ∈ E : O ( 1 ) time Finding the vertices adjacent to i : O ( n ) time Diagram from http://en.wikipedia.org/wiki/Graph_(mathematics) Royer (EECS) Graph Decomposition February 6, 2019 4 / 32

  5. Graph basics, 4 Adjacency Matrix Representation Let V = { 1, . . . , n } and a ij = true ⇐ ⇒ ( i , j ) ∈ E . 1 2 3 4 1 F T F T 2 F F F T 3 T T F F 4 F F T F Testing if ( i , j ) ∈ E : O ( 1 ) time Finding the vertices adjacent to i : O ( n ) time Diagram from http://en.wikipedia.org/wiki/Directed_graph Royer (EECS) Graph Decomposition February 6, 2019 5 / 32

  6. Graph basics, 5 Adjacency List Representation Let V = { 1, . . . , n } and L i = a list of vertices adjacent to i . [ 2, 5 ] 1 [ 1, 3, 5 ] 2 [ 2, 4 ] 3 [ 3, 5, 6 ] 4 [ 1, 2, 4 ] 5 [ 4 ] 6 Testing if ( i , j ) ∈ E : O ( n ) time Finding the vertices adjacent to i : O ( 1 ) time Royer (EECS) Graph Decomposition February 6, 2019 6 / 32

  7. Graph basics, 6 Adjacency List Representation Let V = { 1, . . . , n } and L i = a list of vertices adjacent to i . [ 2, 4 ] 1 2 [ 4 ] [ 1, 2 ] 3 4 [ 3 ] Testing if ( i , j ) ∈ E : O ( n ) time Finding the vertices adjacent to i : O ( 1 ) time Royer (EECS) Graph Decomposition February 6, 2019 7 / 32

  8. Depth-First Exploration, 1 procedure explore( G , v ) // Input: a graph G = ( V , E ) and v ∈ V // Output: for all vertices u , reachable from v : visited [ u ] is set to true visited [ v ] ← true previsit( v ) for each u adjacent to v do if not visited [ u ] then explore( G , u ) postvisit( v ) 3 is adjacent to 4, but 3, 5, and 6 are adjacent to 4 neither 1 nor 2 is adjacent to 4. Royer (EECS) Graph Decomposition February 6, 2019 8 / 32

  9. Depth-First Exploration, 2 Definition u is visited ⇐ ⇒ explore eventually sets visited [ u ] ← true . u is unvisited ⇐ ⇒ explore never sets visited [ u ] ← true . procedure explore( G , v ) Lemma visited [ v ] ← true previsit( v ) Suppose initially visited [ u ] = false for all u ∈ V. for each u adjacent to v do Then explore visits exactly all the vertices reachable from v. if not visited [ u ] then explore( G , u ) Proof: postvisit( v ) Claim 1: If u is visited, then u is reachable from v . (Why?) Claim 1 ′ : If u is not reachable from u , then v is un visited. (Why?) More. . . Royer (EECS) Graph Decomposition February 6, 2019 9 / 32

  10. Depth-First Exploration, 3 procedure explore( G , v ) Lemma visited [ v ] ← true Suppose initially visited [ u ] = false for each previsit( v ) u ∈ V. Then explore visits exactly all the vertices for each u adjacent to v do reachable from v. if not visited [ u ] then explore( G , u ) postvisit( v ) Proof (continued): Claim 2: If u is reachable from v , then u is eventually visited. By way of contradiction, suppose there is an unvisited, reachable u . v � = u . (Why?) Take a path from v to u . [Draw the picture!] Let y be the last visited vertex in the path. [Draw the picture!] Let z be the next vertex after y on the path. [Draw the picture!] But by the algorithm, z must be visited, a contradiction. Therefore, Claim 2 and the lemma follow. Royer (EECS) Graph Decomposition February 6, 2019 10 / 32

  11. Depth-First Exploration of the Entire Graph procedure dfs( G ) // G = ( V , E ) 0 for each v ∈ V do visited [ v ] ← false for each v ∈ V do if not visited [ v ] then 1 3 explore( G , v ) procedure explore( G , v ) 5 6 2 visited [ v ] ← true previsit( v ) for each u adjacent to v do if not visited [ u ] then 4 explore( G , u ) postvisit( v ) Royer (EECS) Graph Decomposition February 6, 2019 11 / 32

  12. Depth-First Exploration of the Entire Graph Graph Decomposition procedure dfs( G ) // G = ( V , E ) 2019-02-06 0 for each v ∈ V do visited [ v ] ← false for each v ∈ V do if not visited [ v ] then 1 3 explore( G , v ) procedure explore( G , v ) 5 6 2 Depth-First Exploration of the Entire Graph visited [ v ] ← true previsit( v ) for each u adjacent to v do if not visited [ u ] then 4 explore( G , u ) postvisit( v ) 8 7 9 6 Alternative graph: 1 5 2 4 3

  13. Depth-First Exploration of the Entire Graph procedure dfs( G ) // G = ( V , E ) Run time analysis: for each v ∈ V do visited [ v ] ← false Each v is explore ’d exactly once. (Why?) for each v ∈ V do if not visited [ v ] then explore( G , v ) In the undirected case, each edge is explore ’d down twice. (Why?) procedure explore( G , v ) In the directed case, each edge is explore ’d visited [ v ] ← true down once. (Why?) previsit( v ) for each u adjacent to v do Under the adjacency list representation, this if not visited [ u ] then explore( G , u ) all takes Θ ( | V | + | E | ) time. (Why?) postvisit( v ) Royer (EECS) Graph Decomposition February 6, 2019 12 / 32

  14. Depth-First Exploration of an Undirected Graph Definition A tree edge is an edge the exploration moves down. (a) A back edge is an edge the exploration fails to move down. (b) A DFS forest is the forest made up of the tree edges. (c) Figures from DPV Royer (EECS) Graph Decomposition February 6, 2019 13 / 32

  15. Connected Components in an Undirected Graph // G = ( V , E ) procedure dfs( G ) for each v ∈ V do visited [ v ] ← false ; cc [ v ] ← 0 count ← 1 for each v ∈ V do if not visited [ v ] then explore( G , v ); count ← count + 1 procedure explore( G , v ) visited [ v ] ← true previsit( v ) for each u adjacent to v do if not visited [ u ] then explore( G , u ) postvisit( v ) procedure previsit( v ) cc [ v ] ← count Royer (EECS) Graph Decomposition February 6, 2019 14 / 32

  16. Previsit and postvisit orderings procedure previsit( v ) Lemma pre [ v ] ← clock For any two distinct vertices u and v, either clock ← clock + 1 [ pre [ u ] , post [ u ]] ∩ [ pre [ v ] , post [ v ]] = ∅ or (a) procedure postvisit( v ) [ pre [ u ] , post [ u ]] ⊂ [ pre [ v ] , post [ v ]] = ∅ or (b) post [ v ] ← clock [ pre [ u ] , post [ u ]] ⊃ [ pre [ v ] , post [ v ]] = ∅ . clock ← clock + 1 (c) Figures from DPV Royer (EECS) Graph Decomposition February 6, 2019 15 / 32

  17. Depth-first search in directed graphs, 1 DFS tree A Types of edges Tree edge: part of the DFS forest (a) Forward Back Forward edge: lead to nonchild decendent in the (b) DFS tree. B Back edge: lead to an ancestor in the DFS tree. (c) e e r T Cross edge: None of the above. They lead to a (d) C D vertex that has been completely explored. Cross Figure from DPV Royer (EECS) Graph Decomposition February 6, 2019 16 / 32

  18. Depth-first search in directed graphs, 2 A 1,16 pre/post ordering for ( u , v ) � � B C 2,11 12,15 [ ] u v v u Tree/Forward edges � � E 3,10 D 13,14 [ ] v u u v Back edges � � H F 4,7 8,9 [ ] u u v v Cross edges G 5,6 Figure from DPV Royer (EECS) Graph Decomposition February 6, 2019 17 / 32

  19. Testing for a Cycle Proposition A directed graph G has a cycle ⇐ ⇒ any depth-first search of G finds a back edge. Claim 1: If there is a back edge, there is a cycle. Easy Claim 2: If there is a cycle, a DFS finds a back edge. Proof: ◮ Suppose G has a cycle. ◮ Suppose u is the first vertex of this cycle a particular DFS finds. ◮ Then the DFS visits all the vertices reachable from u . ◮ In the course of this it must find a back edge. (Why?) Royer (EECS) Graph Decomposition February 6, 2019 18 / 32

  20. Topological Sorting, 1 Definition A dag is a directed graph that is acyclic (i.e., no cycles). (a) Suppose G = ( V , E ) is a dag and u , v ∈ V . (b) ⇒ def there is a path from u to v in G . ( ⋆ ) u ≤ G v ⇐ A topological sort of a dag G is ordering of V : v 1 , . . . , v n such that (c) v i ≤ G v j ⇐ ⇒ i ≤ j . ( ⋆ ) Note: [ u ≤ G v & v ≤ G u ] ⇒ [ u = v ] . (Why?) Royer (EECS) Graph Decomposition February 6, 2019 19 / 32

  21. Topological Sorting, 2 Figure from CLRS Royer (EECS) Graph Decomposition February 6, 2019 20 / 32

Recommend


More recommend