Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Recursive Equations & Solving Recurrences, Study notes of Algorithms and Programming

An introduction to recursion in both mathematics and programming, focusing on recursive mathematical equations called recurrences. The concept of recursion, the importance of solving recurrences, and methods for solving them, including forward and backward substitution. Examples and explanations are given to help illustrate the concepts.

Typology: Study notes

Pre 2010

Uploaded on 08/17/2009

koofers-user-36r
koofers-user-36r 🇺🇸

10 documents

1 / 34

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
RecursionandRecurrences
COMP157
Sept102007
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Understanding Recursive Equations & Solving Recurrences and more Study notes Algorithms and Programming in PDF only on Docsity!

Recursion

and^ RecurrencesCOMP157Sept^10

Recursion

-^ Recursion: When^ a^ function (or

procedure) calls^ itself.^ ‐^ The

Free^ On‐line^ Dictionary

of^ Computing

-^ applies^ to^ both

mathematics^ and

programming

-^ Recursive^ mathematical

equations^ are

called

recurrence^ equations

,^ recurrence^ relations

,^ or

simply^ recurrences

-^ Recurrences

naturally^ occur

in^ the^ analysis

of

recursive^ algorithms.

Solving^ Recurrences • Solving a^ recurrence^ means

to^ find^ an^ explicit (non‐recursive)

formula^ for^ x(n),

or^ to^ prove the^ no^ such^ sequence

exists

-^ In^ algorithm

analysis^ we^ need

this^ solution to^ compare^ functions

or to^ plot^ functions – Recurrences^ can

be^ expensive^ to

Appendix B compute^ directly

Method^ of

Forward^

Substitution

-^ Start^ from^

the^ initial^ condition

-^ Substitute^

into^ recurrence

-^ Repeat^ until

you^ can^ see^

a^ pattern

Forward^ Substitution

Example

-^ x(n)^ =^ 2x(n

‐1)^ +^ 1,^ for^ n^

>^1

x(1)^ =^1 • x(1)^ =^^1 x(2)^ =^ 2x(1)^ +

1 =^ 2*2+1^ =^

x(3)^ =^ 2x(2)^ +

1 =^ 2*3+1^ =^

x(4)^ =^ 2x(3)^ +

1 =^ 2*7+1^ =^

x(5)^ =^ 2x(4)^ +

1 =^ 2*15+1^ =

^31

-^ Pattern:^ x(n)

n^ = 2 – 1,^ for^ n=1^ … 5 Still^ need^ to^ prove

that^ it^ is^ true

for^ n>

Forward^ Substitution

Example

-^ x(n)^ =^ 2x(n

‐1)^ +^ 1,^ for^ n^

>^1

x(1)^ =^1 n^ • x(n)^ =^2 – 1,^

for^ n=1^ … 5

-^ proof^ (by^ substitution

into^ recurrence): x(n)^ =^ 2x(n‐1)

n‐^1 + 1 = 2*(

– 1)^ +^1

n^ = (2– 2)^ +^1 n^ = 2 – 1

-^ proof^ (by^ induction):n+1x(n+1)^ =^2

n^ – 1^ =^ 2*(2^ )^ ‐

n 2 + 1 = 2*(

–1)^ +^1

=^ 2x(n)^ +^1

Backward

Substitution

Example

-^ x(n)^ =^ x(n‐1)

+^ n,^ for^ n^ >^1 x(0) = 0

-^ x(n‐1)^ =

x(n‐2)^ +^ (n‐1),

sub^ into^ x(n): x(n)^ =^ (x(n‐2)

+^ (n‐1))^ +^ n = x(n‐2) +^ (n‐1)^ +^ n x(n‐2) = x(n‐3)^ +^ (n‐2),^

sub^ into^ previous: x(n)^ =^ (x(n‐3)

+^ (n‐2))^ +^ (n‐

1)^ +^ n =^ x(n‐3)^ +^ (n‐

2)^ +^ (n‐1)^ +^ n

-^ Pattern?

Backward

Substitution

Example

-^ x(n)^ =^ x(n‐2)

+^ (n‐1)^ +^ n x(n) = x(n‐3) +^ (n‐2)^ +^ (n‐1)

+^ n

-^ Pattern?^ x(n)^ =^ x(n‐i)

+^ (n‐i+1)^ +^ (n

‐i+2)^ +^ … +^ n when^ n^ =^ i,^ n

‐i =^ 0,^ so^ we^ can

use^ initial condition:x(n)^ =^ x(0)^ +^1

+^2 +^ … +^ n^ =

0 +^1 +^2 +^ … +

n

n = = i ∑= i^0

) (^1) ( + nn 2

Decrease

‐by‐Constant

Recurrences

-^ Decrease‐by

‐constant^ algorithm

use^ the solution^ to^ an

n/b^ size^ problem

to^ solve an^ n^ size^ problem

(typically^ b =2)

-^ Typical^ efficiency

recurrence: T ( n )^ =^ T ( n / b )^

+^ f ( n )

-^ Backward^ substitution

yields: k T ( b )^ =^ T(1)^ +

k i bf )( ∑= i^1

Divide‐and

‐conquer^

Recurrences

-^ Divide‐and

‐conquer^ algorithms

divide^ the problem^ into

several^ smaller

problems,^ which are^ solved^ recursively,

then^ combines

the solutions • Typical^ efficiency

recurrence: T ( n )^ =^ aT ( n / b

)^ +^ f ( n )

-^ Backward^ substitution

yields: T ( n )^ =^

]/)( loglog ) (^1) ([^1 n i a^ i i

abf

b bTn + ∑=

Review:^ Recursive

Algorithms

-^ Give^ a^ recursive

algorithm^ to

compute^ the length^ of^ a^ linked

list base^ case:^ empty^ list

has^ length^ zero recursive^ case:^1 +^ length

of^ list^ with^ head

removed

Review:^ Recursive

Algorithms

-^ Give^ a^ recursive

algorithm^ to

compute^ the length^ of^ a^ linked

list int length(Node*

head) { if^ (head^ ==

NULL)^ return

return^1 +^ length(head

‐>next); }

Review:^ Recursive

Algorithms

-^ Give^ a^ recursive

algorithm^ to

compute^ the sum^ of^ the^ elements

in^ linked^ list double^ sum(Node*

head) { if^ (head^ ==

NULL)^ return

return^ head‐

value^ +^ sum(head

‐>next); }

Review:^ Recursive

Algorithms

-^ Give^ a^ recursive

algorithm^ to

count^ the number^ of^ odd

elements^ in^

a^ linked^ list base^ case:empty^ list^ has^0 odd^ elements recursive^ case:if^ head^ node

element^ is^ odd: 1 + number^ of^ odd^ elements in list with^ head^ removedelse: number of odd^ elements in list with^ head^ removed