Download presentation
Presentation is loading. Please wait.
Published byAnabel Fowler Modified over 8 years ago
1
1 Digraphs Reachability Connectivity Transitive Closure Floyd-Warshall Algorithm JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 v 7
2
2 Digraphs wake up eat work study computer sci. more c.s. play battletris sleep dream of cs16 write c.s. program A typical student day 1 2 3 4 5 6 7 8 9 10 11 make cookies for c.s. prof.
3
3 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 Each edge goes in one direction Edge (a,b) goes from a to b, but not b to a a You’re saying, “Yo, how about an example of how we might be enlightened by the use of digraphs!!” - Well, if you insist...
4
4 Applications Maps: digraphs handle one-way streets (especially helpful in Providence)
5
5 Another Application Scheduling: edge (a,b) means task a must be completed before b can be started cs16 cs15 cs31 cs126 cs32 cs127 cs167 cs141 cs22 old computer scientists never die, they just fall into black holes
6
6 DAG’s dag: (noun) dÂ-g 1. Di-Acyl-Glycerol - My favorite snack! 2.“man’s best friend” 3. directed acyclic graph Say What?! ab c d e ab c d e DAG not a DAG directed graph with no directed cycles
7
7 Depth-First Search Same algorithm as for undirected graphs On a connected digraph, may yield unconnected DFS trees (i.e., a DFS forest) ab c d e f a b c d e f
8
8 Reachability DFS tree rooted at v: vertices reachable from v via directed paths ab c d e f c a b d e b f d c a
9
9 Strongly Connected Digraphs Each vertex can reach all other vertices a b d c e f g
10
10 Strongly Connected Components a b d c e f g { a, c, g } { f, d, e, b }
11
11 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 *
12
12 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” Floyd
13
13 Example BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 v 7 JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 v 7
14
14 Floyd-Warshall Algorithm Assumes that methods areAdjacent and insertDirectedEdge take O(1) time (e.g., adjacency matrix structure) Algorithm FloydWarshall(G) let v1... vn be an arbitrary ordering of the vertices G 0 = G for k = 1 to n do // consider all possible routing vertices vk G k = G k-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 v i and v j if G k-1.areAdjacent(v i,v k ) and G k-1.areAdjacent(v k,v j ) then G k.insertDirectedEdge(v i,v j,null) return G n Digraph G k is the subdigraph of the transitive closure of G induced by paths with intermediate vertices in the set { v 1,..., v k } Running time: O(n 3 )
15
15 Example digraph G JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6
16
16 Example digraph G* JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 v 7
17
17 Topological Sorting For each edge (u,v), vertex u is visited before vertex v wake up eat work cs16 meditation more cs16 play cxhextris make cookies for cs16 HTA sleep dream of cs16 cs16 program A typical student day 1 2 3 4 5 6 7 8 9 10 11
18
18 Topological Sorting Topological sorting may not be unique A B C D A B C D or A C B D - You make the call!
19
19 Topological Sorting Labels are increasing along a directed path A digraph has a topological sorting if and only if it is acyclic (i.e., a dag) A B C D E 1 2 3 4 5
20
20 Algorithm for Topological Sorting method TopologicalSort if there are more vertices letv be a source; // a vertex w/o incoming edges label and removev; TopologicalSort ; A B C D E
21
21 Algorithm (continued) Simulate deletion of sources using indegree counters TopSort(Vertex v); label v; foreach edge(v,w) indeg(w) = indeg(w) 1; if indeg(w) = 0 TopSort(w); Compute indeg(v) for all vertices Foreach vertex v do if v not labeled and indeg(v) = 0 then TopSort(v)
22
22 Example A C E H G D F I B ?0 ?0 ?1 ?3 ?1 ?2 ?2 ?1 ?3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.