Professional Documents
Culture Documents
English
American
A la russe
Running Time
80
the input size.
60
• Average case time is often
40
difficult to determine.
• We focus on the worst case 20
running time. 0
1000 2000 3000 4000
– Easier to analyze
Input Size
– Crucial to applications such as
games etc
Design & Analysis of Algorithm 9
Experimental Studies
• Write a program 9000
implementing the algorithm 8000
Time (ms)
composition 5000
System.currentTimeMillis() to 3000
Algorithm arrayMax(A, n)
# operations
currentMax A[0] 2
for (i =1; i<n; i++) 2n
(i=1 once, i<n n times, i++ (n-1) times: post increment i.e.1+ n + n -1 = 2n )
if A[i] currentMax then 2(n 1)
currentMax A[i] 2(n 1)
return currentMax 1
Total 6n 1
20 of Algorithm
Design & Analysis
Example
• Suppose there are two Algorithms i.e.
– Algorithm 1 -- Algorithm 2
• Algorithm 1 take f(n) time and Algorithm 2 take
g(n^2)
• As n ↑ running time will ↑ linearly in case of first
algorithm, while for the second one, running time
increases exponentially
• When can running time be same for both
algorithms????
– If n is significantly small, the running time of both
operations will be nearly the same
Design & Analysis of Algorithm 21
3-Cases: Algorithm Analysis
• The time required by an algorithm falls under
three types
– Best Case − Minimum time required for program
execution
– Average Case − Average time required for program
execution
– Worst Case − Maximum time required for program
execution
26
Design & Analysis of Algorithm
Big Oh Notation - O
• For non-negative functions, f(n) and g(n), if there exists an integer n0 and a
constant c > 0 such that for all integers n > n0, f(n) ≥ cg(n), then f(n) is
omega of g(n). This is denoted as "f(n) = Ω(g(n))".
33 of Algorithm
Design & Analysis
Asymptotic notations (cont.)
• - notation
34 of Algorithm
Design & Analysis
Asymptotic Notation
•
• Theta Notation Θ
• Theta Notation For non-negative functions, f(n) and g(n), f(n) is theta of
g(n) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)). This is denoted as "f(n) =
Θ(g(n))".
This is basically saying that the function, f(n) is bounded both from the top
and bottom by the same function, g(n).
35 of Algorithm
Design & Analysis
Asymptotic notations (cont.)
• -notation
36 of Algorithm
Design & Analysis
Asymptotic notations (cont.)
• Little-O Notation
• For non-negative functions, f(n) and g(n), f(n) is little o of g(n) if and only if
f(n) = O(g(n)), but f(n) ≠ Θ(g(n)). This is denoted as "f(n) = o(g(n))".
• This represents a loose bounding version of Big O. g(n) bounds from the
top, but it does not bound the bottom.
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
... ...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
(c2 + c1) x N + c2
38
Design & Analysis of Algorithm 38
Another Example
• Algorithm 3 Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2
39
Design & Analysis of Algorithm 39