Directed Graphs BOS ORD JFK SFO DFW LAX MIA Directed Graphs 1 Outline and Reading (§6.4) Reachability (§6.4.1) Directed DFS Strong connectivity Transitive closure (§6.4.2) The Floyd-Warshall Algorithm Directed Acyclic Graphs (DAG’s) (§6.4.4) Topological Sorting Directed Graphs 2 Digraphs A digraph is a graph whose edges are all directed D Short for “directed graph” C Applications E one-way streets flights task scheduling B A Directed Graphs 3 E Digraph Properties D C B A graph G=(V,E) such that Each edge goes in one direction: Edge (a,b) goes from a to b, but not b to a. A If G is simple, m < n(n-1). Directed Graphs 4 Digraph Application Scheduling: edge (a,b) means task a must be completed before b can be started ics21 ics22 ics23 ics51 ics53 ics52 ics161 ics131 ics141 ics121 ics171 The good life ics151 Directed Graphs 5 Directed DFS We can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges only along their direction In the directed DFS algorithm, we have four types of edges discovery edges back edges forward edges cross edges E D C B A directed DFS starting at a vertex s determines the vertices reachable from s We say that u reaches v (and v is reachable from u) if there is a directed path from u to v Directed Graphs A 6 Directed DFS Back edge (v,w) w is an ancestor of v in the E tree of discovery edges Forward edge (v,w) v is an ancestor (but not the parent) of w in the tree of discovery edges Cross edge (v,w) w is in the same level as v or in the next level in the tree of discovery edges Discovery edge (v,w) v is the parent of w in the tree of discovery edges Directed Graphs D C B A 7 Reachability DFS tree rooted at v: vertices reachable from v via directed paths E E D C A D C F A E B D C A Directed Graphs F B 8 Strong Connectivity A digraph G is strongly connected if, for any two vertices u and v of G, u reaches v and v reaches u. Each vertex reaches all other vertices a g c d e b f Directed Graphs 9 Strong Connectivity Algorithm Pick a vertex v in G. Perform a DFS from v in G. G: g c If there’s a w not visited, print “no”. d Let G’ be G with edges reversed. Perform a DFS from v in G’. a e b f If there’s a w not visited, print “no”. Else, print “yes”. a G’: g c d Running time: O(n+m). e b f Directed Graphs 10 Strongly Connected Components A maximal subgraph such that each vertex can reach all other vertices in the subgraph. Can also be done in O(n+m) time using DFS, but is more complicated (similar to biconnectivity). a g c d e {a,c,g} {f,d,e,b} b f Directed Graphs 11 Transitive Closure Given a digraph G, the transitive closure of G is the digraph G* such that G* has the same vertices as G if G has a directed path from u to v (u v), G* has a directed edge from u to v The transitive closure provides reachability information about a digraph Directed Graphs D E B C G A D E B C A G* 12 Computing the Transitive Closure We can perform DFS starting at each vertex 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. O(n(n+m)) Alternatively ... Use dynamic programming: the Floyd-Warshall Algorithm Directed Graphs 13 Floyd-Warshall Transitive Closure Idea #1: Number the vertices 1, 2, …, n. Idea #2: Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices: i Uses only vertices numbered 1,…,k (add this edge if it’s not already in) j Uses only vertices numbered 1,…,k-1 k Uses only vertices numbered 1,…,k-1 Directed Graphs 14 Floyd-Warshall’s Algorithm Floyd-Warshall’s algorithm numbers the vertices of G as v1 , …, vn and computes a series of digraphs G0, …, Gn Algorithm FloydWarshall(G) Input digraph G Output transitive closure G* of G i1 for all v G.vertices() G0=G denote v as vi Gk has a directed edge (vi, vj) ii+1 if G has a directed path from G0 G vi to vj with intermediate for k 1 to n do vertices in the set {v1 , …, vk} Gk Gk - 1 We have that Gn = G* for i 1 to n (i k) do for j 1 to n (j i, k) do In phase k, digraph Gk is if Gk - 1.areAdjacent(vi, vk) computed from Gk - 1 Gk - 1.areAdjacent(vk, vj) Running time: O(n3), if Gk.areAdjacent(vi, vj) assuming areAdjacent is O(1) Gk.insertDirectedEdge(vi, vj , k) (e.g., adjacency matrix) return Gn Directed Graphs 15 Floyd-Warshall Example v7 BOS ORD v4 JFK v2 v6 SFO DFW LAX v1 v3 MIA v5 Directed Graphs 16 Floyd-Warshall, Iteration 1 v7 BOS ORD v4 JFK v2 v6 SFO DFW LAX v1 v3 MIA v5 Directed Graphs 17 Floyd-Warshall, Iteration 2 v7 BOS ORD v4 JFK v2 v6 SFO DFW LAX v1 v3 MIA v5 Directed Graphs 18 Floyd-Warshall, Iteration 3 v7 BOS ORD v4 JFK v2 v6 SFO DFW LAX v1 v3 MIA v5 Directed Graphs 19 Floyd-Warshall, Iteration 4 v7 BOS ORD v4 JFK v2 v6 SFO DFW LAX v1 v3 MIA v5 Directed Graphs 20 Floyd-Warshall, Iteration 5 v7 BOS ORD v4 JFK v2 v6 SFO DFW LAX v1 v3 MIA v5 Directed Graphs 21 Floyd-Warshall, Iteration 6 v7 BOS ORD v4 JFK v2 v6 SFO DFW LAX v1 v3 MIA v5 Directed Graphs 22 Floyd-Warshall, Conclusion v7 BOS ORD v4 JFK v2 v6 SFO DFW LAX v1 v3 MIA v5 Directed Graphs 23 DAGs and Topological Ordering A directed acyclic graph (DAG) is a digraph that has no directed cycles A topological ordering of a digraph is a numbering v1 , …, vn of the vertices such that for every edge (vi , vj), we have i < j Example: in a task scheduling digraph, a topological ordering is a task sequence that satisfies the precedence constraints Theorem A digraph has a topological ordering if and only if it is a DAG D E B C DAG G A v2 v1 Directed Graphs D B C A v4 E v5 v3 Topological ordering of G 24 Topological Sorting Number vertices, so that (u,v) in E implies u < v wake up 1 A typical student day 3 2 study computer sci. eat 4 7 play nap 5 more c.s. 8 write c.s. program 9 make cookies for professors 6 work out 10 sleep 11 dream about graphs Directed Graphs 25 Algorithm for Topological Sorting Note: This algorithm is different than the one in Goodrich-Tamassia Method TopologicalSort(G) HG // Temporary copy of G n G.numVertices() while H is not empty do Let v be a vertex with no outgoing edges Label v n nn-1 Remove v from H Running time: O(n + m). How…? Directed Graphs 26 Topological Sorting Algorithm using DFS Simulate the algorithm by using depth-first search Algorithm topologicalDFS(G) Input dag G Output topological ordering of G n G.numVertices() for all u G.vertices() setLabel(u, UNEXPLORED) for all e G.edges() setLabel(e, UNEXPLORED) for all v G.vertices() if getLabel(v) = UNEXPLORED topologicalDFS(G, v) O(n+m) time. Algorithm topologicalDFS(G, v) Input graph G and a start vertex v of G Output labeling of the vertices of G in the connected component of v setLabel(v, VISITED) for all e G.incidentEdges(v) if getLabel(e) = UNEXPLORED w opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) topologicalDFS(G, w) else {e is a forward or cross edge} Label v with topological number n nn-1 Directed Graphs 27 Topological Sorting Example Directed Graphs 28 Topological Sorting Example 9 Directed Graphs 29 Topological Sorting Example 8 9 Directed Graphs 30 Topological Sorting Example 7 8 9 Directed Graphs 31 Topological Sorting Example 6 7 8 9 Directed Graphs 32 Topological Sorting Example 6 5 7 8 9 Directed Graphs 33 Topological Sorting Example 4 6 5 7 8 9 Directed Graphs 34 Topological Sorting Example 3 4 6 5 7 8 9 Directed Graphs 35 Topological Sorting Example 2 3 4 6 5 7 8 9 Directed Graphs 36 Topological Sorting Example 2 1 3 4 6 5 7 8 9 Directed Graphs 37