Solving Recurrence Relations in Algorithm Analysis, Study notes of Data Structures and Algorithms

This document from the university of san francisco's department of computer science discusses methods for analyzing the running time of recursive programs using recurrence relations. Topics covered include the power function, the master theorem, and the method of iterated multiplication. The document also includes exercises for students to practice solving recurrence relations.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-wo0-1
koofers-user-wo0-1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms
Solving Recurrence Relations
Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science โ€” University of San Francisco โ€“ p.1/30
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Solving Recurrence Relations in Algorithm Analysis and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms Solving Recurrence Relations

Chris Brooks Department of Computer ScienceUniversity of San Francisco

Department of Computer Science โ€” University of San Francisco โ€“ p.1/

4-0:^

Algorithm Analysis

for^ (i=1;

i<=n*n;

i++)

for^ (j=0;

j<i;

j++) sum++;

Department of Computer Science โ€” University of San Francisco โ€“ p.2/

4-2:^

Algorithm Analysis

for^ (i=1;

i<=n*n;

i++)

for^ (j=0;

j<i;

j++) sum++; Exact # of times

sum++

is executed: (^2) n โˆ‘^ i^ = i=

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

โˆˆ^ ฮ˜(

(^4) n)

Department of Computer Science โ€” University of San Francisco โ€“ p.4/

4-3:^

Recursive Functions

long^

power(long

x,^ long

n)

if^ (n

==^ 0)return^ 1; elsereturn

x *^

power(x,

n-1);

How many times is this executed?

Department of Computer Science โ€” University of San Francisco โ€“ p.5/

4-5:^

Recurrence Relations

long^

power(long

x,^ long

n)

if^ (n

==^ 0)return^ 1; elsereturn

x *^

power(x,

n-1);

T^ (0) =

c^1

for some constant

c^1

T^ (n) =

c+^2

T^ (n

โˆ’^ 1)

for some constant

c^2 Department of Computer Science โ€” University of San Francisco โ€“ p.7/

4-6:^

Solving Recurrence Relations

T^ (0) =

c^1 T^ (n) =

T^ (n

โˆ’^ 1) +

c^2

If we knew

T^ (n

โˆ’^ 1)

, we could solve

T^ (n

T^ (n)

=^

T^ (n^

โˆ’^ 1) +

c^2

Department of Computer Science โ€” University of San Francisco โ€“ p.8/

4-8:^

Solving Recurrence Relations

T^ (0) =

c^1 T^ (n) =

T^ (n

โˆ’^ 1) +

c^2

If we knew

T^ (n

โˆ’^ 1)

, we could solve

T^ (n

T^ (n)

=^

T^ (n^

โˆ’^ 1) +

c^2

T^ (n^

โˆ’^ 1) =

T^ (n

โˆ’^ 2) +

c^2

=^ T^ (

n^ โˆ’^ 2) +

c+^2

c^2

=^ T^ (

n^ โˆ’^ 2) + 2

c^2

T^ (n^

โˆ’^ 2) =

T^ (n

โˆ’^ 3) +

c^2

=^ T^ (

n^ โˆ’^ 3) +

c+ 2^2

c^2

=^ T^ (

n^ โˆ’^ 3) + 3

c^2

Department of Computer Science โ€” University of San Francisco โ€“ p.10/

4-9:^

Solving Recurrence Relations

T^ (0) =

c^1 T^ (n) =

T^ (n

โˆ’^ 1) +

c^2

If we knew

T^ (n

โˆ’^ 1)

, we could solve

T^ (n

T^ (n)

=^

T^ (n^

โˆ’^ 1) +

c^2

T^ (n^

โˆ’^ 1) =

T^ (n

โˆ’^ 2) +

c^2

=^ T^ (

n^ โˆ’^ 2) +

c+^2

c^2

=^ T^ (

n^ โˆ’^ 2) + 2

c^2

T^ (n^

โˆ’^ 2) =

T^ (n

โˆ’^ 3) +

c^2

=^ T^ (

n^ โˆ’^ 3) +

c+ 2^2

c^2

=^ T^ (

n^ โˆ’^ 3) + 3

c^2

T^ (n^

โˆ’^ 3) =

T^ (n

โˆ’^ 4) +

c^2

=^ T^ (

n^ โˆ’^ 4) + 4

c^2

Department of Computer Science โ€” University of San Francisco โ€“ p.11/

Solving Recurrence Relations

T^ (0) =

c^1 T^ (n) =

T^ (n

โˆ’^ k) +

k^ โˆ—^ c

for all 2

k

If we set

k^ =

n, we have: T^ (n)

=^

T^ (n^

โˆ’^ n) +

nc^2

=^ T^ (0) +

nc^2 =^ c^1

+^ nc

2 โˆˆ^ ฮ˜(

n)

Department of Computer Science โ€” University of San Francisco โ€“ p.13/

Building a Better

Power

Can we avoid making a linear number of function calls? long^

power(long

x,^ long

n)

if^ (n==0)

return

1;

if^ (n==1)

return

x;

if^ ((n

%^ 2)

==^ 0) return

power(x*x,

n/2);

elsereturn

power(x*x,

n/2)

*^ x;

Department of Computer Science โ€” University of San Francisco โ€“ p.14/

Solving Recurrence Relations

T^ (n)

=^

T^ (n/

2) +^

c^3

Department of Computer Science โ€” University of San Francisco โ€“ p.16/

Solving Recurrence Relations

T^ (n)

=^

T^ (n/

2) +^

c^3

T^ (n/

T^ (n/

c^3

=^ T^ (

n/4) +

c+^3

c^3

=^ T^ (

n/4) + 2

c^3

Department of Computer Science โ€” University of San Francisco โ€“ p.17/

Solving Recurrence Relations

T^ (n)

=^

T^ (n/

2) +^

c^3

T^ (n/

T^ (n/

c^3

=^ T^ (

n/4) +

c+^3

c^3

=^ T^ (

n/4) + 2

c^3

T^ (n/

T^ (n/

c^3

=^ T^ (

n/8) +

c+ 2^3

c^3

=^ T^ (

n/8) + 3

c^3

T^ (n/

T^ (n/

c^3

=^ T^ (

n/16) +

c+ 3^3

c^3

=^ T^ (

n/16) + 4

c^3

Department of Computer Science โ€” University of San Francisco โ€“ p.19/

Solving Recurrence Relations

T^ (n)

=^

T^ (n/

2) +^

c^3

T^ (n/

T^ (n/

c^3

=^ T^ (

n/4) +

c+^3

c^3

=^ T^ (

n/4)

c^3

T^ (n/

T^ (n/

c^3

=^ T^ (

n/8) +

c+ 2^3

c^3

=^ T^ (

n/8)

c^3

T^ (n/

T^ (n/

c^3

=^ T^ (

n/16) +

c+ 3^3

c^3

=^ T^ (

n/16) + 4

c^3

T^ (n/

T^ (n/

c^3

=^ T^ (

n/32) +

c+ 4^3

c^3

=^ T^ (

n/32) + 5

c^3

Department of Computer Science โ€” University of San Francisco โ€“ p.20/