comp 170 l2 l11: recursion, recurrence, and induction l objective n recursion a problem solving...

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

Upload: dina-bruce

Post on 06-Jan-2018

253 views

Category:

Documents


3 download

DESCRIPTION

COMP 170 L2 Page 3

TRANSCRIPT

Page 1: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 3

Page 4: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 4

Page 5: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 5

Page 6: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2

Recursive Solution of Towers of HanoiPage 6

Page 7: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2

Recursive Solution of Towers of HanoiPage 7

Page 8: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2

Correctness of AlgorithmPage 10

Page 11: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2

Running TimePage 12

Page 13: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2

Solving the RecurrencePage 13

Page 14: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 14

Page 15: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 15

Page 16: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 20

Page 21: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 23

Page 24: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2

One Type of First-Order Linear Recurrence

Examples

Page 24

Page 25: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2

Iterating the Recurrence/Top-DownPage 25

Page 26: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2

Iterating the Recurrence/Bottom-UpPage 26

Page 27: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 27

Page 28: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2

One Type of First-Order Linear RecurrencePage 28

Page 29: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 29

Page 30: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 30

Page 31: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2

ExamplePage 31

Page 32: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2

An Application of Theorem 4.1 Geometric Series

Page 32

Page 33: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

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: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 34

Another Type of First-Order Linear Recurrence

Page 35: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 35

Page 36: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 36

Page 37: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 37

Page 38: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 38

Page 39: COMP 170 L2 L11: Recursion, Recurrence, and Induction l Objective n Recursion  A problem solving technique that reduces big problems into smaller ones

COMP 170 L2 Page 39