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

Algorithm Analysis - 1

The document discusses algorithm analysis. It defines an algorithm as a finite sequence of logically related instructions to solve a computational problem. Algorithm analysis involves determining the correctness of the algorithm's output, analyzing the time complexity or amount of work done, and using asymptotic notation. The time complexity analysis focuses on counting the number of primitive operations like assignments as the input size increases. Examples of iterative and recursive algorithms for finding the maximum element in an array and calculating factorials are provided to illustrate time complexity analysis.

Uploaded by

Nikita Shakya
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Algorithm Analysis - 1

The document discusses algorithm analysis. It defines an algorithm as a finite sequence of logically related instructions to solve a computational problem. Algorithm analysis involves determining the correctness of the algorithm's output, analyzing the time complexity or amount of work done, and using asymptotic notation. The time complexity analysis focuses on counting the number of primitive operations like assignments as the input size increases. Examples of iterative and recursive algorithms for finding the maximum element in an array and calculating factorials are provided to illustrate time complexity analysis.

Uploaded by

Nikita Shakya
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Algorithm Analysis

Algorithm
An algorithm is a finite sequence of logically related
instructions to solve a computational problem.

Mathematically, an algorithm can be represented as


a function, F : I ! O, where I is the set of inputs and
O is the set of outputs generated by the algorithm.
The word algorithm comes from the name of Persian
author \Abu Jafar MohammadibnMusa al Khawarizmi
".
Example of finding a maximum
element in an array of size n
 An algorithm is typically described using pseudo code as
follows:
Example : Finding a maximum element in an array
Algo Max-array(A,n)
{
Max = A[1]; Another algorithm : Sort
the array
for i = 2 to n do decreasing/increasing
order and return the first
if ( A[i] > Max ) then Max = A[i]; /last element.
return Max;
}
Types of Algorithm
Types of Algorithm
Iterative
Recursive
Example
Iterative :
Fact(n)
{
for i = 1 to n
fact = fact * i;
return fact;
}
Here the factorial is calculated as 1 2 3 : : : n
Recursive :
Fact(n)
{
if n = 1
return 1;
else
return n * fact(n-1);
}
Here the factorial is calculated as n × (n × 1) ×….. × 1.
Algorithm Analysis
Correctness:

For any algorithm, a proof of correctness is important


which will exhibit the fact that the algorithm indeed
output the desired answer. Often, discovering the
underlying combinatorics is quite challenging.
 Amount of work done (time complexity) :
 Analysis mean, the amount of time and space required to execute
the algorithm.
 The time Complexity does not refer to the actual running time of
an algorithm in terms of millisec (system time). The actual running
time depends on the system configuration.
 For each algorithm, we focus on step count: the number of times
each statement in the algorithm is executed,
 The step count focuses on primitive operations along with basic
operations. Moreover, this number increases with the problem size.
Therefore, we express the step count (time complexity) as a
function of the input size. The notion input size and primitive
operations vary from one problem to another.
Step-count Method and
Asymptotic Notation
Some basic assumptions are;
There is no count for { and } .
Each basic statement like 'assignment' and 'return'
have a count of 1.
If a basic statement is iterated, then multiply by the
number of times the loop is run.
The loop statement is iterated n times, it has a count
of (n + 1). Here the loop runs n times for the true case
and a check is performed for the loop exit (the false
condition), hence the additional 1 in the count.
Example
Example

 Note that the first 'for loop' is executed m + 1 times, i.e., the first m
calls are true calls during which the inner loop is executed and the last
call (m + 1)th call is a false call.
Matrix Addition

Statement s/e Frequency Total steps

Void add (int a[ ][MAX_SIZE]‧‧‧) 0 0 0


{ 0 0 0
int i, j; 0 0 0
for (i = 0; i < row; i++) 1 rows+1 rows+1
for (j=0; j< cols; j++) 1 rows‧(cols+1) rows‧cols+rows
c[i][j] = a[i][j] + b[i][j]; 1 rows‧cols rows‧cols
} 0 0 0

Total 2rows‧cols+2rows+1
Exercise 1
Exercise 2
Exercise 3
Exercise 4
Thank you

You might also like