CS 170: Algorithms Prof David Wagner. Slides edited from a version created by Prof. Satish Rao. For UC-Berkeley CS170 Fall 2014 students use only. Do not re-post or distribute David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 1 / 33
CS 170: Algorithms . . H . H . H . H S David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 2 / 33
Cycle in a directed graph? Fast algorithm for finding out whether directed graph has cycle? For each edge ( u , v ) remove, check if v is connected to u O ( | E | ( | E | + | V | )) . Linear Time (i.e. O ( | V | + | E | ) )? David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 3 / 33
Directed graphs. G = ( V , E ) vertices V . edges E ⊆ V × V . Edge: ( u , v ) From u to v . Source – u Dest – v G Dest B (A,B) C E Source A D F David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 4 / 33
Introspection: pre/post. Previsit(v): 1. Set pre[v] := clock. 2. clock := clock+1 Postvisit(v): 1. Set post[v] := clock. 2. clock := clock+1 DFS(G): 0. Set clock := 0. . . . Clock: goes up to 2 times number of tree edges. First pre: 0 Property: For any two nodes, u and v , [ pre ( u ) , post ( u )] and [ pre ( v ) , post ( v )] are either disjoint or one is contained in other. Interval is “clock interval on stack.” Either both on stack at some point (contained) or not (disjoint.) David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 5 / 33
Directed Acyclic Graphs: Depth First Search Edge: ( u , v ) From u to v . Source – u Dest – v Given a DFS forest, the edge ( u , v ) of the graph is a Tree edge – “Direct call tree of explore”, ( u , v ) ∈ T , pre ( u ) < pre ( v ) < post ( v ) < post ( u ) . Forward edge – “Edge to descendant (not in tree), ( u , v ) / ∈ T , pre ( u ) < pre ( v ) < post ( v ) < post ( u ) Back edge – “Edge to ancestor” ( u , v ) / ∈ T , pre ( v ) < pre ( u ) < post ( u ) < post ( v ) Cross edge – None of the above: ( u , v ) / ∈ T , pre ( v ) < post ( v ) < pre ( u ) < post ( u ) v already explored before u is visited. These are all the possible edges. David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 6 / 33
Directed Acyclic Graph Directed Graph ...without cycles. Why? Hello Goodbye “Hello” before “Goodbye” David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 7 / 33
Example. Acyclic Graph? G B C E A D F David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 8 / 33
Depth first search: directed. 6 1 G G G 7 Back edge ( u , v ) : int( v ) contains int( u ). B B B 8 int ( C ) = [ 3 , 4 ] and int ( B ) = [ 1 , 8 ] . 3 4 C C C 0 2 E E E 9 A A A D D D 12 10 13 5 F F F 11 Back edge ( u , v ) ....edge to ancestor ..........path of tree edges from v to u . Back edge means cycle! = ⇒ not acyclic! David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 9 / 33
Testing for cycle. Thm: A graph has a cycle if and only if there is a back edge in any DFS. Proof: We just saw: Back edge = ⇒ cycle! In the other direction: Assume there is a cycle v 0 → v 1 → v 2 ··· → v k → v 0 Assume that v 0 is the first node explored in the cycle (without loss of generality since can renumber vertices.) When explore ( v 0 ) returns all nodes on cycle explored. All int[ v i ] in int[ v 0 ]! = ⇒ ( v k , v 0 ) is a back edge. Cycle = ⇒ back edge! David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 10 / 33
Fast checking algorithm. Thm: A graph has a cycle if and only if there is back edge. Algorithm ?? Run DFS. O ( | V | + | E | ) time. For each edge ( u , v ) : is int ( u ) in int ( v ) . O ( | E | ) time. O ( | V | + | E | ) time algorithm for checking if graph is acyclic! David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 11 / 33
Directed Acyclic Graph Hello Goodbye “Hello” before “Goodbye” No cycles! Can tell in linear time! Ohhh...Kayyyy... Really want to find ordering for build! David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 12 / 33
Linearize. Topological Sort: For G = ( V , E ) , find ordering of all vertices where each edge goes from earlier vertex to later in acyclic graph. Hello Dinner. Goodbye Tea w/me. Hello Dinner. Goodbye Tea w/me. David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 13 / 33
Topological Sort Example. 6 1 G G G 7 B B B 8 3 4 C C C 0 2 E E E 9 A A A D D D 12 10 13 5 F F F 11 A linear order: A , E , F , B , G , D , C In DFS: When is A popped off stack? Last! When is E popped off? second to last. ... David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 14 / 33
Recommend
More recommend