Top Banner
1 CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Directed Graphs Objectives: Objectives: Introduce directed graphs and Introduce directed graphs and weighted graphs weighted graphs Present algorithms performed on Present algorithms performed on directed graphs: directed graphs: Reachability Reachability transitive closure transitive closure DAG DAG
33

1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

11

CSC401 – Analysis of Algorithms Lecture Notes 15

Directed GraphsDirected Graphs

Objectives:Objectives: Introduce directed graphs and weighted Introduce directed graphs and weighted

graphsgraphs Present algorithms performed on directed Present algorithms performed on directed

graphs:graphs:– ReachabilityReachability– transitive closuretransitive closure– DAGDAG

Page 2: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

22

DigraphsDigraphs

A A digraphdigraph is a is a graph whose edges graph whose edges are all directedare all directed– Short for “directed Short for “directed

graph”graph”

ApplicationsApplications– one-way streetsone-way streets– flightsflights– task schedulingtask scheduling A

C

E

B

D

Page 3: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

33

Digraph PropertiesDigraph Properties

A graph G=(V,E) such thatA graph G=(V,E) such that– Each edge goes in one direction:Each edge goes in one direction:

Edge Edge (a,b(a,b) goes ) goes from a to bfrom a to b, but , but not b to a.not b to a.

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

If we keep in-edges and out-edges in If we keep in-edges and out-edges in separate adjacency lists, we can perform separate adjacency lists, we can perform listing of in-edges and out-edges in time listing of in-edges and out-edges in time proportional to their size.proportional to their size.

A

C

E

B

D

Page 4: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

44

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

The good life

ics141ics131 ics121

ics53 ics52ics51

ics23ics22ics21

ics161

ics151

ics171

Page 5: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

55

Directed DFSDirected DFSWe can specialize the We can specialize the traversal algorithms (DFS traversal algorithms (DFS and BFS) to digraphs by and BFS) to digraphs by traversing edges only along traversing edges only along their directiontheir directionIn the directed DFS In the directed DFS algorithm, we have four algorithm, we have four types of edgestypes of edges– discovery edgesdiscovery edges– back edgesback edges– forward edgesforward edges– cross edgescross edges

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

A

C

E

B

D

Page 6: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

66

ReachabilityReachability DFS DFS treetree rooted at v: vertices rooted at v: vertices reachable from v via directed reachable from v via directed pathspaths

A

C

E

B

D

F

A

C

E D

A

C

E

B

D

F

Strong Strong ConnectivityConnectivity

Each vertex Each vertex can reach all can reach all other verticesother vertices

a

d

c

b

e

f

g

Page 7: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

77

Pick a vertex v in G.Pick a vertex v in G.

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

“no”.“no”.

Let G’ be G with edges Let G’ be G with edges reversed.reversed.

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

“no”.“no”.– Else, print “yes”.Else, print “yes”.

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

Strong Connectivity AlgorithmStrong Connectivity Algorithm

G:

G’:

a

d

c

b

e

f

g

a

d

c

b

e

f

g

Page 8: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

88

Maximal subgraphs such that each vertex Maximal subgraphs such that each vertex can reach all other vertices in the subgraphcan reach all other vertices in the subgraph

Can also be done in O(n+m) time using DFS, Can also be done in O(n+m) time using DFS, but is more complicated (similar to but is more complicated (similar to biconnectivity).biconnectivity).

Strongly Connected ComponentsStrongly Connected Components

{ a , c , g }

{ f , d , e , b }

a

d

c

b

e

f

g

Page 9: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

99

Transitive ClosureTransitive Closure

Given a digraph Given a digraph GG, the , the transitive closure of transitive closure of GG is is the digraph the digraph G*G* such that such that– G*G* has the same has the same

vertices as vertices as GG– if if GG has a directed has a directed

path from path from uu to to v v ((u u vv), ), G*G* has a directed has a directed edge from edge from uu to to vv

The transitive closure The transitive closure provides reachability provides reachability information about a information about a digraphdigraph

B

A

D

C

E

B

A

D

C

E

G

G*

Page 10: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

1010

Computing the Transitive ClosureComputing the Transitive ClosurePerform DFS starting at each vertex: O(n(n+m))Perform DFS starting at each vertex: O(n(n+m))Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm– 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.– Idea #1: Number the vertices 1, 2, …, n.Idea #1: Number the vertices 1, 2, …, n.– Idea #2: Consider paths that use only vertices numbered 1, Idea #2: Consider paths that use only vertices numbered 1,

