Correctness Analysis1
Correctness Analysis1
Correctness Analysis1
Algorithm
A finite set of instructions which if followed accomplish a
particular task. In addition every algorithm must satisfy
following criteria:
1. Input: zero or more quantities externally supplied
2. Output: at least one quantity is produced
3. Definiteness: Each instruction must be clear and
unambiguous.
4. Finiteness: In all cases algorithm must terminate after finite
number of steps.
5. Effectiveness: each instruction must be sufficiently basic.
Two algorithms on two systems
Algorithm A1 50 n log n
Algorithm A2 2n2
A2 Super computer Time taken by Super Computer
108 ins/sec = 2.(106)2/ 108 = 20,000 sec
A1
P.C Time taken by P .C.
106 ins /sec = 50 .106 lg 106 / 106 = 1,000 sec
Thus by using a fast algorithm, the personal computer gives
results 20 times faster than the result given by super
computer using a slow algorithm.
Pre: nothing
Testing
Correctness proof
no infinite recursion
Fn = Fn-2 + Fn-1
function fib(n)
comment return F n
function multiply(y, z)
2. if z is odd
Induction: Suppose that for z > 0, and for all 0 < q z. multiply (y, q) returns yq.
Implementing Algorithm
Algorithm
Programmer
Program
Compiler
Executable
Computer
Constant Multiples
Analyze the resource usage of an algorithm to within a constant multiple.
Programmer ability
Programmer effectiveness
Compiler
Computer hardware
Formal definition: f(n) = O(g(n)) if there exists c, n0 IR+ such that for all n n0,
f(n) c.g(n).
Example
Most big-Os can be proved by induction.
The claim is trivially for n=1,since 0<1. Now suppose n > 1 and log n < n.
Then,
log (n+1)
< log 2n
= log n+1
Formal definition: f(n) = (g(n)) if there exists c > 0 such that there are infinitely
many n IN such that f(n) c.g(n)
Big Theta
Informal Definition: f(n) is (g(n)) if f is essentially the same as g, to
within a constant multiple.
Proof: Suppose for all n n1, f1(n) c1. g1(n) and for all n n2,
f2(n) c2. g2(n).
Let n0 = max{n1, n2} and c0 = max{c1, c2}. Then for all n n0,
Proof: Suppose for all n n1, f1(n) c1. g1(n) and for all n n2,
f2(n) c2. g2(n).
Proof: Suppose for all n n1, f1(n) c1 . g1(n) and for all n n2,
f2(n) c2. g2(n).
Average Case: The expected running time, given some probability distribution on
the inputs (usually uniform). T(n) is the average time taken over all inputs of size
n.
Probabilistic: The expected running time for a random input. (Express the
running time and the probability of getting it.)
Amortized: the running time for a series of executions, divided by the number of
executions.
Example
Consider an algorithm that for all inputs of n bits takes time 2n, and
for one input of size n takes time nn.
Average Case:
nn + (2n 1)n nn
( ) = ( )
2 2
Probablistic: O(n) with probability 1 1 / 2n.
assignment O(1)
procedure entry O(1)
procedure exit O(1)
if statement time for test plus
O(max of two branches)
loop sum over all iterations of
the time for each iteration
Identify the fundamental operation used in the algorithm, and observe that the
running time is a constant multiple of the number of fundamental operations
used. (Advantage: no need to grunge through line-by-line analysis.)
(n i) n(n 1) i
i 1 i 1
n( n 1) n( n 1) / 2
n( n 1) / 2
Algorithms and Problems
Big-Os mean different things when applied to algorithms and
problems.
Bubblesort runs in time O(n2). But is it tight? Maybe I was too
lazy to figure it out, or maybe its unknown.
Bubblesort runs in time (n2). This is tight.
The sorting problem takes time O(n log n). There exists an
algorithm that sorts in time O(n log n), but I dont know if there is
a faster one.
The sorting problem takes time (n log n). There exists an
algorithm that sorts in time O(n log n), and no algorithm can do
any better.
Algorithms Analysis 2
Summary
Analysis of iterative (nonrecursive) algorithms.
The heap: an implementation of the priority queue
Insertion in time O(log n)
Deletion of minimum in time O(log n)
Heapsort
Build a heap in time O(n log n).
Dismantle a heap in time O(n log n).
Worst case analysis O(n log n).
How to build a heap in time O(n).
The Heap
A priority queue is a set with the operations
Insert an element
Delete and return the smallest element
9 5
11 20 10 24
21 15 30 40 12
Note this implies that the value in each parent is the values in
its descendants (Includes self).
To Delete the Minimum
3
?
9 5
11 20 10 24
21 15 30 40 12
Contd
2. Replace root with last leaf.
12
9 5
11 20 10 24
21 15 30 40 12
12
9 5
11 20 10 24
21 15 30 40
9 5
11 20 10 24
21 15 30 40
5
9 12
11 20 10 24
21 15 30 40
Contd
5
9 12
11 20 10 24
21 15 30 40
9 10
11 20 12 24
21 15 30 40
What Does it work?
Why does swapping the new node with its smallest child work?
a a
or
b c c b
a a
or
b c c b
Then we get
b b
or
a c c a
Respectively,
Is b smaller than its children? Yes, since b < a and b c.
Contd
Is c smaller than its children?
1. Put the new element in the next leaf. This preserves the
balance.
Contd
a
b c
d e
d e
Insert:
K-1
2k 1 - 1
nodes k
2k 1
nodes
Contd
Therefore, in a heap with n nodes and k levels:
2k-1 n 2k - 1
k1 log <k
k1 log k
k1 log k1
log =k
k = log + 1
Hence, number of levels is l(n) = log + 1
Example
j 0
j 2 j (l (n)2l ( n ) ) (n log n).
j2j 1
j
(k 1)2 k 1 2
( k 2 k )
Building a Heap Bottom Up
i 1
l ( n ) i
(i 1).2 2 l (n)
i
i 1
/ 2
i
O ( n. ).
i
i 1
/ 2 i
cos t copies
Algorithm Analysis 3
Summary
Recurrence relations
How to derive them
How to solve them
Deriving Recurrence Relations
To derive a recurrence relation for the running time of an algorithm:
See what value of n is used as the base of the recursion. It will usually be a
single value (e.g. n = 1), but may be multiple values. Suppose it is n0.
Figure out what T(n0) is. You can usually use some constant c, but sometimes a
specific number will be needed.
The general T(n) is usually a sum of various choices of T(m) (for the recursive
calls), plus the sum of the other work done. Usually the recursive calls will be
solving a subproblems of the same size f(n), giving a term a.T(f(n)) in the
recurrence relation.
Continue
Base of recursion
Running time for base
c if n n 0
T ( n)
a.T ( f ( n)) g ( n) otherwise
This will not always work, but works most of the time in practice.
The Multiplication Example
We know that for all n > 1,
Now,
T(n+1) = T (n) + d
= dn +c - d +d
= dn + c
= d(n+1) + c - d
Merge Sorting
function mergesort(L, n)
comment sorts a list L of n numbers,
when n is a power of 2
if n 1 then return(L) else
break L into 2 lists L1, L2 of equal size
return(merge (mergesort (L1, n/2),
mergesort (L2, n/2)))
Here we assume a procedure merge which can merge two sorted lists of n
elements into a single sorted list in time O(n).
Mergesort is better than bubblesort (in the worst case, for large enough n).
Recursion Tree Method
A General Theorem
Theorem: if n is a power of c, the solution to the recurrence
d if n 1
T ( n)
aT (n / c) bn otherwise
is O ( n) if a c
T (n) O(n log n) if a c
O(n log ca ) if a c
Examples:
If T(n) = 2T(n/3) + dn, then T(n) = O(n)
If T(n) = 2T(n/2) + dn, then T(n) = O(n log n)
If T(n) = 4T(n/2) + dn, then T(n) = O(n2)
Proof Sketch
If n is a power of c, then
T ( n) a.T ( n / c ) bn
a ( a.T ( n / c 2 ) bn / c ) bn
a 2 .T ( n / c 2 ) abn / c bn
a 2 ( a.T ( n / c 3 ) bn / c 2 ) abn / c bn
a 3 .T ( n / c 3 ) a 2bn / c 2 abn / c bn
.
.
.
i 1
a T ( n / c ) bn ( a / c) j
i i
j 0
log n 1
a log n
T (1) bn (a / c)
j 0
j
log n 1
da log n
bn (a / c)
j 0
j
Continue
Now,
alogen= (clog ea) log en= (clog e n) log ea = nlogea.
Therefore,
log n1
T(n) = d.nlogea+ bn e /
=0
The sum is the hard part. There are three cases to consider,
depending on which of a and c is biggest.
n n
.S n S n i 1
i
i 0 i 0
n 1 1
Therefore
S n ( n 1 1) /( 1)
Infinite Sums: Suppose 0 < < 1 and let S . Then, S i
i
i 0 i 1
(a / c)
j 0
j
(a / c) j c /(c a )
j 0
Therefore,
T (n) d .n log a bcn /(c a) O(n).
(Note that since a < c, the first term is insignificant.)
Case 2: a = c. Then
log n 1
T (n) d .n bn O(n log n)
1 j
j 0
Continue
log en 1
is a geometric progression.
Hence, log n 1
( a / c ) log n
1
j 0
(a / c)
j
(a / c) 1
n log a 1 1
(a / c) 1
O ( n log a 1)
Otherwise.
Continue
This is much harder to analyze , but gives the same result: T(n) =
O(n log n). To see why, think of padding the input with extra
numbers up to the next power of 2. You at most double the number
of inputs, so the running time is
T(n) = 2T(n/2) + 6n
T(n) = 3T(n/3) + 6n 9
T(n) = 2T(n/3) + 5n
T(n) = 4T(n/2) + n
T(n) = 3T(n/2) + 9n