25
Lecture 16: Directed Graphs BOS JFK ORD SFO LAX DFW MIA Courtesy of Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie

Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Lecture 16: Directed Graphs

BOS

JFK

ORD

SFO

LAXDFW

MIA

Courtesy of Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie

Page 2: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

O tlineOutlineDirected GraphsDirected Graphs

PropertiesAlgorithms for Directed GraphsAlgorithms for Directed Graphs

DFS and BFSStrong ConnectivityStrong ConnectivityTransitive ClosureDAG and Topological Orderingp g g

2

Page 3: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

DigraphsA digraph is a graph whose Eg pedges are all directed D

Short for “directed graph”

l

C

BApplicationsone-way streets A

B

flightstask scheduling

3

Page 4: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Digraph Properties E

A graph G=(V,E) such thatEach edge goes in one direction:

d ( ) fC

D

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

If G is simple, m < n*(n-1). A

Bp ( )

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

A

out-edges in time proportional to their sizeOutgoingEdges(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

E is ea hable f om A

4

E is reachable from AE is not reachable from D

Page 5: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Directed DFSAlgorithm DFS(G, v)Input digraph G and a start vertex v of GOutput traverses vertices in G reachable

We can specialize the traversal algorithms (DFS and BFS) to digraphs by

Output traverses vertices in G reachable from v

setLabel(v, VISITED)for all e ∈ G.outgoingEdges(v)BFS) to digraphs by

traversing edges only along their direction

w ← opposite(v,e)if getLabel(w) = UNEXPLORED

DFS(G, w)In the directed DFS

E3

4

algorithm, we have four types of edges

discovery edgesb k d

C

D4back edges

forward edgescross edges

C

B

2

5

A directed DFS starting at a vertex s visits all the vertices reachable from s

Unlike undirected graph if u is

A 1

Unlike undirected graph, if u is reachable from s, it does not mean that s is reachable from u

Page 6: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Reachability E DGraph G

ReachabilityC F

A B

DFS tree rooted at v: vertices reachable from vi di t d th

BFS tree rooted at v: vertices reachable from v via directed

thvia directed paths

E D E D

paths

C

DFS(G C) A

C

B

F

A DFS(G,C) A B

BFS (G,B)

Page 7: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Strong ConnectivityA graph is strongly connected if we can reach all other vertices from any fixed vertex

a g a gac

gc

de

de

bf b

f

t t l t d hstrongly connected graph not strongly connected graphcannot reach a from g

Page 8: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Inefficient Algorithm to Check for Strong Connectivity

stronglyConnected = true

Connectivity

G:g yfor every vertex v in G

DFS(G,v)

a

d

cg

if some vertex w is not visited stronglyConnected = false

d

b

e

f

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

8

Page 9: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Efficient Algorithm to Check for Strong Connectivity

1) Pick a vertex v in G2) Perform a DFS from v in G

Connectivityv

cg

If there’s a vertex w not visited, print “no” and terminate the algorithmelse (all vertices are visited) from v we can reach any other graph vertex

d

b

e

wreach any other graph vertex 3) Let G’ be G with edges reversed G4) Perform a DFS from v in G’.

If th i th f t i G’ th th vc

g

If there is path from v to w in G’, then there is path from w to v in the original graph GIf every vertex is reachable from v in G’, then there is a path from any vertex w to v in the

d

b

e

w

there is a path from any vertex w to v in the original graph G

Then G is strongly connectedTo find path between any nodes b and d:

9

G’p y

first go from b to v, then go from v to dRunning time is O(n + m), perform DFS 2 times

Page 10: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Transitive ClosureD E

Given a digraph G, the transitive closure of G is the digraph G* such that

B

D

C

E

Gthe digraph G* such thatG* has the same vertices as G

AC

G

if G has a directed path from u to v (u ≠ v), G*has a directed edge from gu to v

D EThe transitive closure

i h bili B

C

summarizes reachability information about a digraph

10

A G*

Page 11: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Computing Transitive ClosureWe can perform DFS(G,v) for each vertex v

for all vertices v in graph Gfor all vertices v in graph GDFS(G,v)for all vertices w in graph G

if w is reachable from vput edge (v,w) in G*

Running time is O(n(n+m))This is O(n2) for sparse graphs

A h i if it h O( ) dA graph is sparse if it has O(n) edgesThis is O(n3) for dense graphs

A graph is dense if it has O(n2) edges

11

Page 12: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

DAGs and Topological OrderingA directed acyclic graph(DAG) is a digraph that has no di ected c cles

D Edirected cycles

B

CDAG GA topological ordering of a

digraph is a numbering of

AC

4 5

vertices:1 , 2, …, n

such that for every edge (v, w),

B

D E2

4 5

Example: in a task scheduling di h t l i l d i

y g ( , ),number(v) < number(w)

B

AC

T l i l1

3digraph, a topological ordering of a task sequence satisfies the precedence constraints

A Topological ordering of G

Page 13: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

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

wake up A typical student day1

2 3eat

study computer sci.2 3

4 5

write c.s. program

play

nap more c.s.

6

7

8p g

work out6

9

10

make cookies for professors

13

sleep

dream about graphs

1011

Page 14: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Ordering TheoremTheorem: Digraph admits topological ordering if and only if it is a DAGProof:

Suppose graph is not a DAG that is it has a cycle v1 v2 vn-1Suppose graph is not a DAG, that is it has a cycle v , v ,…, v , 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 edgesIf 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 orderingRemoving this vertex from the graph with incoming andRemoving this vertex from the graph with incoming and outgoing edges leaves the graph acyclic

counter = nRepeat until counter = 0Repeat until counter = 0

1. Find vertex v with no outgoing edges2. Set topological label of v to counter3. Remove v from the graph together

i h ll i i d i dwith all incoming and outgoing edges.4. counter = counter -1

Page 15: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Sorting Algorithm Using DFSSimulate the algorithm by using depth-first searchcounter is an instance (global) variable

Algorithm topologicalDFS(G, v)Input graph G and a start vertex v

Algorithm topologicalDFS(G)Input dag G

Output labeling of the vertices of Gin the connected component of v

setLabel(v, VISITED)

Output topological ordering of Gcounter ← G.numVertices()for all u ∈ G.vertices()

for all e ∈ G.outgoingEdges(v)w ← opposite(v,e)if getLabel(w) = UNEXPLORED

setLabel(u, UNEXPLORED)for all v ∈ G.vertices()

if getLabel(v) = UNEXPLORED

O(n + m) time.

topologicalDFS(G, w)v.topologicalNumber = countercounter ← counter - 1

topologicalDFS(G, v)

15

Page 16: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Sorting Examplep g g p

16

Page 17: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Sorting Examplep g g p

9

17

9

Page 18: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Sorting Example

8

9

18

9

Page 19: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Sorting Example

778

9

19

9

Page 20: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Sorting Examplep g g p

6

7

6

78

9

20

9

Page 21: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Sorting Examplep g g p

56

7

56

78

9

21

9

Page 22: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Sorting Example

4

56

7

56

78

9

22

9

Page 23: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Sorting Examplep g g p

34

56

3

7

56

78

9

23

9

Page 24: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Sorting Examplep g g p

22

34

56

3

7

56

78

9

24

9

Page 25: Lecture 16: Directed Graphs - ORCCAyxie/courses/cs2210b-2011/htmls/... · Directed DFS Algorithm DFS(G, v) Input digraph G and a start vertex v of G Output traverses vertices in G

Topological Sorting Example

22 1

34

56

3

7

56

78

9

25

9