0% found this document useful (0 votes)
7 views22 pages

Unit 1 - Analysis of Algorithm - 1

The document outlines the fundamentals of algorithms, including their definition, characteristics, and the importance of selecting efficient algorithms. It discusses the analysis of algorithms focusing on time and space complexity, along with empirical and theoretical approaches for measuring efficiency. Additionally, it highlights the significance of understanding problem instances and how they relate to algorithm performance.

Uploaded by

n0buhosasyk3
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)
7 views22 pages

Unit 1 - Analysis of Algorithm - 1

The document outlines the fundamentals of algorithms, including their definition, characteristics, and the importance of selecting efficient algorithms. It discusses the analysis of algorithms focusing on time and space complexity, along with empirical and theoretical approaches for measuring efficiency. Additionally, it highlights the significance of understanding problem instances and how they relate to algorithm performance.

Uploaded by

n0buhosasyk3
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/ 22

Unit 1

Analysis of Algorithms

Prof. Purvi Tandel


Outline
• Algorithm
• Efficiency of algorithms
• Performance analysis of algorithm
• Average and worst case analysis Elementary operation
• Asymptotic Notation
• Analyzing control statements
• Solving recurrences

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.

Input Process Output


Ingredients Recipe Cake
08-08-2024 CGPIT,Bardoli 4
What is an algorithm?
• An algorithm is well defined computational procedure that takes some value, or set
of values as input and produces some value, or set of values as output.
Input

Algorithm Program 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.

The algorithm uses high-level constructs Pseudocode involves natural language


like snippet of code. with high-level programming builds.
Cooking recipe can be considered to be an Cooking recipe can be considered to be an
algorithm if it describes precisely how to pseudocode if it include vague notations
make certain dish, giving exact quantities like “add salt to taste” or “cook until
to use and detailed instructions for how tender”.
long to cook it.

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.

An algorithm is a precise or set of rules A flowchart is a diagram of the sequence


specifying how to solve some problem. of operations in a computer program.

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).

Why analysis is required?


• By analyzing some of the candidate algorithms for a problem, the most efficient
one can be easily identified.

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

In this course our focus will be on Theoretical analysis.

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

• The efficiency of an algorithm is a measure of the amount of resources consumed in


solving a problem of size 𝐧.
• The important resource is time, i.e., time complexity.

• To measure the efficiency of an algorithm it is required to measure its time complexity


using any of the following approaches: Empirical
1. To run it and measure how much processor time is needed. Approach
2. Mathematically computing how much time is needed as a function of input size.

Theoretical
Approach

08-08-2024 CGPIT,Bardoli 20
Efficiency of Algorithm

• Running time of an algorithm depends upon,


1. Input Size
2. Nature of Input

• 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.

• In theoretical computation of time complexity, Running time is measured in terms of


number of steps/primitive operations performed.

08-08-2024 CGPIT,Bardoli 21
08-08-2024 CGPIT,Bardoli 22

You might also like