Module-1 Part-1
Module-1 Part-1
Course objectives:
● To learn the methods for analyzing algorithms and evaluating
their performance.
● To demonstrate the efficiency of algorithms using asymptotic
notations.
Contents:
Part-1: Introduction
Part-2: Fundamentals of the Analysis of Algorithm Efficiency
Part-3: Brute Force Approaches
Vandana U
Assistant Professor
Department of AI-DS
MODULE-1
PART-1
INTRODUCTION
Definition:
An Algorithm is a sequence of unambiguous instructions for
solving a problem, i.e., for obtaining a required output for any
legitimate input in a finite amount of time.
Notion of an Algorithm
Algorithm to add 2 numbers:
Start
Input two numbers: num1 and num2
Compute the sum: sum = num1 +
num2
Output the sum
End
Algorithm to input 10 numbers and output
the sum
Algorithm to check if the stack is
full.
Properties of an Algorithm
Non-Ambiguity
Multiplicity Effectiveness
Finiteness
Output
Range of input
Input
Speed
Properties in Detail: 2. Range of Input
1. Non-ambiguity: The range of input must be specified.
Each step in an algorithm should be non-ambiguous. 3. Multiplicity
That means each instruction should be clear and The same algorithm can be represented in
precise. several ways. ie, we can write in simple
4. Speed english ,OR we can write it in the form of
pseudocode.
The algorithm is written using some specific ideas.
But such an algorithm should be efficient and 5. Finiteness:
should produce the output with fast speed. The algorithm should be finite., ie, after
performing required operations it should
6. Input:
terminate.
Zero or more quantities are extremely supplied.
8.Effectiveness:
7. Output: Every instruction must be basic so that it can be
Atleast one quantity is produced. carried out., in principle by a person using only
pencil & paper.
Example 1 : Finding GCD
The greatest common divisor(GCD) of two nonnegative integers m and n
(not-both-zero), denoted gcd(m, n), is defined as the largest integer that
divides both m and n evenly, i.e., with a remainder of zero.
Step 1: A simple algorithm for generating consecutive primes not exceeding any given
integer 'n'.
Step 2: The Algorithm starts by initializing a list of prime candidates with consecutive
integers from 2 to n.
Step 3: Then, on the first iteration of the algorithm, it eliminates all the multiples of 2 ie,4,6
and so on from the list.
Step 4: Then, it moves to the next item on the list, which is 3 , and eliminates its
multiples.(In this straightforward fashion there is an overhead because some numbers, such
as 6, are eliminated more than once).
Step 5: No pass for number 4 is needed, since 4 and all its multiples are multiples of 2 as well
and they were eliminated in the previous pass. The next remaining number on the list, which
is used on the third pass,ie,5.
Step 6: The algorithm continues in this fashion until no more numbers can be eliminated
from the list. The remaining integers of the list are the primes needed.
Example
Algorithm:
So now we can incorporate the sieve of Eratosthenes into the middle-school procedure to get
a legitimate algorithm for computing the greatest common divisor of two positive integers.
Note that special care needs to be exercised if one or both input numbers are equal to 1:
because mathematicians do not consider 1 to be a prime number, strictly speaking, the
method does not work for such inputs.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
A sequence of steps involved in designing and analyzing an algorithm is shown in the figure below.
(ii) Decision making The Decision making is done on the following: Step 2
(a) Ascertaining the Capabilities of the Computational Device
• In random-access machine (RAM), instructions are executed one after another (The central
assumption is that one operation at a time). Accordingly, algorithms designed to be
executed on such machines are called sequential algorithms.
• In some newer computers, operations are executed concurrently, i.e., in parallel. Algorithms
that take advantage of this capability are called parallel algorithms.
• Choice of computational devices like Processor and memory is mainly based on space and
time efficiency
(b) Choosing between Exact and Approximate Problem Solving
• The next principal decision is to choose between solving the
problem exactly or solving it approximately.
• An algorithm used to solve the problem exactly and produce
correct result is called an exact algorithm.
• If the problem is so complex and not able to get exact solution,
then we have to choose an algorithm called an approximation
algorithm. i.e., produces an approximate answer. E.g., extracting
square roots, solving nonlinear equations, and evaluating definite
integrals.
Step 2(Continued)
(c) Algorithm Design Techniques
• An algorithm design technique (or “strategy” or “paradigm”) is a
general approach to solving problems algorithmically that is applicable
to a variety of problems from different areas of computing.
• Algorithms+ Data Structures = Programs
• Though Algorithms and Data Structures are independent, but they are
combined together to develop program. Hence the choice of proper data
structure is required before designing the algorithm.
• Implementation of algorithm is possible only with the help of
Algorithms and Data Structures
• Algorithmic strategy / technique / paradigm are a general
approach by which many problems can be solved algorithmically. E.g.,
Brute Force, Divide and Conquer, Dynamic Programming, Greedy
Technique and so on. Step 2(Continued)
Step 3
(iii) Methods of Specifying an Algorithm
There are three ways to specify an algorithm. They are:
a. Natural language
b. Pseudocode
c. Flowchart
Pseudocode and flowchart are the two options that are most widely used nowadays for
specifying algorithms
a. Natural Language It is very simple and easy to specify an algorithm using natural
language. But many times specification of algorithm by using natural language is not
clear and thereby we get brief specification. Example: An algorithm to perform addition
of two numbers.
Step 3(Continued)
Such a specification creates difficulty while actually implementing it. Hence many
programmers prefer to have specification of algorithm by means of Pseudocode.
b. Pseudocode
• Pseudocode is a mixture of a natural language and programming language constructs.
Pseudocode is usually more precise than natural language.
• For Assignment operation left arrow “←”, for comments two slashes “//”,if condition, for,
while loops are used.
Step 3(Continued)
(iv) Proving an Algorithm’s Correctness
Once an algorithm has been specified then its correctness must be
proved.
An algorithm must yield a required result for every legitimate input in a
finite amount of time.
For example, the correctness of Euclid’s algorithm for computing the
greatest common divisor stems from the correctness of the equality
gcd(m, n) = gcd(n, m mod n).
A common technique for proving correctness is to use mathematical
induction because an algorithm’s iterations provide a natural sequence of
steps needed for such proofs.
The notion of correctness for approximation algorithms is less
straightforward than it is for exact algorithms. The error produced by the
algorithm should not exceed a predefined limit. Step 4
(v) Analyzing an Algorithm
• For an algorithm the most important is efficiency. In fact, there are two
kinds of algorithm efficiency. They are:
• Time efficiency, indicating how fast the algorithm runs, and
• Space efficiency, indicating how much extra memory it uses.
• The efficiency of an algorithm is determined by measuring both time
efficiency and space efficiency.
• So factors to analyze an algorithm are:
Time efficiency of an algorithm
Space efficiency of an algorithm
Simplicity of an algorithm
Generality of an algorithm
Step 5
(vi) Coding an Algorithm
• The coding / implementation of an algorithm is done by a suitable programming
language like C, C++, JAVA.
• The transition from an algorithm to a program can be done either incorrectly or very
inefficiently. Implementing an algorithm correctly is necessary. The Algorithm power
should not reduced by inefficient implementation.
• Standard tricks like computing a loop’s invariant (an expression that does not change its
value) outside the loop, collecting common subexpressions, replacing expensive
operations by cheap ones, selection of programming language and so on should be known
to the programmer.
• Typically, such improvements can speed up a program only by a constant factor, whereas a
better algorithm can make a difference in running time by orders of magnitude. But
once an algorithm is selected, a 10–50% speedup may be worth an effort.
• It is very essential to write an optimized code (efficient code) to reduce the burden of
compiler Step 6
THANK YOU