Professional Documents
Culture Documents
• Time
• Space
How can you practically compare 2
algorithms???
• Run them on the same machine
• Implement them using the same language
• Only time units such as nano-seconds or micro
seconds should not be used…WHY???
• Logical units expressing the relation between
the size of file ‘n’ and the amount of time ‘t’
required to process it should be used
• Same input data should be used
Problems with this approach
• Effort
• One program may be better written than the
other
• Choice of tests may favor one of the
algorithms
The condition always executes one more time than the loop
itself
Nested ‘for’ loops
for (i=0;i<n;++i)
{ for (j=0;j<m;++j)
sum++
}
The statement sum++ executes n*m times
So in a nested for loop if the loop variables are independent then:
When approximating T(n) we can IGNORE the 100 term for very large value of n
and say that T(n) can be approximated by 7(n)
Example 2
• T(n) = n2 + 100n + log10n +1000
n T(n) n2 100n log10n 1000
When approximating T(n) we can IGNORE the last 3 terms and say say that T(n) can
be approximated by n2
Big-Oh or Big-O
• Definition:
f(n) is O(g(n)) if there exist positive numbers c & N such that f(n)<=cg(n)
for all n>=N
This is O(1)
sum++;
This is O(n)
for (i=0;i<n;++i)
sum++;
This is O(n2)
for (i=0;i<n;++i)
for (j=0;j<n;++j)
sum++;
Growth rates
Which growth rate is better???
• Add n!, nn , 2n to the
graph and see where
they fit
Here we have:
• Constant
• Linear
• Logarithmic
• Quadratic
• cubic
1 0 0 1 1 2
2 1 2 4 8 4
8 3 24 64 512 256
sum = 0; i sum
for (i=1;i<n;i=i*2) 1 1
sum = sum+1; 2 1+1=2
4 2+1=3
8 3+1=4
…
n/2 log2n
n “exit loop”…
sum not incremented
loop will not execute for i=n further
take half
take half
take half
You didn’t color all cells…..everytime you discarded half the cells
EXAMPLE OF O(n)…suppose you are coloring the table
take half
EXAMPLE OF O(n2)…suppose you are coloring the table
take half
EXAMPLE OF O(2n)…suppose you are coloring the table
Expr
i j
– the sum of all values of Expr with each value of i between j--k
• Example: 4
2i 1
i0
i 1
i
2
O N 2
• Intuition:
– sum = 1 + 2 + 3 + ... + (N-2) + (N-1) + N
– sum = (1 + N) + (2 + N-1) + (3 + N-2) + ... // rearranged
// N/2 pairs total
• sum of squares:
N N 1 2 N 1
N
i 1
2
i
6
O N 3
Series runtime examples
• What is the exact runtime and complexity class
(Big-Oh)?
int sum = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N * 2; j++) {
sum++;
}
}
– Runtime = N · 2N = O(N2).
int sum = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
sum++;
}
}
SOME MATH
Normally these formulas are very handy :
If x y z then y log x z
n
n
ai
i 1 2
(a1 a n )
n
x (n 1) x ( n 1) nx ( n 2 )
k 0
kx
k
( x 1 ) 2
n
n
i
i 1 2
(n 1)
n n(n 1)( 2n 1)
i2 ( for n 1)
i 1 6
m 1
m
1 r
k 0
r k
1 r
SOME RULES
Rule 1
for (i=0;i<n;i=i+k) Anything inside the loop will run approximately n/k times
Rule 2
for (i=n;i>0;i=i-k) Anything inside the loop will run approximately n/k times
Rule 3
for (i=1;i<n;i=i*k) Anything inside the loop will run approximately logkn times
Rule 4
If the loop variables are independent then the total times a statement inside a
nested loop is executed is equal to the product of the times the individual loops
run
e.g. for (i=0;i<n;++i)
for (j=0;j<m;++j)
A statement inside the above nested loop will run n*m times
Some rules…(contd.)
Rule 5
for(i=1;i<=n;++i)
for (j=1;j<=i;++j)
The above nested loop approximately runs (1/2)n(n+1) times.
The variable j depends upon the value of i
Rule 6
for(i=1;i<=n;i=i*2)
for (j=1;j<=i;++j)
The statements in the above nested loop approximately run 2n-1 times.
Rule 7
for(i=0;i<n;i=i+1)
for (j=i;j<n;++j)
The statements in the above nested loop approximately run n(n+1)/2
times.