data structures
play

Data Structures Output: for all v V . d [ v ] = discovery time ( - PowerPoint PPT Presentation

Depth-first Search Input: G = ( V , E ), directed or undirected. Data Structures Output: for all v V . d [ v ] = discovery time ( v turns from white to gray) f [ v ] = finishing time ( v turns from gray to black) [ v


  1. Depth-first Search � Input: G = ( V , E ), directed or undirected. Data Structures � Output: � for all v ∈ V . � d [ v ] = discovery time ( v turns from white to gray) � f [ v ] = finishing time ( v turns from gray to black) � π [ v ] : predecessor of v = u , such that v was discovered during the DFS, Topological Sort scan of u ’s adjacency list. � Forest of depth-first trees : G π = ( V,E π ) E π = {( π [ v ], v ), v ∈ V and π [ v ] ≠ null } Dana Shapira 1 2 DFS(G) Example (DFS) DFS-Visit( u ) u v w DFS-Visit( u ) color [ u ] ← GRAY 1. color [ u ] ← GRAY 1. 1/ 1. for each vertex u ∈ V [ G ] time ← time + 1 2. 1. for each vertex u ∈ V [ G ] time ← time + 1 2. 2. do color [ u ] ← white d [ u ] ← time 3. 2. do color [ u ] ← white d [ u ] ← time 3. 3. π [ u ] ← NULL for each v ∈ Adj [ u ] 4. 3. π [ u ] ← NULL for each v ∈ Adj [ u ] 4. 4. time ← 0 5. do if color [ v ] = WHITE 4. time ← 0 5. do if color [ v ] = WHITE 5. for each vertex u ∈ V [ G ] then π [ v ] ← u 5. for each vertex u ∈ V [ G ] 6. then π [ v ] ← u 6. x z y 6. do if color [ u ] = white 7. DFS-Visit( v ) 6. do if color [ u ] = white 7. DFS-Visit( v ) 7. then DFS-Visit( u ) color [ u ] ← BLACK 8. 7. then DFS-Visit( u ) color [ u ] ← BLACK 8. f [ u ] ← time ← time + 1 9. f [ u ] ← time ← time + 1 9. Running time is θ ( V+E ) 3 4

  2. Example (DFS) Parenthesis Theorem u u v v w w Theorem Theorem For all u, v , exactly one of the following holds: 9/12 For all u, v , exactly one of the following holds: 1/8 1/8 2/7 1. d [ u ] < f [ u ] < d [ v ] < f [ v ] or d [ v ] < f [ v ] < d [ u ] < f [ u ] and neither u nor v is 1. d [ u ] < f [ u ] < d [ v ] < f [ v ] or d [ v ] < f [ v ] < d [ u ] < f [ u ] and neither u nor v is a descendant of the other. a descendant of the other. 2. d [ u ] < d [ v ] < f [ v ] < f [ u ] and v is a descendant of u . 2. d [ u ] < d [ v ] < f [ v ] < f [ u ] and v is a descendant of u . 3. d [ v ] < d [ u ] < f [ u ] < f [ v ] and u is a descendant of v . 4/5 3/6 3. d [ v ] < d [ u ] < f [ u ] < f [ v ] and u is a descendant of v . 10/11 x x y y z z � So d [ u ] < d [ v ] < f [ u ] < f [ v ] cannot happen. � Corollary v is a proper descendant of u if and only if d [ u ] < d [ v ] < f [ v ] < f [ u ]. 5 6 Example (Parenthesis Theorem) White-path Theorem y z s t Theorem Theorem 3/6 2/9 1/10 11/16 v is a descendant of u if and only if at time d [ u ], there is a v is a descendant of u if and only if at time d [ u ], there is a path u v consisting of only white vertices. B F C path u v consisting of only white vertices. B 4/5 7/8 12/13 14/15 C C C v x w u (s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t) 7 8

  3. Classification of Edges Identification of Edges � Edge type for edge ( u, v ) can be identified when it is first � Tree edge: Edges in G π . v was found by exploring ( u, v ). explored by DFS. � Identification is based on the color of v . � Back edge: ( u, v ), where u is a descendant of v in G π . � White – tree edge. � Gray – back edge. � Forward edge: ( u, v ), where v is a descendant of u , but not a � Black – forward or cross edge. tree edge. � Cross edge: any other edge. Can go between vertices in same depth-first tree or in different depth-first trees. 9 10 Identification of Edges Directed Acyclic Graph - DAG Theorem: Theorem: In DFS of an undirected graph, we get only tree and back edges. � partial order: In DFS of an undirected graph, we get only tree and back edges. No forward or cross edges. No forward or cross edges. � a > b and b > c ⇒ a > c . � But may have a and b such that neither a > b nor b > a . Proof: � Let ( u,v ) ∈ E . w.l.o.g let d [ u ] < d [ v ]. � Can always make a total order (either a > b or b > a for all Then v must be discovered and finished before u is finished. a ≠ b ) from a partial order. � If the edge ( u,v ) is explored first in the direction u → v , then v is white until that time then it is a tree edge . � If the edge is explored in the direction, v → u , u is still gray at the time the edge is first explored, then it is a back edge. 11 12

  4. Characterizing a DAG Characterizing a DAG Lemma Lemma Lemma Lemma A directed graph G is acyclic iff a DFS of G yields no back edges. A directed graph G is acyclic iff a DFS of G yields no back edges. A directed graph G is acyclic iff a DFS of G yields no back edges. A directed graph G is acyclic iff a DFS of G yields no back edges. Proof (Cont.): Proof: � ⇐ : � ⇒ : � c : cycle in G , v : first vertex discovered in c , ( u, v ) : preceding � Suppose there is a back edge ( u, v ). Then v is an ancestor edge in c . of u in depth-first forest. � At time d [ v ], vertices of c form a white path v u . Why? � Therefore, there is a path v u , so v u v is a cycle. � By white-path theorem, u is a descendent of v in depth-first v u forest. v u � Therefore, (u, v) is a back edge. B B 13 14 Topological Sort Topological Sort � Performed on a DAG. Want to “sort” a directed acyclic graph (DAG). � Linear ordering of the vertices of G such that if ( u , v ) ∈ E , then u appears somewhere before v . B A D Topological-Sort ( G ) Topological-Sort ( G ) 1. call DFS( G ) to compute finishing times f [ v ] for all v ∈ V 1. call DFS( G ) to compute finishing times f [ v ] for all v ∈ V C E 2. as each vertex is finished, insert it onto the front of a linked list 2. as each vertex is finished, insert it onto the front of a linked list 3. return the linked list of vertices 3. return the linked list of vertices C Running time is θ ( V+E ) D A B E 15 16

  5. Example Example B B A D A D 1/ 5/8 1/4 9/10 6/7 2/3 C C E E Linked List: Linked List: 2/3 6/7 1/4 5/8 9/10 E A B C D 17 18 Correctness Proof Strongly Connected Components � Just need to show if ( u, v ) ∈ E , then f [ v ] < f [ u ]. � G is strongly connected if every pair ( u , v ) of vertices in G � When we explore ( u, v ), what are the colors of u and v ? is reachable from each other. � u is gray. � Is v gray, too? � A strongly connected component ( SCC ) of G is a maximal � No , because then v would be an ancestor of u . set of vertices C ⊆ V such that for all u, v ∈ C , both u v � ⇒ ( u, v ) is a back edge. and v u exist. � ⇒ contradiction of Lemma (DAG has no back edges). � Is v white? � v is a descendant of u . � By parenthesis theorem, d [ u ] < d [ v ] < f [ v ] < f [ u ]. � Is v black? � Then v is already finished. � Since we’re exploring ( u, v ), we have not yet finished u . � ⇒ f [ v ] < f [ u ]. 19 20

  6. Component Graph G SCC is a DAG � G SCC = ( V SCC , E SCC ) . Lemma � V SCC has one vertex for each SCC in G . Lemma Let C and C ′ be distinct SCC’s in G , let u , v ∈ C , u ′ , v ′ ∈ C ′ , and Let C and C ′ be distinct SCC’s in G , let u , v ∈ C , u ′ , v ′ ∈ C ′ , and � E SCC has an edge if there is an edge between the suppose there is a path u u ′ in G . Then there cannot also be a path suppose there is a path u u ′ in G . Then there cannot also be a path v ′ corresponding SCC’s in G . v in G . v ′ v in G . Proof: G SCC for the example considered: � Suppose there is a path v ′ v in G . � Then there are paths u u ′ v ′ and v ′ v u in G . � Therefore, u and v ′ are reachable from each other, so they are not in separate SCC’s. 21 22 Transpose of a Directed Graph Algorithm to determine SCCs � G T = transpose of directed G . � G T = ( V , E T ) , E T = { ( u , v ) : ( v , u ) ∈ E } . SCC( G ) SCC( G ) � G T is G with all edges reversed. call DFS( G ) to compute finishing times f [ u ] for all u 1. call DFS( G ) to compute finishing times f [ u ] for all u 1. � Can create G T in Θ ( V + E ) time if using adjacency compute G T 2. compute G T 2. call DFS( G T ), but in the main loop, consider vertices in order lists. 3. call DFS( G T ), but in the main loop, consider vertices in order 3. of decreasing f [ u ] (as computed in first DFS) � G and G T have the same SCC’s. ( u and v are of decreasing f [ u ] (as computed in first DFS) output the vertices in each tree of the depth-first forest 4. output the vertices in each tree of the depth-first forest 4. reachable from each other in G if and only if formed in second DFS as a separate SCC formed in second DFS as a separate SCC reachable from each other in G T .) Running time is θ ( V+E ) 23 24

  7. Example Example G G T a b c d a b c d 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 e f g e h f g h 25 26 Example How does it work? � Idea: � By considering vertices in second DFS in decreasing order of finishing times from first DFS, we are visiting vertices of the cd component graph in topologically sorted order. abe � Because we are running DFS on G T , we will not be visiting any v from a u , where v and u are in different components. � Notation: h fg � d [ u ] and f [ u ] always refer to first DFS. � Extend notation for d and f to sets of vertices U ⊆ V : � d ( U ) = min u ∈ U { d [ u ] } (earliest discovery time) � f ( U ) = max u ∈ U { f [ u ] } (latest finishing time) 27 28

Recommend


More recommend