






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 30
This page cannot be seen from the preview
Don't miss anything!























Chris Brooks Department of Computer ScienceUniversity of San Francisco
Department of Computer Science โ University of San Francisco โ p.1/
for^ (i=1;
i<=n*n;
i++)
for^ (j=0;
j<i;
j++) sum++;
Department of Computer Science โ University of San Francisco โ p.2/
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/
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/
long^
power(long
x,^ long
n)
if^ (n
==^ 0)return^ 1; elsereturn
x *^
power(x,
n-1);
c^1
for some constant
c^1
T^ (n) =
c+^2
T^ (n
for some constant
c^2 Department of Computer Science โ University of San Francisco โ p.7/
c^1 T^ (n) =
T^ (n
c^2
If we knew
T^ (n
, we could solve
T^ (n
T^ (n)
T^ (n^
c^2
Department of Computer Science โ University of San Francisco โ p.8/
c^1 T^ (n) =
T^ (n
c^2
If we knew
T^ (n
, we could solve
T^ (n
T^ (n)
T^ (n^
c^2
T^ (n^
T^ (n
c^2
n^ โ^ 2) +
c+^2
c^2
n^ โ^ 2) + 2
c^2
T^ (n^
T^ (n
c^2
n^ โ^ 3) +
c+ 2^2
c^2
n^ โ^ 3) + 3
c^2
Department of Computer Science โ University of San Francisco โ p.10/
c^1 T^ (n) =
T^ (n
c^2
If we knew
T^ (n
, we could solve
T^ (n
T^ (n)
T^ (n^
c^2
T^ (n^
T^ (n
c^2
n^ โ^ 2) +
c+^2
c^2
n^ โ^ 2) + 2
c^2
T^ (n^
T^ (n
c^2
n^ โ^ 3) +
c+ 2^2
c^2
n^ โ^ 3) + 3
c^2
T^ (n^
T^ (n
c^2
n^ โ^ 4) + 4
c^2
Department of Computer Science โ University of San Francisco โ p.11/
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/
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/
T^ (n)
T^ (n/
c^3
Department of Computer Science โ University of San Francisco โ p.16/
T^ (n)
T^ (n/
c^3
T^ (n/
T^ (n/
c^3
n/4) +
c+^3
c^3
=^ T^ (
n/4) + 2
c^3
Department of Computer Science โ University of San Francisco โ p.17/
T^ (n)
T^ (n/
c^3
T^ (n/
T^ (n/
c^3
n/4) +
c+^3
c^3
=^ T^ (
n/4) + 2
c^3
T^ (n/
T^ (n/
c^3
n/8) +
c+ 2^3
c^3
n/8) + 3
c^3
T^ (n/
T^ (n/
c^3
n/16) +
c+ 3^3
c^3
n/16) + 4
c^3
Department of Computer Science โ University of San Francisco โ p.19/
T^ (n)
T^ (n/
c^3
T^ (n/
T^ (n/
c^3
n/4) +
c+^3
c^3
=^ T^ (
n/4)
c^3
T^ (n/
T^ (n/
c^3
n/8) +
c+ 2^3
c^3
n/8)
c^3
T^ (n/
T^ (n/
c^3
n/16) +
c+ 3^3
c^3
n/16) + 4
c^3
T^ (n/
T^ (n/
c^3
n/32) +
c+ 4^3
c^3
n/32) + 5
c^3
Department of Computer Science โ University of San Francisco โ p.20/