Recursion Techniques by Ali Alilooee
Recursion Techniques by Ali Alilooee
Ali Alilooee
Recursive
problems
Substitution
method Recursion
Basic
recursive
algorithms
Recursion Ali Alilooee
trees method
Taught by: Greg Ryslik :)
Some Ohio State University
formulas for
recurrences
Presentation Outline
Recursion
Ali Alilooee
Recursion
Ali Alilooee
Question
Recursive
problems
How to solve a problem recursively?
Substitution
method
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Divide-and-Conquer
Recursion
Ali Alilooee
Question
Recursive
problems
How to solve a problem recursively?
Substitution
method
Basic
We solve a problem recursively by applying three following steps:
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Divide-and-Conquer
Recursion
Ali Alilooee
Question
Recursive
problems
How to solve a problem recursively?
Substitution
method
Basic
We solve a problem recursively by applying three following steps:
recursive
algorithms Divide the problem into a number of subproblems that are
Recursion smaller instances of the same problem.
trees method
Some
formulas for
recurrences
Divide-and-Conquer
Recursion
Ali Alilooee
Question
Recursive
problems
How to solve a problem recursively?
Substitution
method
Basic
We solve a problem recursively by applying three following steps:
recursive
algorithms Divide the problem into a number of subproblems that are
Recursion smaller instances of the same problem.
trees method
Some
Conquer the subproblems by solving them recursively. If
formulas for
recurrences
the subproblem sizes are small enough, however, just solve
the subproblems in a straightforward manner.
Divide-and-Conquer
Recursion
Ali Alilooee
Question
Recursive
problems
How to solve a problem recursively?
Substitution
method
Basic
We solve a problem recursively by applying three following steps:
recursive
algorithms Divide the problem into a number of subproblems that are
Recursion smaller instances of the same problem.
trees method
Some
Conquer the subproblems by solving them recursively. If
formulas for
recurrences
the subproblem sizes are small enough, however, just solve
the subproblems in a straightforward manner.
Combine the solutions to the subproblems into the
solution for the original problem.
Merge sort (Sort 39, 27, 43, 3, 9, 82, 10)
Recursion 39 27 43 3 9 82 10
Ali Alilooee
Recursive 39 27 43 3 9 82 10
problems
Substitution
method
Basic
39 27 43 3 9 82 10
recursive
algorithms
Recursion
trees method
39 27 43 3 9 82 10
Some
formulas for
recurrences 27 39 3 43 9 82 10
3 27 39 43 9 10 82
3 9 10 27 39 43 82
Presentation Outline
Recursion
Ali Alilooee
Recursion
Ali Alilooee
Recursive
problems
Substitution
A recurrence is an equation or inequality that describes a
method function in terms of its value on smaller inputs.
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Recurrences
Recursion
Ali Alilooee
Recursive
problems
Substitution
A recurrence is an equation or inequality that describes a
method function in terms of its value on smaller inputs.
Basic
recursive
For example the following recurrence.
algorithms
if n = 1
(
Recursion C
trees method
T (n) = n (1)
Some 2T + Cn if n 6= 1
formulas for 2
recurrences
Find the asymptotic worst-case running time (T (n))
of the following function.
Recursion
Then we have
Ali Alilooee T (n) ≥ 2k (T (n − 2k)
Recursive
problems
Substitution
method
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
At least exponential
Recursion
Then we have
Ali Alilooee T (n) ≥ 2k (T (n − 2k)
Recursive
problems
Substitution The question is how much more we can continue this
method
Basic
process.
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
At least exponential
Recursion
Then we have
Ali Alilooee T (n) ≥ 2k (T (n − 2k)
Recursive
problems
Substitution The question is how much more we can continue this
method
Basic
process.
recursive
algorithms We continue this process until n − 2k gets zero.
Recursion
trees method
Some
formulas for
recurrences
At least exponential
Recursion
Then we have
Ali Alilooee T (n) ≥ 2k (T (n − 2k)
Recursive
problems
Substitution The question is how much more we can continue this
method
Basic
process.
recursive
algorithms We continue this process until n − 2k gets zero.
Recursion Then we have
trees method
Some n
formulas for n − 2k = 0 =⇒ k = .
recurrences 2
At least exponential
Recursion
Then we have
Ali Alilooee T (n) ≥ 2k (T (n − 2k)
Recursive
problems
Substitution The question is how much more we can continue this
method
Basic
process.
recursive
algorithms We continue this process until n − 2k gets zero.
Recursion Then we have
trees method
Some n
formulas for n − 2k = 0 =⇒ k = .
recurrences 2
Recursion
Then we have
Ali Alilooee T (n) ≥ 2k (T (n − 2k)
Recursive
problems
Substitution The question is how much more we can continue this
method
Basic
process.
recursive
algorithms We continue this process until n − 2k gets zero.
Recursion Then we have
trees method
Some n
formulas for n − 2k = 0 =⇒ k = .
recurrences 2
T (n) ∈ Ω(2n ).
Find the asympthotic running time
Recursion
Input: Array A of n elements.
Ali Alilooee
procedure func(A, n)
Recursive
problems
1: if (n ≤ 20) then return (A[n]);
Substitution
2: end if
method 3: for i ← 1 to bn/2c do
Basic
recursive 4: A[i] ← A[i] + A[2 ∗ i];
algorithms
5: end for
3n
Recursion
trees method 6: x ← func A, ;
Some
5
formulas for
recurrences
Find the asympthotic running time
Recursion
Input: Array A of n elements.
Ali Alilooee
procedure func(A, n)
Recursive
problems
1: if (n ≤ 20) then return (A[n]);
Substitution
2: end if
method 3: for i ← 1 to bn/2c do
Basic
recursive 4: A[i] ← A[i] + A[2 ∗ i];
algorithms
5: end for
3n
Recursion
trees method 6: x ← func A, ;
Some
5
formulas for
recurrences
We need tondthe asymptotic behavior of the recurrence
3n
T (n) = T + Cn.
5
Find the asympthotic running time
Recursion
Input: Array A of n elements.
Ali Alilooee
procedure func(A, n)
Recursive
problems
1: if (n ≤ 20) then return (A[n]);
Substitution
2: end if
method 3: for i ← 1 to bn/2c do
Basic
recursive 4: A[i] ← A[i] + A[2 ∗ i];
algorithms
5: end for
3n
Recursion
trees method 6: x ← func A, ;
Some
5
formulas for
recurrences
We need tondthe asymptotic behavior of the recurrence
3n
T (n) = T + Cn. Remember
5
3n 32 n 3
T =T + (Cn).
5 52 5
Recursion Then we have
2
3n 3 n 3
Ali Alilooee
T (n) = T + Cn = T 2
+ (Cn) + Cn
Recursive
5 5 5
3
problems
3 n 32 3
Substitution = T + (Cn) + (Cn) + Cn
method 53 52 5
Basic ..
recursive
algorithms
.
k−1 i
3 n 3
k
Recursion
X
trees method = T + Cn
5 k
i=0
5
Some
formulas for
recurrences
Recursion Then we have
2
3n 3 n 3
Ali Alilooee
T (n) = T + Cn = T 2
+ (Cn) + Cn
Recursive
5 5 5
3
problems
3 n 32 3
Substitution = T + (Cn) + (Cn) + Cn
method 53 52 5
Basic ..
recursive
algorithms
.
k−1 i
3 n 3
k
Recursion
X
trees method = T + Cn
5 k
i=0
5
Some
formulas for
recurrences we should continue this process until we reach to k for which
we have
3 1 3
k k
n=1⇒ =
5 n 5
Recursion Then we have
2
3n 3 n 3
Ali Alilooee
T (n) = T + Cn = T 2
+ (Cn) + Cn
Recursive
5 5 5
3
problems
3 n 32 3
Substitution = T + (Cn) + (Cn) + Cn
method 53 52 5
Basic ..
recursive
algorithms
.
k−1 i
3 n 3
k
Recursion
X
trees method = T + Cn
5 k
i=0
5
Some
formulas for
recurrences we should continue this process until we reach to k for which
we have
3 1 3
k k
n=1⇒ =
5 n 5
3
i
Pk−1
T (n) = T (1) + Cn i=0 =
5
Recursion Then we have
2
3n 3 n 3
Ali Alilooee
T (n) = T + Cn = T 2
+ (Cn) + Cn
Recursive
5 5 5
3
problems
3 n 32 3
Substitution = T + (Cn) + (Cn) + Cn
method 53 52 5
Basic ..
recursive
algorithms
.
k−1 i
3 n 3
k
Recursion
X
trees method = T + Cn
5 k
i=0
5
Some
formulas for
recurrences we should continue this process until we reach to k for which
we have
3 1 3
k k
n=1⇒ =
5 n 5
3 3
i i
Pk−1 Pk−1
T (n) = T (1) + Cn i=0 = C + Cn i=0 .
5 5
Recursion
Then we have
Ali Alilooee
Recursive
problems
Substitution
method
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Recursion
Then we have
Ali Alilooee
2 k−1
3 3 3
Recursive T (n) = Cn + Cn + Cn + · · · + Cn + C
problems 5 5 5
Substitution
method
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Recursion
Then we have
Ali Alilooee
2 k−1
3 3 3
Recursive T (n) = Cn + Cn + Cn + · · · + Cn + C
problems 5 5 5
Substitution 2 k−1 !
method 3 3 3 1
= Cn 1 + + + ··· + +
Basic
recursive 5 5 5 n
algorithms
Recursion
trees method
Some
formulas for
recurrences
Recursion
Then we have
Ali Alilooee
2 k−1
3 3 3
Recursive T (n) = Cn + Cn + Cn + · · · + Cn + C
problems 5 5 5
Substitution 2 k−1 !
method 3 3 3 1
= Cn 1 + + + ··· + +
Basic
recursive 5 5 5 n
algorithms
2 k−1 k !
3 3 3 3
Recursion
trees method = Cn 1 + + + ··· + +
Some
5 5 5 5
formulas for
recurrences
Recursion
Then we have
Ali Alilooee
2 k−1
3 3 3
Recursive T (n) = Cn + Cn + Cn + · · · + Cn + C
problems 5 5 5
Substitution 2 k−1 !
method 3 3 3 1
= Cn 1 + + + ··· + +
Basic
recursive 5 5 5 n
algorithms
2 k−1 k !
3 3 3 3
Recursion
trees method = Cn 1 + + + ··· + +
Some
5 5 5 5
formulas for
k i
recurrences X 3
= Cn
i=0
5
Recursion
Then we have
Ali Alilooee
2 k−1
3 3 3
Recursive T (n) = Cn + Cn + Cn + · · · + Cn + C
problems 5 5 5
Substitution 2 k−1 !
method 3 3 3 1
= Cn 1 + + + ··· + +
Basic
recursive 5 5 5 n
algorithms
2 k−1 k !
3 3 3 3
Recursion
trees method = Cn 1 + + + ··· + +
Some
5 5 5 5
formulas for
k i ∞ i
recurrences X 3 X 3
= Cn < Cn
i=0
5 i=0
5
Recursion
Then we have
Ali Alilooee
2 k−1
3 3 3
Recursive T (n) = Cn + Cn + Cn + · · · + Cn + C
problems 5 5 5
Substitution 2 k−1 !
method 3 3 3 1
= Cn 1 + + + ··· + +
Basic
recursive 5 5 5 n
algorithms
2 k−1 k !
3 3 3 3
Recursion
trees method = Cn 1 + + + ··· + +
Some
5 5 5 5
formulas for
k i ∞ i
recurrences
3 3 5
X X
= Cn < Cn = Cn
i=0
5 i=0
5 2
Recursion
Then we have
Ali Alilooee
2 k−1
3 3 3
Recursive T (n) = Cn + Cn + Cn + · · · + Cn + C
problems 5 5 5
Substitution 2 k−1 !
method 3 3 3 1
= Cn 1 + + + ··· + +
Basic
recursive 5 5 5 n
algorithms
2 k−1 k !
3 3 3 3
Recursion
trees method = Cn 1 + + + ··· + +
Some
5 5 5 5
formulas for
k i ∞ i
recurrences
3 3 5
X X
= Cn < Cn = Cn
i=0
5 i=0
5 2
5
Then Cn < T (n) < Cn
2
Recursion
Then we have
Ali Alilooee
2 k−1
3 3 3
Recursive T (n) = Cn + Cn + Cn + · · · + Cn + C
problems 5 5 5
Substitution 2 k−1 !
method 3 3 3 1
= Cn 1 + + + ··· + +
Basic
recursive 5 5 5 n
algorithms
2 k−1 k !
3 3 3 3
Recursion
trees method = Cn 1 + + + ··· + +
Some
5 5 5 5
formulas for
k i ∞ i
recurrences
3 3 5
X X
= Cn < Cn = Cn
i=0
5 i=0
5 2
5
Then Cn < T (n) < Cn =⇒ T (n) ∈ Θ(n).
2
Extra Extra Credits
Recursion
Ali Alilooee
Recursive
problems
3
Substitution
method Let T (n) = T n + Cn and T (1) = C , use the
5
Basic
recursive mathematical induction and show
algorithms
5
Recursion
trees method T (n) ≤ Cn, for all n ≥ 1.
2
Some
formulas for
recurrences
Find the asympthotic running time
i=0
Recursion We continue this process until n − 8k = 0 and then k = n8 .
Ali Alilooee Remember that T (0) = C , then we have
Recursive k−1
problems
(n − 8i) log (n − 8i)
X
T (n) = C + C
Substitution
method i=0
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Recursion We continue this process until n − 8k = 0 and then k = n8 .
Ali Alilooee Remember that T (0) = C , then we have
Recursive k−1 k−1
problems
(n − 8i) log (n − 8i) < Cn log n + C
X X
T (n) = C + C n log n
Substitution
method i=0 i=0
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Recursion We continue this process until n − 8k = 0 and then k = n8 .
Ali Alilooee Remember that T (0) = C , then we have
Recursive k−1 k−1
problems
(n − 8i) log (n − 8i) < Cn log n + C
X X
T (n) = C + C n log n
Substitution
method i=0 i=0
Basic k
recursive
X
algorithms =⇒ T (n) < n log n
Recursion i=0
trees method
Some
formulas for
recurrences
Recursion We continue this process until n − 8k = 0 and then k = n8 .
Ali Alilooee Remember that T (0) = C , then we have
Recursive k−1 k−1
problems
(n − 8i) log (n − 8i) < Cn log n + C
X X
T (n) = C + C n log n
Substitution
method i=0 i=0
Basic k
recursive
n log n = (k + 1)(n log n)
X
algorithms =⇒ T (n) <
Recursion i=0
trees method
Some
formulas for
recurrences
Recursion We continue this process until n − 8k = 0 and then k = n8 .
Ali Alilooee Remember that T (0) = C , then we have
Recursive k−1 k−1
problems
(n − 8i) log (n − 8i) < Cn log n + C
X X
T (n) = C + C n log n
Substitution
method i=0 i=0
Basic k n
recursive
n log n = (k + 1)(n log n) = + 1 (n log n)
X
algorithms =⇒ T (n) <
8
Recursion i=0
trees method
Some
formulas for
recurrences
Recursion We continue this process until n − 8k = 0 and then k = n8 .
Ali Alilooee Remember that T (0) = C , then we have
Recursive k−1 k−1
problems
(n − 8i) log (n − 8i) < Cn log n + C
X X
T (n) = C + C n log n
Substitution
method i=0 i=0
Basic k n
recursive
n log n = (k + 1)(n log n) = + 1 (n log n)
X
algorithms =⇒ T (n) <
8
Recursion i=0
trees method
n2
n
Some =⇒ T (n) < + 1 (n log n) ≈ log n.
formulas for 8 8
recurrences
Recursion We continue this process until n − 8k = 0 and then k = n8 .
Ali Alilooee Remember that T (0) = C , then we have
Recursive k−1 k−1
problems
(n − 8i) log (n − 8i) < Cn log n + C
X X
T (n) = C + C n log n
Substitution
method i=0 i=0
Basic k n
recursive
n log n = (k + 1)(n log n) = + 1 (n log n)
X
algorithms =⇒ T (n) <
8
Recursion i=0
trees method
n2
n
Some =⇒ T (n) < + 1 (n log n) ≈ log n.
formulas for 8 8
recurrences
n2 n2
≈ log n ⇒ T (n) > log n ⇒ T (n) ∈ Θ(n2 log n).
32 32
Extra Extra Credits
Recursion
Ali Alilooee
Recursive
problems
Substitution
method Let T (n) = T (n − 8) + Cn log n, for n > 8 and T (0) = C . Use
Basic the mathematical induction and show
recursive
algorithms
C C
Recursion n2 log n ≤ T (n) ≤ n2 log n, for all n ≥ 8.
trees method 64 8
Some
formulas for
recurrences
Presentation Outline
Recursion
Ali Alilooee
Recursion
Input: Array A of n elements.
Ali Alilooee
Result: Permutation of A such that
Recursive
problems
Substitution
A[1] ≤ A[2] ≤ · · · ≤ A[n].
method
Basic
recursive
procedure SelectionSort(A[], n)
algorithms 1: if (n ≤ 1) then return ;
Recursion
trees method 2: else
Some 3: for i ← 1 to n − 1 do
formulas for
recurrences 4: if (A[i] > A[n]) then
5: Swap(A[i], A[n]);
6: end if
7: end for
8: SelectionSort(A[], n − 1);
9: end if
Find asymptotic worst-case running time of the
SelectionSort.
Recursion Since the running time of the For loop is Cn then we have the
Ali Alilooee recurrence T (n) = T (n − 1) + Cn. Then we have
Recursive
problems
Substitution
method
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Find asymptotic worst-case running time of the
SelectionSort.
Recursion Since the running time of the For loop is Cn then we have the
Ali Alilooee recurrence T (n) = T (n − 1) + Cn. Then we have
Recursive T (n) = T (n − 1) + Cn =
problems
Substitution
method
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Find asymptotic worst-case running time of the
SelectionSort.
Recursion Since the running time of the For loop is Cn then we have the
Ali Alilooee recurrence T (n) = T (n − 1) + Cn. Then we have
Recursive T (n) = T (n − 1) + Cn = T (n − 2) + C (n − 1) + Cn
problems
Substitution
method
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Find asymptotic worst-case running time of the
SelectionSort.
Recursion Since the running time of the For loop is Cn then we have the
Ali Alilooee recurrence T (n) = T (n − 1) + Cn. Then we have
Recursive T (n) = T (n − 1) + Cn = T (n − 2) + C (n − 1) + Cn
problems
2
Substitution
= T (n − 3) +
X
method C (n − i)
Basic i=0
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Find asymptotic worst-case running time of the
SelectionSort.
Recursion Since the running time of the For loop is Cn then we have the
Ali Alilooee recurrence T (n) = T (n − 1) + Cn. Then we have
Recursive T (n) = T (n − 1) + Cn = T (n − 2) + C (n − 1) + Cn
problems
2
Substitution
= T (n − 3) +
X
method C (n − i)
Basic i=0
recursive
algorithms ..
.
Recursion
trees method = T (n − n) + C (n − (n − 1)) +
Some
formulas for
recurrences
Find asymptotic worst-case running time of the
SelectionSort.
Recursion Since the running time of the For loop is Cn then we have the
Ali Alilooee recurrence T (n) = T (n − 1) + Cn. Then we have
Recursive T (n) = T (n − 1) + Cn = T (n − 2) + C (n − 1) + Cn
problems
2
Substitution
= T (n − 3) +
X
method C (n − i)
Basic i=0
recursive
algorithms ..
.
Recursion
trees method = T (n − n) + C (n − (n − 1)) +
Some
formulas for · · · + C (n − 1) + Cn
recurrences
Find asymptotic worst-case running time of the
SelectionSort.
Recursion Since the running time of the For loop is Cn then we have the
Ali Alilooee recurrence T (n) = T (n − 1) + Cn. Then we have
Recursive T (n) = T (n − 1) + Cn = T (n − 2) + C (n − 1) + Cn
problems
2
Substitution
= T (n − 3) +
X
method C (n − i)
Basic i=0
recursive
algorithms ..
.
Recursion
trees method = T (n − n) + C (n − (n − 1)) +
Some
formulas for · · · + C (n − 1) + Cn
recurrences
n−1
= T (0) + C
X
(n − i)
i=0
Find asymptotic worst-case running time of the
SelectionSort.
Recursion Since the running time of the For loop is Cn then we have the
Ali Alilooee recurrence T (n) = T (n − 1) + Cn. Then we have
Recursive T (n) = T (n − 1) + Cn = T (n − 2) + C (n − 1) + Cn
problems
2
Substitution
= T (n − 3) +
X
method C (n − i)
Basic i=0
recursive
algorithms ..
.
Recursion
trees method = T (n − n) + C (n − (n − 1)) +
Some
formulas for · · · + C (n − 1) + Cn
recurrences
n−1
= T (0) + C
X
(n − i)
i=0
n−
X 1
= C +C (n − i)
i=0
Find asymptotic worst-case running time of the
SelectionSort.
Recursion Since the running time of the For loop is Cn then we have the
Ali Alilooee recurrence T (n) = T (n − 1) + Cn. Then we have
Recursive T (n) = T (n − 1) + Cn = T (n − 2) + C (n − 1) + Cn
problems
2
Substitution
= T (n − 3) +
X
method C (n − i)
Basic i=0
recursive
algorithms ..
.
Recursion
trees method = T (n − n) + C (n − (n − 1)) +
Some
formulas for · · · + C (n − 1) + Cn
recurrences
n−1
= T (0) + C
X
(n − i)
i=0
n− 1 n
Cn = Cn2
X X
= C +C (n − i) ≤
i=0 i=0
Recursion
Ali Alilooee
Recursive
Then
problems =⇒ T (n) ∈ O(n2 ).
Substitution
method
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Recursion
Ali Alilooee
Recursive
Then
problems =⇒ T (n) ∈ O(n2 ).
Substitution
method Note that since we have
Basic
recursive n−1
algorithms X n
T (n) = C + C (n − i) ≥ C ( ) + · · · + C (n)
Recursion
trees method
2
i=0
Some
formulas for
recurrences
Recursion
Ali Alilooee
Recursive
Then
problems =⇒ T (n) ∈ O(n2 ).
Substitution
method Note that since we have
Basic
recursive n−1
algorithms X n
T (n) = C + C (n − i) ≥ C ( ) + · · · + C (n)
Recursion
trees method
2
i=0
Some
formulas for n n n
recurrences ≥ C( ) + C( ) + · · · + C( )
2 2 2
Recursion
Ali Alilooee
Recursive
Then
problems =⇒ T (n) ∈ O(n2 ).
Substitution
method Note that since we have
Basic
recursive n−1
algorithms X n
T (n) = C + C (n − i) ≥ C ( ) + · · · + C (n)
Recursion
trees method
2
i=0
Some
formulas for n n n Cn2
recurrences ≥ C( ) + C( ) + · · · + C( ) =
2 2 2 4
Recursion
Ali Alilooee
Recursive
Then
problems =⇒ T (n) ∈ O(n2 ).
Substitution
method Note that since we have
Basic
recursive n−1
algorithms X n
T (n) = C + C (n − i) ≥ C ( ) + · · · + C (n)
Recursion
trees method
2
i=0
Some
formulas for n n n Cn2
recurrences ≥ C( ) + C( ) + · · · + C( ) =
2 2 2 4
Then T (n) ∈ Θ(n2 ).
Binary Search: Recursive Version
Recursion
Ali Alilooee
Recursive
problems Input: Array A of at least j elements.
Substitution Integers i and j .
method
Basic
Result: Array B containing A[i, i + 1, ..., j]
recursive
algorithms procedure Copy(A[], i, j, B[])
Recursion 1: p ← 1;
trees method
2: for k ← i to j do
Some
formulas for 3: B[p] ← A[k];
recurrences
4: p ← p + 1;
5: end for
Merge
Recursion
Recursion
Ali Alilooee
Ali Alilooee
Recursive
problems
Then we have T (n) = (k + 1)Cn when k is the level of the
Substitution tree.
method
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Recursion
Ali Alilooee
Recursive
problems
Then we have T (n) = (k + 1)Cn when k is the level of the
Substitution tree. So we just need to calculate k .
method
Basic
recursive
algorithms
Recursion
trees method
Some
formulas for
recurrences
Recursion
Ali Alilooee
Recursive
problems
Then we have T (n) = (k + 1)Cn when k is the level of the
Substitution tree. So we just need to calculate k .
method
2 3
k k
Basic
recursive n = 1 =⇒ n =
algorithms 3 2
Recursion
trees method
Some
formulas for
recurrences
Recursion
Ali Alilooee
Recursive
problems
Then we have T (n) = (k + 1)Cn when k is the level of the
Substitution tree. So we just need to calculate k .
method
2 3
k k
Basic
recursive n = 1 =⇒ n = =⇒ k = log1.5 n ≈ log n
algorithms 3 2
Recursion
trees method
Some
formulas for
recurrences
Recursion
Ali Alilooee
Recursive
problems
Then we have T (n) = (k + 1)Cn when k is the level of the
Substitution tree. So we just need to calculate k .
method
2 3
k k
Basic
recursive n = 1 =⇒ n = =⇒ k = log1.5 n ≈ log n
algorithms 3 2
Recursion
trees method
then we have
Some
formulas for
recurrences
T (n) = (log n + 1)Cn ≈ n log n =⇒ T (n) ∈ Θ(n log n).
Presentation Outline
Recursion
Ali Alilooee
Recursion
Ali Alilooee For the following recurrence for Divide and Conquer approach
Recursive
problems T (n) = aT (n/b) + f (n); for a ≥ 1 and b > 1
Substitution
method
we have the following special asymptotic complexities.
Basic
recursive
algorithms T (n) = T (n/2) + c; T (n) ∈ Θ(log2 n)
Recursion
trees method T (n) = T (n/3) + c; T (n) ∈ Θ(log2 n)
Some
formulas for T (n) = T (n/2) + cn; T (n) ∈ Θ(n)
recurrences
T (n) = T (n/3) + cn; T (n) ∈ Θ(n)
T (n) = 2T (n/2) + cn; T (n) ∈ Θ(n log2 n)
T (n) = 3T (n/3) + cn; T (n) ∈ Θ(n log2 n)
More Divide and Conquer
Recursion
Ali Alilooee
Recursive
For the following recurrence for Divide and Conquer approach
problems
Substitution T (n) = aT (n/b) + f (n); for a ≥ 1 and b > 1
method
Basic
recursive we have the following special asymptotic complexities.
algorithms
Recursion
trees method
T (n) = 3T (n/2) + cn; T (n) ∈ Θ(nlog2 3 )
Some T (n) = 4T (n/2) + cn; T (n) ∈ Θ(nlog4 3 ) = Θ(n2 )
formulas for
recurrences T (n) = 2T (n/2) + cn2 ; T (n) ∈ Θ(n2 )
T (n) = 4T (n/2) + cn2 ; T (n) ∈ Θ(n2 log n)
Asymmetric Recurrence Relations
Recursion
Ali Alilooee
Recursive
Assume f (n) ≥ 0 and T (1) > 0.
problems
Substitution T (n) = 2T (n − 1) + f (n); T (n) ∈ Ω(2n )
method
Basic T (n) = 3T (n − 1) + f (n); T (n) ∈ Ω(3n )
recursive
algorithms T (n) = 4T (n − 1) + f (n); T (n) ∈ Ω(4n )
Recursion
trees method
n
Some
formulas for
T (n) = T (n − 1) + T (n − 2) + f (n); T (n) ∈ Ω(2 2 )
recurrences n
T (n) = T (n − 1) + T (n − 2) + T (n − 3) + f (n); T (n) ∈ Ω(2 2 )
Master Theorem
Recursion
Recursion
Recursion
Recursion