EE 355 Unit 18 DFS and Topological Sort Mark Redekopp 2 - - PowerPoint PPT Presentation

ee 355 unit 18
SMART_READER_LITE
LIVE PREVIEW

EE 355 Unit 18 DFS and Topological Sort Mark Redekopp 2 - - PowerPoint PPT Presentation

1 EE 355 Unit 18 DFS and Topological Sort Mark Redekopp 2 Topological Sort Given a graph of dependencies (tasks, prerequisities, etc.) EE 109L MA 245 PHYS 152 topological sort creates a consistent ordering of tasks EE 209 EE 202L


slide-1
SLIDE 1

1

EE 355 Unit 18

DFS and Topological Sort Mark Redekopp

slide-2
SLIDE 2

2

Topological Sort

  • Given a graph of dependencies

(tasks, prerequisities, etc.) topological sort creates a consistent ordering of tasks (vertices) where no dependencies are violated

  • Many possible valid topological
  • rderings exist

– EE 109L, EE 209, EE 354L, EE 454L, EE 457, MATH 245, PHYS 152, EE 202L,… – MATH 245, EE 109L, PHYS 152L, EE 154,… EE 109L EE 209 EE 354L MA 245 EE 202L EE 457 EE 454L EE 338 EE 348 PHYS 152 EE 447 EE 448

slide-3
SLIDE 3

3

Topological Sort

  • Another example

– Getting dressed

  • More Examples:

– Project management scheduling – Build order in a Makefile or

  • ther compile project

– Cooking using a recipe – Instruction execution on an

  • ut-of-order pipelined CPU

– Production of output values in a simulation of a combinational gate network

Underwear Pants Belt Undershirt Shoes Tie Shirt Socks

http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorith ms/GraphAlgor/topoSort.htm

Jacket

slide-4
SLIDE 4

4

Topological Sort

  • Does breadth-first search

work?

  • No. What if we started at EE200…

– We'd go to EE 202L before PHYS 152

  • All parent nodes need to be

completed before any child node

  • BFS only guarantees some

parent has completed before child

  • Turns out a Depth-First Search

will be part of our solution

EE 109L EE 209 EE 354L MA 245 EE 202L EE 457 EE 454L EE 338 EE 348 PHYS 152 EE 447 EE 448

slide-5
SLIDE 5

5

Depth First Search

  • Explores ALL children

before completing a parent

– Note BFS completes a parent before ANY children

  • For DFS let us assign:

– A start time when the node is first found – A finish time when a node is completed

  • If we look at our nodes in

reverse order of finish time (i.e. last one to finish back to first

  • ne to finish) we arrive at a…

– Topological ordering!!!

EE 109L EE 209 EE 354L MA 245 EE 202L EE 457 EE 454L EE 338 EE 348 PHYS 152 EE 447 EE 448

10 1 2 3 4 6 5 7 8 9 11 12 13 15 16 18 14 17 19 20 21 22 24 23 1 2 Start Time Finish Time PHYS 152, MATH 245, EE 202L, EE 348, EE 448, EE 447, EE 338, EE 109L, EE 209, EE 354L, EE 454L, EE 457 Reverse Finish Time Order

slide-6
SLIDE 6

6

DFS & Topological Sort

  • Imagine we flipped our

dependencies (edge direction) and performed a DFS starting at nodes with no incoming edges

  • Now as nodes

“finish/complete” we could enter them in a list and that list would provide a valid topological ordering

EE 109L EE 209 EE 354L MA 245 EE 202L EE 457 EE 454L EE 338 EE 348 PHYS 152 EE 447 EE 448

1 4 5 3 2 12 10 11 9 6 7 8 1st DFS 2nd DFS 3rd DFS 4th DFS 5th DFS

slide-7
SLIDE 7

7

DFS Algorithm

  • Visit a node

– Mark as visited – For each visited neighbor, visit it and perform DFS on all of their children – Only then, mark as finished

  • DFS is recursive!!
  • If cycles in the graph, ensure we

don’t get caught visiting neighbors endlessly

– Color them as we go – White = unvisited, – Gray = visited but not finished – Black = finished

DFS (G) 1 for each vertex u 2 u.color = WHITE 3 u.pred = nil 4 time = 0 5 finish_list = empty_list 6 for each vertex u do 7 if u.color == WHITE then 8 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 u.start = ++time 3 for each vertex v in Adj(u) do 5 if v.color = WHITE then 6 v.pred = u 7 DFS-Visit (G, v) 8 u.color = BLACK 9 finish_list.append(u)

slide-8
SLIDE 8

8

Simplified DFS for Topo-sort

  • For topological sort, all we care

about is finish time

  • If in line 4 of toposort() we

simply iterate through goal/output nodes as our start locations then the ordering in the finish_list will be a correct toposort with input nodes first…

  • This is a recursive solution

– We can have an iterative solution shown later…

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

slide-9
SLIDE 9

9

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

slide-10
SLIDE 10

10

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): u v

slide-11
SLIDE 11

11

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): u v DFS-Visit(G,d):

slide-12
SLIDE 12

12

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): u v DFS-Visit(G,d): DFS-Visit(G,f):

slide-13
SLIDE 13

13

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): u DFS-Visit(G,d): DFS-Visit(G,f): DFS-Visit(G,h):

slide-14
SLIDE 14

14

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): u DFS-Visit(G,d): DFS-Visit(G,f): DFS-Visit(G,h): Finish_list: h

slide-15
SLIDE 15

15

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): u DFS-Visit(G,d): DFS-Visit(G,f): Finish_list: h v

slide-16
SLIDE 16

16

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): DFS-Visit(G,d): DFS-Visit(G,f): Finish_list: h u DFS-Visit(G,g):

slide-17
SLIDE 17

17

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): DFS-Visit(G,d): DFS-Visit(G,f): Finish_list: h, g u DFS-Visit(G,g):

slide-18
SLIDE 18

18

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): DFS-Visit(G,d): DFS-Visit(G,f): Finish_list: h, g, f u

slide-19
SLIDE 19

19

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): DFS-Visit(G,d): Finish_list: h, g, f, d u

slide-20
SLIDE 20

20

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): Finish_list: h, g, f, d u v

slide-21
SLIDE 21

21

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): Finish_list: h, g, f, d u v DFS-Visit(G,c):

slide-22
SLIDE 22

22

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): Finish_list: h, g, f, d u DFS-Visit(G,c): DFS-Visit(G,e):

slide-23
SLIDE 23

23

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): Finish_list: h, g, f, d. e u DFS-Visit(G,c): DFS-Visit(G,e):

slide-24
SLIDE 24

24

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): Finish_list: h, g, f, d. e, c u DFS-Visit(G,c):

slide-25
SLIDE 25

25

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,a): Finish_list: h, g, f, d. e, c, a u

slide-26
SLIDE 26

26

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,b): Finish_list: h, g, f, d. e, c, a u

May iterate through many complete vertices before finding b to launch a new search from

slide-27
SLIDE 27

27

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

DFS-Visit(G,b): Finish_list: h, g, f, d. e, c, a, b u

slide-28
SLIDE 28

28

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b c d e f g h

Finish_list: h, g, f, d. e, c, a, b u

slide-29
SLIDE 29

29

ANOTHER EXAMPLE

With Cycles in the graph

slide-30
SLIDE 30

30

Depth First-Search

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) a b d c h e f g

slide-31
SLIDE 31

31

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

u v

slide-32
SLIDE 32

32

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): v u

slide-33
SLIDE 33

33

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): u v

slide-34
SLIDE 34

34

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): v u

slide-35
SLIDE 35

35

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): u v

slide-36
SLIDE 36

36

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): u v

slide-37
SLIDE 37

37

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): u v

slide-38
SLIDE 38

38

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): DFS-Visit(G,d): u v

slide-39
SLIDE 39

39

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): DFS-Visit(G,d): DFSQ: d u

slide-40
SLIDE 40

40

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): DFSQ: d u v

slide-41
SLIDE 41

41

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): DFSQ: d DFS-Visit(G,e): u v v

slide-42
SLIDE 42

42

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFS-Visit(G,f): DFSQ: d e DFS-Visit(G,e): u

slide-43
SLIDE 43

43

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFS-Visit(G,c): DFS-Visit(G,b): DFS-Visit(G,h): DFS-Visit(G,g): DFSQ: d e f DFS-Visit(G,f): u

slide-44
SLIDE 44

44

Depth First-Search

a b d c h e f g

DFS-Visit(G,a):

Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFSQ: d e f g h b c u

slide-45
SLIDE 45

45

Depth First-Search

a b d c h e f g Toposort(G) 1 for each vertex u 2 u.color = WHITE 3 finish_list = empty_list 4 for each vertex u do 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) 1 u.color = GRAY 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u)

DFSQ: d e f g h b c a

slide-46
SLIDE 46

46

BFS vs. DFS Algorithm

  • BFS and DFS are more similar than you think

– Do we use a FIFO/Queue (BFS) or LIFO/Stack (DFS) to store vertices as we find them

BFS-Visit (G, start_node) 1 for each vertex u 2 u.color = WHITE 3 u.pred = nil 4 bfsq = new deque 5 bfsq.push_back(start_node) 6 while bfsq not empty 7 u = bfsq.pop_front() 8 if u.color == WHITE 9 u.color = GRAY 10 foreach vertex v in Adj(u) do 11 bfsq.push_back(v) DFS-Visit (G, start_node) 1 for each vertex u 2 u.color = WHITE 3 u.pred = nil 4 dfsq = new deque 5 dfsq.push_back(start_node) 6 while dfsq not empty 7 u = dfsq.pop_back() 8 if u.color == WHITE 9 u.color = GRAY 10 foreach vertex v in Adj(u) do 11 dfsq.push_back(v)

slide-47
SLIDE 47

47

ITERATIVE VERSION

slide-48
SLIDE 48

48

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a

slide-49
SLIDE 49

49

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a c e

slide-50
SLIDE 50

50

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a c e c f

slide-51
SLIDE 51

51

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a c e c f d g

slide-52
SLIDE 52

52

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a c e c f d g c h

slide-53
SLIDE 53

53

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a c e c f d g c h b

slide-54
SLIDE 54

54

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a c e c f d g c h b c

slide-55
SLIDE 55

55

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a c e c f d g c h b c d

slide-56
SLIDE 56

56

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a c e c f d g c h b c d

slide-57
SLIDE 57

57

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a c e c f d g c h b c d

slide-58
SLIDE 58

58

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a c e c f d g c h b c

slide-59
SLIDE 59

59

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a c e c f d g c h b

slide-60
SLIDE 60

60

Depth First-Search

DFS (G,s) 1 for each vertex u 2 u.color = WHITE 3 dfsq = new deque 4 dfsq.push_back(s) 5 while dfsq not empty 6 u = dfsq.back() 7 if u.color == WHITE then 8 u.color = GRAY 9 foreach vertex v in Adj(u) do 10 if v.color == WHITE 11 dfsq.push_back(v) 12 else if u.color == GRAY 13 u.color = BLACK 14 dfsq.pop_back() a b d c h e f g

DFSQ: a