{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L11_RecurInduction_print

# L11_RecurInduction_print - COMP170 Discrete Mathematical...

This preview shows pages 1–13. Sign up to view the full content.

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. 143-153 Recursion, Recurrences and Induction Version 2.0: Last updated, May 13, 2007 Slides c 2005 by M. J. Golin and G. Trippen

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Recursion, Recurrences and Induction Recursion Recurrences Iterating a Recurrence Geometric Series First-Order 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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 . For the inductive step, assume that M ( n - 1) = 2 n - 1 - 1 for n > 1 . M ( n ) = 2 M ( n - 1) + 1 = 2(2 n - 1 - 1) + 1 = 2 n - 1 .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern