cs 374 algorithms models of computation
play

CS 374: Algorithms & Models of Computation Chandra Chekuri - PowerPoint PPT Presentation

CS 374: Algorithms & Models of Computation Chandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59 Today Two topics: Structure of directed graphs DFS


  1. CS 374: Algorithms & Models of Computation Chandra Chekuri Manoj Prabhakaran University of Illinois, Urbana-Champaign Fall 2015 Chandra & Manoj (UIUC) CS374 1 Fall 2015 1 / 59

  2. Today Two topics: Structure of directed graphs DFS and its properties One application of DFS to obtain fast algorithms Chandra & Manoj (UIUC) CS374 2 Fall 2015 2 / 59

  3. Strong Connected Components ( SCC s) Algorithmic Problem B A C Find all SCC s of a given directed graph. E F D Previous lecture: Saw an O(n · (n + m)) time algorithm. This lecture: sketch of a O(n + m) time G H algorithm. Chandra & Manoj (UIUC) CS374 3 Fall 2015 3 / 59

  4. Graph of SCC s B A C B , E , F A , C , D E F D G H G H Graph of SCC s G SCC Graph G Meta-graph of SCC s Let S 1 , S 2 , . . . S k be the strong connected components (i.e., SCC s) of G. The graph of SCC s is G SCC Vertices are S 1 , S 2 , . . . S k 1 There is an edge (S i , S j ) if there is some u ∈ S i and v ∈ S j 2 such that (u , v) is an edge in G. Chandra & Manoj (UIUC) CS374 4 Fall 2015 4 / 59

  5. Reversal and SCC s Proposition For any graph G, the graph of SCC s of G rev is the same as the reversal of G SCC . Proof. Exercise. Chandra & Manoj (UIUC) CS374 5 Fall 2015 5 / 59

  6. SCC s and DAG s Proposition For any graph G, the graph G SCC has no directed cycle. Proof. If G SCC has a cycle S 1 , S 2 , . . . , S k then S 1 ∪ S 2 ∪ · · · ∪ S k should be in the same SCC in G. Formal details: exercise. Chandra & Manoj (UIUC) CS374 6 Fall 2015 6 / 59

  7. Part I Directed Acyclic Graphs Chandra & Manoj (UIUC) CS374 7 Fall 2015 7 / 59

  8. Directed Acyclic Graphs Definition 2 3 A directed graph G is a directed acyclic graph ( DAG ) if there is no directed 1 4 cycle in G. Chandra & Manoj (UIUC) CS374 8 Fall 2015 8 / 59

  9. Sources and Sinks 2 3 source sink 1 4 Definition A vertex u is a source if it has no in-coming edges. 1 A vertex u is a sink if it has no out-going edges. 2 Chandra & Manoj (UIUC) CS374 9 Fall 2015 9 / 59

  10. Simple DAG Properties Proposition Every DAG G has at least one source and at least one sink. Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 59

  11. Simple DAG Properties Proposition Every DAG G has at least one source and at least one sink. Proof. Let P = v 1 , v 2 , . . . , v k be a longest path in G . Claim that v 1 is a source and v k is a sink. Suppose not. Then v 1 has an incoming edge which either creates a cycle or a longer path both of which are contradictions. Similarly if v k has an outgoing edge. Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 59

  12. Simple DAG Properties Proposition Every DAG G has at least one source and at least one sink. Proof. Let P = v 1 , v 2 , . . . , v k be a longest path in G . Claim that v 1 is a source and v k is a sink. Suppose not. Then v 1 has an incoming edge which either creates a cycle or a longer path both of which are contradictions. Similarly if v k has an outgoing edge. G is a DAG if and only if G rev is a DAG . 1 G is a DAG if and only each node is in its own strong 2 connected component. Formal proofs: exercise. Chandra & Manoj (UIUC) CS374 10 Fall 2015 10 / 59

  13. Topological Ordering/Sorting 2 3 1 2 3 4 Topological Ordering of G 1 4 Graph G Definition A topological ordering / topological sorting of G = (V , E) is an ordering ≺ on V such that if (u , v) ∈ E then u ≺ v . Informal equivalent definition: One can order the vertices of the graph along a line (say the x -axis) such that all edges are from left to right. Chandra & Manoj (UIUC) CS374 11 Fall 2015 11 / 59

  14. DAG s and Topological Sort Lemma A directed graph G can be topologically ordered iff it is a DAG . Need to show both directions. Chandra & Manoj (UIUC) CS374 12 Fall 2015 12 / 59

  15. DAG s and Topological Sort Lemma A directed graph G can be topologically ordered if it is a DAG . Proof. Consider the following algorithm: Pick a source u , output it. 1 Remove u and all edges out of u . 2 Repeat until graph is empty. 3 Exercise: prove this gives toplogical sort. Exercise: show algorithm can be implemented in O(m + n) time. Chandra & Manoj (UIUC) CS374 13 Fall 2015 13 / 59

  16. Topological Sort: Example c a b e d g f h Chandra & Manoj (UIUC) CS374 14 Fall 2015 14 / 59

  17. DAG s and Topological Sort Lemma A directed graph G can be topologically ordered only if it is a DAG . Proof. Suppose G is not a DAG and has a topological ordering ≺ . G has a cycle C = u 1 , u 2 , . . . , u k , u 1 . Then u 1 ≺ u 2 ≺ . . . ≺ u k ≺ u 1 ! That is... u 1 ≺ u 1 . A contradiction (to ≺ being an order). Not possible to topologically order the vertices. Chandra & Manoj (UIUC) CS374 15 Fall 2015 15 / 59

  18. DAG s and Topological Sort Note: A DAG G may have many different topological sorts. Question: What is a DAG with the most number of distinct topological sorts for a given number n of vertices? Question: What is a DAG with the least number of distinct topological sorts for a given number n of vertices? Chandra & Manoj (UIUC) CS374 16 Fall 2015 16 / 59

  19. Cycles in graphs Question: Given an undirected graph how do we check whether it has a cycle and output one if it has one? Question: Given an directed graph how do we check whether it has a cycle and output one if it has one? Chandra & Manoj (UIUC) CS374 17 Fall 2015 17 / 59

  20. To Remember: Structure of Graphs Undirected graph: connected components of G = (V , E) partition V and can be computed in O(m + n) time. Directed graph: the meta-graph G SCC of G can be computed in O(m + n) time. G SCC gives information on the partition of V into strong connected components and how they form a DAG structure. Above structural decomposition will be useful in several algorithms Chandra & Manoj (UIUC) CS374 18 Fall 2015 18 / 59

  21. Part II Depth First Search (DFS) Chandra & Manoj (UIUC) CS374 19 Fall 2015 19 / 59

  22. Depth First Search DFS is a special case of Basic Search but is a versatile graph exploration strategy. John Hopcroft and Bob Tarjan (Turing Award winners) demonstrated the power of DFS to understand graph structure. DFS can be used to obtain linear time ( O(m + n) ) algorithms for Finding cut-edges and cut-vertices of undirected graphs 1 Finding strong connected components of directed graphs 2 Linear time algorithm for testing whether a graph is planar 3 Many other applications as well. Chandra & Manoj (UIUC) CS374 20 Fall 2015 20 / 59

  23. DFS in Undirected Graphs Recursive version. Easier to understand some properties. DFS(G) DFS ( u ) for all u ∈ V(G) do Mark u as visited for each uv in Out(u) do Mark u as unvisited if v is not visited then Set pred( u ) to null T is set to ∅ add edge uv to T while ∃ unvisited u do set pred( v ) to u DFS(u) DFS(v) Output T Implemented using a global array Visited for all recursive calls. T is the search tree/forest. Chandra & Manoj (UIUC) CS374 21 Fall 2015 21 / 59

  24. Example 1 7 9 2 3 4 5 8 10 6 Edges classified into two types: uv ∈ E is a tree edge: belongs to T 1 non-tree edge: does not belong to T 2 Chandra & Manoj (UIUC) CS374 22 Fall 2015 22 / 59

  25. Properties of DFS tree Proposition T is a forest 1 connected components of T are same as those of G . 2 If uv ∈ E is a non-tree edge then, in T , either: 3 u is an ancestor of v , or 1 v is an ancestor of u . 2 Question: Why are there no cross-edges ? Chandra & Manoj (UIUC) CS374 23 Fall 2015 23 / 59

  26. DFS with Visit Times Keep track of when nodes are visited. DFS(G) DFS ( u ) for all u ∈ V(G) do Mark u as visited Mark u as unvisited pre (u) = ++ time for each uv in Out(u) do T is set to ∅ if v is not marked then time = 0 while ∃ unvisited u do add edge uv to T DFS(u) DFS(v) Output T post (u) = ++ time Chandra & Manoj (UIUC) CS374 24 Fall 2015 24 / 59

  27. Example 1 7 9 2 3 4 5 8 10 6 Chandra & Manoj (UIUC) CS374 25 Fall 2015 25 / 59

  28. pre and post numbers Node u is active in time interval [ pre (u) , post (u)] Proposition For any two nodes u and v , the two intervals [ pre (u) , post (u)] and [ pre (v) , post (v)] are disjoint or one is contained in the other. Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59

  29. pre and post numbers Node u is active in time interval [ pre (u) , post (u)] Proposition For any two nodes u and v , the two intervals [ pre (u) , post (u)] and [ pre (v) , post (v)] are disjoint or one is contained in the other. Proof. Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59

  30. pre and post numbers Node u is active in time interval [ pre (u) , post (u)] Proposition For any two nodes u and v , the two intervals [ pre (u) , post (u)] and [ pre (v) , post (v)] are disjoint or one is contained in the other. Proof. Assume without loss of generality that pre (u) < pre (v) . Then v visited after u . Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59

  31. pre and post numbers Node u is active in time interval [ pre (u) , post (u)] Proposition For any two nodes u and v , the two intervals [ pre (u) , post (u)] and [ pre (v) , post (v)] are disjoint or one is contained in the other. Proof. Assume without loss of generality that pre (u) < pre (v) . Then v visited after u . If DFS(v) invoked before DFS(u) finished, post (v) < post (u) . Chandra & Manoj (UIUC) CS374 26 Fall 2015 26 / 59

Recommend


More recommend