0% found this document useful (0 votes)
8 views25 pages

Lecture 1

Algorithm analysis is the process of determining the computing time and storage requirements of algorithms to choose the best one for a given problem. It involves measuring efficiency through empirical and theoretical approaches, focusing on time and space complexity. Complexity analysis helps characterize algorithms based on resource requirements, which is crucial for understanding their performance in various environments.

Uploaded by

jamsibro140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views25 pages

Lecture 1

Algorithm analysis is the process of determining the computing time and storage requirements of algorithms to choose the best one for a given problem. It involves measuring efficiency through empirical and theoretical approaches, focusing on time and space complexity. Complexity analysis helps characterize algorithms based on resource requirements, which is crucial for understanding their performance in various environments.

Uploaded by

jamsibro140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Chapter Two

Algorithm Analysis
Concepts
What is an algorithm Analysis?

 Algorithm analysis refers to the


process of determining how much
computing time and storage that
algorithms will require.
 In other words, it’s a process of
redacting the resource requirement
of algorithms in a given environment.
2
What is an algorithm Analysis? ………..
 In order to solve a problem, there are
many possible algorithms.

 One has to be able to choose the best


algorithm for the problem at hand
using some scientific method.

 To classify some data structures and


algorithms as good:
 we need precise ways of analyzing them in

terms of resource requirement.


3
 The main resources are:
• Running Time

• Memory Usage

• Communication Bandwidth

Note: Running time is the most important


since computational time is the most
precious resource in most problem
domains.
4
 There are two approaches to measure the
efficiency of algorithms:
1. Empirical
 based on the total running time of the

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

b) Current processor load


 Only the work 10s
 With printing 15s
6

 With printing & browsing the internet >15s


c) Specific data for a particular
run of the program
Input size
Input properties
t1
for(int i=0; i<=n; i++)
cout<<i;
t2
T=t2-t1;
For n=100, T>=0.5s
n=1000, T>0.5s 7
d) Operating System
 Multitasking Vs Single tasking
 Internal structure

8
2. Theoretical
 Determining the quantity of resources
required using mathematical concept.

 Analyze an algorithm according to the


number of basic operations (time
units) required, rather than according
to an absolute amount of time involved.
9
 We use theoretical approach to
determine
the efficiency of algorithm because:

• The number of operation will not vary


under
different conditions.

• It helps us to have a meaningful measure


that
permits comparison of algorithms
independent
of operating platform.

• It helps to determine the complexity of


10

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

 Single Arithmetic Operations

Example: a+b;

 Function Return

Example: return sum;

3. Running time of a selection statement (if,


switch) is the time for the condition evaluation
plus the maximum of the running times for the
individual clauses in the selection.
16
Example: int x;
int sum=0;
if(a>b)
{
sum= a+b;
cout<<sum;
}
else
{
cout<<b;
}
T(n) = 1+1+max(3,1)
=5 17
4. Loop statements:
• The running time for the statements
inside the loop * number of iterations +
time for setup(1) + time for checking
(number of iteration + 1) + time for
update (number of iteration)
• The total running time of statements
inside a group of nested loops is the
running time of the statements * the
product of the sizes of all the loops.
• For nested loops, analyze inside out.
• Always assume that the loop executes
the maximum number of iterations
possible. (Why?)
 Because we are interested in the worst case complexity.

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)

5). int counter(){


int a=0;
cout<<”Enter a number”;
cin>>n;
for(i=0;i<n;i++)
a=a+1;
return 0; }
T(n)=1+1+1+(1+n+1+n)+2n+1
=4n+6=O(n)
22
6). void func( ){
int x=0; int i=0; int j=1;
cout<<”Enter a number”;
cin>>n;
while(i<n){
i=i+1;
}
while(j<n){
j=j+1;
}
}

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)

8). int sum=0;


for(i=0;i<n;i++)
for(j=0;j<n;j++)
sum++;
T(n)=1+1+(n+1)+n+n*(1+(n+1)+n+n)
=3+2n+n2+2n+2n2
=3+2n+3n2+2n
=3n2+4n+3=O(n2)
24
Next Class

• Formal Approach to
Analysis
• Asymptotic Analysis
• The Big-Oh Notation
• Big-Omega Notation
• Theta Notation
25

You might also like