{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L11_RecurInduction_print

L11_RecurInduction_print - COMP170 Discrete Mathematical...

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
2 Recursion, Recurrences and Induction Recursion Recurrences Iterating a Recurrence Geometric Series First-Order Linear Recurrences
Image of page 2
3 Recursion Recursive computer programs or algorithms often lead to inductive analyses A classic example of this is the Towers of Hanoi problem
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
5 Towers of Hanoi legal move legal move not legal legal move
Image of page 5

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

View Full Document Right Arrow Icon
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 .
Image of page 6
7 Towers of Hanoi Recursion Base: If n = 1 moving one disk from i to j is easy. Just move it. General Solution
Image of page 7

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

View Full Document Right Arrow Icon
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)
Image of page 8
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
Image of page 9

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

View Full Document Right Arrow Icon
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
Image of page 10
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
Image of page 11

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

View Full Document Right Arrow Icon
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 .
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern