Daa Unit 1
Daa Unit 1
UNIT I INTRODUCTION
Problem to be solved
Algorithm
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 1
1151CS119-Design and Analysis of Algorithm
Algorithm must terminate after a finite number of steps.
Efficiency : Every instruction must be very basic and runs in short time.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 2
1151CS119-Design and Analysis of Algorithm
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.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 3
1151CS119-Design and Analysis of Algorithm
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 4
1151CS119-Design and Analysis of Algorithm
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.
(iii) Methods of Specifying an Algorithm
There are three ways to specify an algorithm. They are:
a. Natural language
b. Pseudocode
Algorithm Specification
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.
Step 1: Read the first number, say a.
Step 2: Read the first number, say b.
Step 3: Add the above two numbers and store the result in c.
Step 4: Display the result from c.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 5
1151CS119-Design and Analysis of Algorithm
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
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.
FIGURE 1.4 Flowchart symbols and Example for two integer addition.
(iv) Proving an Algorithm’s Correctness
Once an algorithm has been specified then its correctness must be proved.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 6
1151CS119-Design and Analysis of Algorithm
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 7
1151CS119-Design and Analysis of Algorithm
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.
(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.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 8
1151CS119-Design and Analysis of Algorithm
(iii) String processing
A string is a sequence of characters from an alphabet.
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
(iv) Graph problems
A graph is a collection of points called vertices, some of which are connected by
line segments called edges.
Some of the graph problems are graph traversal, shortest path algorithm,
topological sort, traveling salesman problem and the graph-coloring problem and
so on.
(v) Combinatorial problems
These are problems that ask, explicitly or implicitly, to find a combinatorial object
such as a permutation, a combination, or a subset that satisfies certain constraints.
A desired combinatorial object may also be required to have some additional
property such s a maximum value or a minimum cost.
In practical, the combinatorial problems are the most difficult problems in
computing.
The traveling salesman problem and the graph coloring problem are examples
of combinatorial problems.
(vi) Geometric problems
Geometric algorithms deal with geometric objects such as points, lines, and
polygons.
Geometric algorithms are used in computer graphics, robotics, and tomography.
The closest-pair problem and the convex-hull problem are comes under this
category.
(vii) Numerical problems
Numerical problems are problems that involve mathematical equations,
systems of equations, computing definite integrals, evaluating functions, and
so on.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 9
1151CS119-Design and Analysis of Algorithm
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 10
1151CS119-Design and Analysis of Algorithm
primality of a positive integer n. the input is just one number.
The input size by the number b of bits in the n’s binary representation is b=(log2 n)
+1.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 11
1151CS119-Design and Analysis of Algorithm
n log2n n n log2n n2 n3 2n n!
√n
1 1 0 1 0 1 1 2 1
2 1.4 1 2 2 4 4 4 2
4 2 2 4 8 16 64 16 24
8 2.8 3 8 2.4•101 64 5.1•102 2.6•102 4.0•104
10 3.2 3.3 10 3.3•101 102 103 103 3.6•106
16 4 4 16 6.4•101 2.6•102 4.1•103 6.5•104 2.1•1013
102 10 6.6 102 6.6•102 104 106 1.3•1030 9.3•10157
103 31 10 103 1.0•104 106 109
104 102 13 104 1.3•105 108 Very big
1012
2
105 3.2•10 17 105 1.7•106 computation
1010 1015
106 103 20 106 2.0•107 1012 1018
Clearly, the running time of this algorithm can be quite different for the same list size n.
In the worst case, there is no matching of elements or the first matching
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 12
1151CS119-Design and Analysis of Algorithm
element can found at last on the list. In the best case, there is matching of elements
at first on the list.
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.
Yet another type of efficiency is called amortized efficiency. It applies not to a single run of an
algorithm but rather to a sequence of operations performed on the same data structure.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 13
1151CS119-Design and Analysis of Algorithm
The efficiency analysis framework concentrates on the order of growth of an
algorithm’s basic operation count as the principal indicator of the algorithm’s efficiency.
To compare and rank such orders of growth, computer scientists use three
notations, they
O - Big oh notation
Ω - Big omega notation
Θ - Big theta notation
Example 1:
Where t(n) and g(n) are nonnegative functions defined on the set of natural numbers. O =
Asymptotic upper bound = Useful for worst case analysis = Loose bound.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 16
1151CS119-Design and Analysis of Algorithm
i.e., by definition t(n) ≥ cg(n), where c=1 and n0=0
(iii) Θ - Big theta notation
A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈ Θ(g(n)), if t(n) is bounded
both above and below by some positive constant multiples of g(n) for all large n, i.e., if
there exist some positive constants c1 and c2 and some nonnegative integer n0 such that
c2g(n) ≤ t (n) ≤ c1g(n) for all n ≥ n0.
Where t(n) and g(n) are nonnegative functions defined on the set of natural
numbers.
PROOF: The proof extends to orders of growth the following simple fact about four
arbitrary real numbers a1, b1, a2, b2: if a1 ≤ b1 and a2 ≤ b2, then a1 + a2 ≤ 2 max{b1, b2}.
Since t1(n) ∈ O(g1(n)), there exist some positive constant c1 and some
nonnegative integer n1 such that
t1(n) ≤ c1g1(n) for all n ≥ n1.
Similarly, since t2(n) ∈ O(g2(n)),
t2(n) ≤ c2g2(n) for all n ≥ n2.
Let us denote c3 = max{c1, c2} and consider n ≥ max{n1, n2} so
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 17
1151CS119-Design and Analysis of Algorithm
that we can use both inequalities. Adding them yields the following:
t1(n) + t2(n) ≤ c1g1(n) + c2g2(n)
≤ c3g1(n) + c3g2(n)
= c3[g1(n) + g2(n)]
≤ c32 max{g1(n), g2(n)}.
Hence, t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}), with the constants c and n 0
required by the definition O being 2c3 = 2 max{c1, c2} and max{n1, n2},
respectively.
The property implies that the algorithm’s overall efficiency will be determined by
the part with a higher order of growth, i.e., its least efficient part.
therefore t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}).
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 18
1151CS119-Design and Analysis of Algorithm
5. Solve the recurrence or, at least, ascertain the order of growth of its solution.
EXAMPLE 1: Compute the factorial function F(n) = n! for an arbitrary
nonnegative integer n. Since n!= 1•........• (n − 1) • n = (n − 1)! • n, for n ≥ 1 and
0!= 1 by definition, we can compute F(n) = F(n − 1) • n with the following
recursive algorithm.
ALGORITHM F(n)
//Computes n! recursively
//Input: A nonnegative integer n
//Output: The value of n!
if n = 0 return 1
else return F(n − 1) * n
Algorithm analysis
For simplicity, we consider n itself as an indicator of this algorithm’s input size.
i.e. 1.
The basic operation of the algorithm is multiplication, whose number of
executions we denote M(n). Since the function F(n) is computed according
to the formula F(n) = F(n −1)•n for n > 0.
The number of multiplications M(n) needed to compute it must satisfy the equality
M(n − 1) multiplications are spent to compute F(n − 1), and one more
multiplication is needed to multiply the result by n.
Recurrence relations
The last equation defines the sequence M(n) that we need to find. This
equation defines M(n) not explicitly, i.e., as a function of n, but implicitly as a
function of its value at another point, namely n − 1. Such equations are called
recurrence relations or recurrences.
Solve the recurrence relation M(n) = M(n − 1) + 1, i.e., to find an explicit
formula for
M(n) in terms of n only.
To determine a solution uniquely, we need an initial condition that tells us
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 19
1151CS119-Design and Analysis of Algorithm
the value with which the sequence starts. We can obtain this value by inspecting the
condition that makes the algorithm stop its recursive calls:
if n = 0 return 1.
This tells us two things. First, since the calls stop when n = 0, the smallest
value of n for which this algorithm is executed and hence M(n) defined is 0.
Second, by inspecting the pseudocode’s exiting line, we can see that when n = 0,
the algorithm performs no multiplications.
Thus, the recurrence relation and initial condition for the algorithm’s number of
multiplications
M(n):
M(n) = M(n −1) + 1 for n >
0, M(0) = 0 for n = 0.
Consider educational workhorse of recursive algorithms: the Tower of Hanoi puzzle. We have n disks
of different sizes that can slide onto any of three pegs. Consider A (source), B (auxiliary), and C
(Destination). Initially, all the disks are on the first peg in order of size, the largest on the bottom and
the smallest on top. The goal is to move all the disks to the third peg, using the second one as an
auxiliary.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 20
1151CS119-Design and Analysis of Algorithm
ALGORITHM TOH(n, A, C, B)
//Move disks from source to destination recursively
//Input: n disks and 3 pegs A, B, and C
//Output: Disks moved to destination as in the source order.
if n=1
Move disk from A to C
else
Move top n-1 disks from A to B
using C TOH(n - 1, A, B, C)
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 21
1151CS119-Design and Analysis of Algorithm
− 1= 2n−1 + 2n−1 − 1= 2n − 1.
Thus, we have an exponential time algorithm
Algorithm analysis
The number of additions made in computing BinRec(⎝n/2]) is A(⎝n/2]),
plus one more addition is made by the algorithm to increase the returned value by
1. This leads to the recurrence A(n) = A(⎝n/2]) + 1 for n > 1.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 22
1151CS119-Design and Analysis of Algorithm
1.8 MATHEMATICAL ANALYSIS FOR NON-RECURSIVE
ALGORITHMS General Plan for Analyzing the Time
Efficiency of Nonrecursive Algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation (in the innermost loop).
3. Check whether the number of times the basic operation is executed depends
only on the size of an input. If it also depends on some additional property,
the worst-case, average-case, and, if necessary, best-case efficiencies have to
be investigated separately.
4. Set up a sum expressing the number of times the algorithm’s basic operation is
executed.
5. Using standard formulas and rules of sum manipulation either find a closed
form formula for the count or at the least, establish its order of growth.
Algorithm analysis
The measure of an input’s size here is the number of elements in the array, i.e., n.
There are two operations in the for loop’s body:
o The comparison A[i]> maxval and
o The assignment maxval←A[i].
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 23
1151CS119-Design and Analysis of Algorithm
EXAMPLE 2: Consider the element uniqueness problem: check whether all the
Elements in a given array of n elements are distinct.
ALGORITHM UniqueElements(A[0..n − 1])
//Determines whether all the elements in a given array are distinct
//Input: An array A[0..n − 1]
//Output: Returns “true” if all the elements in A are distinct and “false” otherwise
for i ←0 to n − 2 do
for j ←i + 1 to n − 1 do
if A[i]= A[j ] return false
return true
Algorithm analysis
The natural measure of the input’s size here is again n (the number of elements in
the array).
Since the innermost loop contains a single operation (the comparison of two
elements), we
should consider it as the algorithm’s basic operation.
The number of element comparisons depends not only on n but also on
whether there are equal elements in the array and, if there are, which array
positions they occupy. We will limit our investigation to the worst case
only.
One comparison is made for each repetition of the innermost loop, i.e., for
each value of the loop variable j between its limits i + 1 and n − 1; this is
repeated for each value of the outer loop, i.e., for each value of the loop
variable i between its limits 0 and n − 2.
Department of CSE , Vel Tech Rangarajan Dr.Sagunthala R&D Institute of Science and Technology. Page 24