Recursion Part II 11/19/2013 Tower of Hanoi The - - PowerPoint PPT Presentation
Recursion Part II 11/19/2013 Tower of Hanoi The - - PowerPoint PPT Presentation
Recursion Part II 11/19/2013 Tower of Hanoi The problem defini/on: There are three towers Ini:ally, there are N disks on tower i, where the
Tower ¡of ¡Hanoi ¡
The ¡problem ¡defini/on: ¡
- There ¡are ¡three ¡towers ¡
- Ini:ally, ¡there ¡are ¡N ¡disks ¡on ¡tower ¡i, ¡where ¡the ¡smallest ¡
is ¡on ¡the ¡top ¡
- N ¡disks ¡need ¡to ¡move ¡from ¡tower ¡i ¡to ¡tower ¡j ¡
- The ¡third ¡tower ¡can ¡be ¡used ¡to ¡temporarily ¡hold ¡disks ¡
What ¡is ¡a ¡legal ¡move: ¡
- You ¡can ¡move ¡only ¡a ¡disk ¡at ¡the ¡top ¡of ¡a ¡tower ¡
- You ¡can ¡move ¡only ¡one ¡disk ¡at ¡a ¡:me ¡
- You ¡cannot ¡place ¡a ¡disk ¡on ¡top ¡of ¡a ¡smaller ¡one ¡
Solu:on ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Solu:on ¡
Step ¡1: ¡move ¡disks ¡0, ¡..., ¡N-‑2 ¡from ¡tower ¡0 ¡to ¡tower ¡2 ¡ ¡
¡ ¡ ¡ ¡ ¡=> ¡Sub ¡problem ¡of ¡smaller ¡size ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Solu:on ¡
Step ¡2: ¡move ¡disk ¡N-‑1 ¡from ¡tower ¡0 ¡to ¡tower ¡1 ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Solu:on ¡
Step ¡3: ¡move ¡disks ¡0, ¡..., ¡N-‑2 ¡from ¡tower ¡2 ¡to ¡tower ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡=> ¡Sub ¡problem ¡of ¡smaller ¡size ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Tower ¡of ¡Hanoi ¡
How ¡to ¡resolve ¡such ¡a ¡problem ¡
Recursion ¡
Algorithm ¡
Algorithm ¡towerOfHanoi ¡(n, ¡i, ¡j) ¡ ¡ ¡ ¡ ¡ ¡ Input: ¡Disks ¡numbered ¡0, ¡..., ¡n ¡are ¡to ¡be ¡moved ¡from ¡tower ¡i ¡to ¡tower ¡j ¡ ¡ ¡ ¡ ¡ ¡ ¡1. ¡ ¡ ¡ ¡// ¡... ¡base ¡case ¡... ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ // ¡First ¡find ¡the ¡third ¡tower, ¡other ¡than ¡i ¡and ¡j: ¡ ¡ ¡ ¡ ¡ ¡
- 2. k ¡= ¡otherTower ¡(i, ¡j) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
¡ // ¡Step ¡1: ¡move ¡disks ¡0,..,n-‑1 ¡from ¡i ¡to ¡k ¡ ¡ ¡ ¡ ¡ ¡
- 3. towerOfHanoi ¡(n-‑1, ¡i, ¡k) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
// ¡Step ¡2: ¡move ¡disk# ¡n ¡from ¡i ¡to ¡j ¡ ¡ ¡ ¡ ¡ ¡
- 4. move ¡(n, ¡i, ¡j) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
¡ // ¡Step ¡3: ¡move ¡disks ¡0,...,n-‑1 ¡from ¡k ¡to ¡j ¡ ¡ ¡ ¡ ¡ ¡5. ¡ ¡ ¡ ¡towerOfHanoi ¡(n-‑1, ¡k, ¡j) ¡
Algorithm ¡
Algorithm ¡towerOfHanoi ¡(n, ¡i, ¡j) ¡ ¡ ¡ ¡ ¡ ¡ Input: ¡Disks ¡numbered ¡0, ¡..., ¡n ¡are ¡to ¡be ¡moved ¡from ¡tower ¡i ¡to ¡tower ¡j ¡ ¡ // ¡... ¡base ¡case ¡... ¡ ¡ ¡ 1. if ¡there ¡is ¡one ¡disk ¡ ¡ ¡move ¡the ¡disk ¡to ¡tower ¡j ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ // ¡First ¡find ¡the ¡third ¡tower, ¡other ¡than ¡i ¡and ¡j: ¡ ¡ ¡ ¡ ¡ ¡
- 2. k ¡= ¡otherTower ¡(i, ¡j) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
¡ // ¡Step ¡1: ¡move ¡disks ¡0,..,n-‑1 ¡from ¡i ¡to ¡k ¡ ¡ ¡ ¡ ¡ ¡
- 3. towerOfHanoi ¡(n-‑1, ¡i, ¡k) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
// ¡Step ¡2: ¡move ¡disk# ¡n ¡from ¡i ¡to ¡j ¡ ¡ ¡ ¡ ¡ ¡
- 4. move ¡(n, ¡i, ¡j) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
¡ // ¡Step ¡3: ¡move ¡disks ¡0,...,n-‑1 ¡from ¡k ¡to ¡j ¡ ¡ ¡ ¡ ¡ ¡5. ¡ ¡ ¡ ¡towerOfHanoi ¡(n-‑1, ¡k, ¡j) ¡
Tower ¡of ¡Hanoi ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Tower ¡of ¡Hanoi ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Tower ¡of ¡Hanoi ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Tower ¡of ¡Hanoi ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Tower ¡of ¡Hanoi ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Tower ¡of ¡Hanoi ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Tower ¡of ¡Hanoi ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Tower ¡of ¡Hanoi ¡
Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-‑05/fall/cs270/lectures/lec1.html ¡
Tower ¡of ¡Hanoi ¡
¡ ¡ ¡ Code… ¡
Tower ¡of ¡Hanoi ¡
¡ What ¡if ¡we ¡want ¡to ¡maintain ¡the ¡state ¡of ¡ each ¡tower? ¡ ¡ Which ¡data ¡structure ¡would ¡be ¡ideal ¡to ¡use? ¡
STACKS ¡
Tower ¡of ¡Hanoi ¡
¡ Stack ¡implementa/on ¡ ¡ Code… ¡
- The ¡number ¡of ¡moves ¡M(n) ¡required ¡by ¡the ¡
algorithm ¡to ¡solve ¡the ¡n-‑disk ¡problem ¡
M(n) ¡= ¡2M(n-‑1) ¡+ ¡1 ¡ M(1) ¡= ¡1 ¡
Tower ¡of ¡Hanoi ¡
Cost ¡
- Calculate ¡M(n) ¡for ¡
small ¡n ¡and ¡look ¡for ¡ a ¡paJern. ¡ ¡ ¡
n M(n) 1 1 2 3 3 7 4 15 5 31
Tower ¡of ¡Hanoi ¡
Cost ¡
Tower ¡of ¡Hanoi ¡
Cost ¡
- Unwind ¡recurrence, ¡by ¡repeatedly ¡replacing ¡
M(n) ¡by ¡the ¡r.h.s. ¡of ¡the ¡recurrence ¡un:l ¡the ¡ base ¡case ¡is ¡encountered. ¡ M(n) ¡= ¡2M(n-‑1) ¡+ ¡1 ¡ ¡ ¡ ¡ ¡ ¡= ¡2*[2*M(n-‑2)+1] ¡+ ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡22 ¡* ¡M(n-‑2) ¡+ ¡1+2 ¡ ¡ ¡ ¡ ¡ ¡= ¡22 ¡* ¡[2*M(n-‑3)+1] ¡ ¡+ ¡1 ¡+ ¡2 ¡ ¡ ¡ ¡ ¡ ¡= ¡23 ¡* ¡M(n-‑3) ¡+ ¡1+2 ¡+ ¡22 ¡
Tower ¡of ¡Hanoi ¡
Cost ¡ M(n) ¡= ¡2M(n-‑1) ¡+ ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡2*[2*M(n-‑2)+1] ¡+ ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡22 ¡* ¡M(n-‑2) ¡+ ¡1+2 ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡22 ¡* ¡[2*M(n-‑3)+1] ¡ ¡+ ¡1 ¡+ ¡2 ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡23 ¡* ¡M(n-‑3) ¡+ ¡1+2 ¡+ ¡22 ¡
¡ ¡ ¡
¡ ¡ ¡ ¡ ¡= ¡2n-‑1 ¡* ¡M(1) ¡+ ¡1+2 ¡+ ¡22 ¡+ ¡… ¡+ ¡2n-‑2 ¡ ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡1 ¡+ ¡2 ¡+ ¡22 ¡+ ¡… ¡+ ¡2n-‑2 ¡+ ¡2n-‑1 ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡2n-‑1 ¡-‑ ¡1 ¡
¡
n M(n) 1 1 = 21 ¡-‑1 2 3 = 22 ¡-‑1 3 7 = 23 ¡-‑1 4 15 = 24 ¡-‑1 5 31 = 25 ¡-‑1
Tower ¡of ¡Hanoi ¡
Cost ¡
Types ¡of ¡Recursions ¡
Tail ¡recursion: ¡
- Recursion ¡where ¡you ¡don't ¡un-‑do ¡changes ¡
- Easily ¡can ¡be ¡wriJen ¡non-‑recursively ¡(using ¡itera:on) ¡
– For ¡many ¡of ¡these ¡examples ¡(power, ¡factorial, ¡fibonacci), ¡ it's ¡beJer ¡to ¡use ¡itera:on ¡
Backtracking: ¡
- Recursion ¡where ¡you ¡need ¡to ¡un-‑do ¡changes ¡so ¡that ¡
you ¡can ¡properly ¡explore ¡all ¡possibili:es ¡
- very ¡difficult ¡to ¡avoid ¡recursion ¡
Recursion: ¡review ¡
- We ¡must ¡test ¡for ¡the ¡boJom ¡out ¡case ¡before ¡
recursing ¡
- The ¡bo7om-‑out ¡case ¡tests ¡the ¡value ¡(or ¡values) ¡of ¡the ¡
parameter ¡(or ¡parameters) ¡that ¡changes ¡in ¡the ¡ recursion ¡ ¡ ¡ ¡ ¡ ¡=> ¡These ¡are ¡the ¡parameters ¡that ¡control ¡the ¡ recursion ¡
- The ¡recursive ¡calls ¡must ¡change ¡(usually ¡decrease) ¡
the ¡parameters ¡that ¡control ¡the ¡recursion ¡ ¡ ¡ ¡ ¡ ¡ ¡=> ¡Above, ¡there ¡is ¡only ¡one ¡recursive ¡call, ¡but ¡ Tower ¡of ¡Hanoi ¡has ¡two ¡
Recursion: ¡review ¡
What ¡is ¡the ¡parameter ¡that ¡controls ¡recursion? ¡
¡// ¡Compute ¡ab ¡ ¡ ¡ ¡ ¡ sta:c ¡int ¡power ¡(int ¡a, ¡int ¡b) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡BoJom-‑out: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(b ¡== ¡0) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Recursion: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡(a ¡* ¡power ¡(a, ¡b-‑1)); ¡ ¡ ¡ ¡ ¡} ¡
Recursion: ¡review ¡
What ¡is ¡the ¡parameter ¡that ¡controls ¡recursion? ¡
¡// ¡Compute ¡ab ¡ ¡ ¡ ¡ ¡ sta:c ¡int ¡power ¡(int ¡a, ¡int ¡b) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡BoJom-‑out: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(b ¡== ¡0) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Recursion: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡(a ¡* ¡power ¡(a, ¡b-‑1)); ¡ ¡ ¡ ¡ ¡} ¡
Tail ¡ Recursion ¡
Recursion: ¡review ¡
What ¡is ¡the ¡parameter ¡that ¡controls ¡recursion? ¡
sta:c ¡void ¡printPermuta:ons ¡(int ¡numSpaces, ¡int ¡numRemaining, ¡int[] ¡seats, ¡int ¡person) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡BoJom-‑out ¡case. ¡ ¡ ¡if ¡(numRemaining ¡== ¡0) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Print. ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println ¡( ¡Arrays.toString(seats) ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡return; ¡ ¡} ¡ ¡// ¡Otherwise, ¡non-‑base ¡case: ¡look ¡for ¡an ¡empty ¡spot ¡for ¡"person” ¡ ¡for ¡(int ¡i=0; ¡i ¡< ¡seats.length; ¡i++) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(seats[i] ¡== ¡0) ¡{ ¡ ¡ ¡// ¡Empty ¡spot.seats[i] ¡= ¡person; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Recursively ¡assign ¡remaining, ¡star:ng ¡with ¡person+1 ¡ ¡ ¡printPermuta:ons ¡(numSpaces-‑1, ¡numRemaining-‑1, ¡seats, ¡person+1); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Important: ¡we ¡need ¡to ¡un-‑do ¡the ¡sea:ng ¡for ¡other ¡ ¡ ¡ ¡trials.seats[i] ¡= ¡0; ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡} ¡//end-‑for ¡ ¡ ¡ ¡ ¡} ¡
Recursion: ¡review ¡
What ¡is ¡the ¡parameter ¡that ¡controls ¡recursion? ¡
sta:c ¡void ¡printPermuta:ons ¡(int ¡numSpaces, ¡int ¡numRemaining, ¡int[] ¡seats, ¡int ¡person) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡BoJom-‑out ¡case. ¡ ¡ ¡if ¡(numRemaining ¡== ¡0) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Print. ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println ¡( ¡Arrays.toString(seats) ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡return; ¡ ¡} ¡ ¡// ¡Otherwise, ¡non-‑base ¡case: ¡look ¡for ¡an ¡empty ¡spot ¡for ¡"person” ¡ ¡for ¡(int ¡i=0; ¡i ¡< ¡seats.length; ¡i++) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(seats[i] ¡== ¡0) ¡{ ¡ ¡ ¡// ¡Empty ¡spot.seats[i] ¡= ¡person; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Recursively ¡assign ¡remaining, ¡star:ng ¡with ¡person+1 ¡ ¡ ¡printPermuta:ons ¡(numSpaces-‑1, ¡numRemaining-‑1, ¡seats, ¡person+1); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Important: ¡we ¡need ¡to ¡un-‑do ¡the ¡sea:ng ¡for ¡other ¡ ¡ ¡ ¡trials.seats[i] ¡= ¡0; ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡} ¡//end-‑for ¡ ¡ ¡ ¡ ¡} ¡ backtracking ¡
Maze ¡Construc:on ¡and ¡Traversal ¡
- Maze ¡Construc:on: ¡
§ Use ¡recursion ¡to ¡ construct ¡the ¡maze ¡ ¡ cell-‑numbering ¡: ¡
- Rows ¡start ¡at ¡0, ¡and ¡
increase ¡downwards ¡
- Columns ¡start ¡at ¡0 ¡
and ¡increase ¡ rightwards ¡
Maze ¡Construc:on ¡and ¡Traversal ¡
- Maze ¡Traversal: ¡
– Use ¡recursion ¡to ¡ solve ¡the ¡maze ¡ ¡ ¡(find ¡a ¡path ¡from ¡a ¡ given ¡start ¡cell ¡to ¡a ¡ given ¡end ¡cell) ¡
Maze ¡Construc:on ¡cont’d ¡
The ¡Coord ¡class: ¡
public ¡class ¡Coord ¡{ ¡ ¡ ¡ ¡ ¡ ¡public ¡int ¡row=-‑1, ¡col=-‑1; ¡ ¡ ¡ ¡ ¡ ¡ ¡public ¡Coord ¡(int ¡r, ¡int ¡c) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡row ¡= ¡r; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡col ¡= ¡c; ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡public ¡String ¡toString ¡() ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡"[" ¡+ ¡row ¡+ ¡"," ¡+ ¡col ¡+ ¡"]"; ¡ ¡ ¡ ¡ ¡ ¡} ¡ } ¡
Maze ¡Construc:on ¡cont’d ¡
The ¡Coord ¡class: ¡
public ¡class ¡Coord ¡{ ¡ ¡ ¡ ¡ ¡ ¡public ¡int ¡row=-‑1, ¡col=-‑1; ¡ ¡ ¡ ¡ ¡ ¡ ¡public ¡Coord ¡(int ¡r, ¡int ¡c) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡row ¡= ¡r; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡col ¡= ¡c; ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡public ¡String ¡toString ¡() ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡"[" ¡+ ¡row ¡+ ¡"," ¡+ ¡col ¡+ ¡"]"; ¡ ¡ ¡ ¡ ¡ ¡} ¡ } ¡
How ¡to ¡create ¡and ¡access ¡ stored ¡coordinates? ¡
Coord ¡c ¡= ¡new ¡Coord ¡(3,4); ¡ c.row ¡ c.col ¡
Maze ¡Construc:on ¡
Maze ¡class ¡methods: ¡
– The ¡constructor: ¡we ¡create ¡an ¡instance ¡by ¡specifying ¡the ¡ number ¡of ¡rows ¡and ¡columns: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Maze ¡maze ¡= ¡new ¡Maze ¡(5, ¡5); ¡ ¡ – display(): ¡call ¡this ¡method ¡to ¡display ¡the ¡maze: ¡ ¡ ¡ ¡ ¡ ¡maze.display ¡(); ¡ ¡ ¡ – breakWall():Ini:ally, ¡the ¡maze ¡consists ¡of ¡complete ¡cells ¡
- To ¡actually ¡create ¡a ¡walkable ¡maze, ¡we ¡will ¡"break ¡walls" ¡between ¡
neighboring ¡cells ¡
- For ¡example, ¡we ¡can ¡break ¡the ¡wall ¡between ¡cells ¡(3,4) ¡and ¡(2,4) ¡as ¡
follows: ¡ ¡
¡ ¡ ¡maze.breakWall ¡(new ¡Coord(3,4), ¡ ¡new ¡Coord ¡(2,4)); ¡
Maze ¡Construc:on ¡cont’d ¡
- markVisited ¡(Coord ¡c): ¡mark ¡cell ¡c ¡as ¡visited ¡
- markUnVisited ¡(Coord ¡c): ¡mark ¡cell ¡c ¡as ¡not ¡visited ¡
- markAllUnvisited(): ¡mark ¡all ¡cells ¡as ¡unvisited ¡
- isVisited ¡(Coord ¡c): ¡see ¡whether ¡cell ¡c ¡has ¡been ¡visited ¡
- copy(): ¡make ¡a ¡full ¡copy ¡of ¡the ¡maze ¡
¡ ¡ ¡Maze ¡m ¡= ¡new ¡Maze ¡(5,5); ¡ ¡ ¡ ¡ ¡Maze ¡m2 ¡= ¡m.copy(); ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Now ¡m2 ¡is ¡a ¡copy ¡of ¡m ¡ ¡
- setSolu/onPath ¡(LinkedList<Coord> ¡solu/onPath): ¡we ¡will ¡call ¡this ¡
method ¡once ¡we ¡have ¡constructed ¡a ¡solu:on ¡
Maze ¡Construc:on ¡cont’d ¡
- Coord[] ¡getUnvisitedClosedNeighbors ¡(Coord ¡c): ¡ ¡
– get ¡cell ¡c's ¡neighbors ¡that ¡are ¡closed ¡off ¡from ¡c ¡(there's ¡a ¡wall ¡ between) ¡and ¡haven't ¡been ¡visited ¡yet ¡
- Coord[] ¡getClosedNeighbors ¡(Coord ¡c): ¡ ¡
– get ¡neighbors ¡of ¡c ¡that ¡share ¡a ¡wall ¡(unbroken) ¡with ¡c ¡whether ¡ visited ¡or ¡not ¡
- Coord[] ¡getUnvisitedOpenNeighbors ¡(Coord ¡c): ¡ ¡
– get ¡those ¡neighbors ¡of ¡c ¡for ¡which ¡we ¡can ¡walk ¡through ¡to ¡the ¡ neighbor ¡(no ¡wall) ¡and ¡which ¡haven't ¡been ¡visited ¡
Maze ¡Construc:on ¡cont’d ¡
- We ¡will ¡try ¡to ¡generate ¡a ¡maze ¡path ¡of ¡a ¡given ¡path ¡
length ¡ ¡Algorithm: ¡ ¡ recursiveGenerate ¡(Coord ¡c) ¡ ¡ ¡ ¡ ¡ ¡
- 1. ¡ ¡ ¡if ¡path-‑length ¡has ¡been ¡reached ¡ ¡ ¡ ¡ ¡ ¡
- 2. ¡ ¡ ¡ ¡ ¡return ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Otherwise ¡... ¡ ¡ ¡ ¡ ¡ ¡
- 3. ¡ ¡ ¡c' ¡= ¡Pick ¡a ¡random ¡neighbor ¡of ¡cell ¡c ¡ ¡ ¡ ¡ ¡ ¡
- 4. ¡ ¡ ¡recursiveGenerate ¡(c') ¡
Maze ¡Construc:on ¡cont’d ¡
Code… ¡
Maze ¡Construc:on ¡cont’d ¡
Problems ¡with ¡previous ¡code ¡and ¡solu/ons: ¡
– Simple ¡tail ¡recursion ¡can ¡get ¡stuck ¡ ¡ ¡ ¡ ¡ ¡=> ¡We ¡need ¡a ¡way ¡to ¡"un-‑do" ¡paths ¡and ¡try ¡
different ¡neighbors ¡
¡ – We ¡will ¡pick ¡a ¡neighbor ¡to ¡explore ¡recursively ¡ ¡ ¡ ¡ ¡ ¡ ¡=> ¡If ¡that ¡doesn't ¡work ¡out, ¡we'll ¡try ¡another ¡
Maze ¡Construc:on ¡cont’d ¡
Code… ¡
Maze ¡Construc:on ¡cont’d ¡
Problems ¡with ¡previous ¡code ¡and ¡solu/ons: ¡
– The ¡above ¡maze ¡path ¡has ¡no ¡choices ¡ ¡ ¡ ¡ ¡ ¡ ¡=> ¡There's ¡only ¡one ¡way ¡from ¡the ¡top ¡les ¡to ¡the ¡
end ¡of ¡the ¡path ¡
¡ – ¡We ¡will ¡build ¡upon ¡this ¡to ¡create ¡a ¡maze ¡with ¡ choices ¡ ¡ ¡ ¡ ¡ ¡ ¡=> ¡Aser ¡genera:ng ¡the ¡long ¡path, ¡we'll ¡break ¡
some ¡random ¡walls ¡
Maze ¡Construc:on ¡cont’d ¡
Code… ¡
Solving ¡the ¡Maze ¡
Algorithm: ¡ ¡ recursivelyFindPath ¡(Coord ¡c) ¡ ¡ ¡ ¡ ¡ ¡
- 1. if ¡c ¡is ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡
- 2. return ¡true ¡ ¡ ¡ ¡ ¡ ¡
- 3. endif ¡ ¡ ¡ ¡ ¡ ¡
// ¡Otherwise ¡search ¡further ¡... ¡ ¡ ¡ ¡ ¡
- 4. c' ¡= ¡pick ¡a ¡random ¡"open" ¡neighbor ¡ ¡ ¡ ¡ ¡ ¡ ¡
- 5. if ¡c' ¡is ¡null ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
// ¡Stuck: ¡couldn't ¡find ¡a ¡path. ¡ ¡ ¡ ¡ ¡
- 6. return ¡false ¡ ¡ ¡ ¡ ¡ ¡
- 7. endif ¡ ¡ ¡ ¡ ¡ ¡
- 8. return ¡recursivelyFindPath ¡(c') ¡
Solving ¡the ¡Maze ¡cont’d ¡
Code… ¡
Solving ¡the ¡Maze ¡cont’d ¡
Problems ¡with ¡previous ¡code ¡and ¡solu/ons: ¡
– When ¡exploring ¡a ¡neighbor, ¡we ¡need ¡a ¡way ¡to ¡un-‑do ¡(backtrack) ¡if ¡it ¡ doesn't ¡lead ¡to ¡a ¡solu:on ¡ – The ¡idea ¡is ¡to ¡try ¡all ¡possible ¡neighbors, ¡as ¡many ¡as ¡needed: ¡ ¡ ¡ ¡ ¡
recursivelyFindPath ¡(Coord ¡c) ¡ ¡ ¡ ¡ ¡ ¡
- 1. ¡ ¡ ¡if ¡c ¡is ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡
- 2. ¡ ¡ ¡ ¡ ¡return ¡true ¡ ¡ ¡ ¡ ¡ ¡
- 3. ¡ ¡ ¡endif ¡ ¡ ¡ ¡ ¡
- 4. ¡ ¡ ¡// ¡Otherwise ¡search ¡further ¡... ¡ ¡ ¡ ¡ ¡ ¡
- 5. ¡ ¡ ¡for ¡each ¡"open" ¡neighbor ¡c' ¡ ¡ ¡ ¡ ¡ ¡
- 6. ¡ ¡ ¡ ¡ ¡ ¡found ¡= ¡recursivelyFindPath ¡(c') ¡ ¡ ¡ ¡ ¡ ¡
- 7. ¡ ¡ ¡ ¡ ¡ ¡if ¡found ¡ ¡ ¡ ¡ ¡ ¡
- 8. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡add ¡c' ¡to ¡path ¡ ¡ ¡ ¡ ¡ ¡
- 9. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true ¡ ¡ ¡ ¡ ¡ ¡
- 10. ¡ ¡ ¡ ¡ ¡endif ¡ ¡ ¡ ¡ ¡ ¡
- 11. ¡ ¡endfor ¡ ¡ ¡ ¡ ¡ ¡
- 12. ¡ ¡return ¡false ¡
Solving ¡the ¡Maze ¡cont’d ¡
Code… ¡
N-‑Queens ¡problem ¡
- We ¡are ¡given ¡an ¡N ¡x ¡N ¡
chessboard ¡and ¡M ¡ queens ¡(where ¡M ¡<= ¡N) ¡
N-‑Queens ¡problem ¡
- The ¡goal ¡is ¡to ¡place ¡the ¡
queens ¡on ¡the ¡board ¡so ¡ that ¡no ¡queen ¡"aJacks" ¡ another) ¡
- A ¡queen ¡can ¡aJack ¡any ¡
square ¡in ¡its ¡row, ¡ column ¡or ¡diagonal ¡
N-‑Queens ¡problem ¡
- Another ¡example ¡of ¡recursion ¡with ¡
backtracking ¡
- Key ¡ideas: ¡
– We'll ¡proceed ¡column-‑by-‑column ¡(les ¡to ¡right) ¡ – When ¡solving ¡for ¡the ¡current ¡column, ¡try ¡to ¡place ¡ a ¡queen ¡on ¡each ¡possible ¡row, ¡and ¡solve ¡the ¡sub-‑ problem ¡(star:ng ¡with ¡the ¡next ¡column) ¡ recursively ¡
N-‑Queens ¡problem ¡cont’d ¡
Algorithm: ¡solve ¡(n, ¡c) ¡ ¡Input: ¡n ¡= ¡the ¡number ¡of ¡queens ¡to ¡assign, ¡c ¡= ¡the ¡start ¡column ¡
- 1. if ¡there ¡are ¡no ¡more ¡queens ¡to ¡assign ¡
- 2. return ¡true ¡
- 3. if ¡there ¡are ¡no ¡more ¡columns ¡to ¡try ¡ ¡
- 4. return ¡false ¡
- 5. for ¡each ¡row ¡r ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
- 6. if ¡[r,c] ¡is ¡a ¡non-‑aJacked ¡square ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
- 7. place ¡queen ¡n ¡on ¡cell ¡[r,c] ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
- 8. found ¡= ¡solve ¡(n-‑1, ¡c+1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
- 9. if ¡(found) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
- 10. return ¡true ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
- 11. endif ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
- 12. endif ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
- 13. endfor ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
// ¡We ¡reach ¡here ¡if ¡none ¡of ¡the ¡rows ¡in ¡column ¡c ¡worked ¡out. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
- 15. ¡return ¡false ¡
N-‑Queens ¡problem ¡cont’d ¡
- addQueen ¡(row,col): ¡add ¡a ¡queen ¡in ¡square ¡[row,col] ¡
public ¡void ¡addQueen ¡(int ¡row, ¡int ¡col) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡columns[row] ¡= ¡col; ¡ ¡ ¡ ¡ ¡ } ¡
- removeQueen ¡(row,col): ¡remove ¡the ¡queen ¡in ¡
square ¡[row,col] ¡
¡public ¡void ¡removeQueen ¡(int ¡row, ¡int ¡col) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡
¡ ¡ ¡columns[row] ¡= ¡-‑1; ¡ ¡ ¡ ¡ ¡ ¡} ¡
- display(): ¡to ¡bring ¡up ¡a ¡GUI ¡with ¡the ¡board ¡displayed ¡
N-‑Queens ¡problem ¡cont’d ¡
boolean ¡isForbidden ¡(row,col): ¡see ¡if ¡[row,col] ¡is ¡an ¡aJacked ¡square ¡ public ¡boolean ¡isForbidden ¡(int ¡row, ¡int ¡col) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡First, ¡try ¡the ¡row. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(columns[row] ¡>= ¡0) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Now ¡try ¡the ¡columns. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡c=0; ¡c ¡< ¡size; ¡c++) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(columns[row] ¡== ¡col) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Now ¡the ¡diagonals. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for ¡(int ¡r=0; ¡r ¡< ¡size; ¡r++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(columns[r] ¡>= ¡0) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(Math.abs(columns[r]-‑col) ¡== ¡Math.abs(row-‑r)) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡true; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡false; ¡ ¡ ¡ ¡