DAA - Unit 1 & 2
DAA - Unit 1 & 2
UNIT I INTRODUCTION 9
Notion of an Algorithm – Fundamentals of Algorithmic Problem Solving – Important Problem
Types – Fundamentals of the Analysis of Algorithmic Efficiency –Asymptotic Notations and their
properties. Analysis Framework – Empirical analysis - Mathematical analysis for Recursive and
Non-recursive algorithms - Visualization.
1.1NOTATION OF AN 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.
– Characteristics of an algorithm:
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 1
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Example:
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.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 2
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
gcd(60, 24) can be computed as follows: gcd(60, 24) = gcd(24, 12) = gcd(12, 0) = 12.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 3
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
– gcd(60, 24) = 2 . 2 . 3 = 12
Sieve of Eratosthenes
• Generating consecutive primes not exceeding any given integer n > 1.
• As an example, consider the application of the algorithm to finding the list of
• primes not exceeding n = 25:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Divisible by 2
23 5 7 9 11 13 15 17 19 21 23 25
Divisible by 3
23 5 7 11 13 17 19 23 25
Divisible by 5
23 5 7 11 13 17 19 23
ALGORITHM Sieve(n)
• //Implements the sieve of Eratosthenes
• //Input: A positive integer n > 1
• //Output: Array L of all prime numbers less than or equal to n
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 4
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Example problem 1:
1) Write an algorithm to find the maximum element in an array
Algorithm:
Algorithm max(A,N)
// A is an array of size N
result=A[0]
for i=0 to n-1 do
ifA[i]>result
then
result=A[i]
return result
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 5
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 6
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 7
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
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.
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.
ALGORITHM Sum(a,b)
//Problem Description: This algorithm performs addition of two numbers
//Input: Two integers a and b
//Output: Addition of two integers
c←a+b
return c
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.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 8
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 9
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
(i) Sorting
The sorting problem is to rearrange the items of a given list in nondecreasing (ascending)
order.
Sorting can be done on numbers, characters, strings or records.
To sort student records in alphabetical order of names or by student number or by student
grade-point average. Such a specially chosen piece of information is called a key.
An algorithm is said to be in-place if it does not require extra memory, E.g., Quick sort.
A sorting algorithm is called stable if it preserves the relative order of any two equal elements
in its input.
(ii) Searching
The searching problem deals with finding a given value, called a search key, in a given set.
E.g., Ordinary Linear search and fast binary search.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 10
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Strings comprise letters, numbers, and special characters; bit strings, which comprise zeros
and ones; and gene sequences, which can be modeled by strings of characters from the four
character alphabet {A, C, G, T}. It is very useful in bioinformatics.
Searching for a given word in a text is called string matching
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 11
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
DEFINITION
A function t (n) is said to be in O(g(n)), denoted t (n) ∈ O(g(n)), if t (n) is bounded
above by some constant multiple of g(n) for all large n, i.e., if there exist some positive
constant c and some nonnegative integer n0 such that
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 12
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Since, the definition gives us a lot of freedom in choosing specific values for constants c
and n0. We have c=101 and n0=5
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 13
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Example 1:
F(n) = 2n2+8 g1(n)=5n g2(n)=7n
C1=5 and c2=7
If 5n 2n2+8 7n
n=1 5 < 10 > 7
n=2 10 < 12 >14
n=3 15 < 27 >21
Therefore, f(n) εθ(g(n)) for all n> 2
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 14
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Example 2:
t1(n) ∈ O[g1(n)]
t1(n) < C1[g1(n)] for all n>n1
t2(n) ∈ O[g2(n)]
t2(n) < C2[g2(n)] for all n>n2
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 15
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Property 3:
If f(n) = O [g(n)]
Then
af(n) is O [g(n)] for any a.
Property 4:
f(n) = O [g(n)], h(n) = O [gl (n)]
then
f(n) + h(n) = O [g(n) + O [gl (n)]
Property 5:
If f(n) ∈ O [g(n)]
Then g(n) ∈ Ω [f(n)]
Property 6:
If there exists a function f1 such that f1 (n) = f2 (n) * c
Where c is the constant then f1 (n) and f2 (n) are
Equivalent that is
Property 7:
In a polynomial the highest power term dominates other terms,
For example:
If we obtain 3n3 + 2n2 +10 then its time complexity is O(n3)
Property 8:
F(n) and g(n) are functions
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 16
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Drawbacks
Dependence on the speed of a particular computer.
Dependence on the quality of a program implementing the algorithm.
The compiler used in generating the machine code.
The difficulty of clocking the actual running time of the program.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 17
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
So, we need metric to measure an algorithm’s efficiency that does not depend on these
extraneous factors.
One possible approach is to count the number of times each of the algorithm’s operations is
executed. This approach is excessively difficult.
The most important operation (+, -, *, /) of the algorithm, called the basic operation. Computing the
number of times the basic operation is executed is easy. The total running time is determined by basic
operations count.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 18
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Clearly, the running time of this algorithm can be quite different for the same list size n.
Worst-case efficiency
The worst-case efficiency of an algorithm is its efficiency for the worst case input of size n.
The algorithm runs the longest among all possible inputs of that size.
For the input of size n, the running time is Cworst(n) = n.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 19
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Backward substitution:
M(n) = M(n − 1) + 1 substitute M(n − 1) = M(n − 2) + 1
= [M(n − 2) + 1]+ 1
= M(n − 2) + 2 substitute M(n − 2) = M(n − 3) + 1
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 20
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
= [M(n − 3) + 1]+ 2
= M(n − 3) + 3
– Algorithm:
• //if only one disk has to be moved
• If(n=1) then
write (“the disk is moved from A to C
return
else
//move top n-1 disk from A to B using a
TOH (n-1, A,B,C)
//move remaining disk from B to C using a
TOH(n-1,B,C,A)
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 21
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Algorithm analysis:
– number of disks :n
– basic operation : 1 disk move
– number of moves : M(n)
– M(n) = M(n − 1) + 1+ M(n − 1) for n > 1
– initial condition M(1) = 1
= 23M(n − 3) + 22 + 2 + 1
EXAMPLE 3:
An investigation of a recursive version of the algorithm which finds the number of
binary digits in the binary representation of a positive decimal integer.
ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 return 1
else return BinRec(Ln/2J)+ 1
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 22
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Algorithm analysis:
backward substitutions
A(2k) = A(2k−1) + 1 substitute A(2k−1) = A(2k−2) + 1
k−2 k−2
= [A(2 ) + 1]+ 1= A(2 ) + 2 substitute A(2k−2) = A(2k−3) + 1
k−3 k−3
= [A(2 ) + 1]+ 2 = A(2 ) + 3 . . .
...
= A(2k−i) + i
...
= A(2k−k) + k.
Thus, we end up with A(2k) = A(1) + k = k, or, after returning to the original variable n = 2k and
hence k = log2 n,
A(n) = log2 n ϵ Θ (log2 n).
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 23
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
EXAMPLE 1:
Consider the problem of finding the value of the largest element in a list of n
numbers. Assume that the list is implemented as an array for simplicity.
Algorithm analysis
– Input size :n
– Operation that is most performed : for loop
– 2 operation in the loop body : comparison and assignment
– Comparison is considered as the basic operation
– Number of comparison will be same for all size of array
– No need to distinguish among the worst, average & best cases
– Let C(n) the number of times this comparison is executed
– algorithm makes one comparison on each execution of the loop, which is repeated for
each value of the loop’s variable i within the bounds 1 and n − 1
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 24
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
EXAMPLE 2:
Consider the element uniqueness problem: check whether all the elements in a
given array of n elements are distinct.
Algorithm analysis
– Input :n
– Basic operation: comparison of two elements
– limit our investigation to the worst case only
– 2 worst case scenario - arrays with no equal elements and arrays in which the last two
elements are the only pair of equal elements.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 25
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
EXAMPLE 3
Given two n × n matrices A and B, find the time efficiency of the definition-based algorithm for
computing their product C = AB. By definition, C is an n × n matrix whose elements are
computed as the scalar (dot) products of the rows of matrix A and the columns of matrix B:
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 26
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
Algorithm analysis
– An input’s size is matrix order n.
– There are two arithmetical operations (multiplication and addition) in the innermost
loop. But we consider multiplication as the basic operation.
– Let us set up a sum for the total number of multiplications M(n) executed by the
algorithm.
– Since this count depends only on the size of the input matrices, we do not have to
investigate the worst-case, average-case, and best-case efficiencies separately.
– There is just one multiplication executed on each repetition of the algorithm ’ s
innermost loop, which is governed by the variable k ranging from the lower bound 0 to
the upper bound n − 1.
– Therefore, the number of multiplications made for every pair of specific values of
variables i and j is
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 27
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
EXAMPLE 4
The following algorithm finds the number of binary digits in the binary representation of a
positive decimal integer.
ALGORITHM Binary(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
count ←1
while n > 1 do
count ←count + 1
n←n/2
return count
Algorithm analysis
– An input’s size is n.
– The loop variable takes on only a few values between its lower and upper limits.
– Since the value of n is about halved on each repetition of the loop, the answer should
be about log2 n.
– The exact formula for the number of times.
– The comparison n > 1 will be executed is actually log2 n + 1.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 28
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
– They include checking the accuracy of a theoretical assertion about the algorithm’s
efficiency
– comparing the efficiency of several algorithms for solving the same problem or different
implementations of the same algorithm
– developing a hypothesis about the algorithm’s efficiency class, and ascertaining the
efficiency of the program implementing the algorithm on a particular machine
– goal of the experiment – how the algorithm’s efficiency is to be measured
- insert a counter (or counters) into a program implementing the algorithm to
count the number of times the algorithm’s basic operation is executed
- time the program implementing the algorithm in question - use a system’s
command, such as the time command in UNIX
– It is important to keep several facts in mind,
(1) First, a system’s time is typically not very accurate, and you might get somewhat
different results on repeated runs of the same program on the same inputs. An
obvious remedy is to make several such measurements and then take their average
(or the median) as the sample’s observation point.
(2) Second, given the high speed of modern computers, the running time may fail to
register at all and be reported as zero. The standard trick to overcome this obstacle
is to run the program in an extra loop many times, measure the total running time,
and then divide it by the number of the loop’s repetitions.
(3) Third, on a computer running under a time-sharing system such as UNIX, the
reported time may include the time spent by the CPU on other programs,
which obviously defeats the purpose of the experiment.
– Profiling—is an important resource in the empirical analysis of an algorithm’s running time;
the data in question can usually be obtained from the system tools available in most
computing environments.
– That is - measuring time spent on different segments of a program can pinpoint a bottleneck
in the program’s performance that can be missed by an abstract deliberation about the
algorithm’s basic operation. Getting such data—called profiling
– make decisions about the sample size
– The principal advantage of size changing according to a pattern is that its impact is easier to
analyze.
– important issue concerning sizes in an experiment’s sample is whether several instances of the
same size should be included
– Generating random numbers on a digital computer is known to present a difficult problem
because, in principle, the problem can be solved only approximately. This is the reason
computer scientists prefer to call such numbers pseudorandom
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 29
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
– Alternatively, you can implement one of several known algorithms for generating
(pseudo)random numbers. The most widely used and thoroughly studied of such algorithms is
the linear congruential method.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 30
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
1.4.6 VISUALIZATION
In addition to the mathematical and empirical analyses of algorithms, there is yet a third way to
study algorithms. It is called algorithm visualization and can be defined as the use of images to
convey some useful information about algorithms
That information can be a visual illustration of an algorithm’s operation, of its performance on
different kinds of inputs, or of its execution speed versus that of other algorithms for the same
problem.
There are two principal variations of algorithm visualization:
o Static algorithm visualization
o Dynamic algorithm visualization, also called algorithm animation
Static algorithm visualization shows an algorithm’s progress through a series of still images.
Algorithm animation, on the other hand, shows a continuous, movie-like presentation of an
algorithm’s operations. Animation is an arguably more sophisticated option, which, of course, is
much more difficult to implement.
There are two principal applications of algorithm visualization: research and education.
Potential benefits for researchers are based on expectations that algorithm visualization may help
uncover some unknown features of algorithms. For example, Tower of Hanoi algorithm in which
odd- and even-numbered disks was colored in two different colors. He noticed that two disks of
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 31
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
the same color never came in direct contact during the algorithm’s execution. This observation
helped him in developing a better nonrecursive version of the classic algorithm.
The application of algorithm visualization to education seeks to help students learning algorithms.
The available evidence of its effectiveness is decisively mixed.
Question bank
UNIT – 1
2 marks
1. Why is the need of studying algorithms?
From a practical standpoint, a standard set of algorithms from different areas of computing
must be known, in addition to be able to design them and analyze their efficiencies. From a
theoretical standpoint the study of algorithms is the cornerstone of computer science.
2. What is algorithmic?
The study of algorithms is called algorithmic. It is more than a branch of computer science. It
is the core of computer science and is said to be relevant to most of science, business and technology.
3. What is an 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 finite amount of time.
An algorithm is step by step procedure to solve a problem.
5. What is the formula used in Euclid’s algorithm for finding the greatest common divisor of
two numbers?
Euclid‘s algorithm is based on repeatedly applying the equality
Gcd(m,n)=gcd(n,m mod n) until m mod n is equal to 0, since gcd(m,0)=m.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 32
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
6. What are the three different algorithms used to find the gcd of two numbers?
The three algorithms used to find the gcd of two numbers are
Euclid‘s algorithm
Consecutive integer checking algorithm
Middle school procedure
9. What is pseudocode?
A pseudocode is a mixture of a natural language and programming language constructs to
specify an algorithm. A pseudocode is more precisethan a natural language and its usage often yields
more concise algorithm descriptions.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 33
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
INTRODUCTION ]
The two classic geometric problems are
The closest pair problem: given n points in a plane find the closest pair among them
The convex hull problem: find the smallest convex polygon that would include all the points
of a given set.
K.Kamaraj, Assistant
Worst case, best case and average case efficiencies
Professor / CSE
It can be identified easily because it is usually the most time consuming operation in the
algorithms innermost loop.
KPRIET,
The functions 2n and n! are exponential growth functions, because these two functions grow
so fast that their values become astronomically large even for rather smaller values of n.
Coimbatore. Page
which is an input or inputs of size n for which the algorithm runs the longest among all possible
inputs of that size.
size.
34
which is an input or inputs for which the algorithm runs the fastest among all possible inputs of that
classical geometric
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 34
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
24. Mention the useful property, which can be applied to the asymptotic notations and its use?
If t1(n) ε O(g1(n)) and t2(n) ε O(g2(n)) then t1(n)+t2(n) ε max {g1(n),g2(n)} this property is
also true for Ω and θ notations. This property will be useful in analyzing algorithms that comprise of
two consecutive executable parts.
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 35
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
13 marks
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 36
CS8451
Design and Analysis
of Algorithms [UNIT I - INTRODUCTION ]
14. Suppose W satisfies the following recurrence equation and base case (where c is a constant:
W(n) =c.n + W(n/2) and W(1) = 1. What is the asymptotic order of W(n).
15. Solve the following recurrence relations
Prepared By: Mr. K.Kamaraj, Assistant Professor / CSE Department, KPRIET, Coimbatore. Page 37