Professional Documents
Culture Documents
COMP 6651 Lecture1 Winter 2022
COMP 6651 Lecture1 Winter 2022
COMP 6651 Lecture1 Winter 2022
Dr. B. Jaumard
January 7, 2022
Outline
1 General Remarks
2 Getting Started
3 Growth of Functions
Asymptotic Notation
Examples
Simplifications of expressions of asymptotic analysis
Performance analysis of an algorithm
4 Recurrence Equations
Definitions
Homogeneous, constant coefficients, order k
Non homogeneous, constant coefficients, order k
The master theorem
Divide-and-conquer algorithms
5 Computing Complexity
6 Ref.
References
General Remarks Getting Started Growth of Functions Recurrence Equations Computing Complexity Ref.
Prerequisites
Prerequisites
COMP 5361: Discrete Structures and Formal Languages
COMP 5511: Principles of Data Structures
You are expected to have a good working knowledge of
the material you were taught in those courses:
sets, relations, functions, logic, proof techniques,
graph theory,
counting techniques, permutations and combinations,
binary search trees, hashing, stacks and queues, sorting.
You are expected to be able to write programs using either
C++ or Java
It will be difficult to follow or appreciate this course without
a proper background preparation.
Getting Started
Examples
Sort n items: if all items are approximately of the same
fixed size, n is a measure of the input size.
Find whether an integer i is a prime number: input size is
the number of digits in i.
Algorithm Analysis: Predict the resources that the
algorithm requires.
Empirical Methods: Run experiments and measure the
time and space.
Analytical Methods: Analyze the structure of an
algorithm and derive, using mathematical methods, the
time and space needed.
We will focus on analytical methods.
Examples
2n2 + 5n − 6 = O(2n )
2n2 + 5n − 6 = O(n3 )
2n2 + 5n − 6 = O(n2 )
2n2 + 5n − 6 6= O(n)
2n2 + 5n − 6 6= Θ(2n )
2n2 + 5n − 6 6= Θ(n3 )
2n2 + 5n − 6 = Θ(n2 )
2n2 + 5n − 6 6= Θ(n)
Examples (con’t)
2n2 + 5n − 6 6= Ω(2n )
2n2 + 5n − 6 6= Ω(n3 )
2n2 + 5n − 6 = Ω(n2 )
2n2 + 5n − 6 = Ω(n)
2n2 + 5n − 6 = o(2n )
2n2 + 5n − 6 = o(n3 )
2n2 + 5n − 6 6= o(n2 )
2n2 + 5n − 6 6= o(n)
Operations
c is a constant:
O(f (n) + c) = O(f (n))
O(cf (n)) = O(f (n))
Using limits
g(n)
If lim =0
n→∞ f (n)
then g(n) = O(f (n)) and f (n) = Ω(g(n))
g(n)
If lim =∞
n→∞ f (n)
then g(n) = Ω(f (n)) and f (n) = O(g(n))
g(n)
If lim = c where c is a nonzero constant
n→∞ f (n)
then f (n) = Θ(g(n)) and g(n) = Θ(f (n))
Binary search
Examples
Characteristic equation: r 2 = r + 2
Two distinct roots: r1 = 2, r2 = −1
General form of the solution: an = α1 2n + α2 (−1)n
α1 and α2 satisfy:
{
α1 × 20 + α2 × (−1)0 = 2 (n = 0)
α1 × 21 + α2 × (−1)1 = 7 (n = 1)
{
α1 + α2 = 2 (n = 0)
⇔ ⇒ α1 = 3 and α2 = −1
2α1 − α2 = 7 (n = 1)
Solution is {an } where an = 3 × 2n − (−1)n
r 4 − 2r 3 + 2r − 1 = 0 is equivalent to (r − 1)3 (r + 1) = 0.
...
f (n) = βn
(p )
search an under the form: Cnk βn where
C is a constant
if β is a root of the characteristic equation
then k is the multiplicity of β,
else k = 0 with the convention n0 = 1
Binary search
Suppose that the number of
IF the array has one element THEN
elements n = 2k
compare x to that element and return the
appropriate answer The required number of
ELSE comparisons using the binary
get the element M in the ’middle’ of the array; search, dn is defined by:
IF x ≥ M THEN
call the algorithm recursively on the ’second half’ d1 = 1
of the array
dn = dn/2 + 1 (n ≥ 2)
ELSE
call the algorithm recursively on the ’first half’ of
the array.
How to solve it ?
d1 = 1 and dn = dn/2 + 1, (n ≥ 2)
Nonlinear recurrence equation !
Linearized using a change of variable: n = 2k ,→ ak = d2k
ak = ak −1 + 1 k ≥ 1, a0 = 1
This is a linear non homogeneous recurrence equation with f (n) = 1
1a. Homogeneous part: ak − ak −1 = 0 (h ) (p )
3. ak = ak + ak = k + C1
1b. Characteristic Equation: r − 1 = 0, r = 1 4a. Satisfying initial condition
(h ) a0 = 1 = 0 + C1 , C1 = 1.
1c. ak = C1 (r )k = C1 (1)k = C1 is the generic
solution for the homogeneous part 4b. Finally ak = k + 1.
(p ) 4c. d2k = k + 1, recall that n = 2k , i.e,
2a. A particular solution: ak = Bk + C
k = log2 n
2b. Constant values of the particular solution:
(p ) (p ) (p ) 4d. dn = log2 n + 1 = O(log n)
ak must satisfy: ak = ak −1 + 1 ,i.e.,
Bk + C = B(k − 1) + C + 1 =⇒ B = 1, we
(p )
take C = 0. ak =k
COMP 6651 / Winter 2022 , Dr. B. Jaumard 29
General Remarks Getting Started Growth of Functions Recurrence Equations Computing Complexity Ref.
Some Justifications
(h )
Step 1. General solution of the homogeneous part ak : wait for
Step 4 before computing the values of the constants
Step 2. Particular solution of the non homogeneous equation
Search for a polynomial of degree 1, as the ”f (k )”
term is a polynomial of degree 0, and 1 is a root of
the characteristic equation with multiplicity 1
Any value of C is valid, therefore we select the
(p )
value leading to the simplest expression of ak
(h ) (p )
Step 3. Superposition of the two solutions: ak + ak
Step 4. We need first to compute the constant values using the
initial conditions, and come back to the variables of the
initial equation.
Theorem
Let a ≥ 1 and b > 1 be constants, let f (n) be a function, and let
T (n) be defined on the nonnegative integers by the recurrence
Theorem
Let a ≥ 1 and b ≥ 2 be integer constants, let c and k be positive
constants, and let T (n) be defined by the recurrence
T (n) = aT (n/b) + nk .
Theorem
Example 1
Let a ≥ 1 and b ≥ 2 be integer
constants, let c and k be positive T1 (n) = 7T1 (n/2) + n2
constants, and let T (n) be defined
T1 (n) = Θ(nlog2 7 ) = Θ(n2.81 )
by the recurrence
Divide-and-conquer algorithms
a = 1, b = 2
BinSearch (x , A, i , j)
cost for breaking a problem into
subproblem: D(n) = Θ(1)
// search where x is in array
// between A[i] and A[j]
cost for combining solutions of
subproblems into a solution of
IF (i > j) return -1; the problem: C(n) = Θ(1)
mid ←− (i + j)/2;
IF (x < A[mid]) return
T (n) = T (n/2) + C(n) if n ≥ 1
BinSearch(x , A, i , mid − 1);
ELSE IF (x > A[mid]) return
BinSearch(x , A, mid + 1, j); T (n) = Θ(log n)
ELSE return mid;
a = 2, b = 2
Sequence of Statements
Sequence of statements
statement 1;
statement 2;
...
statement k ;
To compute the total runtime ⇝ add the times for all statements:
Total runtime = time(statement 1) + time(statement 2)
+ ... + time(statement k )
If each statement is "simple" (only involves basic operations)
then the time for each statement is constant and the total time
is also constant: O(1).
if-then-else statements
if (condition) {
sequence 1 of statements
}
else {
sequence 2 of statements
}
Loops
for (i = 1 to N) {
sequence of statements
}
for (i = 0 to N) {
for (j = 0 to M) {
sequence of statements
}
}
p
T (n) = 2T ( n) + 1 (1)
T (1) = 1. (2)