directed graphs (digraphs) - cs.kent.edu

35
Directed Graphs (Digraphs) JFK BOS MIA ORD LAX DFW SFO

Upload: others

Post on 17-Apr-2022

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Directed Graphs (Digraphs) - cs.kent.edu

Directed Graphs (Digraphs)

JFK

BOS

MIA

ORD

LAXDFW

SFO

Page 2: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 2

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)• Topological Sorting

Page 3: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 3

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

A

C

E

B

D

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

Properties:• 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 the sets of in-edges and out-edges in time proportional to their size.

Page 4: Directed Graphs (Digraphs) - cs.kent.edu

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

ComputerArchitecture

Algorithms

CSII Calc IIDiscreteStructures

CSIBCSIA Calc I

OS

CSIII

Digraphs 4

Page 5: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 5

Directed DFS• We can specialize the traversal

algorithms (DFS and BFS) to digraphs by traversing edges only along their direction

• In the directed DFS algorithm, we have four types of edges– discovery edges– back edges– forward edges– cross edges

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

A

B

D

C

E

Page 6: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 6

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

Applications:• Dead code detection/elimination• Garbage collection

A

C

E

B

D

F

A

C

E D

A

C

E

B

D

F

Reachable from C

Reachable from B

Page 7: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 7

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

a

d

c

b

e

f

g

Page 8: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 8

Determine if G is strongly connected• 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: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 9

A strongly connected component 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).

Strongly Connected Components

{ a , c , g }

{ f , d , e , b }

a

d

c

b

e

f

g

Page 10: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 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: Directed Graphs (Digraphs) - cs.kent.edu

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

Page 12: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 12

Floyd-Warshall Transitive Closure• Number the vertices 1, 2, …, n.• Consider paths that use only vertices numbered 1, 2, …, k, as

intermediate vertices:

k

j

i

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

Uses only verticesnumbered 1,…, k-1

Uses only verticesnumbered 1,…, k-1

Page 13: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 13

Floyd-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 vii ¬ i + 1

G0 ¬ Gfor k ¬ 1 to n do

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

for j ¬ 1 to n (j ¹ i, k) doif Gk - 1.areAdjacent(vi, vk) Ù

Gk - 1.areAdjacent(vk, vj)if ¬Gk.areAdjacent(vi, vj)

Gk.insertDirectedEdge(vi, vj , k)return Gn

Page 14: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 14

Floyd-Warshall Example

JFK

BOS

MIA

ORD

LAXDFW

SFO

v 2

v1

v 3

v 4

v 5

v 6

v7

Page 15: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 15

Floyd-Warshall, Iteration 1

JFK

BOS

MIA

ORD

LAXDFW

SFO

v 2

v1

v 3

v 4

v 5

v 6

v7

Page 16: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 16

Floyd-Warshall, Iteration 2

JFK

BOS

MIA

ORD

LAXDFW

SFO

v 2

v1

v 3

v 4

v 5

v 6

v7

Page 17: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 17

Floyd-Warshall, Iteration 3

JFK

BOS

MIA

ORD

LAXDFW

SFO

v 2

v1

v 3

v 4

v 5

v 6

v7

Page 18: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 18

Floyd-Warshall, Iteration 4

JFK

BOS

MIA

ORD

LAXDFW

SFO

v 2

v1

v 3

v 4

v 5

v 6

v7

Page 19: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 19

Floyd-Warshall, Iteration 5

JFK

MIA

ORD

LAXDFW

SFO

v 2

v1

v 3

v 4

v 5

v 6

v7BOS

Page 20: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 20

Floyd-Warshall, Iteration 6

JFK

MIA

ORD

LAXDFW

SFO

v 2

v1

v 3

v 4

v 5

v 6

v7BOS

Page 21: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 21

Floyd-Warshall, Conclusion

JFK

MIA

ORD

LAXDFW

SFO

v 2

v1

v 3

v 4

v 5

v 6

v7BOS

Page 22: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 22

DAGs and Topological Ordering• A directed acyclic graph (DAG) is a

digraph that has no directed cycles• A topological ordering of a digraph is a

numbering v1 , …, vn of the vertices such that for every edge (vi , vj), we have i < j

• Ex: in a task scheduling digraph, a topological order is 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

xkcd #754

Page 23: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 23

write program

play

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

wake up

eat

nap

study computer sci.

more CS

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: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 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 Gn ¬ 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: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 25

Topological Sorting Algorithm using DFS

Simulate the algorithm by using DFS

• O(n+m) time.

Algorithm topologicalDFS(G, v)Input graph G and a start vertex v of GOutput labeling of the vertices of G

in the connected component of vsetLabel(v, VISITED)for all e Î G.outgoingIncidentEdges(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 nn ¬ 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) = UNEXPLOREDtopologicalDFS(G, v)

Page 26: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 26

Topological Sorting Example

Page 27: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 27

Topological Sorting Example

9

Page 28: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 28

Topological Sorting Example

8

9

Page 29: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 29

Topological Sorting Example

7

8

9

Page 30: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 30

Topological Sorting Example

7

8

6

9

Page 31: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 31

Topological Sorting Example

7

8

56

9

Page 32: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 32

Topological Sorting Example

7

4

8

56

9

Page 33: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 33

Topological Sorting Example

7

4

8

56

3

9

Page 34: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 34

Topological Sorting Example2

7

4

8

56

3

9

Page 35: Directed Graphs (Digraphs) - cs.kent.edu

Digraphs 35

Topological Sorting Example2

7

4

8

56

1

3

9