``````Recursive Algorithm Analysis
Modeling the running time of recursive code.
Kevin Lin, with thanks to many others.
1

2
Linear vs. Linearithmic (N log N) vs. Quadratic Runtimes
Algorithm Design (Jon Kleinberg, Éva Tardos/Pearson Education)
?: How large of a difference is there between N and N log N? Between N log N and N2?

Consider the following recurrence.

Visualize the recursion tree.
Give a closed-form solution for T(N) by unrolling the recurrence. Simplify.
3
One Subproblem
Q
Q1: Visualize the recursion tree.

Q2: Give a closed-form solution for T(N) by unrolling the recurrence. Simplify.

Consider the following recurrence.

Visualize the recursion tree.
Give a closed-form solution for T(N) by unrolling the recurrence. Simplify.
4
One Subproblem
A

Consider the following recurrence.

Visualize the recursion tree.
Give a closed-form solution for T(N) by unrolling the recurrence. Simplify.
5
One Subproblem
N
A
N/2
N/4
N/8
1
cN
cN/2
cN/4
cN/8
d

Constant Work Per Call
Give a recurrence relation for f3.
6
static int f3(int n) {
if (n <= 1)
return 1;
return f3(n-1) + f3(n-1);
}
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
Q
?: What is the recursive work? What is the non-recursive work?

Q1: Give a recurrence relation for f3.

Constant Work Per Call
Give a recurrence relation for f3.

7
static int f3(int n) {
if (n <= 1)
return 1;
return f3(n-1) + f3(n-1);
}
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
A

Constant Work Per Call
Give a recurrence relation for f3.
8
static int f3(int n) {
if (n <= 1)
return 1;
return f3(n-1) + f3(n-1);
}
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
A

Identifying Patterns
Approach: Identify a pattern and then sum over all recursive levels.
9
static int f3(int n) {
if (n <= 1)
return 1;
return f3(n-1) + f3(n-1);
}
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
8d
4c
2c
c

Summing Over Levels
Approach: Identify a pattern and then sum over all recursive levels.

What is the value of the last term in the sum for T(N)?

Give a simple asymptotic runtime bound.
10
static int f3(int n) {
if (n <= 1)
return 1;
return f3(n-1) + f3(n-1);
}
Q
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
Q1: What is the value of the last term in the sum for T(N)?

Q2: Give a simple asymptotic runtime bound.

Summing Over Levels
Approach: Identify a pattern and then sum over all recursive levels.

What is the value of the last term in the sum for T(N)? _____ d

Give a simple asymptotic runtime bound.

11
static int f3(int n) {
if (n <= 1)
return 1;
return f3(n-1) + f3(n-1);
}
A
3
2
2
1
1
1
1
3
2
2
1
1
1
1
4
?: What is the value of the last term in the sum for T(N)?

Q1: What is the value of the last term in the sum for T(N)?

Q2: Give a simple asymptotic runtime bound.

Summing Over Levels
As long as Q is a power of 2, then
12
A
Since Q = 2N - 1

Runtime: g0
Give best and worst case runtime. Assume k(N) runs in constant time and returns a boolean.
13
Q
static void g0(int N) {
if (N == 0)
return;
g0(N / 2);
if (k(N))
g0(N / 2);
}
Q1: Give best and worst case runtime. Assume k(N) runs in constant time and returns a boolean.

Runtime: g0
14
A
``````