Lec03-Nonrec Analysis PDF
Lec03-Nonrec Analysis PDF
Winter 2019
Andrew P. Black
run time
T(n) ≈ cop ⨉ C(n)
"2
Analysis of time efficiency
! Time efficiency is analyzed by determining
the number of repetitions of the “basic
operation”
! Almost always depends on the size of the
input
! “Basicoperation”: the operation that
contributes most towards the running time
of the algorithm cost of basic
op: constant
run time
T(n) ≈ cop ⨉ C(n)
"2
Analysis of time efficiency
! Time efficiency is analyzed by determining
the number of repetitions of the “basic
operation”
! Almost always depends on the size of the
input
! “Basicoperation”: the operation that
contributes most towards the running time
of the algorithm cost of basic
op: constant number
of times basic op
run time is executed
T(n) ≈ cop ⨉ C(n)
"2
Problem Input size measure Basic operation
Multiplication of two
matrices
Checking primality of
a given integer n
"3
Complete the table
Problem Input size measure Basic operation
B: Matrix dimension, or
Multiplication of two B: Multiplication of
total number of
matrices two numbers
elements
"4
Problem Input size measure Basic operation
B: Matrix dimension, or
total number of
elements
C: size of n = number of
digits
D: #vertices and/or
edges
"5
Problem Input size measure Basic operation
B: Multiplication of
two numbers
C: Division
D: Visiting a vertex or
traversing an edge
"6
Problem Input size measure Basic operation
A: Number of list’s
items, i.e. n
B: Matrix dimension, or
Multiplication of two
total number of
matrices
elements
C: size of n = number of
digits
D: #vertices and/or
edges
"7
Problem Input size measure Basic operation
A: Key comparison
C: Division
D: Visiting a vertex or
traversing an edge
"8
Problem Input size measure Basic operation
A: Number of list’s
items, i.e. n
B: Matrix dimension, or
total number of
elements
D: #vertices and/or
edges
"9
Problem Input size measure Basic operation
A: Key comparison
B: Multiplication of
two numbers
Checking primality of
C: Division
a given integer n
D: Visiting a vertex or
traversing an edge
"10
Problem Input size measure Basic operation
A: Number of list’s
items, i.e. n
B: Matrix dimension, or
total number of
elements
C: size of n = number of
digits
"11
Problem Input size measure Basic operation
A: Key comparison
B: Multiplication of
two numbers
C: Division
"12
Problem Input size measure Basic operation
Multiplication of two
matrices
Checking primality of
a given integer n
"13
Best-case, average-case, worst-case
! For some algorithms, efficiency depends on the input:
! Worst case: Cworst(n) – maximum over inputs of size n
! Best case: Cbest(n) – minimum over inputs of size n
! Average case: Cavg(n) – “average” over inputs of size n
" Number of times the basic operation will be executed on
typical input
# Not the average of worst and best case
" Expected number of basic operations under some
assumption about the probability distribution of all
possible inputs
"14
Discuss:
B. linear — O(n)
C. quadratic — O(n2)
"15
Discuss:
B. linear — O(n)
C. quadratic — O(n2)
"16
Discuss:
B. linear — O(n)
C. quadratic — O(n2)
"17
General Plan for Analysis of
non-recursive algorithms
1. Decide on parameter n indicating input size
"18
analyzing such algorithms.
EXAMPLE 1 Consider the problem of finding the value of the largest element
“Basic Operation”
in a list of n numbers. For simplicity, we assume that the list is implemented as
an array. The following is pseudocode of a standard algorithm for solving the
problem.
The obvious measure of an input’s size here is the number of elements in the
array,!
Why choose > as the basic operation?
i.e., n. The operations that are going to be executed most often are in the
algorithm’s for loop. There are two operations in the loop’s body: the comparison
A[i] > maxval and the assignment maxval ← A[i]. Which of these two operations
should we " Why not i
consider basic? Since the← i+1?
comparison is executed on each repetition
of the loop and the assignment is not, we should consider the comparison to be
Or [ ] ?
" basic operation. Note that the number of comparisons will be the
the algorithm’s
same for all arrays of size n; therefore, in terms of this metric, there is no need to
distinguish among the worst, average, and best cases here.
"19
Same Algorithm:
ALGORITHM MaxElement (A: List)
// Determines the value of the largest element in the list A
// Input: a list A of real numbers
// Output: the value of the largest element of A
maxval ← A.first
for each in A do
if each > maxval
maxval ← each
return maxval
"20
From Algorithm to Formula
! We want a formula for the # of basic ops
! Basic op will normally be in inner loop
3 basic operations
h
X
!
3
<latexit sha1_base64="PgrGfqzWsuLxxVb2ld/iMhb1CnM=">AAAB+XicdZDLSgMxFIYz9VbrbdSlm2ARXJVMbel0IRTduKxgL9COQyZN29BkZkgyhTLMm7hxoYhb38Sdb2N6EVT0h8DHf87hnPxBzJnSCH1YubX1jc2t/HZhZ3dv/8A+PGqrKJGEtkjEI9kNsKKchbSlmea0G0uKRcBpJ5hcz+udKZWKReGdnsXUE3gUsiEjWBvLt+2+SoSfskue3afjDF74dhGVylVUdxE0UENV1zVQrdRRGUGnhBYqgpWavv3eH0QkETTUhGOleg6KtZdiqRnhNCv0E0VjTCZ4RHsGQyyo8tLF5Rk8M84ADiNpXqjhwv0+kWKh1EwEplNgPVa/a3Pzr1ov0UPXS1kYJ5qGZLlomHCoIziPAQ6YpETzmQFMJDO3QjLGEhNtwiqYEL5+Cv+HdrnkGL6tFBtXqzjy4AScgnPggBpogBvQBC1AwBQ8gCfwbKXWo/VivS5bc9Zq5hj8kPX2Ccikk8A=</latexit>
i=l
"21
Works for nested loops too
0 1
n
X2 n
X1
@ 1A
i=0 j=i+1
<latexit sha1_base64="Vg9ZBRgSeUB979vv8vT2hoziRi8=">AAACH3icdZDLSgMxFIYzXmu9VV26CRZBEUumWG0XgujGpYJVoVNLJs20sZnMkJwRyjBv4sZXceNCEXHn25heBBU9EPj4/3M4Ob8fS2GAkA9nYnJqemY2N5efX1hcWi6srF6aKNGM11kkI33tU8OlULwOAiS/jjWnoS/5ld87GfhXd1wbEakL6Me8GdKOEoFgFKzUKux7JglbqTgk2U2qdssZ9iQPAG/hkXF7KHbcoeVm2MWeFp0u4O1WoUhK5QqpVQm2cEAq1aqFyl6NlAl2S2RYRTSus1bh3WtHLAm5AiapMQ2XxNBMqQbBJM/yXmJ4TFmPdnjDoqIhN810eF+GN63SxkGk7VOAh+r3iZSGxvRD33aGFLrmtzcQ//IaCQTVZipUnABXbLQoSCSGCA/Cwm2hOQPZt0CZFvavmHWppgxspHkbwtel+H+4LJdcy+d7xaPjcRw5tI420BZy0QE6QqfoDNURQ/foET2jF+fBeXJenbdR64QznllDP8r5+ASTcaFr</latexit>
"22
Works for nested loops too
0 1
n
X2 n
X1
@ 1A
i=0 j=i+1
<latexit sha1_base64="Vg9ZBRgSeUB979vv8vT2hoziRi8=">AAACH3icdZDLSgMxFIYzXmu9VV26CRZBEUumWG0XgujGpYJVoVNLJs20sZnMkJwRyjBv4sZXceNCEXHn25heBBU9EPj4/3M4Ob8fS2GAkA9nYnJqemY2N5efX1hcWi6srF6aKNGM11kkI33tU8OlULwOAiS/jjWnoS/5ld87GfhXd1wbEakL6Me8GdKOEoFgFKzUKux7JglbqTgk2U2qdssZ9iQPAG/hkXF7KHbcoeVm2MWeFp0u4O1WoUhK5QqpVQm2cEAq1aqFyl6NlAl2S2RYRTSus1bh3WtHLAm5AiapMQ2XxNBMqQbBJM/yXmJ4TFmPdnjDoqIhN810eF+GN63SxkGk7VOAh+r3iZSGxvRD33aGFLrmtzcQ//IaCQTVZipUnABXbLQoSCSGCA/Cwm2hOQPZt0CZFvavmHWppgxspHkbwtel+H+4LJdcy+d7xaPjcRw5tI420BZy0QE6QqfoDNURQ/foET2jF+fBeXJenbdR64QznllDP8r5+ASTcaFr</latexit>
"22
Works for nested loops too
0 1
n
X2 n
X1
@ 1A
i=0 j=i+1
<latexit sha1_base64="Vg9ZBRgSeUB979vv8vT2hoziRi8=">AAACH3icdZDLSgMxFIYzXmu9VV26CRZBEUumWG0XgujGpYJVoVNLJs20sZnMkJwRyjBv4sZXceNCEXHn25heBBU9EPj4/3M4Ob8fS2GAkA9nYnJqemY2N5efX1hcWi6srF6aKNGM11kkI33tU8OlULwOAiS/jjWnoS/5ld87GfhXd1wbEakL6Me8GdKOEoFgFKzUKux7JglbqTgk2U2qdssZ9iQPAG/hkXF7KHbcoeVm2MWeFp0u4O1WoUhK5QqpVQm2cEAq1aqFyl6NlAl2S2RYRTSus1bh3WtHLAm5AiapMQ2XxNBMqQbBJM/yXmJ4TFmPdnjDoqIhN810eF+GN63SxkGk7VOAh+r3iZSGxvRD33aGFLrmtzcQ//IaCQTVZipUnABXbLQoSCSGCA/Cwm2hOQPZt0CZFvavmHWppgxspHkbwtel+H+4LJdcy+d7xaPjcRw5tI420BZy0QE6QqfoDNURQ/foET2jF+fBeXJenbdR64QznllDP8r5+ASTcaFr</latexit>
"22
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1
Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai
Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1
Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai
Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1
Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai
Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1
Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai
Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1
Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai
Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1
Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai
Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1
Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai
Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1
Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai
Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1
Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai
Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1
Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai
Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Where do the Summation formulae come
from?
! Answer:mathematics.
! Example:
n
!
X 1
= lim ln n
n!1
i=1
i
"24
Where do the Summation formulae come
from?
! Answer:mathematics.
! Example:
n
!
X 1
Harmonic = lim ln n
series n!1
i=1
i
"24
Where do the Summation formulae come
from?
! Answer:mathematics.
! Example:
n
!
X 1
Harmonic = lim ln n
series n!1
i=1
i
y = 1/x
"24
Where do the Summation formulae come
from?
! Answer:mathematics.
! Example:
n
!
X 1
Harmonic = lim ln n
series n!1
i=1
i
y = 1/x
ln x
"24
What does Levitin’s ⇡ mean?
! “becomes almost equal to as n → ∞”
! So formula 8
nX
lg i ⇡ n lg n
i=1
" means
n
!
X
lim lg i n lg n =0
n!1
i=1
"25
Example: Counting Binary Digits
"26
Example: Counting Binary Digits
"26
Example: Counting Binary Digits
a. 1 + 3 + 5 + 7 + ... + 999
b. 2 + 4 + 8 + 16 + ... + 1024
!n+1 !n+1 !n−1
c. i=3 1 d. i=3 i e. i=0 i(i + 1)
!n j+1
!n !n P n
!n−1
f. j=1 3 g. i=1 j=1 ij h. i=1
i=0 1/i(i + 1)
2. Find the order of growth of the following sums.
!n−1 2 2
!n−1
a. i=0 (i +1) b. i=2 lgi2
"27
Ex!2.3, Problem 2
b. 2 + 4 + 8 + 16 + ... + 1024
Use the Θ(g(n)) notation with the simplest function g(n) possible.
3. The sample variance of n measurements x1 , x2 , ..., xn can be compute
!n 2
!n
i=1 (xi − x̄) i=1 xi
where x̄ =
n−1 n
or !n !n "28
2 2
i=1 xi − ( i=1 xi ) /n
a. i=0 (i2 +1)2 b. i=2 lgi2
Ex 2.3, Problem 3
!n i−1
!n−1 !i−1
c. i=1 (i + 1)2 d. i=0 j=0 (i + j)
Use the Θ(g(n)) notation with the simplest function g(n) possible.
3. The sample variance of n measurements x1 , x2 , ..., xn can be computed as
!n 2
!n
i=1 (xi − x̄) i=1 xi
where x̄ =
n−1 n
or !n !n
2 2
i=1 xi − ( i=1 xi ) /n
.
n−1
Find and compare the number of divisions, multiplications, and addi-
tions/subtractions (additions and subtractions are usually bunched to-
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.
Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.
Algorithm Mystery( n)
//Input: A nonnegative integer n
S←0
for i ← 1 to n do
S ←S+i∗i
returnS
Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.
Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.
Algorithm Mystery( n)
//Input: A nonnegative integer n
S←0
for i ← 1 to n do
S ←S+i∗i
returnS
Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.
Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.
How many times is the
Algorithm Mystery( n) basic operation executed?
//Input: A nonnegative integer n
S←0
for i ← 1 to n do
A. once
S ←S+i∗i
returnS B. n times
a. What does this algorithm compute?
C. lg n times
b. What is its basic operation?
D. none of the above
c. How many times is the basic operation executed?
Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.
What is the efficiency
class of this algorithm?
Algorithm Mystery( n)
//Input: A nonnegative integer n [b is # of bits needed to
S←0 represent n]
for i ← 1 to n do
S ←S+i∗i
returnS A. ⇥(1)
B.
a. What does this algorithm compute? ⇥(n)
b. What is its basic operation?
C. ⇥(b)
c. How many times is the basic operation executed?
D. ⇥(2b )
d. What is the efficiency class of this algorithm?
16
e. Suggest an improvement or a better algorithm altogether and indi- "33
cate its efficiency class. If you cannot do it, try to prove that, in fact, it
Ex 2.3, Problem 4 (cont)
d. What is the efficiency class of this algorithm?
Ex 2.3, Problem 9
8. Determine the asymptotic order of growth for the total number of times all
the doors are toggled in the Locker Doors puzzle (Problem 11 in Exercises
1.1).
9. Prove the formula
n
! n(n + 1)
i = 1 + 2 + ... + n =
i=1
2
17
"36
Ex 2.3, Problem 11
10. Consider the following version of an important algorithm that we will
study later in the book.
Algorithm GE (A[0..n − 1, 0..n])
//Input: An n-by-n + 1 matrix A[0..n − 1, 0..n] of real numbers
for i ← 0 to n − 2 do
for j ← i + 1 to n − 1 do
for k ← i to n do
A[j, k] ← A[j, k] − A[i, k] ∗ A[j, i] / A[i, i]
a.◃ Find the time efficiency class of this algorithm.
a. Find the time efficiency class of this algorithm
b.◃ What glaring inefficiency does this pseudocode contain and how can
b. beWhat
it glaringtoinefficiency
eliminated does this
speed the algorithm code contain, and how
up?
can it be eliminated?
11. von Neumann’s neighborhood How many one-by-one squares are gener-
c. Estimate
ated the reduction
by the algorithm in runwith
that starts time.
a single square square and on each
of its n iterations adds new squares all round the outside. How many
one-by-one squares are generated on the nth iteration? [Gar99, p.88] (In
"37
the parlance of cellular automata theory, the answer is the number of cells
a.◃ Find the time efficiency class of this algorithm.
How
11. von many one-by-one
Neumann’s neighborhoodsquares
How manyare one-by-one
generatedsquares
by theare gener-
ofalgorithm that adds
startsnew
with a single square,
the and on each
How of its
ated by the algorithm that starts with a single square square and on each
its n iterations squares all round outside. many
n iterations
one-by-one adds
squares arenew squares
generated around
on the the outside.
nth iteration? How
[Gar99, many
p.88] (In
one-by-one
the squares
parlance of cellular are generated
automata theory, theon the nisththe
answer iteration?
number ofHere
cells
are2 the neighborhoods
below: for n = 0, 1, and 2.
in the von Neumann neighborhood of range n.) The results for n = 0, 1,
and are illustrated