Chapter 02
Chapter 02
Werner Nutt
Acknowledgments
• The course follows the book “Introduction to Algorithms‘”,
by Cormen, Leiserson, Rivest and Stein, MIT Press
[CLRST]. Many examples displayed in these slides are
taken from their book.
• These slides are based on those developed by
Michael Böhlen for this course.
(See http://www.inf.unibz.it/dis/teaching/DSA/)
• The slides also include a number of additions made by
Roberto Sebastiani and Kurt Ranalter when they taught
later editions of this course
(See http://disi.unitn.it/~rseba/DIDATTICA/dsa2011_BZ//)
• Complexity of algorithms
• Asymptotic analysis
• Correctness of algorithms
• Complexity of algorithms
• Asymptotic analysis
• Correctness of algorithms
Analysis of Algorithms
• Efficiency:
– Running time
– Space used
cost times
for j := 2 to n do c1 n
key := A[j] c2 n-1
// Insert A[j] into A[1..j-1]
i := j-1 c3 n-1
n
while i>0 and A[i]>key do c4 ∑ j= 2 t j
n
A[i+1] := A[i] c5 ∑ j= 2 -t j−1.
n
i-- c6 ∑ j= 2 -t j−1.
A[i+1]:= key c7 n-1
• This is modeled by tj
Performance Analysis
• Often it is sufficient to count the number of iterations
of the core (innermost) part
– no distinction between comparisons, assignments, etc
(that means, roughly the same cost for all of them)
– gives precise enough results
• In some cases the cost of selected operations dominates
all other costs.
– disk I/O versus RAM operations
– database systems
Worst/Average/Best Case
• Analyzing Insertion Sort’s
– Worst case: elements sorted in inverse order, tj=j,
total running time is quadratic (time = an2+bn+c)
Worst/Average/Best Case/2
}
running time
5n
average case ???
4n
1n
A B C D E F G
input instance
Worst/Average/Best Case/3
6n average-case
Running time
5n
best-case
4n
3n
2n
1n
1 2 3 4 5 6 7 8 9 10 11 12
…..
Input instance size
Best/Worst/Average Case/4
Worst case is most often used:
– It is an upper-bound
– In certain application domains (e.g., air traffic control,
surgery) knowing the worst-case time complexity is of
crucial importance
– For some algorithms, worst case occurs fairly often
– The average case is often as bad as the worst case
j := 1
while j 5 n and A[j] != q do j++
if j 5 n then return j
else return -1
Translate FindFirstRec
into an Iterative Method
Observations:
• FindFirstRec makes a recursive call only at the end
(the method is “tail recursive”)
• In each call, the arguments change
• There is no need to maintain a recursion stack
Idea:
• Instead of making a recursive call, just change the variable values
• Do so, as long as the base case is not reached
• When the base case is reached, perform the corresponding
actions
• Should we care?
• Complexity of algorithms
• Asymptotic analysis
• Correctness of algorithms
Asymptotic Analysis
• Goal: simplify the analysis of the running time
by getting rid of details, which are
affected by specific implementation and hardware
– “rounding” of numbers: 1,000,001 8 1,000,000
– “rounding” of functions: 3n2 + 2n + 8 8 n2
Asymptotic Notation
Running Time
– f(n) 2 O(g(n)) iff
there exist c > 0 and n0 > 0,
s.t. f(n) 5 c g(n) for n 6 n0
– f(n) and g(n) are functions n0 Input Size
over non-negative integers
Used for worst-case analysis
3, 12, 27, 64
• Note:
– 50 n log n is O(n2)
– 50 n log n is O(n100)
but this is less informative than saying
– 50 n log n is O(n log n)
Asymptotic Notation/2
• The “big-Omega” Ω0Notation
– asymptotic lower bound
– f(n) = Ω(g(n)) iff
there exist c > 0 and n0 > 0,
Running Time
f -n.
s.t. c g(n) 5 f(n), for n 6 n0 c :g ( n)
• Used to describe lower bounds
of algorithmic problems
– E.g., searching in n0
Input Size
a sorted array
with linear search is Ω(n),
with binary search is Ω(log n)
Asymptotic Notation/3
• The “big-Theta” Θ0Notation
– asymptotically tight bound
– f(n) = Θ(g(n)) if there exists c 2⋅g- n.
Running Time
f -n.
s.t. for n 6 n0 c 1⋅g - n .
Asymptotic Notation/4
• Analogy with real numbers
– f(n) = O(g(n)) 4 f 5 g
– f(n) = Ω(g(n)) 4 f 6 g
– f(n) = Θ(g(n)) 4 f 2 g
• Abuse of notation:
f(n) = O(g(n)) actually means
f(n) 9 O(g(n))
Running Time
1 second 1 minute 1 hour
T(n) in µs
400n 2,500 150,000 9,000,000
20n log n 4,096 166,666 7,826,087
2n2 707 5,477 42,426
n4 31 88 244
2n 19 25 31
• Complexity of algorithms
• Asymptotic analysis
• Correctness of algorithms
Special Cases
Sortedness
for i := 1 to n
if A[i] 6 A[i+1] then return FALSE
return TRUE
Sortedness/2
for i := 1 to n
if A[i] 6 A[i+1] then return FALSE
return TRUE
Sortedness/3
for i := 1 to n–1
if A[i] 6 A[i+1] then return FALSE
return TRUE
●
Start of loop, i=1 è OK
●
End of loop, i=n-1 è OK
●
A=[1,2,3] è First iteration, from i=1 to i=2 è OK
●
A=[1,2,2] è ERROR (if A[i]=A[i+1] for some i)
Sortedness/4
for i := 1 to n-1
if A[i] 3 A[i+1] then return FALSE
return TRUE
l := 1; r := n
do
m := ⌊(l+r)/2⌋
if A[m] = q then return m
else if A[m] > q then r := m-1
else l := m+1
while l < r
return -1
• Start of loop è OK
• End of loop, l=r è Error! Example: search 3 in [3 5 7]
• Start of loop è OK
• End of loop, l=r è OK
• First iteration è OK
• A=[1,1,1] è OK
• Empty array, n=0 è Error! Tries to access A[0]
• One-element array, n=1 è OK
Master Informatique Data Structures and Algorithms 43
Part 2 Complexity and Correctness of Algorithms
l := 1; r := n
while l < r do
m := ⌊(l+r)/2⌋
if A[m] <= q
then l := m+1 else r := m
if A[l-1] = q
then return l-1 else return -1
l := 1; r := n
while l <= r do
m := ⌊(l+r)/2⌋
if A[m] <= q
then l := m+1 else r := m
if A[l-1] = q
then return l-1 else return -1
for j := 2 to n do
key := A[j]; i := j-1;
while A[i] > key and i > 0 do
A[i+1] := A[i]; i--;
A[i+1] := key
• Complexity of algorithms
• Asymptotic analysis
• Correctness of algorithms
Correctness of Algorithms
• An algorithm is correct if for every legal input,
it terminates and produces the desired output.
n Partial correctness
IF this point is reached, THEN this is the output
n Total correctness
INDEED this point is reached, AND this is the output
Assertions
• To prove partial correctness we associate a number of
assertions (statements about the state of the execution)
with specific checkpoints in the algorithm.
j := 1
while j 5 n and A[j] != q do j++
if j 5 n then return j
else return -1
Loop Invariants
• Invariants: assertions that are valid every time the
beginning of the loop is reached
(many times during the execution of an algorithm)
• We must show three things about loop invariants:
– Initialization: it is true prior to the first iteration.
– Maintenance: if it is true before an iteration,
then it is true after the iteration.
– Termination: when a loop terminates,
the invariant gives a useful property to show the
correctness of the algorithm
• Termination: 1 ≤ l, r ≤ n, l ≤ r
Two cases:
l := m+1 implies lnew = ⌊(l+r)/2⌋+ 1 > lold
r := m-1 implies rnew = ⌊(l+r)/2⌋ - 1 < rold
• The range gets smaller during each iteration and
the loop will terminate when l ≤ r no longer holds
Master Informatique Data Structures and Algorithms 58
Part 2 Complexity and Correctness of Algorithms
for j := 2 to n do
key := A[j]
i := j-1
while i>0 and A[i]>key do
A[i+1] := A[i]
i--
A[i+1] := key
Initialization:
External loop: (i), (ii) j = 2: A[1..1] 9 Aorig[1..1] and is trivially sorted
Internal loop: i = j-1:
– A[1...j-1] = Aorig[1..j-1] , since nothing has happend
– A[j+1..j] = Aorig[j..j-1] , since both sides are empty
– A[k] > key holds trivially for all k in the empty set
for j := 1 to n-1 do
for i := n downto j+1 do
if A[i-1] > A[i] then
swap(A,i-1,i)
A 1 2 3 4 5 7 9 8 6
1 j n
Strategy
• Start from the back
and compare pairs 44 55 12 42 94 18 06 67
of adjacent elements.
• Swap the elements 06 44 55 12 42 94 18 67
if the larger comes 06 12 44 55 18 42 94 67
before the smaller.
06 12 18 44 55 42 67 94
• In each step
the smallest element 06 12 18 42 44 55 67 94
of the unsorted part 06 12 18 42 44 55 67 94
is moved to the beginning
of the unsorted part and the 06 12 18 42 44 55 67 94
sorted part grows by one. 06 12 18 42 44 55 67 94
Exercise
• Apply the same approach that we used for insertion sort
to prove the correctness of bubble sort and
selection sort.
Math Refresher
●
Arithmetic progression
n
∑i= 0 i=0,1,...,n=n -n,1./ 2
●
Geometric progression (for a number a ≠ 1)
n
∑i= 0 a i=1,a 2 ,...,a n=-1−a n,1 ./-1−a.
Induction Principle
We want to show that property P is true
for all integers n 6 n0.
Basis: prove that P is true for n0.
Inductive step: prove that if P is true for all k
such that n0 5 k 5 n – 1 then P is also true for n.
Summary
●
Algorithmic complexity
• Asymptotic analysis
– Big O and Theta notation
– Growth of functions and asymptotic notation
• Correctness of algorithms
– Pre/Post conditions
– Invariants
• Special case analysis