Directed Graphs (Digraphs)

BOS

ORD

JFK

SFO

DFW LAX

MIA Outline and Reading

Reachability (6.4.1) • Directed DFS • Strong connectivity

Transitive closure (6.4.2) • The Floyd-Warshall Algorithm

Directed Acyclic Graphs (DAGs) (6.4.4) •

Digraphs 2 Digraphs A digraph (short for “”) is a graph whose edges are all directed • Ex: Edge (a,b) goes from a to b, but not b to a.

Properties: E • If G is simple, m ≤ n(n-1). D • If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of the sets of in-edges and out-edges in time C proportional to their size. B

A Applications include one-way streets, flights, and task scheduling.

Digraphs 3 Digraph Application Scheduling: edge (a,b) means task a must be completed before b can be started.

Computer Calc I CSIA CSIB Architecture

Discrete CSII OS Calc II Structures

Algorithms CSIII

Digraphs 4 Directed DFS • We can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges only along their D direction

E • In the directed DFS algorithm, we have four types of edges – discovery edges B

– back edges C – forward edges

– cross edges A

• A directed DFS starting at a vertex s determines the vertices reachable from s

Digraphs 5 Reachability DFS rooted at v: vertices reachable from v via directed paths

Applications: • Dead code detection/elimination • Garbage collection E D Reachable from C C E D A

C F E D Reachable from B A B C F

A B Digraphs 6 Strong Connectivity

Each vertex can reach all other vertices • How can we test if G is strongly connected?

a g c

d e

b f

Digraphs 7 Strong Connectivity Algorithm Determine if G is strongly connected a • Pick a vertex v in G G: g c • Perform a DFS from v in G – If there’s a w not visited, print “no” d e • Let G’ be G with edges reversed f b • Perform a DFS from v in G’ – If there’s a w not visited, print “no” a g – Else, print “yes” G’: c

d e

Running time: O(n+m). f b

Digraphs 8 Strongly Connected Components A strongly connected is 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 { a , c , g } g c

d e { f , d , e , b }

b f

Digraphs 9

Given a digraph G, the transitive D E closure of G is the digraph G* such that • G* has the same vertices as G B G • if G has a directed path from u to v C (u ¹ v), G* has a directed edge from u to v A

The transitive closure provides reachability information about a D E digraph. B

C

A G*

Digraphs 10 Computing the Transitive Closure

• One idea: perform DFS starting at each vertex – This is O(n(n+m)) time – Recall that m is O(n2)

• Second idea: use dynamic programming – Observe that 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. – This becomes part of our subproblem characterization – This is known as Floyd-Warshall’s algorithm, which runs in O(n3) time using an adjacency matrix

Digraphs 11 Floyd-Warshall Transitive Closure

• Number the vertices 1, 2, …, n. • Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices:

Uses only vertices numbered 1, …, k (add this edge if it’s not already in) i

Uses only vertices j numbered 1,…, k-1 Uses only vertices k numbered 1,…, k-1

Digraphs 12 Floyd-Warshall’s Algorithm

• Numbers the vertices of G as v1 , Algorithm FloydWarshall(G) …, vn and computes a series of digraphs G , …, G Input digraph G 0 n Output transitive closure G* of G – G0=G i ¬ 1 – Gk has a directed edge (vi, vj) for all v Î G.vertices() if G has a directed path from denote v as vi vi to vj with intermediate i ¬ i + 1 vertices in the set {v1 , …, vk} G0 ¬ G for k ¬ 1 to n do • We have that Gn = G* Gk ¬ Gk - 1 • In phase k, digraph Gk is for i ¬ 1 to n (i ¹ k) do computed from Gk - 1 for j ¬ 1 to n (j ¹ i, k) do 3 • Running time: O(n ), assuming if Gk - 1.areAdjacent(vi, vk) Ù areAdjacent is O(1) (e.g., Gk - 1.areAdjacent(vk, vj) adjacency matrix) if ¬Gk.areAdjacent(vi, vj) Gk.insertDirectedEdge(vi, vj , k) return Gn Digraphs 13 Floyd-Warshall Example v7 BOS

