Top Banner
CS 473 Lecture 14 1 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort
32

CS473-Algorithms I

Jan 02, 2016

Download

Documents

hilda-glover

CS473-Algorithms I. Lecture 15 Graph Searching: Depth-First Search and Topological Sort. Depth-First Search. Graph G ( V , E ) directed or undirected Adjacency list representation Goal : Systematically explore every vertex and every edge Idea : search deeper whenever possible - PowerPoint PPT Presentation
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: CS473-Algorithms I

CS 473 Lecture 14 1

CS473-Algorithms I

Lecture 15

Graph Searching:

Depth-First Search and Topological Sort

Page 2: CS473-Algorithms I

CS 473 Lecture 14 2

Depth-First Search

• Graph G(V,E) directed or undirected

• Adjacency list representation

• Goal: Systematically explore every vertex and every edge

• Idea: search deeper whenever possible– Using a LIFO queue (Stack; FIFO queue used in BFS)

Page 3: CS473-Algorithms I

CS 473 Lecture 14 3

Depth-First Search

• Maintains several fields for each vV

• Like BFS, colors the vertices to indicate their states. Each vertex is– Initially white, – grayed when discovered, – blackened when finished

• Like BFS, records discovery of a white v during scanning Adj[u] by [v] u

Page 4: CS473-Algorithms I

CS 473 Lecture 14 4

Depth-First Search

• Unlike BFS, predecessor graph G produced by DFS forms spanning forest

• G(V,E) where

E{([v],v): vV and [v] NIL}

• G depth-first forest (DFF) is composed of disjoint depth-first trees (DFTs)

Page 5: CS473-Algorithms I

CS 473 Lecture 14 5

Depth-First Search

• DFS also timestamps each vertex with two timestamps

• d[v]: records when v is first discovered and grayed

• f[v]: records when v is finished and blackened

• Since there is only one discovery event and finishing event for each vertex we have 1 d[v] f[v] 2|V|

1 2 d[v] f[v] 2|V|

Time axis for the color of a vertex

Page 6: CS473-Algorithms I

CS 473 Lecture 14 6

Depth-First Search

DFS(G)

for each uV docolor[u] white [u] NIL

time 0

for each uV doif color[u] white

then DFS-VISIT(G, u)

DFS-VISIT(G, u)color[u] gray d[u] time time 1

for each v Adj[u] do if color[v] white then [v] u

DFS-VISIT(G, v)

color[u] black f[u] time time 1

Page 7: CS473-Algorithms I

CS 473 Lecture 14 7

Depth-First Search• Running time: (VE)• Initialization loop in DFS : (V)• Main loop in DFS: (V) exclusive of time to execute

calls to DFS-VISIT

• DFS-VISIT is called exactly once for each vV since– DFS-VISIT is invoked only on white vertices and– DFS-VISIT(G, u) immediately colors u as gray

• For loop of DFS-VISIT(G, u) is executed |Adj[u]| time• Since |Adj[u]| E, total cost of executing loop of

DFS-VISIT is (E)

Page 8: CS473-Algorithms I

CS 473 Lecture 14 8

Depth-First Search: Example

x y z

s t

w v u

Page 9: CS473-Algorithms I

CS 473 Lecture 14 9

Depth-First Search: Example

x y z

s t

w v u

1

Page 10: CS473-Algorithms I

CS 473 Lecture 14 10

Depth-First Search: Example

x y z

s t

w v u

1

2

Page 11: CS473-Algorithms I

CS 473 Lecture 14 11

Depth-First Search: Example

x y z

s t

w v u

1

2

3

Page 12: CS473-Algorithms I

CS 473 Lecture 14 12

Depth-First Search: Example

x y z

s t

w v u

1

2

3

Page 13: CS473-Algorithms I

CS 473 Lecture 14 13

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4

Page 14: CS473-Algorithms I

CS 473 Lecture 14 14

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5

Page 15: CS473-Algorithms I

CS 473 Lecture 14 15

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5

Page 16: CS473-Algorithms I

CS 473 Lecture 14 16

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

Page 17: CS473-Algorithms I

CS 473 Lecture 14 17

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

Page 18: CS473-Algorithms I

CS 473 Lecture 14 18

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

8

Page 19: CS473-Algorithms I

CS 473 Lecture 14 19

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

8

Page 20: CS473-Algorithms I

CS 473 Lecture 14 20

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

8

9

Page 21: CS473-Algorithms I

CS 473 Lecture 14 21

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

8

9

Page 22: CS473-Algorithms I

CS 473 Lecture 14 22

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7

8

9 10

Page 23: CS473-Algorithms I

CS 473 Lecture 14 23

Depth-First Search: Example

x y z

s t

w v u

1

2

3 4 5 6

7 9 10

8 11

Page 24: CS473-Algorithms I

CS 473 Lecture 14 24

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12

Page 25: CS473-Algorithms I

CS 473 Lecture 14 25

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12 13

Page 26: CS473-Algorithms I

CS 473 Lecture 14 26

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12 13

Page 27: CS473-Algorithms I

CS 473 Lecture 14 27

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12 13

Page 28: CS473-Algorithms I

CS 473 Lecture 14 28

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12 13

14

Page 29: CS473-Algorithms I

CS 473 Lecture 14 29

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12 13

14

Page 30: CS473-Algorithms I

CS 473 Lecture 14 30

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12 13

14 15

Page 31: CS473-Algorithms I

CS 473 Lecture 14 31

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12

14 15

13 16

Page 32: CS473-Algorithms I

CS 473 Lecture 14 32

Depth-First Search: Example

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12

14 15

13 16

x y z

s t

w v u

2

3 4 5 6

7 9 10

8 111 12

14 15

13 16

DFS(G) terminated Depth-first forest (DFF)