lecture-9 - university of illinois at chicago › ~xiaorui › cs401 › slides ›...

21
CS 401 Depth First Search / Topological Order Xiaorui Sun 1

Upload: others

Post on 27-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

CS 401

Depth First Search / Topological Order

Xiaorui Sun

1

Page 2: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

Last lecture: DFS

Initialization: mark all vertices undiscovered

DFS(!) Mark ! discovered

for each edge {!, $}if ($ is undiscovered)

Mark $ discoveredDFS($)

Mark ! fully-explored

2

Page 3: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

3

DFS(A)A,1

B J

I

H

C

G

FD

E

K L

M

Suppose edge listsat each vertex are sorted alphabetically

Color code:undiscovereddiscoveredfully-explored

Call Stack(Edge list):

A (B,J)

st[] = {1}

Page 4: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

4

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Color code:undiscovereddiscoveredfully-explored

Call Stack:(Edge list)

TA-DA!!

st[] = {}

Page 5: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

5

DFS(A)A,1

B,2 J,10

I,9

H,8

C,3

G,7

F,6D,4

E,5

K,11 L,12

M,13

Edge code:Tree edgeBack edge

Page 6: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

6

DFS(A) A,1

B,2

J,10

I,9

H,8

C,3

G,7

F,6

D,4

E,5

K,11L,12

M,13

Edge code:Tree edgeBack edgeNo Cross Edges!

Page 7: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

Non-Tree Edges in DFSLemma: For every edge {", $}, if {", $} is not in DFS tree, then one of " or $ is an ancestor of the other in the tree.

Proof:Suppose that " is visited first.Therefore DFS(") was called before DFS($)

Since {", $} is not in DFS tree, $ was visited when the edge {", $}was examined during DFS(")

Therefore $ was visited during the call to DFS(") so $ is a descendant of ".

7

Page 8: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

Non-Tree Edges in DFSBFS tree ≠ DFS tree, but, as with BFS, DFS has found a tree in the graph s.t. non-tree edges are "simple" in some way.

All non-tree edges join a vertex and one of its descendents/ancestors in the DFS tree

8

Page 9: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

Properties of (undirected) DFSLike BFS(!):• DFS(!) visits " iff there is a path in G from ! to "

So, we can use DFS to find connected components• Edges into then-undiscovered vertices define a tree –

the "depth first spanning tree" of G

Unlike the BFS tree: • The DF spanning tree isn't minimum depth• Its levels don't reflect min distance from the root• Non-tree edges never join vertices on the same or

adjacent levels

9

Page 10: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

Directed Graph and Topological Ordering

Page 11: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

Directed Graphs

11

1

210

9

8

3

4

56

7

1112

13Multi edge

self loop

Page 12: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

Node = intersection, edge = one-way street

Directed Graphs

12

Page 13: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

Precedence ConstraintsIn a directed graph, an edge (", $) means task " must occur before task $.

Applications• Course prerequisite:

course " must be taken before $• Compilation:

must compile module " before $• Computing overflow:

output of job " is part of input to job $• Manufacturing or assembly:

sand it before paint it13

Page 14: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

Directed Acyclic Graphs (DAG)Def: A DAG is a directed acyclic graph, i.e., one that contains no directed cycles.

Def: A topological order of a directed graph G = (V, E) is an ordering of its nodes as !", !$, … , !& so that for every edge (!(, !)) we have + < -.

14a DAG

2 3

6 5 4

7 1a topological ordering of that DAG–all edges left-to-right

1 2 3 4 5 6 7

Page 15: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

DAGs: A Sufficient ConditionLemma: If ! has a topological order, then ! is a DAG.

Proof. (by contradiction)

Suppose that ! has a topological order 1,2, … , & and that ! also has a directed cycle '.

Let ( be the lowest-indexed node in ', and let ) be the node just before (; thus (), () is an (directed) edge.

By our choice of (, we have ( < ).On the other hand, since (), () is an edge and 1,… , & is a topological order, we must have ) < (, a contradiction

15

1 i j n

the directed cycle C

the supposed topological order: 1,2,…,n

Page 16: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

DAGs: A Sufficient Condition

16

G has a topological order G is a DAG?

Page 17: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

Every DAG has a source nodeLemma: If ! is a DAG, then ! has a node with no incoming edges (i.e., a source).

Proof. (by contradiction)Suppose that ! is a DAG and it has no source

Pick any node ", and begin following edges backward from ". Since "has at least one incoming edge ($, ") we can walk backward to $.

Then, since $ has at least one incoming edge (', $), we can walk backward to '.

Repeat until we visit a node, say w, twice.

Let C be the sequence of nodes encountered between successive visits to w. C is a cycle.

17

w x u v

Cw x u v

Page 18: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

DAG => Topological OrderLemma: If ! is a DAG, then ! has a topological order

Proof. (by induction on n)Base case: true if " = 1.

Hypothesis: Every DAG with " − 1 vertices has a topological ordering.

Inductive Step: Given DAG with " > 1 nodes, find a source node '.

! − { ' } is a DAG, since deleting ' cannot create cycles.

By hypothesis, ! − { ' } has a topological ordering.

Place ' first in topological ordering; then append nodes of ! − {'}in topological order. This is valid since ' has no incoming edges.

18

Reminder: Always remove vertices/edges to use hypothesis

Page 19: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

A Characterization of DAGs

19

G has a topological order G is a DAG

Page 20: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

20

Topological Order Algorithm 1: Example

2 3

6 5 4

7 1

Page 21: lecture-9 - University of Illinois at Chicago › ~xiaorui › cs401 › slides › lecture-9.pdf · 4 DFS(A),1,2 ,10,9,8,3,7,4 ,6,5 1 ,12,13 Color code: undiscovered discovered fully-explored

21

Topological order: 1, 2, 3, 4, 5, 6, 7

Topological Order Algorithm 1: Example

2 3

6 5 4

7 1

1 2 3 4 5 6 7

Running time: O(n+m)• Adjacency list• Maintain # outgoing edge for each node