Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trevor Brown DC 2338, Office hour M3-4pm

Similar presentations


Presentation on theme: "Trevor Brown DC 2338, Office hour M3-4pm"β€” Presentation transcript:

1 Trevor Brown trevor.brown@uwaterloo.ca DC 2338, Office hour M3-4pm
CS 341: Algorithms Trevor Brown DC 2338, Office hour M3-4pm

2 This time Depth-first search (DFS) Complexity DFS forest edge types
Application: cycle detection via DFS Application: topological sort via DFS (also without DFS)

3 Depth First Search (DFS)

4 Basic DFS Properties to recall
Also gets a discovery time 𝑑[𝑣] at this point Nodes start white A node 𝑣 turns gray when it is discovered, which is when the first call to 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑣) happens After 𝑣 is turned gray, we recurse on its neighbours After recursing on all neighbours, we turn 𝑣 black Recursive calls on neighbours end first, so neighbours turn black first Also gets a finish time 𝑓[𝑣] at this point

5 Complexity with adjacency lists is 𝑂(𝑛+π‘š)
Home exercise: complexity with adjacency matrix?

6 Classifying edges in the DFS forest
Recall the example:

7 What are the edge types in the example graph?
Can we classify edges without inspecting the DFS forest? Perhaps using 𝑑[…], 𝑓 … and/or π‘π‘œπ‘™π‘œπ‘’π‘Ÿ[…]?

8 Definitions Definition: we use 𝑰 𝒖 to denote (𝑑[𝑒],𝑓[𝑒]), which we call the interval of 𝒖 Definition: 𝒗 is white-reachable from 𝒖 if there is a path from 𝑒 to 𝑣 containing only white nodes 2 3 12 7 𝒖 4 9 11 13 6 1 8 10 5

9 Exploring d[], f[] and colour[]
Observe: every node 𝑣 that is white-reachable from 𝑒 when we call 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒) (discover 𝑒) becomes gray after 𝒖 and black before 𝒖 (so 𝐼 𝑣 is nested inside 𝐼 𝑒 ) Note: 𝑣 is discovered during the recursive 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑 calls, which is equivalent to 𝒗 being a descendent of 𝒖 in the DFS forest Start 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒) and colour 𝑒 grey Perform 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑 calls recursively… 𝒖 𝒖 𝒖 Colour 𝑒 black and return from 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒)

10 more generally Theorem: Let 𝒖,𝒗 be any nodes. The following statements are all equivalent. (𝑣 is discovered during 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒)) (𝑣 is white-reachable from 𝑒 when we call 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒)) (𝑣 is a descendant of 𝒖 in the DFS forest) (𝑣 turns grey after 𝑒 and black before 𝑒) ( 𝐼 𝑣 nested inside 𝐼 𝑒 )

11 𝑣 is not a descendent, and not an ancestor
Classification of edge types in DFS DFS inspects every edge in the graph. When DFS inspects an edge {𝒖,𝒗}, the colour of 𝑣 and relationship between the intervals of 𝑒 and 𝑣 determine the edge type. 𝑣 discovered during π‘«π‘­π‘Ίπ‘½π’Šπ’”π’Šπ’•(𝒖) but not at the first level of recursion (else {𝑒,𝑣} is a tree edge). 𝒖 𝒗 Q1? Q2? 𝑣 is a child of 𝑒 Q4? Q3? 𝑣 is a descendent of 𝑒 Q6? Q5? 𝑣 is an ancestor of 𝑒 Q8? Q7? 𝑣 is not a descendent, and not an ancestor Break up this slide? Recall: (𝑣 is discovered during 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒)) ⇔ (𝑣 is white-reachable from 𝑒 when we call 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒)) ⇔ (𝑣 is a descendant of 𝒖 in the DFS forest) ⇔ (𝑣 turns grey after 𝑒 and black before 𝑒) ⇔ ( 𝐼 𝑣 nested inside 𝐼 𝑒 ) So the recursive call in 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒) that discovered 𝑣 must finish (and set 𝑣 black) before 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒) inspects {𝑒,𝑣}

