COMP 170 L2 L11: Recursion, Recurrence, and Induction Objective Recursion A problem solving technique that reduces big problems into smaller ones Induction Proving correctness of recursive programs Recurrence Running time for recursive programs Page 1
Page 1. L11: Recursion, Recurrence, and Induction. Objective Recursion A problem solving technique that reduces big problems into smaller ones Induction Proving correctness of recursive programs Recurrence Running time for recursive programs. Page 2. Outline. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
COMP 170 L2
L11: Recursion, Recurrence, and Induction
Objective
Recursion A problem solving technique that reduces big problems into smaller ones
Induction Proving correctness of recursive programs
Recurrence Running time for recursive programs
Page 1
COMP 170 L2
Outline
Recursive algorithm for Towers of Hanoi
Problem and algorithm
Correctness
Running time
Recurrence in general
Example: Number of subsets
Example: Loan repayment
Solving recurrences
One type of First-Order linear recurrences
Another type of First-Order linear recurrences
Page 2
COMP 170 L2Page 3
COMP 170 L2Page 4
COMP 170 L2Page 5
COMP 170 L2
Recursive Solution of Towers of HanoiPage 6
COMP 170 L2
Recursive Solution of Towers of HanoiPage 7
COMP 170 L2
Recursive Solution of Towers of Hanoi
Task:
Move n disks from peg i to peg j
Algorithm
Recursion base: When n=1, move one disk from i to j
Recursion
Page 8
COMP 170 L2
Outline
Recursive algorithm for Towers of Hanoi
Problem and algorithm
Correctness
Running time
Recurrence in general
Example: Number of subsets
Example: Loan repayment
Solving recurrences
Geometric serious
First-Order linear recurrences
Page 9
COMP 170 L2
Correctness of AlgorithmPage 10
COMP 170 L2
Outline
Recursive algorithm for Towers of Hanoi
Problem and algorithm
Correctness
Running time
Recurrence in general
Example: Number of subsets
Example: Loan repayment
Solving recurrences
One type of First-Order linear recurrences
Another type of First-Order linear recurrences
Page 11
COMP 170 L2
Running TimePage 12
COMP 170 L2
Solving the RecurrencePage 13
COMP 170 L2Page 14
COMP 170 L2Page 15
COMP 170 L2
Outline
Recursive algorithm for Towers of Hanoi
Problem and algorithm
Correctness
Running time
Recurrence in general
Example: Number of subsets
Example: Loan repayment
Solving recurrences
One type of First-Order linear recurrences
Another type of First-Order linear recurrences
Page 16
COMP 170 L2
Recurrence
Recurrence/recurrence equation is a way to specify functions on the
set of integers
It tells us how to
get the n-th value f(n)
from the first n-b values: f(b), f(b+1), …, f(n-1)
Need to give the value for the base case f(b) to complete the
description
Page 17
COMP 170 L2
Number of subsets
S(n): number of subsets of set {1, 2, 3, …., n} of size n
Question:
How to compute S(n) from S(n-1), S(n-2), …?
Consider the case: n=3
First row: subsets of {1, 2}
Second: subsets of {1, 2}, each adjoined by 3
So: S(3) = 2 S(2)
Page 18
COMP 170 L2
Number of Subsets
In general, subsets of set {1, 2, 3, …., n} can be divided into two
groups
subsets of set {1, 2, 3, …., n-1}
subsets of set {1, 2, 3, …., n-1}, each adjoined by n.