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 (vertices) where no dependencies are violated • Many possible valid topological EE 354L EE 338 EE 348 orderings exist – EE 109L, EE 209, EE 354L, EE 454L, EE 457, MATH 245, EE 457 EE 454L EE 447 EE 448 PHYS 152, EE 202L,… – MATH 245, EE 109L, PHYS 152L, EE 154,…
3 Topological Sort • Another example Socks Underwear Undershirt – Getting dressed • More Examples: Pants Shirt – Project management scheduling Tie Shoes Belt – Build order in a Makefile or other compile project Jacket – Cooking using a recipe http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorith – Instruction execution on an ms/GraphAlgor/topoSort.htm out-of-order pipelined CPU – Production of output values in a simulation of a combinational gate network
4 Topological Sort • Does breadth-first search EE 109L MA 245 PHYS 152 work? – No. What if we started at EE200… – We'd go to EE 202L before PHYS EE 209 EE 202L 152 • All parent nodes need to be completed before any child EE 354L EE 338 EE 348 node • BFS only guarantees some parent has completed before EE 457 EE 454L EE 447 EE 448 child • Turns out a Depth-First Search will be part of our solution
5 Depth First Search • Explores ALL children 11 22 23 24 1 10 EE 109L MA 245 PHYS 152 before completing a parent 9 12 21 2 – Note BFS completes a parent EE 209 EE 202L before ANY children • For DFS let us assign: 13 14 3 8 15 20 – A start time when the node is first EE 354L EE 338 EE 348 found – A finish time when a node is 4 5 6 7 16 18 19 17 completed EE 457 EE 454L EE 447 EE 448 • If we look at our nodes in reverse order of finish time (i.e. 1 Start Time last one to finish back to first Finish Time 2 one to finish) we arrive at a… Reverse Finish Time Order – Topological ordering!!! PHYS 152, MATH 245, EE 202L, EE 348, EE 448, EE 447, EE 338, EE 109L, EE 209, EE 354L, EE 454L, EE 457
6 DFS & Topological Sort • Imagine we flipped our 6 7 1 EE 109L MA 245 PHYS 152 dependencies (edge direction) and performed a 2 8 DFS starting at nodes with EE 209 EE 202L no incoming edges 12 3 9 • Now as nodes EE 354L EE 338 EE 348 “finish/complete” we 5 th DFS could enter them in a list 4 5 11 10 and that list would provide EE 457 EE 454L EE 447 EE 448 a valid topological ordering 1 st DFS 2 nd DFS 3 rd DFS 4 th DFS
7 DFS Algorithm DFS (G) 1 for each vertex u • Visit a node 2 u.color = WHITE – Mark as visited 3 u.pred = nil 4 time = 0 – For each visited neighbor, visit it 5 finish_list = empty_list and perform DFS on all of their 6 for each vertex u do children 7 if u.color == WHITE then – Only then, mark as finished 8 DFS-Visit (G, u, finish_list) • DFS is recursive!! DFS-Visit (G, u) • If cycles in the graph, ensure we 1 u.color = GRAY 2 u.start = ++time don’t get caught visiting 3 for each vertex v in Adj(u) do neighbors endlessly 5 if v.color = WHITE then – Color them as we go 6 v.pred = u 7 DFS-Visit (G, v) – White = unvisited, 8 u.color = BLACK – Gray = visited but not finished 9 finish_list.append(u) – Black = finished
8 Simplified DFS for Topo-sort Toposort(G) • For topological sort, all we care 1 for each vertex u about is finish time 2 u.color = WHITE 3 finish_list = empty_list • If in line 4 of toposort() we 4 for each vertex u do simply iterate through 5 if u.color == WHITE then goal/output nodes as our start 6 DFS-Visit (G, u, finish_list) locations then the ordering in DFS-Visit (G, u) the finish_list will be a correct 1 u.color = GRAY toposort with input nodes 2 for each vertex v in Adj(u) do 3 if v.color = WHITE then first… 4 DFS-Visit (G, v) • This is a recursive solution 5 u.color = BLACK 6 finish_list.append(u) – We can have an iterative solution shown later…
9 Depth First-Search Toposort(G) g b 1 for each vertex u d 2 u.color = WHITE f 3 finish_list = empty_list a h 4 for each vertex u do e c 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)
10 Depth First-Search Toposort(G) g b 1 for each vertex u d 2 u.color = WHITE f v u 3 finish_list = empty_list a h 4 for each vertex u do e c 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,a):
11 Depth First-Search Toposort(G) g b 1 for each vertex u d 2 u.color = WHITE f v u 3 finish_list = empty_list a h 4 for each vertex u do e c 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,d): DFS-Visit(G,a):
12 Depth First-Search Toposort(G) g b 1 for each vertex u d 2 u.color = WHITE f v u 3 finish_list = empty_list a h 4 for each vertex u do e c 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,f): DFS-Visit(G,d): DFS-Visit(G,a):
13 Depth First-Search Toposort(G) g b 1 for each vertex u d 2 u.color = WHITE f u 3 finish_list = empty_list a h 4 for each vertex u do e c 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,h): DFS-Visit(G,f): DFS-Visit(G,d): DFS-Visit(G,a):
14 Depth First-Search Toposort(G) g b 1 for each vertex u d 2 u.color = WHITE f u 3 finish_list = empty_list a h 4 for each vertex u do e c 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) Finish_list: 1 u.color = GRAY h 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,h): DFS-Visit(G,f): DFS-Visit(G,d): DFS-Visit(G,a):
15 Depth First-Search v Toposort(G) g b 1 for each vertex u d 2 u.color = WHITE f u 3 finish_list = empty_list a h 4 for each vertex u do e c 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) Finish_list: 1 u.color = GRAY h 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,f): DFS-Visit(G,d): DFS-Visit(G,a):
16 Depth First-Search u Toposort(G) g b 1 for each vertex u d 2 u.color = WHITE f 3 finish_list = empty_list a h 4 for each vertex u do e c 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) Finish_list: 1 u.color = GRAY h 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,g): DFS-Visit(G,f): DFS-Visit(G,d): DFS-Visit(G,a):
17 Depth First-Search u Toposort(G) g b 1 for each vertex u d 2 u.color = WHITE f 3 finish_list = empty_list a h 4 for each vertex u do e c 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) Finish_list: 1 u.color = GRAY h, 2 for each vertex v in Adj(u) do g 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) DFS-Visit(G,g): DFS-Visit(G,f): DFS-Visit(G,d): DFS-Visit(G,a):
18 Depth First-Search Toposort(G) g b 1 for each vertex u u d 2 u.color = WHITE f 3 finish_list = empty_list a h 4 for each vertex u do e c 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) Finish_list: 1 u.color = GRAY h, 2 for each vertex v in Adj(u) do g, f 3 if v.color = WHITE then 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) DFS-Visit(G,f): DFS-Visit(G,d): DFS-Visit(G,a):
19 Depth First-Search Toposort(G) g b 1 for each vertex u d 2 u.color = WHITE f u 3 finish_list = empty_list a h 4 for each vertex u do e c 5 if u.color == WHITE then 6 DFS-Visit (G, u, finish_list) DFS-Visit (G, u) Finish_list: 1 u.color = GRAY h, 2 for each vertex v in Adj(u) do g, f, 3 if v.color = WHITE then d 4 DFS-Visit (G, v) 5 u.color = BLACK 6 finish_list.append(u) DFS-Visit(G,d): DFS-Visit(G,a):
Recommend
More recommend