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 BFS Complexity Proof of optimal distances
Depth-first search (DFS)

3 BFS: Time and space complexity

4 Complexity? With adjacency list? With adjacency matrix?

5 BFS: Proof of optimal distances

6 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

7 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!

8 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

9 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

10 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

11 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

12 Outputting an optimal path

13 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

14 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)

15 shortest path to a mouse cursor around obstacles
User interfaces: shortest path to a mouse cursor around obstacles

16 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?

17 Preview: doing this quickly, on a large scale

18 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!

19 Depth First Search (DFS)

20 ?

21

22 Preview: Visualizing DFS

23 Copying to blackboard, then back to the code

24 For your notes…


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

Similar presentations


Ads by Google