© 2004 goodrich, tamassia directed graphs1 jfk bos mia ord lax dfw sfo

35
© 2004 Goodrich, Tamassia Directed Graphs 1 Directed Graphs JFK BOS MIA ORD LAX DFW SFO

Upload: beatriz-ducksworth

Post on 01-Apr-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

Directed Graphs 1© 2004 Goodrich, Tamassia

Directed GraphsJFK

BOS

MIA

ORD

LAXDFW

SFO

Page 2: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 2

Digraphs (§ 12.4)

A digraph is a graph whose edges are all directed Short for “directed

graph”

Applications one-way streets flights task scheduling

A

C

E

B

D

Page 3: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 3

Digraph Properties

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.

If G is simple, m < n*(n-1).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

C

E

B

D

Page 4: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 4

Digraph ApplicationScheduling: edge (a,b) means task a must be completed before b can be started

The good life

ics141ics131 ics121

ics53 ics52ics51

ics23ics22ics21

ics161

ics151

ics171

Page 5: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 5

Directed DFSWe can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges only along their directionIn the directed DFS algorithm, we have four types of edges discovery edges back edges forward edges cross edges

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

A

C

E

B

D

Page 6: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 6

Reachability

DFS tree rooted at v: vertices reachable from v via directed paths

A

C

E

B

D

FA

C

E D

A

C

E

B

D

F

Page 7: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 7

Strong ConnectivityEach vertex can reach all other vertices

a

d

c

b

e

f

g

Page 8: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 8

Pick a vertex v in G.Perform a DFS from v in G. If there’s a w not visited, print

“no”.

Let G’ be G with edges reversed.Perform a DFS from v in G’. If there’s a w not visited, print

“no”. Else, print “yes”.

Running time: O(n+m).

Strong Connectivity Algorithm

G:

G’:

a

d

c

b

e

f

g

a

d

c

b

e

f

g

Page 9: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 9

Maximal subgraphs such that each vertex can reach all other vertices in the subgraphCan also be done in O(n+m) time using DFS, but is more complicated (similar to biconnectivity).

Strongly Connected Components

{ a , c , g }

{ f , d , e , b }

a

d

c

b

e

f

g

Page 10: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 10

Transitive ClosureGiven 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

B

A

D

C

E

B

A

D

C

E

G

G*

Page 11: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 11

Computing the Transitive Closure

We can perform DFS starting at each vertex O(n(n+m))

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.

Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm

Page 12: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 12

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:

k

j

i

Uses only verticesnumbered 1,…,k-1 Uses only vertices

numbered 1,…,k-1

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

Page 13: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 13

Floyd-Warshall’s AlgorithmFloyd-Warshall’s algorithm numbers the vertices of G as v1 , …, vn and computes a series of digraphs G0, …, Gn

G0=G Gk has a directed edge (vi,

vj) if G has a directed path from vi to vj with intermediate vertices in the set {v1 , …, vk}

We have that Gn = G*

In phase k, digraph Gk is computed from Gk - 1

Running time: O(n3), assuming areAdjacent is O(1) (e.g., adjacency matrix)

Algorithm FloydWarshall(G)Input digraph GOutput transitive closure G* of Gi 1for all v G.vertices()

denote v as vi

i i + 1G0 Gfor k 1 to n do

Gk Gk - 1 for i 1 to n (i k) do

for j 1 to n (j i, k) do

if Gk -

1.areAdjacent(vi, vk)

Gk - 1.areAdjacent(vk, vj)

if Gk.areAdjacent(vi, vj)

Gk.insertDirectedEdge(vi, vj , k)

return Gn

Page 14: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 14

Floyd-Warshall Example

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 15: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 15

Floyd-Warshall, Iteration 1

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 16: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 16

Floyd-Warshall, Iteration 2

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 17: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 17

Floyd-Warshall, Iteration 3

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 18: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 18

Floyd-Warshall, Iteration 4

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 19: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 19

Floyd-Warshall, Iteration 5

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Page 20: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 20

Floyd-Warshall, Iteration 6

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Page 21: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 21

Floyd-Warshall, Conclusion

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Page 22: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 22

DAGs and Topological Ordering

A directed acyclic graph (DAG) is a digraph that has no directed cyclesA 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 a task sequence that satisfies the precedence constraints

TheoremA digraph admits a topological ordering if and only if it is a DAG

B

A

D

C

E

DAG G

B

A

D

C

E

Topological ordering of

G

v1

v2

v3

v4 v5

Page 23: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 23

write c.s. program

play

Topological SortingNumber vertices, so that (u,v) in E implies u < v wake up

eat

nap

study computer sci.

more c.s.

work out

sleep

dream about graphs

A typical student day1

2 3

4 5

6

7

8

9

1011

make cookies for professors

Page 24: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 24

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

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

Algorithm for Topological Sorting

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 edgesLabel v nn n - 1Remove v from H

Page 25: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 25

Topological Sorting Algorithm using DFS

Simulate the algorithm by using depth-first search

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) =

UNEXPLOREDw 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 n n - 1

Algorithm topologicalDFS(G)Input dag GOutput 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)

Page 26: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 26

Topological Sorting Example

Page 27: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 27

Topological Sorting Example

9

Page 28: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 28

Topological Sorting Example

8

9

Page 29: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 29

Topological Sorting Example

78

9

Page 30: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 30

Topological Sorting Example

78

6

9

Page 31: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 31

Topological Sorting Example

78

56

9

Page 32: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 32

Topological Sorting Example

7

4

8

56

9

Page 33: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 33

Topological Sorting Example

7

4

8

56

3

9

Page 34: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 34

Topological Sorting Example

2

7

4

8

56

3

9

Page 35: © 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO

© 2004 Goodrich, Tamassia Directed Graphs 35

Topological Sorting Example

2

7

4

8

56

1

3

9