AD3351-DESIGN AND ANALYSIS
OF ALGORITHM
UNIT-1 INTRODUCTION
Prepared by
Mrs. Benilda M C
Assistant Professor/AI&DS
NOTION OF ALGORITHM
ALGORITHM
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 ALGORITHM
Some Important Points on Algorithm
CHARACTERISTICS OF AN
ALGORITHM
Input - Valid inputs must be clearly specified
Output – Produces at least one output- the result or solution of the problem
Definiteness - Each step must be clear and unambiguously specified
Finiteness - Terminates after a finite number of steps
Effectiveness - Steps must be simple enough to be performed using basic operations.
GCD(m,n) - Definition
gcd(m,n) – the largest integer that divides both m and n evenly, ie., with
a remainder of zero.
General method – gcd(m,n)
Eg: Compute GCD(60,24)
• Find common factors of 2 given numbers:
• Product of those common factors – gcd
=> gcd(24,18)=?
To illustrate the notion of the algorithm we consider 3
methods to solve the same problem – Computing the
greatest common divisor of 2 non-negative, not-both-
zero integers m,n => gcd(m,n)
Euclid’s Algorithm
Consecutive Integer Checking Algorithm
Middle-School Algorithm
Euclid’s Algorithm:
Compute gcd(60,24) using Euclid’s algorithm
Given: m=60 and n=24
Iteration 1:
n=24≠0 -> r=60 mod 24 -> r=12
Now, m=n=24 and n=r=12
Iteration 2:
n=12≠0 -> r=24 mod 12 -> r=0
Now, m=n=12 and n=r=0
Iteration 3:
n=0, condition fails and returns m value
Therefore, gcd(60,24)=12.
Middle-School Algorithm
GCD(24,12)
Given: m=24 and n=12
Step 1: prime factors of m => 2,2,2,3
Step 2: prime factors of n => 2,2,3
Step 3: common factors => 2,2,3
Step 4: product of common factors => 2x2x3=12
Consecutive Integer Checking Algorithm
Contd..
Eg: (i) Compute gcd(9,6) using Consecutive Integer
Checking Algorithm
(i) Compute gcd(24,12) using Consecutive Integer
Checking Algorithm
(iii) Compute gcd(60,24) using Consecutive Integer
Checking Algorithm
Drawback:
Doesn’t work properly when one of its inputs is ZERO
Sieve of Eratosthenes
Simple algorithm for generating consecutive primes not exceeding
any given integer n>1
Starts by initializing a list of primes from 2 to n
First Iteration – eliminate all multiples of 2 except 2
Next Iteration - eliminate all multiples of 3 except 3
Continuous until no more elements to be eliminated
Remaining integers – primes
Algorithm : Sieve of Eratosthenes
Step1: Create a list of consecutive integers from 2 through n – (1,2,3,
…..,n).
Step 2: Initially let p=2 the smallest prime no.
Step 3: Enumerate the multiples of p by counting in increments of p from
2p to n and mark them in the list (these will be 2p,3p,4p,…, the p itself
should not be marked).
Step 4: Find the smallest no in the list greater than p that is not marked. If
there is no such no stop. Otherwise let p now equal to this new no and
repeat from Step 3.
Step 5: When the algorithm terminates the numbers remaining not marked
in the list are all the primes below n.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
Important Problem Types
The different problem types are
1. Sorting
2. Searching
3. String processing
4. Graph problems
5. Combinatorial problems
6. Geometric problems
7. Numerical problems
Sorting
Sorting problem is one which rearranges the items of a given list in
ascending order.
For example in student’s information, we can choose to sort student records
in alphabetical order of names or by student register number. Such a
specially chosen piece of information is called a key.
Sorting solves many problems, the most important of them is searching:
dictionaries, telephone books, class lists, and so on are sorted.
Contd..
There are two important properties of sorting algorithms.
A sorting algorithm is called stable, if it preserves the relative order of any two equal
elements in its input.
For example, we have a list of students sorted alphabetically and if we sort the
student list based on their GPA: a stable algorithm will yield a list in which students
with same GPA will be sorted alphabetically.
The second feature of an algorithm is ‘in place’, if it does not require extra memory.
There are some sorting algorithms that are in place and those that are not.
Searching
The searching problem deals with finding a given value, called a search
key, in a given set.
The searching can be either a straightforward sequential search algorithm or
binary search algorithm based on different forms of representing the set.
Some algorithms work faster but require more memory, some are very fast
but applicable only to sorted arrays.
Searching, mainly deals with addition and deletion of records.
String processing
A String is a sequence of characters. It is mainly used in string handling
algorithms.
Most common ones are text strings, which consists of letters, numbers
and special characters.
Bit strings consist of zeroes and ones and gene sequences, which can be
modeled by strings of four-character alphabet{A,C,G,T}
Graph Problems
A graph is a collection of points called vertices,some of which are connected by
line segments called edges.
Graphs can be used for modeling a wide variety of real-life applications such as
transportation, communication networks, project scheduling, and games.
It includes graph traversal, shortest-path and topological sorting algorithms.
The most well-known examples of computationally hard problems are the
traveling salesman problem and graph-coloring problem
Contd…
The TSP is the problem of finding the shortest tour through n cities that visits
every city exactly once.
It arises in modern applications as circuit board and VLSI chip fabrication, X-
ray crystallography and genetic engineering.
The graph-coloring problem is to assign the smallest number of colors to
vertices of a graph so that no two adjacent vertices are of the same color .
Combinatorial Problems
The traveling salesman problem and the graph-coloring problem are examples of
combinatorial problems.
These are problems that ask us to find a combinatorial object such as
permutation, combination or a subset that satisfies certain constraints and has
some desired (e.g. maximizes a value or minimizes a cost).
Geometric Problems
Geometric algorithms deal with geometric objects such as points, lines and
polygons.
It also includes various geometric shapes such as triangles, circles etc.
The applications for these algorithms are in computer graphic, robotics etc.
The two problems most widely used are the closest-pair problems, given ‘n’
points in the plane, find the closest pair among them.
The convex-hull problem is to find the smallest convex polygon that would
include all the points of a given set.
Numerical Problems
This is another large special area of applications, where the problems involve
mathematical objects of continuous nature: solving equations, computing definite
integrals and evaluating functions and so on.
These problems can be solved only approximately.
These require real numbers, which can be represented in a computer only
approximately.
It can also lead to an accumulation of round-off errors.
Video Link
https://youtu.be/fLYV-Z76dUM