Trevor Brown trevor.brown@uwaterloo.ca DC 2338, Office hour M3-4pm CS 341: Algorithms Trevor Brown trevor.brown@uwaterloo.ca DC 2338, Office hour M3-4pm
This time BFS Complexity Proof of optimal distances Depth-first search (DFS)
BFS: Time and space complexity
Complexity? With adjacency list? With adjacency matrix?
BFS: Proof of optimal distances
Definitions: discovering and processing a node We use 𝒖<𝒗 to denote “𝒖 discovered before 𝒗” Observe: A node must be discovered before it can be processed! Discover (enqueue) starting node s Start processing node u Corollary: if 𝑢<𝑣 then 𝒖 is processed before 𝒗 (and vice versa) Discover (enqueue) node v Let’s use 𝒅 𝒖 as shorthand for 𝒅𝒊𝒔𝒕 𝒖 Finish processing node u
Proof of Optimal BFS distances: Claim 1 of 3 Lemma 1: if 𝒖<𝒗 then 𝒅 𝒖 ≤ 𝒅 𝒗 Proof by contradiction: assume ∃𝑢,𝑣 such that 𝒖<𝒗 but 𝒅 𝒖 > 𝒅 𝒗 Let 𝑣 be the earliest discovered node such that, for some 𝑢, we have 𝑢<𝑣 and 𝑑 𝑢 > 𝑑 𝑣 Consider predecessors 𝑢′ of 𝑢 and 𝑣′ of 𝑣 Note 𝑑 𝑢′ = 𝑑 𝑢 −1, and 𝑑 𝑢 > 𝑑 𝑣 , so 𝑑 𝑢′ ≥ 𝑑 𝑣 Also, 𝑑 𝑣′ = 𝑑 𝑣 −1, so 𝑑 𝑣′ < 𝑑 𝑢′ It turns out 𝑑 𝑣′ < 𝑑 𝑢′ implies 𝒗 ′ <𝒖′. Why? Since 𝒗 ′ < 𝒖 ′ , we know 𝑣 ′ is processed before 𝑢 ′ And, 𝑣 is discovered while processing 𝑣′, which is before 𝑢′ is processed, which is when 𝑢 is discovered. So 𝒗<𝒖! If not, then we have 𝒖 ′ < 𝒗 ′ . But 𝑣 ′ <𝑣, so neither 𝑣 ′ nor 𝑢′ can violate the lemma. So we have 𝑢 ′ < 𝑣 ′ ⇒ 𝒅 𝒖′ ≤ 𝒅 𝒗′ Which is a contradiction!
Proof of Optimal BFS distances: Claim 2 of 3 Lemma 2: if there is an edge {𝒖,𝒗}, then 𝒅 𝒖 − 𝒅 𝒗 ≤𝟏 Proof by cases: WLOG suppose 𝑢<𝑣 Case 1: 𝑣 is white when we process 𝑢 Then 𝑑 𝑣 = 𝑑 𝑢 +1. QED
Proof of Optimal BFS distances: Claim 2 of 3 Lemma 2: if there is an edge {𝒖,𝒗}, then 𝒅 𝒖 − 𝒅 𝒗 ≤𝟏 Proof by cases: WLOG suppose 𝑢<𝑣 Case 2: 𝑣 is grey when we process 𝑢 Since 𝑣 is not white, we did not discover it from 𝑢 We discovered 𝑣 earlier when processing some 𝑣 ′ ≠𝑢 Since 𝑣′ was processed before 𝑢, we have 𝑣 ′ <𝑢 So, by Lemma 1 we have 𝑑 𝑣 ′ ≤ 𝑑 𝑢 . Also note 𝑑 𝑣 = 𝑑 𝑣 ′ +1. Rearrange to get 𝑑 𝑣 ′ = 𝑑 𝑣 −1. Substituting 𝑑 𝑣 ′ into 𝑑 𝑣 ′ ≤ 𝑑 𝑢 we get 𝒅 𝒖 ≥ 𝒅 𝒗 −𝟏 Also, since 𝑢<𝑣, Lemma 1 implies 𝒅 𝒖 ≤ 𝒅 𝒗 So, 𝒅 𝒗 −𝟏≤ 𝒅 𝒖 ≤ 𝒅 𝒗 . QED
Proof of Optimal BFS distances: Claim 2 of 3 Lemma 2: if there is an edge {𝒖,𝒗}, then 𝒅 𝒖 − 𝒅 𝒗 ≤𝟏 Proof by cases: WLOG suppose 𝑢<𝑣 Case 3: 𝑣 is black when we process 𝑢 Then 𝑣 is finished processing before 𝑢 Therefore 𝑣<𝑢 This contradicts our assumption that 𝑢<𝑣. QED
Proof of optimal distances: Claim 3 of 3 Theorem: 𝒅 𝒗 is the length of the shortest path from 𝒔 to 𝒗 Let 𝛿 𝑣 denote the length of the shortest path from 𝑠 to 𝑣 The path 𝑣→𝜋 𝑣 →𝜋 𝜋 𝑣 →…→𝑠 has distance 𝑑 𝑣 , so 𝜹 𝒗 ≤ 𝒅 𝒗 . We show 𝜹 𝒗 ≥ 𝒅 𝒗 by induction on the value of distance 𝛿 𝑣 Base case: suppose 𝛿 𝑣 =0. Then 𝑣=𝑠 and 𝑑 𝑣 =0, so 𝛿 𝑣 ≥ 𝑑 𝑣 . Inductive hypothesis: “if 𝛿 𝑣 =𝑘−1 then 𝛿 𝑣 ≥ 𝑑 𝑣 .” We prove: “if 𝛿 𝑣 =𝑘 then 𝛿 𝑣 ≥ 𝑑 𝑣 .” (So, suppose 𝑑 𝑣 =𝑘) Then ∃ a shortest path 𝑠→ 𝑣 1 →…→ 𝑣 𝑘−1 → 𝑣 𝑘 =𝑣 with length 𝛿 𝑣 =𝑘 By Lemma 2, we have 𝑑 𝑣 𝑘 ≤ 𝑑 𝑣 𝑘−1 +1, so 𝑑 𝑣 ≤ 𝑑 𝑣 𝑘−1 +1 By the inductive hypothesis, we have 𝛿 𝑣 𝑘−1 ≥ 𝑑 𝑣 𝑘−1 so 𝑑 𝑣 𝑘−1 = 𝛿 𝑣 𝑘−1 =𝑘−1 So 𝑑 𝑣 ≤ 𝑑 𝑣 𝑘−1 +1 becomes 𝑑 𝑣 ≤ 𝑘−1 +1=𝑘. Rearranging to 𝑘≥ 𝑑 𝑣 and substituting 𝛿 𝑣 =𝑘 we get 𝜹 𝒗 ≥ 𝒅 𝒗 . QED
Outputting an optimal path
How to represent a grid graph? Game AI: path finding in a grid-graph BFS from here 5 4 3 4 3 2 5 1
Game AI: path finding with waypoints Divide game world into linear paths, then send game characters in straight lines between waypoints Use BFS to find shortest sequence of waypoints (with fewest waypoints)
shortest path to a mouse cursor around obstacles User interfaces: shortest path to a mouse cursor around obstacles
How to Output an actual path Suppose you want to output a path from 𝑠 to 𝑣 with minimum distance (not just the distance to 𝑣) Algorithm (what do you think?) Similar to extracting an answer from a DP array! Work backwards through the predecessors Start at 𝑣 (set 𝑐𝑢𝑟:=𝑣) Inside a loop, visit the predecessor node (by printing 𝑐𝑢𝑟 and then setting 𝑐𝑢𝑟≔𝜋 𝑐𝑢𝑟 ) Stop when you hit 𝑠 (when 𝑐𝑢𝑟=𝑠) Note: path is printed in reverse! Solution?
Preview: doing this quickly, on a large scale
Recall the Pac-man example Shortest path to here? Recall the Pac-man example 5 5 4 3 Destination 𝑣 BFS from here 4 4 3 3 2 2 Predecessor 𝜋 𝑣 Each time you visit a predecessor, push it into a stack 5 1 Predecessor 𝜋 𝜋 𝑣 1 I.e., push 𝑣=5, then push 𝜋 𝑣 =4, then push 𝜋 𝜋 𝑣 =3, then 2, … At the end, pop all off the stack. This gives 0, 1, 2, …, 5 = the path!
Depth First Search (DFS)
?
Preview: Visualizing DFS https://www.cs.usfca.edu/~galles/visualization/DFS.html
Copying to blackboard, then back to the code
For your notes…