Docsity

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

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/