v ORD 4

JFK v 2 v 6 SFO

DFW LAX v v 3 1 MIA v 5 Digraphs 14 Floyd-Warshall, Iteration 1 v7 BOS

v ORD 4

JFK v 2 v 6 SFO

DFW LAX v v 3 1 MIA v 5 Digraphs 15 Floyd-Warshall, Iteration 2 v7 BOS

v ORD 4

JFK v 2 v 6 SFO

DFW LAX v v 3 1 MIA v 5 Digraphs 16 Floyd-Warshall, Iteration 3 v7 BOS

v ORD 4

JFK v 2 v 6 SFO

DFW LAX v v 3 1 MIA v 5 Digraphs 17 Floyd-Warshall, Iteration 4 v7 BOS

v ORD 4

JFK v 2 v 6 SFO

DFW LAX v v 3 1 MIA v 5 Digraphs 18 Floyd-Warshall, Iteration 5 v7 BOS

v ORD 4

JFK v 2 v 6 SFO

DFW LAX v v 3 1 MIA v 5 Digraphs 19 Floyd-Warshall, Iteration 6 v7 BOS

v ORD 4

JFK v 2 v 6 SFO

DFW LAX v v 3 1 MIA v 5 Digraphs 20 Floyd-Warshall, Conclusion v7 BOS

v ORD 4

JFK v 2 v 6 SFO

DFW LAX v v 3 1 MIA v 5 Digraphs 21 DAGs and Topological Ordering • A (DAG) is a digraph that has no directed cycles D E • A topological ordering of a digraph is a

numbering v1 , …, vn of the vertices such B that for every edge (vi , vj), we have i < j C • Ex: in a task scheduling digraph, a topological order is a task sequence that A DAG G satisfies the precedence constraints v4 v5 D E Theorem v2 A digraph admits a topological ordering if B and only if it is a DAG v3

v1 C A Topological ordering of G

Digraphs 22 xkcd #754 Topological Sorting

Number vertices, so that (u,v) in E implies u < v

1 wake up A typical student day 2 3 eat study computer sci.

4 5 nap more CS 7 play 8 write program 6 9 work out make cookies for professors 10 sleep 11 dream about graphs Digraphs 23 Algorithm for Topological Sorting

• Note: This algorithm is different than the one in Goodrich-Tamassia

Method TopologicalSort(G) H ¬ G // 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 n ¬ n - 1 Remove v from H

• Running time: O(n + m). How…?

Digraphs 24 Topological Sorting Algorithm using DFS

Simulate the algorithm by using DFS Algorithm topologicalDFS(G, v) Input graph G and a start vertex v of G Algorithm topologicalDFS(G) Output labeling of the vertices of G in the connected component of v Input dag G setLabel(v, VISITED) Output topological ordering of G n ¬ G.numVertices() for all e Î G.outgoingIncidentEdges(v) for all u Î G.vertices() if getLabel(e) = UNEXPLORED setLabel(u, UNEXPLORED) w ¬ opposite(v,e) for all e Î G.edges() if getLabel(w) = UNEXPLORED setLabel(e, UNEXPLORED) setLabel(e, DISCOVERY) for all v Î G.vertices() topologicalDFS(G, w) if getLabel(v) = UNEXPLORED else topologicalDFS(G, v) {e is a forward or cross edge} Label v with topological number n n ¬ n - 1 • O(n+m) time.

Digraphs 25 Topological Sorting Example

Digraphs 26 Topological Sorting Example

9

Digraphs 27 Topological Sorting Example

8

9

Digraphs 28 Topological Sorting Example

7 8

9

Digraphs 29 Topological Sorting Example

6

7 8

9

Digraphs 30 Topological Sorting Example

6 5

7 8

9

Digraphs 31 Topological Sorting Example

4

6 5

7 8

9

Digraphs 32 Topological Sorting Example

3 4

6 5

7 8

9

Digraphs 33 Topological Sorting Example

2

3 4

6 5

7 8

9

Digraphs 34 Topological Sorting Example

2 1

3 4

6 5

7 8

9

Digraphs 35