12 useful fact: Parenthesis theorem
Theorem: for each for each pair of nodes 𝑒,𝑣 the intervals of 𝑒 and 𝑣 are either disjoint or nested Proof: Suppose the intervals are not disjoint. Then either 𝑑 𝑣 ∈ 𝐼 𝑒 or 𝑑[𝑒]∈ 𝐼 𝑣 WLOG suppose 𝑑 𝑣 ∈ 𝐼 𝑒 Then 𝑣 is discovered during 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒), so 𝑣 is white-reachable from 𝑒 when 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒) starts So, 𝑣 must turn black before 𝑒, so 𝑓 𝑣 <𝑓[𝑒] So the intervals are nested. QED π‘«π‘­π‘Ίπ‘½π’Šπ’”π’Šπ’•(𝒖) 𝑑[𝑒] 𝑓[𝑒] 𝑑[𝑣] 𝑓[𝑣] ∈ 𝑓[𝑣] 𝑑[𝑒] 𝑓[𝑒] 𝑑[𝑣] 𝑓[𝑣] ∈

13 Classification of edge types in DFS
So, 𝐼 𝑣 must be earlier. DFS inspects every edge in the graph. When DFS inspects an edge {𝒖,𝒗}, the colour of 𝑣 and relationship between the intervals of 𝑒 and 𝑣 determine the edge type. If 𝐼 𝑒 were earlier, then 𝑣 would be discovered before 𝒖 finishes (because of edge {𝑒,𝑣}), so intervals would not be disjoint! 𝒖 𝒗 Intervals 𝑰 𝒖 and 𝑰 𝒗 must be disjoint. But which is earlier? Q8? Q7? 𝑣 is not a descendent, and not an ancestor Break up this slide? Recall: (𝑣 is discovered during 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒)) ⇔ (𝑣 is white-reachable from 𝑒 when we call 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑(𝑒)) ⇔ (𝑣 is a descendant of 𝒖 in the DFS forest) ⇔ ( 𝐼 𝑣 nested inside 𝐼 𝑒 )

14 DFS Application: Testing whether a graph is a DAG

15 Back edge: points to an ancestor in the DFS forest

16 … Case ⇐ : Suppose βˆƒ directed cycle. Show βˆƒ back edge.
Let 𝑣 1 , 𝑣 2 ,…, 𝑣 π‘˜ , 𝑣 1 be a directed cycle WLOG let 𝑣 1 be earliest discovered node in the cycle 𝒖 𝒗 𝒗 π’Œ = = 𝒗 𝟏 𝑑[ 𝑣 π‘˜ ] 𝑑[ 𝑣 1 ] 𝑓[ 𝑣 1 ] 𝑓[ 𝑣 π‘˜ ] 𝑑[ 𝑣 π‘˜ ] 𝑑[ 𝑣 1 ] 𝑓[ 𝑣 1 ] 𝑓[ 𝑣 π‘˜ ] 𝑑[ 𝑣 1 ] 𝑑[ 𝑣 π‘˜ ] 𝑓[ 𝑣 π‘˜ ] 𝑓[ 𝑣 1 ] 𝑑[ 𝑣 1 ] 𝑓[ 𝑣 1 ] 𝑑[ 𝑣 π‘˜ ] 𝑓[ 𝑣 π‘˜ ] Discovered before 𝑣 2 ,…, 𝑣 π‘˜ 𝑣 2 𝑣 2 𝑣 3 𝑣 3 𝑣 1 𝑣 1 Recall: every node 𝑣 𝑖 that is white-reachable from 𝑣 1 when we discover 𝑣 1 (call 𝐷𝐹𝑆𝑉𝑖𝑠𝑖𝑑( 𝑣 1 )) turns black before 𝒗 𝟏 (𝑓 𝑣 𝑖 <𝑓[ 𝑣 1 ]) 𝑣 4 𝑣 4 Consider edge { 𝒗 π’Œ , 𝒗 𝟏 } Recall: nodes become gray when discovered Since 𝑑 𝑣 1 <𝑑 𝑣 π‘˜ , { 𝒗 π’Œ , 𝒗 𝟏 } must be a back or cross edge. Why? 𝑣 π‘˜ 𝑣 π‘˜ So 𝑣 π‘˜ must turn black before 𝑣 1 , and we have 𝑓 𝑣 π‘˜ <𝑓[ 𝑣 1 ]. 𝑣 5 𝑣 5 … 𝑣 6 𝑣 6 So when 𝒗 𝟏 is discovered, 𝑣 2 ,…, 𝑣 π‘˜ are all white Thus, { 𝒗 π’Œ , 𝒗 𝟏 } must be a back edge. QED

