L11: Recursion, Recurrence, and Induction

Post on 13-Jan-2016

51 Views

Category:

Documents

5 Downloads

Preview:

Click to see full reader

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

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.

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

Base case:

S(0) =1

Page 19

COMP 170 L2Page 20

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

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

COMP 170 L2Page 23

COMP 170 L2

One Type of First-Order Linear Recurrence

Examples

Page 24

COMP 170 L2

Iterating the Recurrence/Top-DownPage 25

COMP 170 L2

Iterating the Recurrence/Bottom-UpPage 26

COMP 170 L2Page 27

COMP 170 L2

One Type of First-Order Linear RecurrencePage 28

COMP 170 L2Page 29

COMP 170 L2Page 30

COMP 170 L2

ExamplePage 31

COMP 170 L2

An Application of Theorem 4.1 Geometric Series

Page 32

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

COMP 170 L2Page 34

Another Type of First-Order Linear Recurrence

COMP 170 L2Page 35

COMP 170 L2Page 36

COMP 170 L2Page 37

COMP 170 L2Page 38

COMP 170 L2Page 39

top related