Académique Documents
Professionnel Documents
Culture Documents
Outline
• What is algorithm
• Characteristics of algorithm
• Performance/ Analysis of Algorithm
• Time complexity
• Space complexity
Algorithm
• Algorithm is a set of well defined instructions
in sequence to solve the problem.
• A step by step procedure that will accomplish
a desired task.
• A problem solving procedure + contain the
execution of a sequence of actions in a precise
order to complete a desired task.
Characteristics of algorithm
• Every Algorithm must satisfy the following properties:
• Input- There should be 0 or more inputs supplied
externally to the algorithm.
• Output- There should be at least 1 output obtained.
• Definiteness- Every step of the algorithm should be
clear and well defined.
• Finiteness- The algorithm should have finite number
of steps.
• Correctness- Every step of the algorithm must
generate a correct output.
Algorithm must have
• Specifications- Description of the procedure
• Pre-condition- Condition on input
• Body of the algorithm- Sequence of clear and
unambiguous instructions.
• Post-condition- Condition on output
Performance/ Analysis of Algorithm
• An algorithm is said to be efficient and fast, if it
takes less time to execute and consumes less
memory space.
• Analysis is the process of estimating the running
time of an algorithm or Used to find the best
algorithm to solve the problem.
• The performance of an algorithm is measured on
the basis of following properties :
– Time Complexity
– Space Complexity
Efficiency and complexity
• Efficiency
– How much time or space is required
– Measured in terms of common basic operations
• Complexity
– How efficiency varies with the size of the task
– Expressed in terms of standard functions of n
– E.g. O(n), O(n2 ), O(log n), O(n log n)
Time complexity
• It is the amount of time an algorithm takes to
execute.
• No. of memory access, no. of comparison, no. of
shifting operations etc. required some time.
• So time depends on the no. of instruction
executed.
• Time complexity is represented by function f(n).
• f(n) represents the no. of times all the statements
of algorithm will execute where n is the
input(size) of algorithm.
Common Time Complexities
• The function can any of the following:
1. Constant i.e f(n)= 1
2. Linear f(n)=n+1
3. Quadratic f(n)= n2 +1
4. Cubic f(n)= n3
5. Double Exponential f(n)=22n
6. Logarithmic f(n)=log n
7. Exponential f(n)=2n, 2n2
Example 1
• Consider the following algorithm fragment:
for i = 1 to n do
sum = sum + i ;
• The for loop executed n+1 times for i values
1,2,....... n, n+1.
• Each instruction in the body of the loop is
executed once for each value of i = 1,2,......, n.
• So number of steps executed is 2n+1.
Example 2
• Consider another algorithm fragment:
for i = 1 to n do
for j = 1 to n do
k = k +1
• From previous example, number of instruction
executed in the inner loop is which is the body of
outer loop.
• Total number of instruction executed is
= (n+1)+n(2n+1)= 2n2 +2n+1
Problem in absolute time
• To measure the time complexity in absolute time
unit has the following problems:
– The time required for an algorithm depends on
number of instructions executed, which is a complex
polynomial.
– The execution time of an instruction depends on
computer's power. Since, different computers take
different amount of time for the same instruction.
– Different types of instructions take different amount
of time on same computer.
• Complexity analysis technique abstracts away
these machine dependent factors .
Types of cases
• Usually, the time required by an algorithm
falls under three types −
• Best Case − Minimum time required for
program execution.
• Average Case − Average time required for
program execution.
• Worst Case − Maximum time required for
program execution.
Notation of Time Complexity
• It defines the order of growth of any algorithm
which is useful to measure the performance of
any algorithm.
• The different notations are:
1. Big oh O(worst case) upper bound
2. Big omega Ω(best case) lower bound
3. Theta Θ(average case)
4. Little oh o(strictly worst case)
5. Little omega Ω (strictly best case)
Notations
• Three main types of asymptotic order
notations are used in practice:
1. Big oh O(worst case) upper bound
2. Big omega Ω(best case) lower bound
3. Theta Θ(average case)
Big oh (O)
• The Big O notation defines an upper bound of an
algorithm, it bounds a function only from above.