recursion
play

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


  1. Recursion ¡ Part ¡II ¡ 11/19/2013 ¡

  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: ¡ o You ¡can ¡move ¡only ¡a ¡disk ¡at ¡the ¡top ¡of ¡a ¡tower ¡ o You ¡can ¡move ¡only ¡one ¡disk ¡at ¡a ¡:me ¡ o You ¡cannot ¡place ¡a ¡disk ¡on ¡top ¡of ¡a ¡smaller ¡one ¡

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

  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 ¡

  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 ¡

  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 ¡

  7. Tower ¡of ¡Hanoi ¡ How ¡to ¡resolve ¡such ¡a ¡problem ¡ Recursion ¡

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

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

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

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

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

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

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

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

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

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

  18. Tower ¡of ¡Hanoi ¡ ¡ ¡ ¡ Code… ¡

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

  20. Tower ¡of ¡Hanoi ¡ ¡ Stack ¡implementa/on ¡ ¡ Code… ¡

  21. Tower ¡of ¡Hanoi ¡ Cost ¡ • 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 ¡

  22. Tower ¡of ¡Hanoi ¡ Cost ¡ • Calculate ¡M(n) ¡for ¡ n M(n) small ¡n ¡and ¡look ¡for ¡ 1 1 a ¡paJern. ¡ ¡ ¡ 2 3 3 7 4 15 5 31

  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 ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡2 2 ¡ * ¡ M(n-­‑2) ¡+ ¡1+2 ¡ ¡ ¡ ¡ ¡ ¡= ¡2 2 ¡ * ¡[2*M(n-­‑3)+1] ¡ ¡+ ¡1 ¡+ ¡2 ¡ ¡ ¡ ¡ ¡ ¡= ¡2 3 ¡ * ¡ M(n-­‑3) ¡+ ¡1+2 ¡+ ¡2 2 ¡

  24. Tower ¡of ¡Hanoi ¡ Cost ¡ M(n) ¡= ¡2M(n-­‑1) ¡+ ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡2*[2*M(n-­‑2)+1] ¡+ ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡2 2 ¡ * ¡ M(n-­‑2) ¡+ ¡1+2 ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡2 2 ¡ * ¡[2*M(n-­‑3)+1] ¡ ¡+ ¡1 ¡+ ¡2 ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡2 3 ¡ * ¡ M(n-­‑3) ¡+ ¡1+2 ¡+ ¡2 2 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡2 n-­‑1 ¡ * ¡ M(1) ¡+ ¡1+2 ¡+ ¡2 2 ¡ + ¡… ¡+ ¡2 n-­‑2 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡1 ¡+ ¡2 ¡+ ¡2 2 ¡ + ¡… ¡+ ¡2 n-­‑2 ¡ + ¡2 n-­‑1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡= ¡2 n-­‑1 ¡ -­‑ ¡1 ¡ ¡

  25. Tower ¡of ¡Hanoi ¡ Cost ¡ n M(n) 1 = 2 1 ¡-­‑1 1 3 = 2 2 ¡-­‑1 2 7 = 2 3 ¡-­‑1 3 15 = 2 4 ¡-­‑1 4 31 = 2 5 ¡-­‑1 5

  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 ¡

  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 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend