2 Program Complexities
2 Program Complexities
Program Complexities
1
Algorithms
What is an algorithm?
A sequence of elementary computational steps that transform
the input into the output
What for?
A tool for solving well-specified computational problems, e.g.,
Sorting, Matrix Multiplication
What do we need to do with an algorithm?
Correctness Proof:
for every input instance, it halts with the correct output
Performance Analysis (1 second or 10 years?):
How does the algorithm behave as the problem size gets large
both in running time and storage requirement
2
A Sorting Problem
Input : <a0, a1, … , an-1>
Example:
<22, 51, 34, 44, 67, 11> => <11, 22, 34, 44, 51, 67>
3
Insertion Sort
Note that when we are
dealing with kth number, the
5, 3, 1, 2, 6, 4 first k-1 numbers are
already sorted.
3, 5, 1, 2, 6, 4
The kth number is inserted
1, 3, 5, 2, 6, 4 in the correct position.
1, 2, 3, 5, 6, 4
1, 2, 3, 5, 6, 4
1, 2, 3, 4, 5, 6
4
Insertion Sort
5, 3, 1, 2, 6, 4 To sort A[0,1,…,n-1] in place
3, 5, 1, 2, 6, 4 Steps:
1, 3, 5, 2, 6, 4 Pick element A[j]
Move A[j-1,…,0] to the right until
1, 2, 3, 5, 6, 4
proper position for A[j] is found
1, 2, 3, 5, 6, 4
1, 2, 3, 4, 5, 6 Example 1 3 5 2 6 4
0 … j j+1 . . 1 3 5 2 6 4
1 3 5 5 6 4
Currently Currently 1 3 3 5 6 4
sorted unsorted 1 2 3 5 6 4
part part
5
Insertion Sort
A[0] A[1] A[2] A[3] A[4] A[5]
Insertion-Sort (A)
1. for j=1 to n-1 j=1 5 3 1 2 6 4
2. key=A[j]
3. i=j-1
j=2 3 5 1 2 6 4
4. while i>=0 and A[i]>key j=3 1 3 5 2 6 4
5. A[i+1]=A[i]
6. i=i-1 j=4 1 2 3 5 6 4
7. A[i+1]=key j=5 1 2 3 5 6 4
1 2 3 4 5 6
j=3 1 3 5 2 6 4
1 3 5 5 6 4
1 3 3 5 6 4
1 2 3 5 6 4 6
Correctness of Algorithm
Why can the algorithm correctly sort?
We only consider algorithms with loops
Find a property as loop invariant
How to show something is loop invariant?
Initialization:
It is true prior to the first iteration of the loop
Maintenance:
If it is true before an iteration, it remains true before the
next iteration
Termination:
When the loop terminates, the invariant gives a useful
property that helps to show the algorithm is correct
7
Running time of Insertion Sort
Insertion-Sort(A) Cost times
1 for j = 1 to n-1 c1 n
2 key = A[j] n-1
3 i = j-1 c2 n-1
4 while i >= 0 and A[i] > key j=1..n-1 (tj+1)
5 A[i+1] = A[i] c3 j=1..n-1 tj
6 i=i-1 j=1..n-1 tj
c4
7 A[i+1] = key n-1
c1, c2, .. =c5running time for
executing line 1, line 2, etc.
tj = no. ofc6 times that line 5,6
are executed, for each j.
The running time T(n) c7
= c1*n+c2*(n-1)+c3*(n-1)+c4*(j=1..n-1 (tj+1))+c5*(j=1..n-1 tj)+c6*(j=1..n-1 tj)+c7*(n-1)
8
Analyzing Insertion Sort
T(n) = c1*n+c2*(n-1)+c3*(n-1)+c4*(j=1..n-1 (tj+1))+
c5*(j=1..n-1 tj)+c6*(j=1..n-1 tj)+c7*(n-1)
Worse case:
Reverse sorted: for example 6,5,4,3,2,1
inner loop body executed for all previous elements.
tj=j.
Worst case Reverse sorted inner loop body executed for all
previous elements. So, tj=j.
Average case Half elements in A[0..j-1] are less than A[j]. So, tj = j/2
Best case Already sorted inner loop body never executed. So,
tj=0.
11
Kinds of Analysis
Worst case
Average case
Best case
0 1 2 3 4 n
f(n)
90000
log2n
80000
microseconds
70000 Sqrt n
60000 n
50000 nlog2n
40000 n2
30000
n4
20000
2n
10000
0 n n!
Why n=o(n2)?
When n is 10000000000000000
n2 is 100000000000000000000000000000000
Differ a lot! 22
Asymptotic Notation
Relationship between typical functions
log n = o (n)
n = o (n log n)
nc = o (2n) where nc may be n2, n4, etc.
If f(n)=n+log n, we call log n lower order terms
(You are not required to analyze, but remember these
relations)
log n < sqrt(n) < n < nlog n < n2 < n4 < 2n < n!
23
Asymptotic Notation
When calculating asymptotic running time
Drop low-order terms
Ignore leading constants
Example 1: T(n) = An2+Bn+C
An2
T(n) = O(n2)
Example 2: T(n) = Anlogn+Bn2+Cn+D
Bn2
T(n) = O(n2)
24
Asymptotic Performance
Insertion-Sort(A)
Very often the 1 for j = 1 to n-1
O(n2)
algorithm 2 key = A[j]
complexity can 3 i = j-1
be observed 4 while i >= 0 and A[i] > key
directly from 5 A[i+1] = A[i]
simple 6 i=i-1
algorithms 7 A[i+1] = key
There are 4 very useful rules for such Big-Oh analysis ...
25
Asymptotic Performance
General rules for Big-Oh Analysis:
27
Asymptotic Performance
Example of Big-Oh Analysis:
void function5(int n)
{ int i;
for (i=0;i<n;i++)
Suppose
if (IsSignificantData(i)) IsSignificantData is O(n),
SpecialTreatment(i); SpecialTreatment is O(n log n)
29
Comparison of Good and Bad
Coin Flipping
There are N rows of coins
Each row consists of 9 coins
They formulate a matrix of size N*9
Some coins are heads up and some are tails up
We can flip a whole row or a whole column
every time
Your program need to find a flipping method
that can make the number of “heads up” coins
maximum
30
Asymptotic Performance
Recursion
int Power(int base,int pow)
{ if (pow==0) return 1;
else return base*Power(base,pow-1);
}
Example
32=9
Power(3,2)=3*Power(3,1)
Power(3,1)=3*Power(3,0)
Power(3,0)=1
32
Tower of Hanoi
Given some rods for stacking disks.
The problem:
Rules: Use fewest steps to move all disks
from the source rod to the target
(1) The disks must be stacked in
without violating the rules through
order of size. the whole process (given one
(2) Each time move 1 disk. intermediate rod for buffering)?
34
Tower of Hanoi
void Towers (int n, int Source, int Target, int Interm)
{ if (n==1)
cout<<“From”<<Source<<“To”<<Target<<endl;
else
{ Towers(n-1, Source, Interm, Target);
Towers(1, Source, Target, Interm);
Towers(n-1, Interm, Target, Source);
}
}
36
Learning Objectives
1. Understand the meaning of O and o and
able to use
2. Analyze program complexities for simple
programs
3. Able to compare which function grows
faster
4. Able to do worst case analysis