Introduction
• Algorithm :Step by step procedure for
performing some task in finite amount of time
• Data Structure :is a systematic way of
organizing and accessing data
2.
Methodologies for analyzingalgorithm
• Determine the running time
• Perform several experiments with various size
of inputs
• Plotting the performance of each run of the
algorithm
• X- i/p size n
• Y-running time
3.
Limitation of experimentalmodel
• Experiments can be done only on a limited set
of test inputs
• Experiments have been performed in the
same hardware and software
• Solution:
• It required analytical framework
– Consider all possible inputs
– Independent from hardware and software
4.
Pseudo code
• Itsnot an computer program but facilitate the
high level analysis of data structure called
pseudo code
5.
RAM(Random Access model)
•we can define set of primitive operations that are
largely independent from the programming language
used.
• Identified them from the pseudo code
• Primitive operations include following:
– Assigning a value
– Calling a method
– Performing an arithmetic operation
– Comparing two variables
– Indexing in to an array
– Object reference
– Returning from a method
6.
RAM
• Instead oftrying to determine the specific
execution time of each primitive operation,
we will simply count how many primitive
operation are executed.
• Assumption : the running time of different
primitive operations will be same
• This approach of simply counting primitive
operations give rise to a computational model
called RAM
7.
Example
• Algorithm: Arraymax(A,n)
• Input: An array A sorting n>=1 integer
• Output: The maximum element in A
Current max <- A[0]
For i<-1 to n-1 do
If current max <A[i] then
Current max <- A[i]
Return current max
8.
Calculation of primitiveoperations
• Initializing the variable current max to A[0]
corresponds to two primitive operations.
(indexing in to an array and assigning a value)
• For loop I initialized to 1 (one primitive
operation)
• Before entering in to loop , termination
condition is checked , i<n (performing n times)
9.
Calculation of primitiveoperations
• For loop executed (n-1) times.
– At each iteration two primitive operations required
(comparison and indexing)
– If this condition is false the total four primitive operations
are required (2 (comparison + indexing)+ 2 (i=i+1, assigning
and increment))
– If true then (above 4 + 2 (current max <- A[i] , indexing and
assigning))
• Therefore body of for loop contributes between 4(n-1)
and 6(n-1) units of cost
• Returning a value corresponds to one primitive
operation
10.
Calculation of primitiveoperations
• At least
2 + 1 +n + 4(n-1) + 1 = 5n
• At most
2 + 1 +n + 6(n-1) + 1 = 7n – 2
Best case: T(n)=5n
Worst case: T(n)=7n-2
X←new array ofnintegers --------------------------1
s←0 --------------------------1
fori←1 to n−1 do ----------------------1 + n
s←s+X[i] --------------------5(n-1)
s<- s/n --------------------------2
Return s --------------------------1
• Output: 6+5(n-1)+n=6n+1
13.
• In suchcases average case analysis would
often be valuable, it is typically quite
challenging.
• It requires to define probability distribution on
the set of inputs which is difficult task
14.
How do wecompare algorithms?
• We need to define a number of objective
measures.
(1) Compare execution times?
Not good: times are specific to a particular
computer !!
(2) Count the number of statements executed?
Not good: number of statements vary with
the programming language as well as the
style of the individual programmer.
15.
Example
• Associate a"cost" with each statement.
• Find the "total cost“ by finding the total number of
times each statement is executed.
Algorithm 1 Algorithm 2
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
----------- -------------
n*c1 n*c2 + (n-1)*c1
16.
Ideal Solution
• Expressrunning time as a function of the
input size n (i.e., f(n)).
• Compare different functions corresponding to
running times.
• Such an analysis is independent of machine
time, programming style, etc.
17.
Asymptotic Analysis
• Tocompare two algorithms with running
times f(n) and g(n), we need a rough measure
that characterizes how fast each function
grows.
• Hint: use rate of growth
• Compare functions in the limit, that is,
asymptotically!
(i.e., for large values of n)
18.
Rate of Growth
•Consider the example of buying gold and paper:
Cost: cost_of_Gold + cost_of_paper
Cost ~ cost_of_Gold (approximation)
• The low order terms in a function are relatively
insignificant for large n
n4 + 100n2 + 10n + 50 ~ n4
i.e., we say that n4 + 100n2 + 10n + 50 and n4 have
the same rate of growth
19.
Types of Analysis
•Worst case
– Provides an upper bound on running time
– An absolute guarantee that the algorithm would not run longer, no
matter what the inputs are
• Best case
– Provides a lower bound on running time
– Input is the one for which the algorithm runs the fastest
• Average case
– Provides a exact bound on running time
– Assumes that the input is random
Lower Bound RunningTime Upper Bound
Theorem
1. If d(n)is O(f(n)) then ad(n) is O(f(n)) for any constant
a>0.
2. If d(n) is O(f(n)) and e(n) is O(g(n)) then d(n)+e(n) is
O(f(n)+g(n)).
3. If d(n) is O(f(n)) and e(n) is O(g(n)) then d(n)e(n) is
O(f(n)g(n)).
4. If d(n) is O(f(n)) and f(n) is O(g(n)) then d(n) is O(g(n)).
22.
5. If f(n)is a polynomial of degree k, then f(n) is
O(nk)
6. ac is O(1), for any constants c>0 and a>1.
7. log nc is O(log n) for any constant c > 0.
8. logc n is O(nd) for any positive constants c and
n.