ASYMPTOTIC NOTATION
ASYMPTOTIC NOTATION
Structures
Lecture II
Simonas Šaltenis
Nykredit Center for Database
Research
Aalborg University
[email protected]
This Lecture
Correctness of algorithms
Growth of functions and asymptotic
notation
Some basic math revisited
Divide and conquer example – the
merge sort
Correctness of Algorithms
The algorithm is correct if for any legal
input it terminates and produces the
desired output.
Automatic proof of correctness is not
possible
But there are practical techniques and
rigorous formalisms that help to
reason about the correctness of
algorithms
Partial and Total
Correctness
Partial correctness
IF this point is reached, THEN this is the desired output
Running Time
f(n) and g(n) are functions
over non-negative
integers
Used for worst-case n0 Input Size
analysis
Asymptotic Notation (2)
The “big-Omega”
Notation
asymptotic lower bound
f(n) = (g(n)) if there exists
constants c and n0, s.t. c g(n)
f (n )
Running Time
f(n) for n n0
c g (n)
Used to describe best-case
running times or lower
bounds of algorithmic
problems n0 Input Size
E.g., lower-bound of searching
in an unsorted array is (n).
September 17,
2001
Asymptotic Notation (3)
Simple Rule: Drop lower order terms
and constant factors.
50 n log n is O(n log n)
7n - 3 is O(n)
8n2 log n + 5n2 + n is O(n2 log n)
Note: Even though (50 n log n) is
O(n5), it is expected that such an
approximation be of as small an order
as possible
Asymptotic Notation (4)
The “big-Theta”
Notation
asymptoticly tight bound
f(n) = (g(n)) if there exists
constants c1, c2, and n0, s.t. c 2 g (n )
f (n )
Running Time
c1 g(n) f(n) c2 g(n) for
n n0 c1 g (n )
f(n) = (g(n)) if and only if
f(n) = (g(n)) and f(n) =
n0
(g(n)) Input Size
i 0 1 a
( n 1 1) ( n 2 n 2 n)
( n 1) n
2 2
n(n 1)
2
Divide and Conquer
Divide and conquer method for
algorithm design:
Divide: If the input size is too large to deal
with in a straightforward manner, divide
the problem into two or more disjoint
subproblems
Conquer: Use divide and conquer
recursively to solve the subproblems
Combine: Take the solutions to the
subproblems and “merge” these solutions
into a solution for the original problem
MergeSort: Algorithm
Divide: If S has at least two elements (nothing
needs to be done if S has zero or one elements),
remove all the elements from S and put them
into two sequences, S1 and S2 , each containing
about half of the elements of S. (i.e. S1 contains
the first n/2elements and S2 contains the
remaining n/2elements.
Conquer: Sort sequences S1 and S2 using
MergeSort.
Combine: Put back the elements into S by
merging the sorted sequences S1 and S2 into one
sorted sequence
Merge Sort: Algorithm
Merge-Sort(A,
Merge-Sort(A, p, p, r)
r)
if
if pp << rr then
then
q(p+r)/2
q(p+r)/2
Merge-Sort(A,
Merge-Sort(A, p, p, q)
q)
Merge-Sort(A,
Merge-Sort(A, q+1,q+1, r)r)
Merge(A,
Merge(A, p, p, q,
q, r)
r)
Merge(A,
Merge(A, p,
p, q,
q, r)
r)
Take
Take the
the smallest
smallest ofof the
the two
two topmost
topmost elements
elements of
of
sequences
sequences A[p..q]
A[p..q] and
and A[q+1..r]
A[q+1..r] andand put
put into
into the
the
resulting
resulting sequence.
sequence. Repeat
Repeat this,
this, until
until both
both sequences
sequences
are
are empty.
empty. Copy
Copy the
the resulting
resulting sequence
sequence into
into A[p..r].
A[p..r].
MergeSort (Example) - 1
MergeSort (Example) - 2
MergeSort (Example) - 3
MergeSort (Example) - 4
MergeSort (Example) - 5
MergeSort (Example) - 6
MergeSort (Example) - 7
MergeSort (Example) - 8
MergeSort (Example) - 9
MergeSort (Example) - 10
MergeSort (Example) - 11
MergeSort (Example) - 12
MergeSort (Example) - 13
MergeSort (Example) - 14
MergeSort (Example) - 15
MergeSort (Example) - 16
MergeSort (Example) - 17
MergeSort (Example) - 18
MergeSort (Example) - 19
MergeSort (Example) - 20
MergeSort (Example) - 21
MergeSort (Example) - 22
Recurrences
Recursive calls in algorithms can be
described using recurrences
A recurrence is an equation or
inequality that describes a function in
terms of its value on smaller inputs
Example: Merge Sort
(1) if n 1
T (n)
2T (n / 2) (n) if n 1
Next Week
Solving recurrences