This preview shows pages 1–13. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 COMP170 Discrete Mathematical Tools for Computer Science Discrete Math for Computer Science K. Bogart, C. Stein and R.L. Drysdale Section 4.2, pp. 143153 Recursion, Recurrences and Induction Version 2.0: Last updated, May 13, 2007 Slides c 2005 by M. J. Golin and G. Trippen 2 Recursion, Recurrences and Induction • Recursion • Recurrences • Iterating a Recurrence • Geometric Series • FirstOrder Linear Recurrences 3 Recursion • Recursive computer programs or algorithms often lead to inductive analyses • A classic example of this is the Towers of Hanoi problem 4 Towers of Hanoi • 3 pegs; n disks of different sizes. • A legal move takes a disk from one peg and moves it onto another peg so that it is not on top of a smaller disk • Problem: Find a (efficient) way to move all of the disks from one peg to another 5 Towers of Hanoi legal move legal move not legal legal move ⇒ ⇒ ⇒ ⇒ 6 Towers of Hanoi Start with n disks on leftmost peg using only legal moves Given i, j ∈ { 1 , 2 , 3 } let { i, j } = { 1 , 2 , 3 }  { i }  { j } ⇒ Problem move all disks to rightmost peg. i.e., { 1 , 2 } = 3 , { 1 , 3 } = 2 , { 2 , 3 } = 1 . 7 Towers of Hanoi Recursion Base: If n = 1 moving one disk from i to j is easy. Just move it. ⇒ General Solution 8 Towers of Hanoi To move n > 1 disks from i to j move top n 1 disks from i to { i, j } 1) move largest disk from i to j 2) move top n 1 disks from { i, j } to j . 3) 9 To move n disks from i to j i) move top n 1 disks from i to { i, j } ii) move largest disk from i to j iii) move top n 1 disks from { i, j } to j . • To prove Correctness of solution we are implicitly using induction • p ( n ) is statement that algorithm is correct for n • p (1) is statement that algorithm works for n = 1 disks, which is obviously true • p ( n 1) ⇒ p ( n ) is “recursion” statement that if our algorithm works for n 1 disks, then we can build a correct solution for n disks 10 To move n disks from i to j i) move top n 1 disks from i to { i, j } ii) move largest disk from i to j iii) move top n 1 disks from { i, j } to j . Running Time • M (1) = 1 M ( n ) is number of disk moves needed for n disks • If n > 1 , then M ( n ) = 2 M ( n 1) + 1 11 • M ( n ) = 2 M ( n 1) + 1 for n > 1 . • We saw that M (1) = 1 and that • Iterating the recurrence gives M (1) = 1 , M (2) = 3 , M (3) = 7 , M (4) = 15 , M (5) = 31 , . . . • We guess that M ( n ) = 2 n 1 . • We’ll prove this by induction • Later, we’ll see how to solve without guessing 12 Formally, given M ( n ) = 1 if n = 1 , 2 M ( n 1) + 1 otherwise . Proof: (by induction) , The base case n = 1 is true, since 2 1 1 = 1 ....
View
Full
Document
This note was uploaded on 08/25/2010 for the course COMP COMP170 taught by Professor M.j.golin during the Spring '10 term at HKUST.
 Spring '10
 M.J.Golin
 Computer Science, Recursion

Click to edit the document details