Download presentation
Presentation is loading. Please wait.
Published byDina Bruce Modified over 9 years ago
1
COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion A problem solving technique that reduces big problems into smaller ones n Induction Proving correctness of recursive programs n Recurrence Running time for recursive programs Page 1
2
COMP 170 L2 Outline l Recursive algorithm for Towers of Hanoi n Problem and algorithm n Correctness n Running time l Recurrence in general n Example: Number of subsets n Example: Loan repayment l Solving recurrences n One type of First-Order linear recurrences n Another type of First-Order linear recurrences Page 2
3
COMP 170 L2 Page 3
4
COMP 170 L2 Page 4
5
COMP 170 L2 Page 5
6
COMP 170 L2 Recursive Solution of Towers of Hanoi Page 6
7
COMP 170 L2 Recursive Solution of Towers of Hanoi Page 7
8
COMP 170 L2 Recursive Solution of Towers of Hanoi l Task: n Move n disks from peg i to peg j l Algorithm n Recursion base: When n=1, move one disk from i to j n Recursion Page 8
9
COMP 170 L2 Outline l Recursive algorithm for Towers of Hanoi n Problem and algorithm n Correctness n Running time l Recurrence in general n Example: Number of subsets n Example: Loan repayment l Solving recurrences n Geometric serious n First-Order linear recurrences Page 9
10
COMP 170 L2 Correctness of Algorithm Page 10
11
COMP 170 L2 Outline l Recursive algorithm for Towers of Hanoi n Problem and algorithm n Correctness n Running time l Recurrence in general n Example: Number of subsets n Example: Loan repayment l Solving recurrences n One type of First-Order linear recurrences n Another type of First-Order linear recurrences Page 11
12
COMP 170 L2 Running Time Page 12
13
COMP 170 L2 Solving the Recurrence Page 13
14
COMP 170 L2 Page 14
15
COMP 170 L2 Page 15
16
COMP 170 L2 Outline l Recursive algorithm for Towers of Hanoi n Problem and algorithm n Correctness n Running time l Recurrence in general n Example: Number of subsets n Example: Loan repayment l Solving recurrences n One type of First-Order linear recurrences n Another type of First-Order linear recurrences Page 16
17
COMP 170 L2 Recurrence l Recurrence/recurrence equation is a way to specify functions on the set of integers l It tells us how to n get the n-th value f(n) n from the first n-b values: f(b), f(b+1), …, f(n-1) l Need to give the value for the base case f(b) to complete the description Page 17
18
COMP 170 L2 Number of subsets l S(n): number of subsets of set {1, 2, 3, …., n} of size n l Question: n How to compute S(n) from S(n-1), S(n-2), …? l Consider the case: n=3 n First row: subsets of {1, 2} n Second: subsets of {1, 2}, each adjoined by 3 n So: S(3) = 2 S(2) Page 18
19
COMP 170 L2 Number of Subsets l In general, subsets of set {1, 2, 3, …., n} can be divided into two groups n subsets of set {1, 2, 3, …., n-1} n subsets of set {1, 2, 3, …., n-1}, each adjoined by n. n So: S(n) = 2 S(n-1) l Base case: n S(0) =1 Page 19
20
COMP 170 L2 Page 20
21
COMP 170 L2 Monthly Payment for Loan l Initial loan amount: A l Annual interest rate: p l Monthly Payment: M l T(n): total amount still due after n months Page 21
22
COMP 170 L2 Outline l Recursive algorithm for Towers of Hanoi n Problem and algorithm n Correctness n Running time l Recurrence in general n Example: Number of subsets n Example: Loan repayment l Solving recurrences n One type of First-Order linear recurrences n Another type of First-Order linear recurrences Page 22
23
COMP 170 L2 Page 23
24
COMP 170 L2 One Type of First-Order Linear Recurrence Examples Page 24
25
COMP 170 L2 Iterating the Recurrence/Top-Down Page 25
26
COMP 170 L2 Iterating the Recurrence/Bottom-Up Page 26
27
COMP 170 L2 Page 27
28
COMP 170 L2 One Type of First-Order Linear Recurrence Page 28
29
COMP 170 L2 Page 29
30
COMP 170 L2 Page 30
31
COMP 170 L2 Example Page 31
32
COMP 170 L2 An Application of Theorem 4.1 Geometric Series Page 32
33
COMP 170 L2 Outline l Recursive algorithm for Towers of Hanoi n Problem and algorithm n Correctness n Running time l Recurrence in general n Example: Number of subsets n Example: Loan repayment l Solving recurrences n One type of First-Order linear recurrence n Another type of First-Order linear recurrence Page 33
34
COMP 170 L2 Page 34 Another Type of First-Order Linear Recurrence
35
COMP 170 L2 Page 35
36
COMP 170 L2 Page 36
37
COMP 170 L2 Page 37
38
COMP 170 L2 Page 38
39
COMP 170 L2 Page 39
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.