L11: Recursion, Recurrence, and Induction

39
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

description

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

Transcript of L11: Recursion, Recurrence, and Induction

Page 1: L11: Recursion, Recurrence, and Induction

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 2: L11: Recursion, Recurrence, and Induction

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

Page 3: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 3

Page 4: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 4

Page 5: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 5

Page 6: L11: Recursion, Recurrence, and Induction

COMP 170 L2

Recursive Solution of Towers of HanoiPage 6

Page 7: L11: Recursion, Recurrence, and Induction

COMP 170 L2

Recursive Solution of Towers of HanoiPage 7

Page 8: L11: Recursion, Recurrence, and Induction

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

Page 9: L11: Recursion, Recurrence, and Induction

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

Page 10: L11: Recursion, Recurrence, and Induction

COMP 170 L2

Correctness of AlgorithmPage 10

Page 11: L11: Recursion, Recurrence, and Induction

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

Page 12: L11: Recursion, Recurrence, and Induction

COMP 170 L2

Running TimePage 12

Page 13: L11: Recursion, Recurrence, and Induction

COMP 170 L2

Solving the RecurrencePage 13

Page 14: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 14

Page 15: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 15

Page 16: L11: Recursion, Recurrence, and Induction

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

Page 17: L11: Recursion, Recurrence, and Induction

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

Page 18: L11: Recursion, Recurrence, and Induction

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

Page 19: L11: Recursion, Recurrence, and Induction

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.

So: S(n) = 2 S(n-1)

Base case:

S(0) =1

Page 19

Page 20: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 20

Page 21: L11: Recursion, Recurrence, and Induction

COMP 170 L2

Monthly Payment for Loan

Initial loan amount: A

Annual interest rate: p

Monthly Payment: M

T(n): total amount still due after n months

Page 21

Page 22: L11: Recursion, Recurrence, and Induction

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 22

Page 23: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 23

Page 24: L11: Recursion, Recurrence, and Induction

COMP 170 L2

One Type of First-Order Linear Recurrence

Examples

Page 24

Page 25: L11: Recursion, Recurrence, and Induction

COMP 170 L2

Iterating the Recurrence/Top-DownPage 25

Page 26: L11: Recursion, Recurrence, and Induction

COMP 170 L2

Iterating the Recurrence/Bottom-UpPage 26

Page 27: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 27

Page 28: L11: Recursion, Recurrence, and Induction

COMP 170 L2

One Type of First-Order Linear RecurrencePage 28

Page 29: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 29

Page 30: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 30

Page 31: L11: Recursion, Recurrence, and Induction

COMP 170 L2

ExamplePage 31

Page 32: L11: Recursion, Recurrence, and Induction

COMP 170 L2

An Application of Theorem 4.1 Geometric Series

Page 32

Page 33: L11: Recursion, Recurrence, and Induction

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 recurrence

Another type of First-Order linear recurrence

Page 33

Page 34: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 34

Another Type of First-Order Linear Recurrence

Page 35: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 35

Page 36: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 36

Page 37: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 37

Page 38: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 38

Page 39: L11: Recursion, Recurrence, and Induction

COMP 170 L2Page 39