2. Introduction
What we should Analyze?
• Correctness
• Does the input/output relationship achieved by the algorithm
requirement?
• Time Complexity
• The amount of computer time it needs to run to completion.
• Space complexity
• The amount of memory it needs to run to completion.
• Optimality
• Is it impossible to do better?
3. Introduction
Which algorithm is better?
• All feasible algorithms are correct, but which is the best?
• Measure the amount of time used.
• Measure the amount of memory used.
5. Time Complexity
• It is the amount of computer time required to execute the algorithm.
• T= compilation time + Execution time.
• The execution time depends upon many factors which are not known
therefore we try to estimate it.
• To estimate execution time (te)we count only the number of
steps.
• Time complexity is expressed as a function of input size.
6. Random Access Machine (RAM) Model
• RAM model is used to analyze the algorithms
• It has one processor.
• Executes one instruction at a time.
• Each simple operation (+, -, =, if, call) takes 1 step.
• Loops and subroutine calls are not simple operations. They depend
upon the size of the data and the contents of a subroutine.
7. Step Counts
• Comments and declarative statements such as int, struct, #include,
class, etc., counted as zero steps.
• An assignment statement which does not involve any calls to other
programs is counted as one step.
8. Examples
• Find the sum of integers from 1 to n.
• Sort the elements of an array.
• Find the min from the elements stored in an array.
• Find whether a given number is prime or not.
9. Examples
• Sum of integers
Algorithm-1
int sum (int n)
{
for i=1 to n
sum= sum +i
} return sum;
Algorithm-2
int sum (int n)
{
If (n==1)
return 1
else
return (n + sum(n-1))
}
10. Analysis Types
• Worst Case
• T(n)= maximum number of steps taken on any input of size n.
•Best Case
• T(n)= minimum number of steps taken on any input of size n.
• Average Case
• T(n)= average number of steps taken on any input of size n.
• Best case is defined as which input of size n is cheapest among all
inputs of size n.
12. Examples
• Sorting elements in an array
Insertion sort (A)
{
for j=2 to n
{
Key= A[j]
i=j-1
While (i>0 and A[i]>key)
{
A[i+1]=A[i]
i=i-1
}
A[i+1]=key
}
13. Exact Analysis is hard
• Best, worst, and average cases are difficult to deal with precisely
because the details are very complicated.
• The exact number of steps will depend on exactly what machine or
language is being used.
• To avoid that problem, the Asymptotic complexity is generally used.
• For large enough inputs, the running time is mainly affected by the
higher order term.
• Therefore we are interested in the rate of growth, or order of growth,
of the running time.
14. Asymptotic Complexity
• Asymptotic complexity provides meaningful statements about the
time and space complexities of a program.
• Focus on what’s important by abstracting away low order terms and
constant factors.
• Why important:
• Shows simple characterization of algorithm’s efficiency.
• Comparison of the performance.
25. Recurrences
• A recurrence is an equation or inequality that describes a function
in terms of its value on smaller inputs.
• There three methods of solving recurrences:
• Substitution Method
• Recursion-Tree Method
• Master Method