Lecture 16: Directed Graphs

BOS

ORD

JFK

SFO

DFW LAX

MIA

Courtesy of Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Outline ƒ Directed Graphs ƒ Properties ƒ Algorithms for Directed Graphs ƒ DFS and BFS ƒ Strong Connectivity ƒ ƒ DAG and Toppgological Ordering

2 Digraphs

ƒ A digraph is a ggpraph whose E edges are all directed D ƒ Short for “” C ƒ Applications B ƒ one-way streets A ƒ flights ƒ task scheduling

3 Digraph Properties E D ‰ A graph G=(V,E) such that „ Each edge goes in one direction: C Š Ed(dge (A,B) goes from A to B,

Š Edge (B,A) goes from B to A, B

‰ If G is simple, m < n*(n-1). A ‰ If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of in-edges and out-edges in time proportional to their size

„ OutgoingEdges(A): (A,C), (A,D), (A,B) „ IngoingEdges(A): (E,A),(B,A) ƒ We say that vertex w is reachable from vertex v if there is a directed path from v to w ƒ EiseahablefomAE is reachable from A ƒ E is not reachable from D 4 Algorithm DFS(G, v) Directed DFS Input digraph G and a start vertex v of G Output traverses vertices in G reachable from v ‰ We can specialize the setLabel(v, VISITED) traversal algorithms (DFS and for all e ∈ G.outgoingEdges(v) BFS) to digraphs by traversing edges only along w ← opposite(v,e) their direction if getLabel(w) = UNEXPLORED DFS(G, w) ƒ In the directed DFS algorithm, we have 3 four types of edges E „ discovery edges 4 „ bkdback edges D „ forward edges „ cross edges ƒ A directed DFS starting at a C 2 vertex s visits all the vertices reachable from s B 5 „ Unlike undirected graph , if u is reachable from s, it does not mean that s is reachable from u A 1 Graph G Reachability E D

C F

A B

‰ DFS rooted at v: „ BFS tree rooted at v: vertices vertices reachable from v reachable from v via directed viditdia directed path s paths

E D E D

C C F DFS(G,C) A A B BFS (G,B) Strong Connectivity

‰ A graph is strongly connected if we can reach all other vertices from any fixed vertex

a g a g c c

d d e e

f f b b strongly connected graph nottt strong ly connec tdted grap h cannot reach a from g Inefficient Algorithm to Check for Strong Connectivity

stronggylyConnected = true G: a for every vertex v in G g c DFS(G,v) d if some vertex w is not visited e

stronglyConnected = false f b

ƒ DFS(G,v) visits every vertex reachable from v ƒ Simplffle, yet very inefficient, O[(n+m)n]

8 Efficient Algorithm to Check for Strong Connectivity v 1) Pick a vertex v in G g 2) Perform a DFS from v in G c ƒ If there’s a vertex w not visited, print “no” and d terminate the algorithm e

ƒ else (all vertices are visited) from v we can b reach any other graph vertex w 3) Let G’ be G with edges reversed G 4) Perform a DFS from v in G’. ƒ If there is path f rom v to w iG’ththin G’, then there v is path from w to v in the original graph G g c ƒ If every vertex is reachable from v in G’, then there is a path from any vertex w to v in the d original graph G e

ƒ Then G is strongly connected b ƒ To find ppyath between any nodes b and d: w first go from b to v, then go from v to d G’ ƒ Running time is O(n + m), perform DFS 2 times 9 Transitive Closure D E ƒ Given a digraph G, the transitive closure of G is B G the digraph G* such that C ƒ G* has the same vertices A as G ƒ if G has a directed path from u to v (u ≠ v), G* has a directed edge from u to v D E ƒ The transitive closure summarizes reach abili ty B information about a digraph C A G*

10 Computing Transitive Closure

‰ We can perform DFS(G,v) for each vertex v for all vertices v in graph G DFS(G,v) for all vertices w in graph G if w is reachable from v put edge (v,w) in G*

‰ Running time is O(n(n+m)) „ This is O(n2) for sparse graphs Š AhiifithO()dA graph is sparse if it has O(n) edges „ This is O(n3) for dense graphs Š A graph is dense if it has O(n2) edges

11 DAGs and Topological Ordering

‰ A (DAG) is a digraph that has no D E directed cy cles B ƒ A topological ordering of a DAG G digraph is a numbering of C vertices: A 1 , 2, …, n such that for everyyg edge (v, w), 4 5 number(v) < number(w) D E ƒ Example: in a task scheduling 2 B digraph , a t opol ogi cal ord eri ng 3 of a task sequence satisfies 1 C the precedence constraints A TlilTopological ordering of G Topological Ordering

‰ Scheduling problem: edge (a,b) means task a must be completed before b can be started ‰ Number vertices, so that u. number < v.number for any edge (u,v)

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

4 5 nap more c.s. 7 play 8 write c.s. ppgrogram 6 9 work out make cookies for professors 10 sleep 11

13 dream about graphs Topological Ordering Theorem

Theorem: Digraph admits topological ordering if and only if it is a DAG Proof: Suppose graph is not a DAG , that is it has a v1, v2,…, vn-1, vn, v1. If topological ordering existed, it would imply that v1.num < v2.num < …< vn-1.num < vn.num < v1.num ¾ If graph is a DAG , there is a vertex v with no outgoing edges ƒ if every vertex has an outgoing edge, then performing DFS we will encounter a back edge which means cycle ¾ This vertex can be the last one in topological ordering ¾ Removing this vertex from the graph with incoming and outgoing edges leaves the graph acyclic counter = n Repeat until counter =0= 0 1. Find vertex v with no outgoing edges 2. Set topological label of v to counter 3. Remove v from the graph together wihith all lli incomi ng and outgoi ng ed ges. 4. counter = counter -1 Algorithm Using DFS

‰ Simulate the algorithm by using depth-first search

‰ counter is an instance (global) variable

Algorithm topologicalDFS(G) Algorithm topologicalDFS(G, v) Input dag G Input graph G and a start vertex v Output topological ordering of G Output labeling of the vertices of G counter ← G.numVertices() in the connected of v for all u ∈ G.vertices() setLabel(v, VISITED) setLabel(u, UNEXPLORED) for all e ∈ G.outgoingEdges(v) for all v ∈ G.vertices() w ← opposite(v,e) if getLabel(v) = UNEXPLORED if getLabel(w) = UNEXPLORED topologicalDFS(G, v) topologicalDFS(G, w) v.topologicalNumber = counter ‰ O(n + m) time. counter ← counter - 1

15 Toppgological Sorting Examp le

16 Toppgological Sorting Examp le

9

17 Topological Sorting Example

8

9

18 Topological Sorting Example

7 8

9

19 Toppgological Sorting Examp le

6

7 8

9

20 Toppgological Sorting Examp le

6 5

7 8

9

21 Topological Sorting Example

4 6 5

7 8

9

22 Toppgological Sorting Examp le

3 4 6 5

7 8

9

23 Toppgological Sorting Examp le

2

3 4 6 5

7 8

9

24 Topological Sorting Example

2 1

3 4 6 5

7 8

9

25