Digraphs 9/2/2002 3:16 AM

Outline and Reading Digraphs Digraphs Traversals of digraphs (§6.4.1) (§6.4.2) D E Floyd-Warshall’s algorithm (§6.4.2) B Directed acyclic graphs (§6.4.3) C A Topological ordering (§6.4.3)

9/2/2002 3:16 AM Digraphs 1 9/2/2002 3:16 AM Digraphs 2

Digraphs Directed DFS

We can specialize the A digraph is a traversal algorithms (DFS and E BFS) to digraphs by E whose traversing edges only along edges are all directed D their direction D In the directed DFS Applications algorithm, we have four C types of edges C ! one-way streets ! discovery edges ! flights B ! back edges B ! forward edges ! task scheduling ! cross edges A A A directed DFS starting a a vertex s determines the vertices reachable from s

9/2/2002 3:16 AM Digraphs 3 9/2/2002 3:16 AM Digraphs 4

Transitive Closure Floyd-Warshall’s Algorithm

Algorithm FloydWarshall(G) Given a digraph G, the transitive D E Floyd-Warshall’s Input digraph G closure of G is the digraph G* algorithm numbers the such that Output transitive closure G* of G B vertices of a digraph G as i ← 1 ! G* has the same vertices as G G v1 , …, vn and computes a for all v ∈ G.vertices() ! if G has a directed path from u to C series of digraphs denote v as v v (u ≠ v), G* has a directed edge i G0, …, Gn i ← i + 1 from u to v A ! G =G ← 0 G0 G The transitive closure provides ← ! Gk has a directed edge for k 1 to n do reachability information about a ← (vi, vj) if G has a directed Gk Gk − 1 digraph ← ≠ D E path from vi to vj with for i 1 to n (i ≠ k) do We can compute the transitive intermediate vertices in for j ← 1 to n (j ≠ i, k) do closure in time O(n(n + m)) by the set {v , …, v } B 1 k if G − .areAdjacent(v , v ) ∧ repeated applications of directed k 1 i k We have that Gn = G* Gk − 1.areAdjacent(vk, vj) DFS C if ¬G .areAdjacent(v , v ) In phase k, digraph Gk is k i j computed from G Gk.insertDirectedEdge(vi, vj , k) A G* k − 1 return Gn 9/2/2002 3:16 AM Digraphs 5 9/2/2002 3:16 AM Digraphs 6

1 Digraphs 9/2/2002 3:16 AM

Example DAGs and Topological Ordering v v4 5 D E A (DAG) is a D E v2 digraph that has no directed cycles = = = v B B G G0 G1 G2 v4 5 A topological ordering of a digraph D E is a numbering C v3 v C v1 2 v1 , …, vn A B of the vertices such that for every A DAG G edge (v , v ), we have i < j v v i j v4 5 v C 3 Example: in a task scheduling v v D E 1 4 5 A digraph, a topological ordering a D E v2 task sequence that satisfies the v2 B precedence constraints B G G = G = G* Theorem v v C v3 3 4 5 3 1 A digraph admits a topological v C A ordering if and only if it is a DAG 1 Topological A ordering of G 9/2/2002 3:16 AM Digraphs 7 9/2/2002 3:16 AM Digraphs 8

2