Download presentation
Presentation is loading. Please wait.
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
?
22
Preview: Visualizing DFS
23
Copying to blackboard, then back to the code
24
For your notesβ¦
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.