CS 374: Algorithms & Models of Computation Chandra Chekuri University of Illinois, Urbana-Champaign Spring 2017 Chandra Chekuri (UIUC) CS374 1 Spring 2017 1 / 1
Today Two topics: Structure of directed graphs DFS and its properties One application of DFS to obtain fast algorithms Chandra Chekuri (UIUC) CS374 2 Spring 2017 2 / 1
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 Chekuri (UIUC) CS374 3 Spring 2017 3 / 1
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 Chekuri (UIUC) CS374 4 Spring 2017 4 / 1
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 Chekuri (UIUC) CS374 5 Spring 2017 5 / 1
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 Chekuri (UIUC) CS374 6 Spring 2017 6 / 1
Part I Directed Acyclic Graphs Chandra Chekuri (UIUC) CS374 7 Spring 2017 7 / 1
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 Chekuri (UIUC) CS374 8 Spring 2017 8 / 1
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 Chekuri (UIUC) CS374 9 Spring 2017 9 / 1
Simple DAG Properties Proposition Every DAG G has at least one source and at least one sink. Chandra Chekuri (UIUC) CS374 10 Spring 2017 10 / 1
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 Chekuri (UIUC) CS374 10 Spring 2017 10 / 1
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 Chekuri (UIUC) CS374 10 Spring 2017 10 / 1
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 Chekuri (UIUC) CS374 11 Spring 2017 11 / 1
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 Chekuri (UIUC) CS374 12 Spring 2017 12 / 1
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 Chekuri (UIUC) CS374 13 Spring 2017 13 / 1
Topological Sort: Example c a b e d g f h Chandra Chekuri (UIUC) CS374 14 Spring 2017 14 / 1
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 Chekuri (UIUC) CS374 15 Spring 2017 15 / 1
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 Chekuri (UIUC) CS374 16 Spring 2017 16 / 1
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 Chekuri (UIUC) CS374 17 Spring 2017 17 / 1
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 Chekuri (UIUC) CS374 18 Spring 2017 18 / 1
Part II Depth First Search (DFS) Chandra Chekuri (UIUC) CS374 19 Spring 2017 19 / 1
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 Chekuri (UIUC) CS374 20 Spring 2017 20 / 1
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 Chekuri (UIUC) CS374 21 Spring 2017 21 / 1
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 Chekuri (UIUC) CS374 22 Spring 2017 22 / 1
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 Chekuri (UIUC) CS374 23 Spring 2017 23 / 1
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 Chekuri (UIUC) CS374 24 Spring 2017 24 / 1
Example 1 7 9 2 3 4 5 8 10 6 Chandra Chekuri (UIUC) CS374 25 Spring 2017 25 / 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. Chandra Chekuri (UIUC) CS374 26 Spring 2017 26 / 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. Chandra Chekuri (UIUC) CS374 26 Spring 2017 26 / 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 . Chandra Chekuri (UIUC) CS374 26 Spring 2017 26 / 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) . Chandra Chekuri (UIUC) CS374 26 Spring 2017 26 / 1
Recommend
More recommend