Recursion Part II 11/19/2013 Tower of Hanoi The - - PowerPoint PPT Presentation

recursion
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Recursion ¡

Part ¡II ¡ 11/19/2013 ¡

slide-2
SLIDE 2

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 ¡
slide-3
SLIDE 3

Solu:on ¡

Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-­‑05/fall/cs270/lectures/lec1.html ¡

slide-4
SLIDE 4

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 ¡

slide-5
SLIDE 5

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 ¡

slide-6
SLIDE 6

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 ¡

slide-7
SLIDE 7

Tower ¡of ¡Hanoi ¡

How ¡to ¡resolve ¡such ¡a ¡problem ¡

Recursion ¡

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Tower ¡of ¡Hanoi ¡

Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-­‑05/fall/cs270/lectures/lec1.html ¡

slide-11
SLIDE 11

Tower ¡of ¡Hanoi ¡

Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-­‑05/fall/cs270/lectures/lec1.html ¡

slide-12
SLIDE 12

Tower ¡of ¡Hanoi ¡

Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-­‑05/fall/cs270/lectures/lec1.html ¡

slide-13
SLIDE 13

Tower ¡of ¡Hanoi ¡

Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-­‑05/fall/cs270/lectures/lec1.html ¡

slide-14
SLIDE 14

Tower ¡of ¡Hanoi ¡

Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-­‑05/fall/cs270/lectures/lec1.html ¡

slide-15
SLIDE 15

Tower ¡of ¡Hanoi ¡

Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-­‑05/fall/cs270/lectures/lec1.html ¡

slide-16
SLIDE 16

Tower ¡of ¡Hanoi ¡

Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-­‑05/fall/cs270/lectures/lec1.html ¡

slide-17
SLIDE 17

Tower ¡of ¡Hanoi ¡

Credit ¡to: ¡hJps://www.cs.drexel.edu/~jjohnson/2004-­‑05/fall/cs270/lectures/lec1.html ¡

slide-18
SLIDE 18

Tower ¡of ¡Hanoi ¡

¡ ¡ ¡ Code… ¡

slide-19
SLIDE 19

Tower ¡of ¡Hanoi ¡

¡ What ¡if ¡we ¡want ¡to ¡maintain ¡the ¡state ¡of ¡ each ¡tower? ¡ ¡ Which ¡data ¡structure ¡would ¡be ¡ideal ¡to ¡use? ¡

STACKS ¡

slide-20
SLIDE 20

Tower ¡of ¡Hanoi ¡

¡ Stack ¡implementa/on ¡ ¡ Code… ¡

slide-21
SLIDE 21
  • 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 ¡

slide-22
SLIDE 22
  • 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 ¡

slide-23
SLIDE 23

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 ¡

slide-24
SLIDE 24

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 ¡

¡

slide-25
SLIDE 25

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 ¡

slide-26
SLIDE 26

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 ¡
slide-27
SLIDE 27

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 ¡

slide-28
SLIDE 28

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)); ¡ ¡ ¡ ¡ ¡} ¡

slide-29
SLIDE 29

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 ¡

slide-30
SLIDE 30

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 ¡ ¡ ¡ ¡ ¡} ¡

slide-31
SLIDE 31

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 ¡

slide-32
SLIDE 32

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 ¡

slide-33
SLIDE 33

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

slide-34
SLIDE 34

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 ¡+ ¡"]"; ¡ ¡ ¡ ¡ ¡ ¡} ¡ } ¡

slide-35
SLIDE 35

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 ¡

slide-36
SLIDE 36

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)); ¡

slide-37
SLIDE 37

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 ¡

slide-38
SLIDE 38

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 ¡

slide-39
SLIDE 39

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') ¡
slide-40
SLIDE 40

Maze ¡Construc:on ¡cont’d ¡

Code… ¡

slide-41
SLIDE 41

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 ¡

slide-42
SLIDE 42

Maze ¡Construc:on ¡cont’d ¡

Code… ¡

slide-43
SLIDE 43

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 ¡

slide-44
SLIDE 44

Maze ¡Construc:on ¡cont’d ¡

Code… ¡

slide-45
SLIDE 45

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') ¡
slide-46
SLIDE 46

Solving ¡the ¡Maze ¡cont’d ¡

Code… ¡

slide-47
SLIDE 47

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 ¡
slide-48
SLIDE 48

Solving ¡the ¡Maze ¡cont’d ¡

Code… ¡

slide-49
SLIDE 49

N-­‑Queens ¡problem ¡

  • We ¡are ¡given ¡an ¡N ¡x ¡N ¡

chessboard ¡and ¡M ¡ queens ¡(where ¡M ¡<= ¡N) ¡

slide-50
SLIDE 50

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 ¡

slide-51
SLIDE 51

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 ¡

slide-52
SLIDE 52

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 ¡
slide-53
SLIDE 53

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 ¡
slide-54
SLIDE 54

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; ¡ ¡ ¡ ¡

slide-55
SLIDE 55

N-­‑Queens ¡problem ¡cont’d ¡

Code… ¡