0% found this document useful (0 votes)
28 views28 pages

Module-1 Part-1

Module 1 focuses on algorithm analysis and performance evaluation, introducing key concepts such as algorithm properties, efficiency, and design techniques. It covers methods for specifying algorithms, including natural language, pseudocode, and flowcharts, as well as the importance of proving correctness and analyzing efficiency. Examples like Euclid's algorithm for GCD and the Sieve of Eratosthenes illustrate practical applications of these concepts.
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)
28 views28 pages

Module-1 Part-1

Module 1 focuses on algorithm analysis and performance evaluation, introducing key concepts such as algorithm properties, efficiency, and design techniques. It covers methods for specifying algorithms, including natural language, pseudocode, and flowcharts, as well as the importance of proving correctness and analyzing efficiency. Examples like Euclid's algorithm for GCD and the Sieve of Eratosthenes illustrate practical applications of these concepts.
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/ 28

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

Algorithm 1:Euclid’s algorithm is based on applying repeatedly the equality


gcd(m,n)=gcd(n, m mod n), where m mod n is the remainder of the division
of m by n, until m mod n is equal to 0. Since gcd(m, 0) = m, the last value of
m is also the greatest common divisor of the initial m and n. gcd(60, 24) can
be computed as follows:gcd(60, 24) = gcd(24, 12) = gcd(12, 0) = 12.
Euclid’s algorithm for computing gcd(m, n) in simple steps
Step 1 : If n = 0, return the value of m as the answer and stop; otherwise, proceed
to Step 2.
Step 2 : Divide m by n and assign the value of the remainder to r.
Step 3 : Assign the value of n to m and the value of r to n. Go to Step 1
Euclid’s algorithm for computing gcd(m, n) expressed in pseudocode
ALGORITHM Euclid_gcd(m, n)
//Computes gcd(m, n) by Euclid’s algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠ 0 do How do we know that Euclid’s
r ←m mod n Algorithm eventually comes to a
m←n stop?
With Each iteration the value of
n←r
the second integer eventually
return m becomes 0, the algorithm stops.
Example 1
Example 2
Algorithm 2:
Consecutive integer checking algorithm for computing gcd(m, n)
Step 1 : Assign the value of min{m, n} to t.
Step 2 : Divide m by t. If the remainder of this division is 0, go to Step 3;
otherwise, go to Step 4.
Step 3 : Divide n by t. If the remainder of this division is 0, return the value
of t as the answer and stop; otherwise, proceed to Step 4.
Step 4 : Decrease the value of t by 1. Go to Step 2
Example 1
Algorithm 3:
Middle-school procedure for computing gcd(m, n)
Step 1: Find the prime factors of ‘m’.
Step 2: Find the prime factors of ‘n’.
Step 3: Identify all the common factors in the two prime expansions found in
Step 1 and Step 2. (If p is a common factor occurring pm and pn times
in m and n, respectively, it should be repeated min{pm , pn} times.)
Step 4: Compute the product of all the common factors and return it as the
greatest common divisor of the numbers given.
Example 1
Example 2: Sieve of Erathosthenes:

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.

Figure: Algorithm design and analysis process.


(i) Understanding the Problem
1. This is the first step in designing of algorithm. Step 1
2. Read the problem’s description carefully to understand the problem statement completely.
3. Ask questions for clarifying the doubts about the problem.
4. Identify the problem types and use existing algorithm to find solution.
5. Input (instance) to the problem and range of the input get fixed.

(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

Figure: Algorithm Specifications

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.

This specification is more useful for implementation of any language.


c. Flowchart
In the earlier days of computing, the dominant method for specifying algorithms was a
flowchart, this representation technique has proved to be inconvenient.
Flowchart is a graphical representation of an algorithm. It is a a method of expressing an
algorithm by a collection of connected geometric shapes containing descriptions of the
algorithm’s steps.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy