0% found this document useful (0 votes)
2 views

algorithm-analysis

Uploaded by

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

algorithm-analysis

Uploaded by

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

ALGORITHM ANALYSIS

Prof. Sainath Patil, VCET Vasai Rd..


ALGORITHM
• An algorithm is a clearly specified set of simple
instructions to be followed to solve a problem.

• Once an algorithm is given for a problem and


decided to be correct, an important step is to
determine how much in the way of resources,
such as time or space, the algorithm will require.

Prof. Sainath Patil, VCET Vasai Rd..


Characteristics
Characteristics of an algorithm are;
• Input
• Output
• Finiteness
• Definiteness
• Effectiveness

Prof. Sainath Patil, VCET Vasai Rd..


ALGORITHM ANALYSIS
• Analyzing an algorithm should quantify the
requirement of computing resources during
execution.
• Most important of these resources are
computer time and memory.
• Analysis of algorithm focuses on computation
of
– Space complexity
– Time complexity

Prof. Sainath Patil, VCET Vasai Rd..


• Space complexity:
– Space require to store program and data if any
during execution of the program.

Prof. Sainath Patil, VCET Vasai Rd..


• Time complexity:
• Time require to execute a program.
• It also called as running time or computing time
complexity
• Normally depends on the size of the input.

Prof. Sainath Patil, VCET Vasai Rd..


Time complexity
• Best Case,
• Worst case
• Average case behavior

– Generally the quantity required is the worst-case


time.
– One reason for this is that it provides a bound for
all input, including particularly bad input.

Prof. Sainath Patil, VCET Vasai Rd..


Asymptotic Notations
• Big O:
T(n) = O(f(n)) if there are constants c and n0 such that
T(n) <= cf(n) when n>= n0.
eg. 1,000n is larger than n2 for small values of n

• Big Ω:
T(n) = Ω(g(n)) if there are constants c and n0 such
that T(n) >=cg(n) when n >=n0.

• Big Ө:
T(n) = Ө(h(n)) if and only if T(n) = O(h(n)) and T(n) = Ω(h(n)).

Prof. Sainath Patil, VCET Vasai Rd..


Running Time Calculations
General Rules:
• RULE 1-FOR LOOPS:
The running time of a for loop is at most the running time of the statements
inside the for loop (including tests) times the number of iterations.
eg.
sum( int n )
{
int i, partial_sum;
/*1*/ partial_sum = 0;
/*2*/ for( i=1; i<=n; i++ )
/*3*/ partial_sum += i*i*i;
/*4*/ return( partial_sum );
}
The declarations count for no time.
Lines 1 and 4 count for one unit each.
Line 3 counts for three units per time executed (two multiplications and one addition) and is
executed n times, for a total of 3n units.
Line 2 has the hidden costs of initializing i, testing i <n, and incrementing i. The total cost of all these
is 1 to initialize, n + 1 for all the tests, and n for all the increments, which is 2n + 2.
We ignore the costs of calling the function and returning, for a total of 5n + 4. Thus, we say that this
function is O (n). Prof. Sainath Patil, VCET Vasai Rd..
Running Time Calculations
RULE 2-NESTED FOR LOOPS:
The total running time of a statement inside a group
of nested for loops is the running time of the
statement multiplied by the product of the sizes of all
the for loops.
eg.
for( i=0; i<n; i++ )
for( j=0; j<n; j++ )
k++;

Prof. Sainath Patil, VCET Vasai Rd..


Running Time Calculations
RULE 3-CONSECUTIVE STATEMENTS:
just add.
eg.
for( i=0; i<n; i++)
a[i] = 0;

for( i=0; i<n; i++ )


for( j=0; j<n; j++ )
a[i] += a[j] + i + j;

Prof. Sainath Patil, VCET Vasai Rd..


Running Time Calculations
RULE 4-lF/ELSE:
For the fragment

if( cond )
S1
else
S2
the running time of an if/else statement is never more than the running
time of the test plus the larger of the running times of S1 and S2.

Prof. Sainath Patil, VCET Vasai Rd..


Running Time Calculations Example
• MAXIMUM SUBSEQUENCE SUM PROBLEM:
Statement: Given integers a1, a2, . . . , an , find the
maximum value of Σak k is from i through j.

• Example:
For input -2, 11, -4, 13, -5, -2, the answer is 20
(a2through a4).

Prof. Sainath Patil, VCET Vasai Rd..


Solutions for the Maximum Subsequence Sum Problem
Algorithm 1:
int max_subsequence_sum( int a[], unsigned int n )
{
int this_sum, max_sum, best_i, best_j, i, j, k;
max_sum = 0; best_i = best_j = -1;
for( i=0; i<n; i++ )
for( j=i; j<n; j++ )
{
this_sum=0;
for( k = i; k<=j; k++ )
this_sum += a[k];
if( this_sum > max_sum )
{
max_sum = this_sum;
best_i = i;
best_j = j;
}
}
return( max_sum );
}
Prof. Sainath Patil, VCET Vasai Rd..
Solutions for the Maximum Subsequence Sum Problem
Algorithm 2:
int max_subsequence_sum( int a[], unsigned int n )
{
int this_sum, max_sum, best_i, best_j, i, j, k;
max_sum = 0; best_i = best_j = -1;
for( i=0; i<n; i++ )
{
this_sum = 0;
for( j=i; j<n; j++ )
{
this_sum += a[j];
if( this_sum > max_sum )
{ max_sum = this_sum; best_i = i; best_j = j;
}
}
}
return( max_sum );
} Prof. Sainath Patil, VCET Vasai Rd..
Solutions for the Maximum Subsequence Sum Problem
Algorithm 3
max_sub_sequence_sum( int a[], unsigned int n )
{
return max_sub_sum( a, 0, n-1 );
}

Int max_sub_sum( int a[], int left, int right )


{
int max_left_sum, max_right_sum;
int max_left_border_sum, max_right_border_sum;
int left_border_sum, right_border_sum;
int center, i;
if ( left == right ) /* Base Case */
if( a[left] > 0 )
return a[left];
else
return 0;
Prof. Sainath Patil, VCET Vasai Rd..
center = (left + right )/2;
max_left_sum = max_sub_sum( a, left, center );
max_right_sum = max_sub_sum( a, center+1, right );
max_left_border_sum = 0; left_border_sum = 0;
for( i=center; i>=left; i-- )
{
left_border_sum += a[i];
if( left_border_sum > max_left_border_sum )
max_left_border_sum = left_border_sum;
}
max_right_border_sum = 0; right_border_sum = 0;
for( i=center+1; i<=right; i++ )
{
right_border_sum += a[i];
if( right_border_sum > max_right_border_sum )
max_right_border_sum = right_border_sum;
}
m= max_left_border_sum + max_right_border_sum;
m1= max( max_left_sum, max_right_sum, );
Return (max(m,m1));
}
Prof. Sainath Patil, VCET Vasai Rd..
Solutions for the Maximum Subsequence Sum Problem
Algorithm 4
int max_subsequence_sum( int a[], unsigned int n )
{
int this_sum, max_sum, best_i, best_j, i, j;
i = this_sum = max_sum = 0; best_i = best_j = -1;
for( j=0; j<n; j++ )
{
this_sum += a[j];
if( this_sum > max_sum )
{ /* update max_sum, best_i, best_j */
max_sum = this_sum; best_i = i; best_j = j;
}
else
if( this_sum < 0 )
{
i = j + 1;
this_sum = 0;
}
}
return( max_sum );
}

Prof. Sainath Patil, VCET Vasai Rd..


Logarithms in the Running Time
Euclid's Algorithm
int gcd( unsigned int m, unsigned int n )
{
int rem;
while( n > 0 )
{
rem = m % n;
m = n;
n = rem;
}
return( m );
}

The algorithm works by continually computing remainders until 0 is reached. The last
nonzero remainder is the answer. Thus, if m = 1,989 and n = 1,590,
then the sequence of remainders is 399, 393, 6, 3, 0.
Therefore, gcd (1989, 1590) = 3. As the example shows, this is a fast algorithm.
Prof. Sainath Patil, VCET Vasai Rd..

You might also like