2, …, k, as intermediate vertices: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 11: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

1111

Floyd-Warshall’s AlgorithmFloyd-Warshall’s AlgorithmFloyd-Warshall’s algorithm Floyd-Warshall’s algorithm numbers the vertices of numbers the vertices of GG as as vv1 1 , …, v, …, vnn and computes a and computes a series of digraphs series of digraphs GG00, …, G, …, Gnn

– GG00==GG

– GGkk has a directed edge has a directed edge ((vvii, v, vjj) ) if if G G has a directed path has a directed path from from vvii to to vvjj with with intermediate vertices in the intermediate vertices in the set set {{vv1 1 , …, v, …, vkk}}

We have that We have that GGn n = = G*G*

In phase In phase kk, digraph , digraph GGkk is is computed from computed from GGk k 11

Running time: O(nRunning time: O(n33), ), assuming areAdjacent is assuming areAdjacent is O(1) (e.g., adjacency O(1) (e.g., adjacency matrix)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) dofor 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 12: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

1212

Floyd-Warshall ExampleFloyd-Warshall Example

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 13: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

1313

Floyd-Warshall, Iteration 1Floyd-Warshall, Iteration 1

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 14: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

1414

Floyd-Warshall, Iteration 2Floyd-Warshall, Iteration 2

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 15: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

1515

Floyd-Warshall, Iteration 3Floyd-Warshall, Iteration 3

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 16: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

1616

Floyd-Warshall, Iteration 4Floyd-Warshall, Iteration 4

JFK

BOS

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

Page 17: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

1717

Floyd-Warshall, Iteration 5Floyd-Warshall, Iteration 5

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Page 18: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

1818

Floyd-Warshall, Iteration 6Floyd-Warshall, Iteration 6

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Page 19: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

1919

Floyd-Warshall, ConclusionFloyd-Warshall, Conclusion

JFK

MIA

ORD

LAXDFW

SFO

v2

v1v3

v4

v5

v6

v7

BOS

Page 20: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

2020

DAGs and Topological OrderingDAGs and Topological OrderingA directed acyclic graph (DAG) A directed acyclic graph (DAG) is a digraph that has no directed is a digraph that has no directed cyclescycles

A topological ordering of a A topological ordering of a digraph is a numbering digraph is a numbering

vv1 1 , …, v, …, vnn

of the vertices such that for of the vertices such that for every edge every edge ((vvi i , v, vjj)), we have , we have i i j j

Example: in a task scheduling Example: in a task scheduling digraph, a topological ordering digraph, a topological ordering a task sequence that satisfies a task sequence that satisfies the precedence constraintsthe precedence constraints

TheoremTheorem

A digraph admits a topological A digraph admits a topological ordering if and only if it is a ordering if and only if it is a DAGDAG

B

A

D

C

E

DAG G

B

A

D

C

E

Topological ordering of

G

v1

v2

v3

v4 v5

Page 21: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

2121

write c.s. program

play

Topological SortingTopological SortingNumber vertices, so that (u,v) in E implies Number vertices, so that (u,v) in E implies u < vu < 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 22: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

2222

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

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

Algorithm for Topological SortingAlgorithm 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 23: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

2323

Topological Sorting Algorithm using DFSTopological Sorting Algorithm using DFS

Simulate the algorithm by Simulate the algorithm by using depth-first searchusing depth-first search

O(n+m) time.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) UNEXPLORED

w opposite(v,e)if getLabel(w)

UNEXPLOREDsetLabel(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 24: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

2424

Topological Sorting ExampleTopological Sorting Example

Page 25: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

2525

Topological Sorting ExampleTopological Sorting Example

9

Page 26: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

2626

Topological Sorting ExampleTopological Sorting Example

8

9

Page 27: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

2727

Topological Sorting ExampleTopological Sorting Example

78

9

Page 28: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

2828

Topological Sorting ExampleTopological Sorting Example

78

6

9

Page 29: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

2929

Topological Sorting ExampleTopological Sorting Example

78

56

9

Page 30: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

3030

Topological Sorting ExampleTopological Sorting Example

7

4

8

56

9

Page 31: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

3131

Topological Sorting ExampleTopological Sorting Example

7

4

8

56

3

9

Page 32: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

3232

Topological Sorting ExampleTopological Sorting Example 2

7

4

8

56

3

9

Page 33: 1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.

3333

Topological Sorting ExampleTopological Sorting Example 2

7

4

8

56

1

3

9