Unit 1 - Analysis of Algorithm - 1
Unit 1 - Analysis of Algorithm - 1
Analysis of Algorithms
08-08-2024 CGPIT,Bardoli 2
Algorithm
08-08-2024 CGPIT,Bardoli 3
What is an algorithm?
• A step-by-step procedure, to solve the different kinds of problems.
• Suppose, we want to make a Chocolate Cake.
• An unambiguous sequence of computational steps that transform the input into
the output.
08-08-2024 CGPIT,Bardoli 5
Characteristics of an algorithm
• Finiteness: An algorithm must always terminate after a finite number of steps.
• Definiteness: Each step of an algorithm must be precisely defined.
• Input: An algorithm has zero or more inputs.
• Output: An algorithm must have at least one desirable output.
• Effectiveness: All the operations to be performed in the algorithm must be
sufficiently basic so that they can, in principle be done exactly and in a finite length
of time.
08-08-2024 CGPIT,Bardoli 6
Algorithm vs Pseudocode
Algorithm Pseudocode
An algorithm is a sequence of steps which Pseudocode is a detailed description of an
is utilized in order to solve a algorithm which is easier to read and is
computational problem. expressed in an English-like language.
08-08-2024 CGPIT,Bardoli 7
Algorithm vs Pseudocode
08-08-2024 CGPIT,Bardoli 8
Algorithm vs Flowchart
Algorithm Flowchart
An algorithm is step wise analysis of the Flowchart is a pictorial representation of
work to be done. an algorithm.
08-08-2024 CGPIT,Bardoli 9
Algorithm vs Flowchart
08-08-2024 CGPIT,Bardoli 10
Study of an algorithm includes..
• How to device an algorithm
• How to validate an algorithm
• How to analyze an algorithm
• Testing a program
• How to evaluate performance of an algorithm
08-08-2024 CGPIT,Bardoli 11
Importance of choosing a right algorithm
(Simple Multiplication Methods)
08-08-2024 CGPIT,Bardoli 12
Importance of choosing a right algorithm
(Simple Multiplication Methods)
3. à 𝒍𝒂 𝒓𝒖𝒔𝒔𝒆 multiplication
i. Write the multiplicand and multiplier side by side.
ii. Make two columns, one under each operand.
iii. Repeat step iv and v until the number in the left column is 1.
iv. Divide the number in the left hand column by 2, ignoring any
fractions.
v. Double the number in the right hand column by adding it to
itself.
vi. Next cross out each row where the number in the left hand
column is even.
vii. Finally add up the numbers that remain in the right hand
column.
08-08-2024 CGPIT,Bardoli 13
Importance of choosing a right algorithm
(Simple Multiplication Methods)
4. Multiplication by divide and conquer
Both the multiplicand and the multiplier must have the same number of digits and this number be
a power of 2. If not then it can be done by adding zeros on the left if necessary.
i. Multiply left half of the multiplicand by left half of multiplier and shift the
result by no. of digits of multiplier i.e. 4.
ii. Multiply left half of the multiplicand by right half of the multiplier, shift the
result by half the number of digits of multiplier i.e. 2.
iii. Multiply right half of the multiplicand by left half of the multiplier, shift the
result by half the number of digits of multiplier i.e. 2.
iv. Multiply right half of the multiplicand by right half of the multiplier the result is
not shifted at all.
08-08-2024 CGPIT,Bardoli 14
Efficiency of Algorithm
08-08-2024 CGPIT,Bardoli 15
Analysis of Algorithm
What is the analysis of an algorithm?
• Analyzing an algorithm means calculating/predicting the resources that the
algorithm requires.
• Two most important resources are computing time (time complexity) and storage
space (space complexity).
08-08-2024 CGPIT,Bardoli 16
Approaches for selecting efficient algorithm
There are two essential approaches to measuring algorithm efficiency:
Empirical analysis:
Program the algorithm and measure its running time on example instances
Theoretical analysis:
Derive a function which relates the running time to the size of instance
08-08-2024 CGPIT,Bardoli 17
Approaches for selecting efficient algorithm
Empirical (posteriori) approach Theoretical (priori) approach
Programming different competing Determining mathematically the resources
techniques & running them on various needed by each algorithm.
inputs using computer.
Implementation of different techniques Uses the algorithm instead of an
may be difficult. implementation.
The same hardware and software The speed of an algorithm can be
environments must be used for comparing determined independent of the
two algorithms. hardware/software environment.
We have to test our algorithm only on small We can consider larger no. of instances to
no. of instances. test our algorithm.
08-08-2024 CGPIT,Bardoli 18
Problem & Instance
Instance: An Instance of a problem consists of the input needed to compute the
solution to the problem.
Example:
Problem: to multiply two positive numbers
Instance: 981 𝑋 1234
Instance size: Any integer (generally 𝒏) that in some way measures the number of
components in an instance.
Examples:
Sorting problem: Instance size is number of elements to be sorted.
Graph problem: Instance size is number of nodes or edges or both.
08-08-2024 CGPIT,Bardoli 19
Efficiency of Algorithm
Theoretical
Approach
08-08-2024 CGPIT,Bardoli 20
Efficiency of Algorithm
• Generally time grows with the size of input, for example, sorting 100 numbers will take
less time than sorting of 10,000 numbers.
• So, running time of an algorithm is usually measured as a function of input size.
08-08-2024 CGPIT,Bardoli 21
08-08-2024 CGPIT,Bardoli 22