NEW SOUTH WALES
Algorithms: COMP3121/9101
School of Computer Science and Engineering University of New South Wales
3. RECURRENCES – part A
COMP3121/3821/9101/9801 1 / 22
Asymptotic notation
“Big Oh” notation: f(n) = O(g(n)) is an abbreviation for: “There exist positive constants c and n0 such that
0≤f(n)≤cg(n) for all n≥n0”.
COMP3121/3821/9101/9801 2 / 22
Asymptotic notation
“Big Oh” notation: f(n) = O(g(n)) is an abbreviation for: “There exist positive constants c and n0 such that
0≤f(n)≤cg(n) for all n≥n0”.
In this case we say that g(n) is an asymptotic upper bound for f (n).
COMP3121/3821/9101/9801
2 / 22
Asymptotic notation
“Big Oh” notation: f(n) = O(g(n)) is an abbreviation for: “There exist positive constants c and n0 such that
0≤f(n)≤cg(n) for all n≥n0”.
In this case we say that g(n) is an asymptotic upper bound for
f (n).
f(n) = O(g(n)) means that f(n) does not grow substantially faster
than g(n) because a multiple of g(n) eventually dominates f(n).
COMP3121/3821/9101/9801 2 / 22
Asymptotic notation
“Big Oh” notation: f(n) = O(g(n)) is an abbreviation for: “There exist positive constants c and n0 such that
0≤f(n)≤cg(n) for all n≥n0”.
In this case we say that g(n) is an asymptotic upper bound for
f (n).
f(n) = O(g(n)) means that f(n) does not grow substantially faster
than g(n) because a multiple of g(n) eventually dominates f(n). Clearly, multiplying constants c of interest will be larger than 1,
thus “enlarging” g(n).
COMP3121/3821/9101/9801 2 / 22
Asymptotic notation
“Omega” notation: f(n) = Ω(g(n)) is an abbreviation for: “There exists positive constants c and n0 such that
0≤cg(n)≤f(n) for all n≥n0.”
COMP3121/3821/9101/9801 3 / 22
Asymptotic notation
“Omega” notation: f(n) = Ω(g(n)) is an abbreviation for: “There exists positive constants c and n0 such that
0≤cg(n)≤f(n) for all n≥n0.”
In this case we say that g(n) is an asymptotic lower bound for f (n).
COMP3121/3821/9101/9801
3 / 22
Asymptotic notation
“Omega” notation: f(n) = Ω(g(n)) is an abbreviation for: “There exists positive constants c and n0 such that
0≤cg(n)≤f(n) for all n≥n0.”
In this case we say that g(n) is an asymptotic lower bound for
f (n).
f(n) = Ω(g(n)) essentially says that f(n) grows at least as fast as
g(n), because f(n) eventually dominates a multiple of g(n).
COMP3121/3821/9101/9801 3 / 22
Asymptotic notation
“Omega” notation: f(n) = Ω(g(n)) is an abbreviation for: “There exists positive constants c and n0 such that
0≤cg(n)≤f(n) for all n≥n0.”
In this case we say that g(n) is an asymptotic lower bound for
f (n).
f(n) = Ω(g(n)) essentially says that f(n) grows at least as fast as
g(n), because f(n) eventually dominates a multiple of g(n). Since c g(n) ≤ f(n) if and only if g(n) ≤ 1c f(n), we have
f(n) = Ω(g(n)) if and only if g(n) = O(f(n)).
COMP3121/3821/9101/9801 3 / 22
Asymptotic notation
“Omega” notation: f(n) = Ω(g(n)) is an abbreviation for: “There exists positive constants c and n0 such that
0≤cg(n)≤f(n) for all n≥n0.”
In this case we say that g(n) is an asymptotic lower bound for
f (n).
f(n) = Ω(g(n)) essentially says that f(n) grows at least as fast as
g(n), because f(n) eventually dominates a multiple of g(n). Since c g(n) ≤ f(n) if and only if g(n) ≤ 1c f(n), we have
f(n) = Ω(g(n)) if and only if g(n) = O(f(n)).
“Theta” notation: f(n) = Θ(g(n)) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)); thus, f(n) and g(n) have the same asymptotic growth rate.
COMP3121/3821/9101/9801 3 / 22
Recurrences
Recurrences are important to us because they arise in estimations of time complexity of divide-and-conquer algorithms.
COMP3121/3821/9101/9801 4 / 22
Recurrences
Recurrences are important to us because they arise in estimations of time complexity of divide-and-conquer algorithms.
Merge-Sort(A, p, r)
1 ifp
COMP3121/3821/9101/9801 5 / 22
Recurrences
Let a ≥ 1 be an integer and b > 1 a real number; Assume that a divide-and-conquer algorithm:
COMP3121/3821/9101/9801 5 / 22
Recurrences
Let a ≥ 1 be an integer and b > 1 a real number; Assume that a divide-and-conquer algorithm:
reduces a problem of size n to a many problems of smaller size n/b;
COMP3121/3821/9101/9801 5 / 22
Recurrences
Let a ≥ 1 be an integer and b > 1 a real number; Assume that a divide-and-conquer algorithm:
reduces a problem of size n to a many problems of smaller size n/b; the overhead cost of splitting up/combining the solutions for size n/b into a solution for size n is if f(n),
COMP3121/3821/9101/9801 5 / 22
Recurrences
Let a ≥ 1 be an integer and b > 1 a real number; Assume that a divide-and-conquer algorithm:
reduces a problem of size n to a many problems of smaller size n/b; the overhead cost of splitting up/combining the solutions for size n/b into a solution for size n is if f(n),
then the time complexity of such algorithm satisfies
COMP3121/3821/9101/9801 5 / 22
Recurrences
Let a ≥ 1 be an integer and b > 1 a real number; Assume that a divide-and-conquer algorithm:
reduces a problem of size n to a many problems of smaller size n/b; the overhead cost of splitting up/combining the solutions for size n/b into a solution for size n is if f(n),
then the time complexity of such algorithm satisfies T (n) = a T n + f (n)
b
COMP3121/3821/9101/9801 5 / 22
Recurrences
Let a ≥ 1 be an integer and b > 1 a real number; Assume that a divide-and-conquer algorithm:
reduces a problem of size n to a many problems of smaller size n/b; the overhead cost of splitting up/combining the solutions for size n/b into a solution for size n is if f(n),
then the time complexity of such algorithm satisfies T (n) = a T n + f (n)
b
Note: we should be writing
COMP3121/3821/9101/9801
5 / 22
Recurrences
Let a ≥ 1 be an integer and b > 1 a real number; Assume that a divide-and-conquer algorithm:
reduces a problem of size n to a many problems of smaller size n/b; the overhead cost of splitting up/combining the solutions for size n/b into a solution for size n is if f(n),
then the time complexity of such algorithm satisfies T (n) = a T n + f (n)
b
Note: we should be writing
T (n) = a T n + f (n)
b
COMP3121/3821/9101/9801
5 / 22
Recurrences
Let a ≥ 1 be an integer and b > 1 a real number; Assume that a divide-and-conquer algorithm:
reduces a problem of size n to a many problems of smaller size n/b; the overhead cost of splitting up/combining the solutions for size n/b into a solution for size n is if f(n),
then the time complexity of such algorithm satisfies T (n) = a T n + f (n)
b
Note: we should be writing
T (n) = a T n + f (n)
b
but it can be shown that ignoring the integer parts and additive constants is OK when it comes to obtaining the asymptotics.
COMP3121/3821/9101/9801 5 / 22
T (n) = a T n + f (n) b
size of instance = n size of instances = n/b
size of instances = n/b2 .
a many instances
…
….. ….. …
…
…
…. .
. .
depth of recursion: log! 𝑛
……
size of instances = 1
COMP3121/3821/9101/9801
6 / 22
Some recurrences can be solved explicitly, but this tends to be tricky.
COMP3121/3821/9101/9801 7 / 22
Some recurrences can be solved explicitly, but this tends to be tricky.
Fortunately, to estimate efficiency of an algorithm we do not need the exact solution of a recurrence
COMP3121/3821/9101/9801 7 / 22
Some recurrences can be solved explicitly, but this tends to be tricky.
Fortunately, to estimate efficiency of an algorithm we do not need the exact solution of a recurrence
We only need to find:
COMP3121/3821/9101/9801 7 / 22
Some recurrences can be solved explicitly, but this tends to be tricky.
Fortunately, to estimate efficiency of an algorithm we do not need the exact solution of a recurrence
We only need to find:
1 the growth rate of the solution i.e., its asymptotic behaviour;
COMP3121/3821/9101/9801 7 / 22
Some recurrences can be solved explicitly, but this tends to be tricky.
Fortunately, to estimate efficiency of an algorithm we do not need the exact solution of a recurrence
We only need to find:
1 the growth rate of the solution i.e., its asymptotic behaviour;
2 the (approximate) sizes of the constants involved (more about
that later)
COMP3121/3821/9101/9801 7 / 22
Some recurrences can be solved explicitly, but this tends to be tricky.
Fortunately, to estimate efficiency of an algorithm we do not need the exact solution of a recurrence
We only need to find:
1 the growth rate of the solution i.e., its asymptotic behaviour;
2 the (approximate) sizes of the constants involved (more about
that later)
This is what the Master Theorem provides (when it is applicable).
COMP3121/3821/9101/9801 7 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
COMP3121/3821/9101/9801 8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real; f(n) > 0 be a non-decreasing function;
COMP3121/3821/9101/9801 8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f(n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
COMP3121/3821/9101/9801
8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f(n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
COMP3121/3821/9101/9801
8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f(n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
1 If f(n) = O(nlogb a−ε) for some ε > 0,
COMP3121/3821/9101/9801
8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f(n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
1 If f(n) = O(nlogb a−ε) for some ε > 0, then T(n) = Θ(nlogb a);
COMP3121/3821/9101/9801
8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f(n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
1 If f(n) = O(nlogb a−ε) for some ε > 0, then T(n) = Θ(nlogb a);
2 If f(n) = Θ(nlogb a),
COMP3121/3821/9101/9801
8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f(n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
1 If f(n) = O(nlogb a−ε) for some ε > 0, then T(n) = Θ(nlogb a);
2 If f(n) = Θ(nlogb a), then T(n) = Θ(nlogb a log2 n);
COMP3121/3821/9101/9801
8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f(n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
1 If f(n) = O(nlogb a−ε) for some ε > 0, then T(n) = Θ(nlogb a);
2 If f(n) = Θ(nlogb a), then T(n) = Θ(nlogb a log2 n);
3 If f(n) = Ω(nlogb a+ε) for some ε > 0,
COMP3121/3821/9101/9801
8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f(n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
1 If f(n) = O(nlogb a−ε) for some ε > 0, then T(n) = Θ(nlogb a);
2 If f(n) = Θ(nlogb a), then T(n) = Θ(nlogb a log2 n);
3 Iff(n)=Ω(nlogba+ε)forsomeε>0,andforsomec<1andsomen0,
holds for all n > n0,
af (n/b) ≤ cf(n)
COMP3121/3821/9101/9801
8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f(n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
1 If f(n) = O(nlogb a−ε) for some ε > 0, then T(n) = Θ(nlogb a);
2 If f(n) = Θ(nlogb a), then T(n) = Θ(nlogb a log2 n);
3 Iff(n)=Ω(nlogba+ε)forsomeε>0,andforsomec<1andsomen0,
af (n/b) ≤ cf(n) holds for all n > n0, then T(n) = Θ(f(n));
COMP3121/3821/9101/9801
8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f(n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
1 If f(n) = O(nlogb a−ε) for some ε > 0, then T(n) = Θ(nlogb a);
2 If f(n) = Θ(nlogb a), then T(n) = Θ(nlogb a log2 n);
3 Iff(n)=Ω(nlogba+ε)forsomeε>0,andforsomec<1andsomen0,
af (n/b) ≤ cf(n) holds for all n > n0, then T(n) = Θ(f(n));
4 If none of these conditions hold, the Master Theorem is NOT applicable.
COMP3121/3821/9101/9801
8 / 22
Master Theorem:
Let:
a ≥ 1 be an integer and and b > 1 a real;
f(n) > 0 be a non-decreasing function;
T (n) be the solution of the recurrence T (n) = a T (n/b) + f (n);
Then:
1 If f(n) = O(nlogb a−ε) for some ε > 0, then T(n) = Θ(nlogb a);
2 If f(n) = Θ(nlogb a), then T(n) = Θ(nlogb a log2 n);
3 Iff(n)=Ω(nlogba+ε)forsomeε>0,andforsomec<1andsomen0,
af (n/b) ≤ cf(n) holds for all n > n0, then T(n) = Θ(f(n));
4 If none of these conditions hold, the Master Theorem is NOT applicable.
(But often the proof of the Master Theorem can be tweaked to obtain the asymptotic of the solution T(n) in such a case when the Master Theorem does not apply; an example is T (n) = 2T (n/2) + n log n).
COMP3121/3821/9101/9801 8 / 22
Master Theorem – a remark
Note that for any b > 1,
logb n = logb 2 log2 n;
COMP3121/3821/9101/9801 9 / 22
Master Theorem – a remark
Note that for any b > 1,
logb n = logb 2 log2 n;
Since b > 1 is constant (does not depend on n), we have for c = logb 2 > 0
logbn=c log2n; log2 n = 1c logb n;
COMP3121/3821/9101/9801
9 / 22
Master Theorem – a remark
Note that for any b > 1,
logb n = logb 2 log2 n;
Since b > 1 is constant (does not depend on n), we have for c = logb 2 > 0
Thus,
logbn=c log2n; log2 n = 1c logb n;
logb n = Θ(log2 n)
COMP3121/3821/9101/9801
9 / 22
Master Theorem – a remark
Note that for any b > 1,
logb n = logb 2 log2 n;
Since b > 1 is constant (does not depend on n), we have for c = logb 2 > 0
Thus, and also
logbn=c log2n; log2 n = 1c logb n;
logb n = Θ(log2 n) log2 n = Θ(logb n).
So whenever we have f = Θ(g(n) log n) we do not have to specify what base the log is – all bases produce equivalent asymptotic
estimates (but we do have to specify b in expressions such as nlogb a).
COMP3121/3821/9101/9801 9 / 22
Master Theorem – Examples
Let T (n) = 4 T (n/2) + n;
COMP3121/3821/9101/9801 10 / 22
Master Theorem – Examples
Let T (n) = 4 T (n/2) + n; then nlogb a = nlog2 4 = n2;
COMP3121/3821/9101/9801 10 / 22
Master Theorem – Examples
Let T (n) = 4 T (n/2) + n; then nlogb a = nlog2 4 = n2;
thus f(n) = n = O(n2−ε) for any ε < 1.
COMP3121/3821/9101/9801 10 / 22
Master Theorem - Examples
Let T (n) = 4 T (n/2) + n; then nlogb a = nlog2 4 = n2;
thus f(n) = n = O(n2−ε) for any ε < 1. Condition of case 1 is satisfied;
COMP3121/3821/9101/9801 10 / 22
Master Theorem - Examples
Let T (n) = 4 T (n/2) + n; then nlogb a = nlog2 4 = n2;
thus f(n) = n = O(n2−ε) for any ε < 1.
Condition of case 1 is satisfied; thus, T(n) = Θ(n2).
COMP3121/3821/9101/9801
10 / 22
Master Theorem - Examples
Let T (n) = 4 T (n/2) + n; then nlogb a = nlog2 4 = n2;
thus f(n) = n = O(n2−ε) for any ε < 1.
Condition of case 1 is satisfied; thus, T(n) = Θ(n2). Let T (n) = 2T (n/2) + 5 n;
COMP3121/3821/9101/9801
10 / 22
Master Theorem - Examples
Let T (n) = 4 T (n/2) + n; then nlogb a = nlog2 4 = n2;
thus f(n) = n = O(n2−ε) for any ε < 1.
Condition of case 1 is satisfied; thus, T(n) = Θ(n2). Let T (n) = 2T (n/2) + 5 n;
thennlogba =nlog22 =n1 =n;
COMP3121/3821/9101/9801
10 / 22
Master Theorem - Examples
Let T (n) = 4 T (n/2) + n; then nlogb a = nlog2 4 = n2;
thus f(n) = n = O(n2−ε) for any ε < 1.
Condition of case 1 is satisfied; thus, T(n) = Θ(n2). Let T (n) = 2T (n/2) + 5 n;
thennlogba =nlog22 =n1 =n;
thus f(n) = 5n = Θ(n) = Θ(nlog2 2).
COMP3121/3821/9101/9801
10 / 22
Master Theorem - Examples
Let T (n) = 4 T (n/2) + n; then nlogb a = nlog2 4 = n2;
thus f(n) = n = O(n2−ε) for any ε < 1.
Condition of case 1 is satisfied; thus, T(n) = Θ(n2). Let T (n) = 2T (n/2) + 5 n;
thennlogba =nlog22 =n1 =n;
thus f(n) = 5n = Θ(n) = Θ(nlog2 2).
Thus, condition of case 2 is satisfied;
COMP3121/3821/9101/9801
10 / 22
Master Theorem - Examples
Let T (n) = 4 T (n/2) + n; then nlogb a = nlog2 4 = n2;
thus f(n) = n = O(n2−ε) for any ε < 1.
Condition of case 1 is satisfied; thus, T(n) = Θ(n2).
Let T (n) = 2T (n/2) + 5 n;
thennlogba =nlog22 =n1 =n;
thus f(n) = 5n = Θ(n) = Θ(nlog2 2).
Thus, condition of case 2 is satisfied; and so,
T(n) = Θ(nlog2 2 logn) = Θ(nlogn). COMP3121/3821/9101/9801
10 / 22
Master Theorem - Examples
Let T (n) = 3 T (n/4) + n;
COMP3121/3821/9101/9801 11 / 22
Master Theorem - Examples
Let T (n) = 3 T (n/4) + n; then nlogb a = nlog4 3 < n0.8;
COMP3121/3821/9101/9801 11 / 22
Master Theorem - Examples
Let T (n) = 3 T (n/4) + n; then nlogb a = nlog4 3 < n0.8;
thus f(n) = n = Ω(n0.8+ε) for any ε < 0.2.
COMP3121/3821/9101/9801 11 / 22
Master Theorem - Examples
Let T (n) = 3 T (n/4) + n; then nlogb a = nlog4 3 < n0.8;
thus f(n) = n = Ω(n0.8+ε) for any ε < 0.2. Also, af(n/b) = 3f(n/4)
COMP3121/3821/9101/9801 11 / 22
Master Theorem - Examples
Let T (n) = 3 T (n/4) + n; then nlogb a = nlog4 3 < n0.8;
thus f(n) = n = Ω(n0.8+ε) for any ε < 0.2. Also,af(n/b)=3f(n/4)=3/4n
COMP3121/3821/9101/9801 11 / 22
Master Theorem – Examples
Let T (n) = 3 T (n/4) + n; then nlogb a = nlog4 3 < n0.8;
thus f(n) = n = Ω(n0.8+ε) for any ε < 0.2. Also,af(n/b)=3f(n/4)=3/4n
f(n) bi ai 1−c
⌊log n⌋−1
b n
19 / 22
Master Theorem Proof:
Case 3 (continued):
i ci
We got f(n/b ) ≤ ai f(n)
Thus,
i=0 Since we had (1):
⌊log n⌋−1 ⌊log n⌋−1
b nb ci
∞
aif
≤ ai i=0
f(n)
T(n) = Θ(f(n)) COMP3121/3821/9101/9801
f(n) bi ai 1−c
⌊log n⌋−1
b n
19 / 22
Master Theorem Proof: Homework
Exercise 1: Show that condition
f(n) = Ω(nlogb a+ε)
follows from the condition
af(n/b)≤cf(n) forsome0