Lecture 1
Lecture 1
Algorithm Analysis
Concepts
What is an algorithm Analysis?
• Memory Usage
• Communication Bandwidth
program.
Uses actual system clock time.
Example:
t1
for(int i=0; i<=10; i++)
cout<<i;
t2
Running time taken by the above algorithm
(TotalTime) = t2-t1;
5
It is difficult to determine efficiency of
algorithms using this approach,
Because clock-time can vary based on
many factors. For example:
a) Processor speed of the
computer
1.78GHz 2.12GHz
10s <10s
8
2. Theoretical
Determining the quantity of resources
required using mathematical concept.
algorithm.
Complexity Analysis
Complexity Analysis is the systematic study
of the cost of computation, measured either
in:
Time units
Operations performed, or
The amount of storage space required.
11
Two important ways to characterize the
effectiveness of an algorithm are its Space
Complexity and Time Complexity.
Time Complexity: Determine the approximate
amount of time (number of operations) required to
solve a problem of size n.
The limiting behavior of time complexity as
size increases is called the Asymptotic Time
Complexity.
Space Complexity: Determine the approximate
memory required to solve a problem of size n.
The limiting behavior of space complexity as
size increases is called the Asymptotic
Space Complexity.
12
Asymptotic Complexity of an algorithm determines
the size of problems that can be solved by the
algorithm.
Factors affecting the running time of a program:
CPU type (80286, 80386, 80486, Pentium I---IV)
Memory used
Computer used
Programming Language
C (fastest), C++ (faster), Java (fast)
C is relatively faster than Java, because C is relatively nearer to Machine language,
so, Java takes relatively larger amount of time for interpreting/translation to machine
code.
Algorithm used
Input size
Note: Important factors for this course are Input size 13and Algorithm
used.
Complexity analysis involves two distinct phases:
• Algorithm Analysis: Analysis of the
algorithm or data structure to produce a
function T(n) that describes the algorithm in
terms of the operations performed in order to
measure the complexity of the algorithm.
Example: Suppose we have hardware capable of
executing 106 instructions per second. How long
would it take to execute an algorithm whose
complexity function is T(n)=2n2 on an input size of
n=108?
Solution: T(n)= 2n2=2(108)2 = 2*1016
Running time=T(108)/106=2*1016/106=2*1010
seconds.
• Order of Magnitude Analysis: Analysis of
the function T (n) to determine the general
complexity category to which it belongs. 14
There is no generally accepted set of
rules for algorithm analysis.
However, an exact count of operations
is commonly used.
To count the number of operations we
can use the following Analysis Rule.
Analysis Rules:
1. Assume an arbitrary time unit.
2. Execution of one of the following
operations
takes time 1 unit:
Assignment Operation
Example: i=0;
Single Input/Output Operation
Example: cin>>a; 15
cout<<“hello”;
Single Boolean Operations
Example: i>=10
Example: a+b;
Function Return
18
5. Function call:
• 1 for setup + the time for any
parameter calculations + the time
required for the execution of the
function body.
Examples:
1)
int k=0,n;
cout<<“Enter an integer”;
cin>>n
for(int i=0;i<n; i++)
k++;
T(n)= 3+1+n+1+n+n=3n+5
19
2)
int i=0;
while(i<n)
{
cout<<i;
i++;
}
int j=1;
while(j<=10)
{
cout<<j;
j++;
}
T(n)=1+n+1+n+n+1+11+2(10)
= 3n+34 20
3)
int k=0;
for(int i=1 ; i<=n; i++)
for( int j=1; j<=n; j++)
k++;
T(n)=1+1+(n+1)+n+n(1+(n+1)+n+n)
= 2n+3+n(3n+2)
= 2n+3+3n2+2n
= 3n2+4n+3
21
4). int sum=0;
for(i=1;i<=n;i++))
sum=sum+i;
T(n)=1+1+(n+1)+n+(1+1)n
=3+4n=O(n)
T(n)=1+1+1+1+1+n+1+2n+n+2(n-1)
= 6+4n+2n-2
=4+6n=O(n)
23
7). int sum(int n){
int s=0;
for(int i=1;i<=n;i++)
s=s+(i*i*i*i);
return s;
}
T(n)=1+(1+n+1+n+5n)+1
=7n+4=O(n)
• Formal Approach to
Analysis
• Asymptotic Analysis
• The Big-Oh Notation
• Big-Omega Notation
• Theta Notation
25