An algorithm is a well-defined set of steps to solve a problem in a finite amount of time. The complexity of an algorithm measures the time and space required for inputs of different sizes. Time complexity indicates the running time, while space complexity measures storage usage. Analyzing algorithms involves determining their asymptotic worst-case, best-case, and average-case time complexities using notations like Big-O, Omega, and Theta. This provides insights into an algorithm's efficiency under different conditions.
1. Algorithm
An algorithm is a well defined list of steps for solving a particular
problem.
(or)
An Algorithm is a step by step procedure for performing some task in
a finite amount of time.
Every problem can be solved using various different methods. Each
method can be represented by an algorithm.
The choice of the best algorithm depends on the following factors:
An algorithm should be easy to understand code and debug (design).
An algorithm should make use of the computer resources efficiently.
2. Algorithm Complexity:
Complexity of an algorithm is a measure of time and/space required by an
algorithm for an input of a given size(n).
In order to find the complexity of algorithm
Space complexity: It indicates the amount of storage required for running
the algorithm i.e the amount of memory needed by the algorithm to run to
completion.
Time Complexity: Time complexity of an algorithm is the amount of time it
needs in order to run to completion.
Time –Space tradeoff:
Time space trade off is a way of solving a problem in less time by using
more storage space or by solving a problem in very little space by spending
a long time.
3. • All Algorithms must satisfy the following criteria:
• Input: Each algorithm should have zero or more inputs.
• Output: The algorithm should produce correct results at least one
output has to be produced.
• Definiteness: Each instruction should be clear and unambiguous
• Finiteness: The algorithm must terminate after a finite number of
steps.
• Effectiveness: Every instruction in the algorithm must be a basic
statement so that it can be executed by a person using pen and paper.
4. Mathematical Notations And Functions
Various mathematical notations and functions used
in the analysis of algorithm.
1. Floor and ceiling functions:
2. Remainder Function
3. Integer and absolute Value functions
4. Factorial Function
5. Summation Symbol
6. Permutations
7. Exponents and logarithms
5. • Algorithmic Notations:
1. Name of the algorithm
2. Comments
3. Data type
4. Variable Names
5. Steps
6. Assignment Statement
7. Expression
8. Input and Output
9. Go to Statement
10. End statement
11. Functions or procedures.
6. Name of the algorithm:
• Every algorithm is given an Identifying name
Comments
• Every algorithm can have brief description of the tasks. But it gives the
name and types of the variables used in the algorithm. Comments begin
with // and continues until the end of line. Comments are included only for
clarity.
Data type
• Data types such as integer,real,char,boolean and other data structures such
as array, pointers,structure are used.
• Examples: int max,min,sum
• int a[ n]
Variable Names
• A Variable can have alphabets , digits and underscore
• It can’t start with a digit
• A variable name can start with the alphabet and underscore only.
• No white Spaces is allowed
7. • A variable name must not be reserved word or keyword.
• Ex: go to, int
Steps
• Each Algorithm is made of a sequence of numbered steps and each step has an
ordered sequence of statements which steps describe tasks to be performed.
• The assignment statement is indicated by placing equal(=) between the variable
and expression.
Expression
• There are three Expressions
• Arithmetic expression used arithmetic operator such as (+,-,/,*)
• Relational expression used relational operator such as (>,<,<=,>=,==)
• Logical expression used logical operator such as (AND,OR,NOT)
Input and Output
• For input of data READ statement and for output of data WRITE statement are
used.
If more than one input or output data then we can use comma as a separator among
the variable names.
Example : READ x
READ x,y
WRITE x,y
8. Go to Statement
• The go to statement causes unconditional transfer of control to the step
referenced. Thus statement goto step N will cause transfer of control to
step N.
• Example: GOTO Step4;
End statement
• The end statement is used to terminate an algorithm. It is usually the last
step and algorithm name is written after the end.
Example: END LINEARSEARCH
Functions or procedures
• A function is used to return single value to the calling function transfer of
control and returning of the value are accomplished by return statement.
• Example: function function_name(parameters list).
10. Analysis of Algorithm
Analysis of an algorithm is refers to the task of determining how much computing
time and storage an algorithm requires.
To Compute the analysis of algorithm two phases are required.
1. Priori Analysis
2. Posterior Analysis
Priori Analysis:
This is one of the creative analysis of algorithms.
It determine the total time that statement will spend for the execution. It requires
essentially two items of information.
1. The statements frequency count
2. The time taken for one execution.
3. The product of these two numbers is the total time.
A priori Analysis limits itself to determine the frequency count of each statement.
The notations used in priori Analysis are Big Oh notation,Omega notation,Theta
notation.
11. • Posterior Analysis:
• Here we collect actual statistics about the algorithm ,conjunction of the
time and space while executing. Once the algorithm is written it has to be
tested.
• Testing a program consist of two major phases.
• A) Debugging
• B) Profiling
Debugging : It is the process of executing programs on sample data sets that
determine whether we get proper results . If faulty occurs it has to be
corrected.
Profiling: It is the process of executing programs on actual data sets and
measuring the time and space it takes to compute the results during
execution.
12. Asymptotic Notations
Asymptotic analysis refers to computing the running time of any
operation in mathematical units of computation.
Using asymptotic analysis we can conclude the best case
,average case, and worst case scenario of an algorithm.
The efficiency of the algorithm normally expressed using
asymptotic notations.
Following are the commonly used asymptotic notations.
1. Big Oh Notation(O)
2. Omega Notation(Ὠ)
3. Theta Notation(θ)
13. • Big Oh Notation(O)
• Big –Oh- Notation gives an upper bound on function f(n).The
upper bound of f(n) indicates that the function f(n) will be the
worst –case that it does not consume more than this computing
time.
• We define an algorithm’s worst-case time complexity by
using the Big-O notation, which determines the set of
functions grows slower than or at the same rate as the
expression. Furthermore, it explains the maximum amount of
time an algorithm requires considering all input values.
14.
15. • Omega Notation(Ὠ)
It is used to find lower bound behaviour of f(n). The lower
bound implies that the algorithm will take at least amount of
time that to run its completion.
• It defines the best case of an algorithm’s time complexity, the
Omega notation defines whether the set of functions will grow
faster or at the same rate as the expression. Furthermore, it
explains the minimum amount of time an algorithm requires
considering all input values.
16.
17. • Theta Notation(θ)
• It is used when the function f(n) is bounded between big Oh
and Omega.
• For some functions the lower bound and upper bound may be
same.
• There exist a special Notation to denote for function having
the same time complexity for lower bound and upper bound
and this notation is called theta notation.
• It defines the average case of an algorithm’s time complexity.
• f(n)= θ(g(n)) if and only if there exists three
positive constants c1, c2 and a natural number n0 such that
c1|g(n)| ≤ f(n) ≤ c2 | g(n)| for all n ≥ n0
19. Worst case, Best case, and Average case efficiencies
Best case:
• The best case occurs when the element to be searched is
found at the first location. In that case The algorithm make
only one comparisons.
• The number of comparisons = 1
• Tbest (n)=1
Worst Case:
• The worst case occurs when the element to be searched is
found at the last position or element to be searched is not
found at any location. In both the cases the number of
comparisons required is n.
• Tworst(n)=n
20. Average case :
• To find the average case efficiency, the following assumptions
are made.
a) The probability of successful search is equal to P (0< P< 1)
b) The probability of first match occurring in the i th position
of the array is the same for every i.
TAvg(n)=(n+1)/2