17 Turning the lemma into an algorithm
Search for back edges How to identify a back-edge? Back edge When we observe an edge from 𝑒 to 𝑣, check if 𝒗 is gray 𝒖 𝒗 𝒗

18 DFS: Testing whether a graph is a DAG

19

20 Home exercise Try running the algorithm on this graph:

21 Topological sort Finding node orderings that satisfy given constraints

22 Example problem: getting dressed in the morning
Dependency graph Edge {𝑒,𝑣} means 𝑒 must be completed before 𝑣 Example problem: getting dressed in the morning Watch any time Pants before belt Socks before shoes Could do various things first. Which ones are possible? What do they have in common?

23 Topological sort IFF there is a (directed) cycle!
Might not be possible! How can this happen? Try to order nodes linearly so there are only pointers from left to right! Topological sort

24 more realistic Use cases (1/2)
Compiler design: ordering program compilation steps Not just compilers… Linkers, build systems (makefiles, etc.) Can do parse.y, then parse.h, then parse.c, then main.c, then parse.o, then main.o, then prog… Cell evaluation-order in spreadsheets… Can verify pointers only go left-to-right. Other orders are possible… Manufacturing processes, chemical reaction processes, … all sorts of planning and scheduling

25 More realistic use cases (2/2): task scheduling
Goal: output a serial order of tasks that can be run without worrying about dependencies (because all dependencies are already satisfied by the time a task is run). (Nodes are numbered according to one such order.) Can even schedule tasks to run in parallel! Can do 1||2 then 3||13 then 4| 5 |15 etc. Similar to instruction scheduling and pipelining in modern processors!

26 Formal definitions

27 One of these must be repeated. So there is a cycle!
Useful fact One of these must be repeated. So there is a cycle! … 𝒗 𝒏+𝟏 𝒗 𝒏 𝒗 πŸ‘ 𝒗 𝟐 𝒗 𝟏

28 Existence of topological sort
Not a very efficient algorithm We can build a more efficient algorithm by using DFS

29 Recall this fact from the DAG-testing application
13 To see why we get a topological ordering, suppose D is a DAG and we order nodes from largest to smallest finish time, so 𝒇 𝒗 𝟏 > 𝒇 𝒗 𝟐 >…> 𝒇 𝒗 π’βˆ’πŸ > 𝒇 𝒗 𝒏 So edges are left-to-right, hence this is a topological order! … … … 𝒗 𝟏 𝒗 𝟐 𝒗 π’Š 𝒗 𝒋 𝒗 𝒏 But this contradicts the node ordering! Suppose a right-to-left edge { 𝑣 𝑗 , 𝑣 𝑖 } exists Since edge { 𝑣 𝑗 , 𝑣 𝑖 } exists, the lemma implies 𝑓 𝑣 𝑖 < 𝑓 𝑣 𝑗

30 Topological ordering via DFS

31 𝑂(𝑛+π‘š) with adjacency lists
Running time? 𝑂(𝑛+π‘š) with adjacency lists We are pushing smallest finishing times first into a stack, so when we pop them out, we will get largest finishing time first Save each node when it finishes Save each node when it finishes

32 Example On the board…

33 A different algorithm for topological sort
Running time is 𝑂(𝑛+π‘š) with adjacency lists

34 Home exercise (Kahn’s algorithm)
Run on this graph…


Download ppt "Trevor Brown DC 2338, Office hour M3-4pm"

Similar presentations


Ads by Google