220 likes | 585 Views
Digraphs. Reachability Connectivity Transitive Closure Floyd-Warshall Algorithm. v. 7. BOS. v. ORD. 4. JFK. v. v. 2. 6. SFO. DFW. LAX. v. 3. v. 1. MIA. v. 5. Digraphs. 1. A typical student day. wake up. 3. 2. eat. study computer sci. 5. 4. work. more c.s. 7.
E N D
Digraphs • Reachability • Connectivity • Transitive Closure • Floyd-Warshall Algorithm v 7 BOS v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
Digraphs 1 A typical student day wake up 3 2 eat study computer sci. 5 4 work more c.s. 7 play 8 write c.s. program 6 battletris 9 make cookies for c.s. prof. 10 sleep 11 dream of cs16
What’s a Digraph? • a) A small burrowing animal with long sharp teeth and a unquenchable lust for the blood of computer science majors • b) A distressed graph • c) A directed graph a Each edge goes in one direction Edge (a,b) goes from a to b, but not b to a You’re saying, “Yo, how about an example of how we might be enlightened by the use of digraphs!!” - Well, if you insist. . .
Applications Maps: digraphs handle one-way streets (especially helpful in Providence)
Another Application cs15 • Scheduling: edge (a,b) means task a must be completed before b can be started cs16 cs22 cs126 cs127 cs141 cs32 cs167 cs31 old computer scientists never die, they just fall into black holes
DAG’s • dag: (noun) dÂ-g 1. Di-Acyl-Glycerol - My favorite snack! 2.“man’s best friend” 3. directed acyclic graph Say What?! directed graph with no directed cycles a b a b c c e d e d DAG not a DAG
Depth-First Search • Same algorithm as for undirected graphs • On a connected digraph, may yield unconnected DFS trees (i.e., a DFS forest) a b c f e d a e b d f c
Reachability • DFS tree rooted at v: vertices reachable from v via directed paths a b c f e d e c f b a c a b d d
Strongly Connected Digraphs • Each vertex can reach all other vertices a g c d e f b
Strongly Connected Components a { a , c , g } g c { f , d , e , b } d e f b
Transitive Closure • Digraph G* is obtained from G using the rule: If there is a directed path in G from a to b, then add the edge (a,b) to G* * G G
Computing the Transitive Closure • We can perform DFS starting at each vertex • Time: O(n(n+m)) • Alternatively ... Floyd-Warshall Algorithm: if there's a way to get from A to B and from B to C then there's a way to get from A to C. “Pink” Flo yd
JFK Example v 7 BOS v ORD 4 v 2 v 6 SFO DFW v LAX 7 BOS v 3 v 1 MIA v ORD 4 v 5 JFK v 2 v 6 SFO DFW LAX v 3 v 1 MIA v 5
Floyd-Warshall Algorithm AlgorithmFloydWarshall(G) let v1 ... vn be an arbitrary ordering of the vertices G0 = G for k = 1 to n do // consider all possible routing vertices vk Gk= Gk-1// these are the only ones you need to store for each (i, j = 1, ..., n) (i != j) (i, j != k) do // for each pair of vertices vi and vj if Gk-1.areAdjacent(vi,vk) and Gk-1.areAdjacent(vk,vj) then Gk.insertDirectedEdge(vi,vj,null) return Gn Assumes that methods areAdjacent and insertDirectedEdge take O(1) time (e.g., adjacency matrix structure) Digraph Gk is the subdigraph of the transitive closure of G induced by paths with intermediate vertices in the set { v1, ..., vk } Running time: O(n3)
Example BOS • digraph G v ORD 4 JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
Example • digraph G* v 7 BOS v 4 ORD JFK v v 2 6 SFO DFW LAX v 3 v 1 MIA v 5
Topological Sorting 1 A typical student day w ak e up • For each edge (u,v), vertex u is visited before vertex v 3 2 eat cs16 meditation 5 4 w ork more cs16 7 play 8 cs16 program 6 cxhe xtris 9 mak e cookies for cs16 HT A 10 sleep 11 dream of cs16
Topological Sorting • Topological sorting may not be unique A A B C D or A C B D C B D - You make the call!
Topological Sorting • Labels are increasing along a directed path • A digraph has a topological sortingif and only ifit is acyclic (i.e., a dag) 1 A 2 3 B C 5 4 E D
Algorithm for Topological Sorting A method T opolog icalSor t if there are more v ertices let v be a source; B C // a v erte x w/o incoming edges label and remo v e v ; ; T opolog icalSor t E D
Algorithm (continued) • Simulate deletion of sources using indegree counters T opSort (V erte x v); label v; f or each edge(v ,w) inde g(w) = inde g(w) - 1; if inde g(w) = 0 T opSort (w); Compute indeg(v) for all vertices Foreach vertex v do if v not labeled and indeg(v) = 0 then TopSort(v)
Example ? 0 ? 0 A B ? 1 C D ? 3 ? 2 E F ? 1 G ? 2 H ? 1 I ? 3