depth first search dfs
play

Depth First Search (DFS) Lecture 16 Thursday, October 26, 2017 - PowerPoint PPT Presentation

Algorithms & Models of Computation CS/ECE 374, Fall 2017 Depth First Search (DFS) Lecture 16 Thursday, October 26, 2017 Sariel Har-Peled (UIUC) CS374 1 Fall 2017 1 / 60 Today Two topics: Structure of directed graphs DFS and its


  1. 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 ) . If DFS( v ) invoked after DFS( u ) finished, pre ( v ) > post ( u ) . pre and post numbers useful in several applications of DFS Sariel Har-Peled (UIUC) CS374 10 Fall 2017 10 / 60

  2. 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 ) . If DFS( v ) invoked after DFS( u ) finished, pre ( v ) > post ( u ) . pre and post numbers useful in several applications of DFS Sariel Har-Peled (UIUC) CS374 10 Fall 2017 10 / 60

  3. DFS in Directed Graphs DFS( G ) Mark all nodes u as unvisited T is set to ∅ time = 0 while there is an unvisited node u do DFS( u ) Output T DFS( u ) Mark u as visited pre ( u ) = ++ time for each edge ( u , v ) in Out ( u ) do if v is not visited add edge ( u , v ) to T DFS( v ) post ( u ) = ++ time Sariel Har-Peled (UIUC) CS374 11 Fall 2017 11 / 60

  4. Example B A C E F D G H Sariel Har-Peled (UIUC) CS374 12 Fall 2017 12 / 60

  5. Example [1, 16] B A C [2, 11] [12, 15] B A C E F D [3, 10] [6, 7] [13, 14] E F D [4, 5] G H [8, 9] G H Sariel Har-Peled (UIUC) CS374 12 Fall 2017 12 / 60

  6. DFS Properties Generalizing ideas from undirected graphs: DFS( G ) takes O ( m + n ) time. 1 Edges added form a branching : a forest of out-trees. Output of 2 DFS ( G ) depends on the order in which vertices are considered. If u is the first vertex considered by DFS ( G ) then DFS ( u ) 3 outputs a directed out-tree T rooted at u and a vertex v is in T if and only if v ∈ rch ( u ) For any two vertices x , y the intervals [ pre ( x ) , post ( x )] and 4 [ pre ( y ) , post ( y )] are either disjoint or one is contained in the other. Note: Not obvious whether DFS( G ) is useful in directed graphs but it is. Sariel Har-Peled (UIUC) CS374 13 Fall 2017 13 / 60

  7. DFS Properties Generalizing ideas from undirected graphs: DFS( G ) takes O ( m + n ) time. 1 Edges added form a branching : a forest of out-trees. Output of 2 DFS ( G ) depends on the order in which vertices are considered. If u is the first vertex considered by DFS ( G ) then DFS ( u ) 3 outputs a directed out-tree T rooted at u and a vertex v is in T if and only if v ∈ rch ( u ) For any two vertices x , y the intervals [ pre ( x ) , post ( x )] and 4 [ pre ( y ) , post ( y )] are either disjoint or one is contained in the other. Note: Not obvious whether DFS( G ) is useful in directed graphs but it is. Sariel Har-Peled (UIUC) CS374 13 Fall 2017 13 / 60

  8. DFS Properties Generalizing ideas from undirected graphs: DFS( G ) takes O ( m + n ) time. 1 Edges added form a branching : a forest of out-trees. Output of 2 DFS ( G ) depends on the order in which vertices are considered. If u is the first vertex considered by DFS ( G ) then DFS ( u ) 3 outputs a directed out-tree T rooted at u and a vertex v is in T if and only if v ∈ rch ( u ) For any two vertices x , y the intervals [ pre ( x ) , post ( x )] and 4 [ pre ( y ) , post ( y )] are either disjoint or one is contained in the other. Note: Not obvious whether DFS( G ) is useful in directed graphs but it is. Sariel Har-Peled (UIUC) CS374 13 Fall 2017 13 / 60

  9. DFS Properties Generalizing ideas from undirected graphs: DFS( G ) takes O ( m + n ) time. 1 Edges added form a branching : a forest of out-trees. Output of 2 DFS ( G ) depends on the order in which vertices are considered. If u is the first vertex considered by DFS ( G ) then DFS ( u ) 3 outputs a directed out-tree T rooted at u and a vertex v is in T if and only if v ∈ rch ( u ) For any two vertices x , y the intervals [ pre ( x ) , post ( x )] and 4 [ pre ( y ) , post ( y )] are either disjoint or one is contained in the other. Note: Not obvious whether DFS( G ) is useful in directed graphs but it is. Sariel Har-Peled (UIUC) CS374 13 Fall 2017 13 / 60

  10. DFS Properties Generalizing ideas from undirected graphs: DFS( G ) takes O ( m + n ) time. 1 Edges added form a branching : a forest of out-trees. Output of 2 DFS ( G ) depends on the order in which vertices are considered. If u is the first vertex considered by DFS ( G ) then DFS ( u ) 3 outputs a directed out-tree T rooted at u and a vertex v is in T if and only if v ∈ rch ( u ) For any two vertices x , y the intervals [ pre ( x ) , post ( x )] and 4 [ pre ( y ) , post ( y )] are either disjoint or one is contained in the other. Note: Not obvious whether DFS( G ) is useful in directed graphs but it is. Sariel Har-Peled (UIUC) CS374 13 Fall 2017 13 / 60

  11. DFS Tree Edges of G can be classified with respect to the DFS tree T as: Tree edges that belong to T 1 A forward edge is a non-tree edges ( x , y ) such that 2 pre ( x ) < pre ( y ) < post ( y ) < post ( x ) . A backward edge is a non-tree edge ( y , x ) such that 3 pre ( x ) < pre ( y ) < post ( y ) < post ( x ) . A cross edge is a non-tree edges ( x , y ) such that the intervals 4 [ pre ( x ) , post ( x )] and [ pre ( y ) , post ( y )] are disjoint. Sariel Har-Peled (UIUC) CS374 14 Fall 2017 14 / 60

  12. Types of Edges A Forward Backward B C D Cross Sariel Har-Peled (UIUC) CS374 15 Fall 2017 15 / 60

  13. 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? Sariel Har-Peled (UIUC) CS374 16 Fall 2017 16 / 60

  14. Using DFS ... ... to check for Acylicity and compute Topological Ordering Question Given G, is it a DAG ? If it is, generate a topological sort. Else output a cycle C . DFS based algorithm: Compute DFS( G ) 1 If there is a back edge e = ( v , u ) then G is not a DAG . Output 2 cyclce C formed by path from u to v in T plus edge ( v , u ) . Otherwise output nodes in decreasing post-visit order. Note: no 3 need to sort, DFS ( G ) can output nodes in this order. Algorithm runs in O ( n + m ) time. Correctness is not so obvious. See next two propositions. Sariel Har-Peled (UIUC) CS374 17 Fall 2017 17 / 60

  15. Using DFS ... ... to check for Acylicity and compute Topological Ordering Question Given G, is it a DAG ? If it is, generate a topological sort. Else output a cycle C . DFS based algorithm: Compute DFS( G ) 1 If there is a back edge e = ( v , u ) then G is not a DAG . Output 2 cyclce C formed by path from u to v in T plus edge ( v , u ) . Otherwise output nodes in decreasing post-visit order. Note: no 3 need to sort, DFS ( G ) can output nodes in this order. Algorithm runs in O ( n + m ) time. Correctness is not so obvious. See next two propositions. Sariel Har-Peled (UIUC) CS374 17 Fall 2017 17 / 60

  16. Using DFS ... ... to check for Acylicity and compute Topological Ordering Question Given G, is it a DAG ? If it is, generate a topological sort. Else output a cycle C . DFS based algorithm: Compute DFS( G ) 1 If there is a back edge e = ( v , u ) then G is not a DAG . Output 2 cyclce C formed by path from u to v in T plus edge ( v , u ) . Otherwise output nodes in decreasing post-visit order. Note: no 3 need to sort, DFS ( G ) can output nodes in this order. Algorithm runs in O ( n + m ) time. Correctness is not so obvious. See next two propositions. Sariel Har-Peled (UIUC) CS374 17 Fall 2017 17 / 60

  17. Back edge and Cycles Proposition G has a cycle iff there is a back-edge in DFS( G ) . Proof. If: ( u , v ) is a back edge implies there is a cycle C consisting of the path from v to u in DFS search tree and the edge ( u , v ) . Only if: Suppose there is a cycle C = v 1 → v 2 → . . . → v k → v 1 . Let v i be first node in C visited in DFS . All other nodes in C are descendants of v i since they are reachable from v i . Therefore, ( v i − 1 , v i ) (or ( v k , v 1 ) if i = 1 ) is a back edge. Sariel Har-Peled (UIUC) CS374 18 Fall 2017 18 / 60

  18. Proof Proposition If G is a DAG and post ( v ) > post ( u ) , then ( u , v ) is not in G. Proof. Assume post ( v ) > post ( u ) and ( u , v ) is an edge in G . We derive a contradiction. One of two cases holds from DFS property. Case 1: [ pre ( u ) , post ( u )] is contained in [ pre ( v ) , post ( v )] . Implies that u is explored during DFS( v ) and hence is a descendent of v . Edge ( u , v ) implies a cycle in G but G is assumed to be DAG! Case 2: [ pre ( u ) , post ( u )] is disjoint from [ pre ( v ) , post ( v )] . This cannot happen since v would be explored from u . Sariel Har-Peled (UIUC) CS374 19 Fall 2017 19 / 60

  19. Example c a b e d g f h Sariel Har-Peled (UIUC) CS374 20 Fall 2017 20 / 60

  20. Part II Strong connected components Sariel Har-Peled (UIUC) CS374 21 Fall 2017 21 / 60

  21. Strong Connected Components ( SCC s) B A C Algorithmic Problem 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 algorithm. G H Sariel Har-Peled (UIUC) CS374 22 Fall 2017 22 / 60

  22. Graph of SCC s B A C B, E, F A, C, D E F D G H Graph of SCC s G SCC G H 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. Sariel Har-Peled (UIUC) CS374 23 Fall 2017 23 / 60

  23. 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. Sariel Har-Peled (UIUC) CS374 24 Fall 2017 24 / 60

  24. 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. Sariel Har-Peled (UIUC) CS374 25 Fall 2017 25 / 60

  25. Part III Directed Acyclic Graphs Sariel Har-Peled (UIUC) CS374 26 Fall 2017 26 / 60

  26. 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. Sariel Har-Peled (UIUC) CS374 27 Fall 2017 27 / 60

  27. Is this a DAG? a c b b a k e d c d g f e n f i h k j g p h m l i r j n o p q m t s l r s o v u w u t w q v Sariel Har-Peled (UIUC) CS374 28 Fall 2017 28 / 60

  28. 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 Sariel Har-Peled (UIUC) CS374 29 Fall 2017 29 / 60

  29. 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. Sariel Har-Peled (UIUC) CS374 30 Fall 2017 30 / 60

  30. 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. Sariel Har-Peled (UIUC) CS374 30 Fall 2017 30 / 60

  31. 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. Sariel Har-Peled (UIUC) CS374 30 Fall 2017 30 / 60

  32. 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. Sariel Har-Peled (UIUC) CS374 31 Fall 2017 31 / 60

  33. DAG s and Topological Sort Lemma A directed graph G can be topologically ordered iff it is a DAG . Need to show both directions. Sariel Har-Peled (UIUC) CS374 32 Fall 2017 32 / 60

  34. 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 topological sort. Exercise: show algorithm can be implemented in O ( m + n ) time. Sariel Har-Peled (UIUC) CS374 33 Fall 2017 33 / 60

  35. Topological Sort: Example c a b e d g f h Sariel Har-Peled (UIUC) CS374 34 Fall 2017 34 / 60

  36. 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. Sariel Har-Peled (UIUC) CS374 35 Fall 2017 35 / 60

  37. 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? Sariel Har-Peled (UIUC) CS374 36 Fall 2017 36 / 60

  38. 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? Sariel Har-Peled (UIUC) CS374 37 Fall 2017 37 / 60

  39. 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 Sariel Har-Peled (UIUC) CS374 38 Fall 2017 38 / 60

  40. Part IV Linear time algorithm for finding all strong connected components of a directed graph Sariel Har-Peled (UIUC) CS374 39 Fall 2017 39 / 60

  41. Finding all SCC s of a Directed Graph Problem Given a directed graph G = ( V , E ) , output all its strong connected components. Straightforward algorithm: Mark all vertices in V as not visited. for each vertex u ∈ V not visited yet do find SCC ( G , u ) the strong component of u : Compute rch( G , u ) using DFS ( G , u ) Compute rch( G rev , u ) using DFS ( G rev , u ) SCC ( G , u ) ⇐ rch( G , u ) ∩ rch( G rev , u ) ∀ u ∈ SCC ( G , u ) : Mark u as visited. Running time: O ( n ( n + m )) Is there an O ( n + m ) time algorithm? Sariel Har-Peled (UIUC) CS374 40 Fall 2017 40 / 60

  42. Finding all SCC s of a Directed Graph Problem Given a directed graph G = ( V , E ) , output all its strong connected components. Straightforward algorithm: Mark all vertices in V as not visited. for each vertex u ∈ V not visited yet do find SCC ( G , u ) the strong component of u : Compute rch( G , u ) using DFS ( G , u ) Compute rch( G rev , u ) using DFS ( G rev , u ) SCC ( G , u ) ⇐ rch( G , u ) ∩ rch( G rev , u ) ∀ u ∈ SCC ( G , u ) : Mark u as visited. Running time: O ( n ( n + m )) Is there an O ( n + m ) time algorithm? Sariel Har-Peled (UIUC) CS374 40 Fall 2017 40 / 60

  43. Finding all SCC s of a Directed Graph Problem Given a directed graph G = ( V , E ) , output all its strong connected components. Straightforward algorithm: Mark all vertices in V as not visited. for each vertex u ∈ V not visited yet do find SCC ( G , u ) the strong component of u : Compute rch( G , u ) using DFS ( G , u ) Compute rch( G rev , u ) using DFS ( G rev , u ) SCC ( G , u ) ⇐ rch( G , u ) ∩ rch( G rev , u ) ∀ u ∈ SCC ( G , u ) : Mark u as visited. Running time: O ( n ( n + m )) Is there an O ( n + m ) time algorithm? Sariel Har-Peled (UIUC) CS374 40 Fall 2017 40 / 60

  44. Structure of a Directed Graph B A C B , E , F A , C , D E F D G H G H Graph of SCC s G SCC Graph G Reminder G SCC is created by collapsing every strong connected component to a single vertex. Proposition For a directed graph G, its meta-graph G SCC is a DAG . Sariel Har-Peled (UIUC) CS374 41 Fall 2017 41 / 60

  45. Linear-time Algorithm for SCC s: Ideas Exploit structure of meta-graph... Wishful Thinking Algorithm Let u be a vertex in a sink SCC of G SCC 1 Do DFS( u ) to compute SCC ( u ) 2 Remove SCC ( u ) and repeat 3 Justification DFS( u ) only visits vertices (and edges) in SCC ( u ) 1 2 3 4 Sariel Har-Peled (UIUC) CS374 42 Fall 2017 42 / 60

  46. Linear-time Algorithm for SCC s: Ideas Exploit structure of meta-graph... Wishful Thinking Algorithm Let u be a vertex in a sink SCC of G SCC 1 Do DFS( u ) to compute SCC ( u ) 2 Remove SCC ( u ) and repeat 3 Justification DFS( u ) only visits vertices (and edges) in SCC ( u ) 1 2 3 4 Sariel Har-Peled (UIUC) CS374 42 Fall 2017 42 / 60

  47. Linear-time Algorithm for SCC s: Ideas Exploit structure of meta-graph... Wishful Thinking Algorithm Let u be a vertex in a sink SCC of G SCC 1 Do DFS( u ) to compute SCC ( u ) 2 Remove SCC ( u ) and repeat 3 Justification DFS( u ) only visits vertices (and edges) in SCC ( u ) 1 ... since there are no edges coming out a sink! 2 3 4 Sariel Har-Peled (UIUC) CS374 42 Fall 2017 42 / 60

  48. Linear-time Algorithm for SCC s: Ideas Exploit structure of meta-graph... Wishful Thinking Algorithm Let u be a vertex in a sink SCC of G SCC 1 Do DFS( u ) to compute SCC ( u ) 2 Remove SCC ( u ) and repeat 3 Justification DFS( u ) only visits vertices (and edges) in SCC ( u ) 1 ... since there are no edges coming out a sink! 2 DFS( u ) takes time proportional to size of SCC ( u ) 3 4 Sariel Har-Peled (UIUC) CS374 42 Fall 2017 42 / 60

  49. Linear-time Algorithm for SCC s: Ideas Exploit structure of meta-graph... Wishful Thinking Algorithm Let u be a vertex in a sink SCC of G SCC 1 Do DFS( u ) to compute SCC ( u ) 2 Remove SCC ( u ) and repeat 3 Justification DFS( u ) only visits vertices (and edges) in SCC ( u ) 1 ... since there are no edges coming out a sink! 2 DFS( u ) takes time proportional to size of SCC ( u ) 3 Therefore, total time O ( n + m ) ! 4 Sariel Har-Peled (UIUC) CS374 42 Fall 2017 42 / 60

  50. Big Challenge(s) How do we find a vertex in a sink SCC of G SCC ? Can we obtain an implicit topological sort of G SCC without computing G SCC ? Answer: DFS( G ) gives some information! Sariel Har-Peled (UIUC) CS374 43 Fall 2017 43 / 60

  51. Big Challenge(s) How do we find a vertex in a sink SCC of G SCC ? Can we obtain an implicit topological sort of G SCC without computing G SCC ? Answer: DFS( G ) gives some information! Sariel Har-Peled (UIUC) CS374 43 Fall 2017 43 / 60

  52. Big Challenge(s) How do we find a vertex in a sink SCC of G SCC ? Can we obtain an implicit topological sort of G SCC without computing G SCC ? Answer: DFS( G ) gives some information! Sariel Har-Peled (UIUC) CS374 43 Fall 2017 43 / 60

  53. Linear Time Algorithm ...for computing the strong connected components in G do DFS( G rev ) and output vertices in decreasing post order. Mark all nodes as unvisited for each u in the computed order do if u is not visited then DFS( u ) Let S u be the nodes reached by u Output S u as a strong connected component Remove S u from G Theorem Algorithm runs in time O ( m + n ) and correctly outputs all the SCCs of G . Sariel Har-Peled (UIUC) CS374 44 Fall 2017 44 / 60

  54. Linear Time Algorithm: An Example - Initial steps Graph G: Reverse graph G rev : B A C B A C = ⇒ E F D E F D G H G H Pre/Post DFS numbering of reverse graph: DFS of reverse graph: [1, 6] [7, 12] B A C [3, 4] B A C ⇒ ⇒ = = [9, 10] [8, 11] [2, 5] E F D E F D [13 , 16] [14 , 15] G H G H Sariel Har-Peled (UIUC) CS374 45 Fall 2017 45 / 60

  55. Linear Time Algorithm: An Example Removing connected components: 1 Do DFS from vertex G Original graph G with rev post remove it. numbers: 6 12 4 B A C 6 12 11 5 10 E F D 4 B A C = ⇒ 15 H 11 5 10 E F D 16 SCC computed: 15 G H { G } Sariel Har-Peled (UIUC) CS374 46 Fall 2017 46 / 60

  56. Linear Time Algorithm: An Example Removing connected components: 2 Do DFS from vertex G Do DFS from vertex H , remove it. remove it. 6 6 12 12 4 4 B A C B A C 11 5 11 5 10 E F D 10 E F D = ⇒ 15 H SCC computed: SCC computed: { G } { G } , { H } Sariel Har-Peled (UIUC) CS374 47 Fall 2017 47 / 60

  57. Linear Time Algorithm: An Example Removing connected components: 3 Do DFS from vertex B Do DFS from vertex H , Remove visited vertices: remove it. { F , B , E } . 6 12 6 4 B A C 4 A C 11 5 10 E F D = ⇒ 5 D SCC computed: SCC computed: { G } , { H } { G } , { H } , { F , B , E } Sariel Har-Peled (UIUC) CS374 48 Fall 2017 48 / 60

  58. Linear Time Algorithm: An Example Removing connected components: 4 Do DFS from vertex A Do DFS from vertex F Remove visited vertices: Remove visited vertices: { A , C , D } . { F , B , E } . 6 4 A C 5 D = ⇒ SCC computed: SCC computed: { G } , { H } , { F , B , E } { G } , { H } , { F , B , E } , { A , C , D } Sariel Har-Peled (UIUC) CS374 49 Fall 2017 49 / 60

  59. Linear Time Algorithm: An Example Final result B A C E F D G H SCC computed: { G } , { H } , { F , B , E } , { A , C , D } Which is the correct answer! Sariel Har-Peled (UIUC) CS374 50 Fall 2017 50 / 60

  60. Obtaining the meta-graph... Once the strong connected components are computed. Exercise: Given all the strong connected components of a directed graph G = ( V , E ) show that the meta-graph G SCC can be obtained in O ( m + n ) time. Sariel Har-Peled (UIUC) CS374 51 Fall 2017 51 / 60

  61. Solving Problems on Directed Graphs A template for a class of problems on directed graphs: Is the problem solvable when G is strongly connected? Is the problem solvable when G is a DAG? If the above two are feasible then is the problem solvable in a general directed graph G by considering the meta graph G SCC ? Sariel Har-Peled (UIUC) CS374 52 Fall 2017 52 / 60

  62. Part V An Application to make Sariel Har-Peled (UIUC) CS374 53 Fall 2017 53 / 60

  63. Make/Makefile (A) I know what make/makefile is. (B) I do NOT know what make/makefile is. Sariel Har-Peled (UIUC) CS374 54 Fall 2017 54 / 60

  64. make Utility [Feldman] Unix utility for automatically building large software applications 1 A makefile specifies 2 Object files to be created, 1 Source/object files to be used in creation, and 2 How to create them 3 Sariel Har-Peled (UIUC) CS374 55 Fall 2017 55 / 60

  65. An Example makefile project: main.o utils.o command.o cc -o project main.o utils.o command.o main.o: main.c defs.h cc -c main.c utils.o: utils.c defs.h command.h cc -c utils.c command.o: command.c defs.h command.h cc -c command.c Sariel Har-Peled (UIUC) CS374 56 Fall 2017 56 / 60

  66. makefile as a Digraph main.c main.o utils.c project defs.h utils.o command.h command.o command.c Sariel Har-Peled (UIUC) CS374 57 Fall 2017 57 / 60

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend