Professional Documents
Culture Documents
1 / 55
Current semester map
Asymptotic notations
2 / 55
Topics cover today
Introduction
Analyzing loops
Analyzing single for-loops
Analysis of nested loops
Average-case analysis
Review on summations
3 / 55
Iteration versus recursion
4 / 55
Summations
You might think that summations are simple, not so much when
dealing with nested loops, which is where things get more interesting
and possibly more complicated...
5 / 55
Iterative Algorithms
whatsit(input of size n)
for (i = 5; i ≤ n; i + +) do
for (j = 1; j ≤ i; j + +) do
k = 3;
while k ≤ n do
- perform one elementary operation -
k = k ∗ 3;
end while
end for
end for
6 / 55
Counting basic operations of iterative algorithms
7 / 55
Single for-loops
for (i = a; i ≤ b; i + +) do
- execute c elementary operations -
end for
Let a = 1; b = 3
for (i = 1; i ≤ 3; i + +) do
- execute c elementary operations -
end for
8 / 55
Single for-loops : loop increment = 1
for (i = a; i ≤ b; i + +) do
- execute f (i) elementary operations -
end for
In this loop for each i between a and b, f (i) elementary operations are
executed
9 / 55
Single for-loops : loop increment = 1
10 / 55
Single for-loops : loop increment 6= 1
In the following single for loop, the index increment is x and for each
iteration step it executes f (i) elementary operations
for (i = a; i ≤ b; i + x)
- execute f (i) elementary operations -
end for
When the index increment differs from 1, the number of loop iterations
is no longer b − a where a and b are respectively the first and last
index of the loop
11 / 55
for (i = a; i ≤ b; i + x)
- execute f (i) elementary operations -
end for
m
X
f (a + ix)
i=0
12 / 55
Example
j = 4; while j ≤ n do -- j = j + 3;
13 / 55
Solving for m
3m ≤ n − 4 < 3(m + 1)
14 / 55
Case 1
How many elementary operations does the following algorithm have ?
for (i = 3; i ≤ n − 3; i + +)
- perform i − 1 elementary operations
end for
I Analyze : This is a single for loop where loop index runs from 3 to
n − 3 and index incremental 1. Hence,
n−3
X
# elementary ops = (i − 1).
i=3
I To find a closed form for the sum, expand it :
# elementary ops = 2 + 3 + 4 + . . . + (n − 4)
This sum nearly matches the known sum of consecutive integers,
except the first term is missing :
15 / 55
Xn−4
# elementary ops = ( p) − 1
p=1
16 / 55
Case 2
k = 2;
while k ≤ n do
- perform k elementary operations -
k = k + 3;
end while
17 / 55
The pattern : k = 2 + 3i, where i is a non-negative integer.
If 2 + 3m is the last value of k
18 / 55
A table of the values that the loop index goes through :
Value of loop index 2+3×0 2+3×1 2+3×2 ···
# of elementary operations 2+3×0 2+3×1 2+3×2 ···
19 / 55
Find a closed form for
(2 + 3 × 0) + (2 + 3 × 1) + (2 + 3 × 2) + · · · + (2 + 3b(n − 2)/3c).
(2 + 2 + 2 + · · · + 2) + (3 × 0 + 3 × 1 + 3 × 2 + · · · + 3 × b(n − 2)/3c)
How many 2’s are in the first sum ? The number of 2’s is equal to the
number of terms in the other sum, so there are b(n − 2)/3c + 1 two’s
altogether. The value of the first sum is 2 × (b(n − 2)/3c + 1).
Each term in the second sum has a common factor of 3, which can be
factored out of the sum.
20 / 55
We now have
n−2 n−2
2× + 1 + 3 × 0 + 1 + 2 + ··· +
3 3
The rightmost sum is one of the known sums (plus a 0 term which
doesn’t change the value of the sum).
b(n−2)/3c
n−2 X
2× +1 +3× i.
3
i=1
21 / 55
Multiplying out the leftmost term and substituting the closed form for
the rightmost sum :
n−2 (b(n − 2)/3c)(b(n − 2)/3c + 1)
2 +2+3×
3 2
The first term and the last term have a common factor of b(n − 2)/3c :
n−2 3 n−2
× 2+ +1 +2
3 2 3
22 / 55
Analysis of nested loops
for (i = 1; i ≤ bn/3c; i + +) do
j =0
while j ≤ 3i do
- execute n − i + 1 elementary operations
j =j +3
end while
end for
23 / 55
Requires the used of nested sums
Pb
Each term of i=a is a sum :
d(i)
b X b d(i)
X X X
f (i, j) = f (i, j)
i=a j=c(i) i=a j=c(i)
24 / 55
d(i)
b X b d(i)
X X X
f (i, j) = f (i, j)
i=a j=c(i) i=a j=c(i)
d(a) d(a+1)
X X
= f (a, j) + f (a + 1, j)
j=c(a) j=c(a+1)
| {z } | {z }
i = a term i = a + 1 term
d(a+2)
X
+ f (a + 2, j) + · · ·
j=c(a+2)
| {z }
i = a + 2 term
d(b)
X
+ f (b, j)
j=c(b)
| {z }
i = b term
25 / 55
Example :
n X
X n n
X n
X n
X
2j = 2j + 2j + 2j
i=2 j=n−i j=n−2 j=n−3 j=n−4
| {z } | {z } | {z }
i = 2 term i = 3 term i = 4 term
n
X
+ ··· + 2j
j=n−n
| {z }
i = n term
26 / 55
We can expand out each of the interior sums, too.
n
X n
X
2j = (2(n − 2) + 2(n − 1) + 2n)
i=2 j=n−i
| {z }
i = 2 term
+ (2(n − 3) + 2(n − 2) + 2(n − 1) + 2n)
| {z }
i = 3 term
+ (2(n − 4) + 2(n − 3) + 2(n − 2) + 2(n − 1) + 2n)
| {z }
i = 4 term
+ · · · + (2 × 0 + 2 × 1 + 2 × 2 + · · · + 2n)
| {z }
i = n term
27 / 55
How to analyze nested loops
for (i = 1; i ≤ bn/3c; i + +) do
j =0
while j ≤ 3i do
- execute n − i + 1 elementary operations
j =j +3
end while
end for
28 / 55
First step :
Analyze the innermost loop, treating the loop index of the outermost
loop as a constant.
j = 0;
while j ≤ 3i do
- do n − i + 1 elementary operations
j = j + 3;
end while
29 / 55
Analyzing
3i − 0
1+b c = i + 1.
3
Hence, the total number of elementary operations in this while-loop is
(i + 1)(n − i + 1).
30 / 55
How to analyze nested loops : continue
for (i = 1; i ≤ bn/3c; i + +) do
- perform (i + 1)(n − i + 1) elementary operations
end for
31 / 55
Pbn/3c
Find a closed form for i=1 (i + 1)(n − i + 1).
First, multiply out the term :
32 / 55
Substitute the closed form for each of the sums (closed form of
Pu 2 u(u+1)(2u+1)
i=1 i = 6 ):
bn/3c bn/3c bn/3c
X X X
− i2 + n i+ (n + 1)
i=1 i=1 i=1
bn/3c(bn/3c + 1)(2bn/3c + 1) bn/3c(bn/3c + 1)
= − +n
6 2
+ bn/3c(n + 1)
33 / 55
Take out the common factor of bn/3c and simplify the rest :
bn/3c(bn/3c + 1)(2bn/3c + 1) bn/3c(bn/3c + 1)
− +n + bn/3c(n + 1)
6 2
−(bn/3c + 1)(2bn/3c + 1) n(bn/3c + 1)
= bn/3c + + (n + 1)
6 2
−(2bn/3c2 + 3bn/3c + 1)
nbn/3c + n
= bn/3c + + (n + 1)
6 2
34 / 55
Put over a common denominator (6), and combine like terms :
−(2bn/3c2 + 3bn/3c + 1)
nbn/3c + n
bn/3c + + (n + 1)
6 2
−(2bn/3c2 + 3bn/3c + 1) + 3nbn/3c + 3n + 6n + 6
= bn/3c
6
bn/3c
= (bn/3c(3n − 2bn/3c − 3) + 9n + 5)
6
bn/3c
(bn/3c(3n − 2bn/3c − 3) + 9n + 5)
6
elementary operations.
35 / 55
Possible problems with a worst-case analysis
36 / 55
Average-Case Analysis
Another way to ignore the differences among inputs of the same size is
to perform an average-case analysis.
where pi is the probability that input i will occur out of all inputs of
size n.
37 / 55
Problem :
What if you don’t know the probabilities with which the inputs of size
n will occur ?
If all inputs of size n are equally likely to occur, then each input i of
size n occurs with probability 1/(number of inputs of size n) and
P
i input of size n {# of elementary ops performed for input i}
Tavg (n) = .
# of inputs of size n
38 / 55
Example Average-Case Analysis
LinearSearch (L[1..n], x)
Foundx = false ; i = 1 ;
while (i ≤ n & Foundx = false) do
if L[i] = x then
Foundx = true ;
else
i = i + 1;
if not Foundx then i = 0 ;
return i ;
39 / 55
Inputs of size n :
40 / 55
Probabilities and assumptions :
41 / 55
Rewrite the table :
# of
Category description Probability elementary Ops
1 x will be found in L[1] p/n 1
2 x will be found in L[2] p/n 2
3 x will be found in L[3] p/n 3
.. .. .. ..
. . . .
n x will be found in L[n] p/n n
n+1 x will not be found in L 1−p n
42 / 55
Average-case running time Tavg (n) of LinearSearch on inputs of size n :
X
Tavg (n) = (pi × {# of elementary ops done for input I })
i input of size n
n
!
X p
= ×i + (1 − p)n
n
i=1
n+1
Tavg (n) = p + (1 − p)n elementary operations.
2
43 / 55
I If x is always found (p = 1), then approximately half the entries of
L are compared to x.
I If x is never found (p = 0), then x is compared to all entries of L.
44 / 55
Consider the following search algorithm :
FiveSearch(L[1..n], x)
j = 5;
NotFound = true;
while (j ≤ n & NotFound) do
if x ≤ L[j] then
NotFound = false;
else
j = j + 5;
end while
if NotFound then
x is not in L
else
LinearSearch(L[j − 4, j],x)
45 / 55
Exercise
46 / 55
Review on Summations : Patterns in Sums
47 / 55
Summation Notation
b
X
f (i)
i=a
I Meaning :
b
X
f (i) = f (a) + f (a + 1) + f (a + 2) + · · · + f (b)
i=a
48 / 55
Example of sums
49 / 55
Operator Priorities
I
P
has the same priority as addition because it represents
additions.
I
P
and + : same priority
Example :
b b
!
X X
2i + 1 = 2i + 1
i=a i=a
I
P
and multiplication : multiplication has higher priority
b
X b
X
i × log2 10 = (i × log2 10)
i=a i=a
50 / 55
Sums and Closed Forms
Definition : A closed form for a sum is some function that gives you
the value of the sum with only a constant number of operations.
I Example :
for(i = 1; i ≤ n; i + +) do
- execute i elementary operations -
end for
I 1 + 2 + 3 + . . . + n = ni=1 i = n(n + 1)/2.
P
51 / 55
Example :
for(i = 1; i ≤ n; i + +) do
- execute n − i operations -
end for
does
n
X
(n − i) = (n − 1) + (n − 2) + (n − 3) +
i=1
··· + 1 + 0
n−1
X
= i
i=1
(n − 1)n
= operations
2
52 / 55
Sums and Closed Forms : Continue
I Sum of a constant c :
b b − a + 1 of them
X z }| {
c = c + c + ··· + c
i=a
= (b − a + 1) × c.
53 / 55
I Similar to sum of squares :
u
X
i(i + 1) = (1 × 2) + (2 × 3) + (3 × 4) +
i=1
· · · + (u × (u + 1))
u(u + 1)(u + 2)
=
3
I Sum of powers of a constant c :
u
X
ci = c0 + c1 + c2 + · · · + cu
i=0
c u+1 − 1
=
c −1
P2 i
Example : i=0 3 = (33 − 1)/2 = 13.
54 / 55
I Sum of the first u integers times increasing powers of a
constant c :
u
X
ic i = 0 × c0 + 1 × c1 + 2 × c2 + · · · + u × cu
i=0
((c − 1)(u + 1) − c)c u+1 + c
=
(c − 1)2
Example :
3
X
i2i = 0 × 20 + 1 × 21 + 2 × 22 + 3 × 23
i=0
= 34
((2 − 1)(3 + 1) − 2)23+1 + 2
=
(2 − 1)2
55 / 55