Unit I DAA
Unit I DAA
lOMoARcPSD|308 972 86
UNIT I INTRODUCTION
Notion of an Algorithm – Fundamentals of Algorithmic Problem Solving – Important Problem Types –
Fundamentals of the Analysis of Algorithm Efficiency – Analysis Framework – Asymptotic Notations and
their properties – Empirical analysis – Mathematical analysis of recursive and Non-recursive algorithms –
Visualization.
Give the Euclid’s algorithms for computing gcd (m,n) [Part A - MAY/JUNE 2016]
What is an algorithm? [Part A – APR/MAY/2017]
Write an algorithm to compute the greatest common divisor of two numbers.[Part A –
APR/MAY/2017& 2018]
What is basic Operation[Part A- APR/MAY 2018]
Define Best, Worst Average case time complexity[Part A- NOV/DEC 2018]
1. NOTION OF AN ALGORITHM
Problem to be solved
Algorithm
It is a step by step procedure with the input to solve the problem in a finite amountof timeto obtain
the required output.
lOMoARcPSD|308 972 86
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 mand n.
60 = 2·2·3·4
24 = 2·2·2·3
gcd(60,24)= 2·2·3 = 12
Practice problem Find
the GCD ( 12,8)Find GCD
( 80,24)
Explain in detail about the fundamentals of algorithmic Problem Solving [PartB- APR/MAY
2016]
List the steps in the fundamentals of algorithmic Problem Solving[Part B-APR/MAY 2013]
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.
Example: An algorithm to perform addition of two numbers.
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.
Such a specification creates difficulty while actually implementing it. Hence manyprogrammersprefer 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.
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.
Flowchart is a graphical representation of an algorithm. It is a a method of expressing an algorithmby a
collection of connected geometric shapes containing descriptions of the algorithm’s steps.
Flow
Stop
connectiv
ity Stop
Stop
state
FIGURE 1.4 Flowchart symbols and Example for two integer addition.
For example, the correctness of Euclid’s algorithm for computing the greatestcommon
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.
Simplicity of an algorithm
Generality of an algorithm
(ii) Searching
The searching problem deals with finding a given value, called a search key, in agiven set.
E.g., Ordinary Linear search and fast binary search.
The searching can be either a straightforward algorithm or binary search algorithmwhich is a
differentform.
These algorithms play an important role in real-life applications because they areused for
storingand retrieving information from large databases.
Some algorithms work faster but require morememory, some are very fast butapplicable only
to sorted arrays.
Searching, mainly deals with addition and deletion of records. In such cases, the data structures
and algorithms are chosen to balance among the required set of operations.
a. Analysis Framework
There are two kinds of efficiencies to analyze the efficiency of any algorithm. They are:
i. Time efficiency, indicating how fast the algorithm runs, and
ii. Space efficiency, indicating how much extra memory it uses.
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. One possible
approach is to count the number of times each of the algorithm’soperations is executed. This
approach is excessively difficult. The most importantoperation (+, -, *, /) of the algorithm,
called the basic operation.Computing thenumber of times, the basic operation is executed is
easy. The total running time is basics
of unit time.
(iii) Orders of Growth
A difference in running times on small inputs is not what really distinguishes efficient algorithms
from inefficient ones.
For example, the greatest common divisor of two small numbers, it is not immediately clear how
much more efficient Euclid’s algorithm is compared to the other algorithms, thedifference in
algorithm efficiencies becomes clear for larger numbers only.
TABLE 1.1 Values (approximate) of several functions important for analysis of algorithms
n log2n n n log2n n2 n3 2n 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•10 2.6•102 4.0•104
2
10 3.2 3.3 10 3.3•101 102 103 103 3.6•106
16 4 4 16 6.4•101 2.6•10 4.1•10 6.5•104 2.1•101
2 3 3
102 10 6.6 10 6.6•102 104 106 1.3•10 9.3•101
2 30 57
103 31 10 103 1.0•104 106 109
Very big
104 102 13 104 1.3•105 108 1012 computation
In the worst case, there is no matching of elements or the first matching element can foundat 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 inputof 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.
Best case efficiency
The best-case efficiency of an algorithm is its efficiency for the best case input ofsize n.
The algorithm runs the fastest among all possible inputs of that size n.
In sequential search, If we search a first element in list of size n. (i.e. first elementequal toa search
key), then the running time is Cbest(n) = 1
because the algorithm will inspect all n elements on all such inputs.
(𝑛+1)
(v)CAvg(n) =
2
Yet another type of efficiency is called amortized efficiency. It applies not to a single run ofan
algorithm but rather to a sequence of operations performed on the same data structure.
It turns out that in some situations a single operation can be expensive, but the total timefor an entire
sequence of n such operations is always significantly better than the worst- case efficiency of that single
operation multiplied by n.
Prove that the if f(n)=O(g(n)) and g(n)=O(f(n)), then f(n)=⍬g(n)[Part A- APR/MAY 2019]
State the transpose symmetry of theta and omega [Part A- NOV/DEC 2019]
Compare the orders of growth of n(n-1)/2 and n2. [Part A - MAY/JUNE 2016]
Elaborate asymptotic analysis of an algorithm with an example. [Part B- NOV/DEC2019]
Give the definition and graphical representation of O-Notation. [Part B -MAY/JUNE 2016]
Briefly explain Big oh Notation, Omega Notation, Theta Notation, Give examples. [PartB-
APR/MAY 2017 & 2018 & 2019]
To compare and rank such orders of growth, computer scientists use threenotations, they are:
o
i. O - Big oh notation
ii. Ω - Big omega notation
iii. Θ - Big theta notation
Let t(n) and g(n) can be any nonnegative functions defined on the set of natural numbers. The
algorithm’s running time t(n) usually indicated by its basic operation count C(n), and g(n), some
simple function to compare with the count.
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
≤ 101n2 (Ӭ 𝑛 ≤ 𝑛2)
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
Example 3: Prove the assertions 100𝑛 + 5 ∈ (𝑛). Proof: 100n
+ 5 ≤ 100n + 5n (for all n ≥ 1)
= 105n
i.e., 100n + 5
≤ 105ni.e., t(n)
≤ cg(n)
±100𝑛 + 5 ∈ (𝑛) with c=105 and n0=1
THEOREM: If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈ O(max{g1(n),g2(n)}).(The
analogous assertions are true for the Ω and Θ notations as well.)
PROOF: The proof extends to orders of growth the following simple fact about fourarbitrary realnumbers
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 integern1 such
that
t1(n) ≤ c1g1(n) for all n ≥ n1.
lOMoARcPSD|308 972 86
The property implies that the algorithm’s overall efficiency will be determined bythe part with a
higher order of growth, i.e., its least efficient part.
± t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}).
Summation formulas
Solve the following recurrence equation using iteration method or tree [Part B - NOV/DEC 2019]
Write an algorithm using recursion that determines the GCD of two numbers. Determine the
time and space Complexity.
Briefly explain the mathematical analysis of recursive and non-recursive algorithm. [Part B-
APR/MAY 2019 & 2022]
Give the General plan for analysing the time efficiency of recursive algorithms and use recurrence
to find number of moves for Towers of Hanoi Problem. [Part B- APR/MAY 2019]
6. MATHEMATICAL ANALYSIS FOR RECURSIVE ALGORITHMS
General Plan for Analyzing the Time Efficiency of Recursive Algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation.
3. Check whether the number of times the basic operation is executed can vary on different inputs of
the same size; if it can, the worst-case, average-case, and best- case efficiencies must be
investigated separately.
4. Set up a recurrence relation, with an appropriate initial condition, for the number of times the
basic operation is executed.
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 computeF(n) = F(n − 1) • n with
the following recursive algorithm.
(ND 2015)
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)•nfor n > 0.
The number of multiplications M(n) needed to compute it must satisfy the equality M(n) = M(n-1)
+ 1 for n > 0
To To
comp multip ly
ute F(n-1) b
F(n-
1)
lOMoARcPSD|308 972 86
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 (𝑛) = 𝑛(𝑛 − 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 thevalue with
which the sequence starts.
We can obtain this value by inspecting the condition that makes the algorithm stopits 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 ofmultiplications
M(n):
M(n) = M(n − 1) + 1 for
n > 0,M(0) = 0 for n
= 0.
= M(n − i) + i
…
= M(n − n) + n
= n.
Therefore M(n)=n
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 Busing
CTOH(n - 1, A, B, C)
Move top n-1 disks from B to C usingATOH(n -
1, B, C, A)
Algorithm analysis
The number of moves M(n) depends on n only, and we get the followingrecurrenceequation for it:
M(n) = M(n − 1) + 1+ M(n − 1) for n > 1.
With the obvious initial condition M(1) = 1, we have the following recurrence relationfor thenumber of
moves M(n):
M(n) = 2M(n − 1) + 1 for
n > 1,M(1) = 1.
We solve this recurrence by the same method of backward substitutions:
M(n) = 2M(n − 1) + 1 sub. M(n − 1) = 2M(n − 2) + 1
= 2[2M(n − 2) + 1]+ 1
= 22M(n − 2) + 2 + 1 sub. M(n − 2) = 2M(n − 3) + 1
19
lOMoARcPSD|308 972 86
= 22[2M(n − 3) + 1]+ 2 + 1
= 23M(n − 3) + 22 + 2 + 1 sub. M(n − 3) = 2M(n − 4) + 1
= 24M(n − 4) + 23 + 22 + 2 + 1
…
= 2iM(n − i) + 2i−1 + 2i−2 + . . . + 2 + 1= 2iM(n − i) + 2i − 1.
…
Since the initial condition is specified for n = 1, which is achieved for i = n
– 1, M(n) = 2n−1M(n − (n − 1)) + 2n−1 – 1 = 2n−1M(1) + 2n−1 − 1= 2n−1 + 2n−1 −
1= 2n − 1.
Thus, we have an exponential time algorithm
EXAMPLE 3: An investigation of a recursive version of the algorithm which finds thenumber ofbinary
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(ہn/2])+ 1
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 tothe recurrence A(n) = A(ہn/2]) +
1 for n > 1.
then, the initial condition is A(1) = 0.
The standard approach to solving such a recurrence is to solve it only for n =
2kA(2k) = A(2k−1) + 1 for k > 0,
A(20) = 0.
backward substitutions
A(2k) = A(2k−1) + 1 substitute A(2k−1) = A(2k−2) + 1
= [A(2k−2) + 1]+ 1= A(2k−2) + 2 substitute A(2k−2) = A(2k−3) + 1
= [A(2k−3) + 1]+ 2 = A(2k−3) + 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 andhence k = log2 n,A(n) =
log2 n ϵ Θ (log2 n).
Write an algorithm for determining the uniqueness of an array. Determine the time complexity
of your algorithm [Part b– Apr/May 2019]
Give the algorithm to check whether all the elements in a given array of n elements are distinct.
Find Worst case complexity of the same. [Part B - MAY/JUNE 2016]
20
lOMoARcPSD|308 972 86
EXAMPLE 1: Consider the problem of finding the value of the largest element in alist of nnumbers.
Assume that the list is implemented as an array for simplicity.
ALGORITHM MaxElement(A[0..n − 1])
//Determines the value of the largest element in a given array
//Input: An array A[0..n − 1] of real numbers
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].
The comparison operation is considered as the algorithm’s basic operation, because the
comparison is executed on each repetition of the loop and not the assignment.
The number of comparisons will be the same for all arrays of size n; therefore, there is no need to
distinguish among the worst, average, and best cases here.
Let C(n) denotes the number of times this comparison is executed. The algorithm makesone
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, inclusive. Therefore, the sum for C(n) is calculated as follows:
𝑛−
𝑛(𝑛) = ∑
=
i.e., Sum up 1 in repeated n-1 times
𝑛−
𝑛(𝑛) = ∑ = 𝑛 − ∈ ()
21
lOMoARcPSD|308 972 86
EXAMPLE 2: Consider the element uniqueness problem: check whether all theElements in agiven
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 inthe array).
Since the innermost loop contains a single operation (the comparison of twoelements), 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.
EXAMPLE 3: Consider matrix multiplication. Given two n × n matrices A and B, find the timeefficiency 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 Aand
the columns of matrix B:
22
lOMoARcPSD|308 972 86
where C[i, j ]= A[i, 0]B[0, j]+ . . . + A[i, k]B[k, j]+ . . . + A[i, n − 1]B[n − 1, j] for every pairofindices 0 ≤ i, j ≤
n − 1.
variablesi and j is
The total number of multiplications M(n) is expressed by the following triple sum:
Now, we can compute this sum by using formula (S1) and rule (R1)
The running time of the algorithm on a particular machine m, we can do it by the product. If
we consider, time spent on the additions too, then the total time on the machine is basis of
running time.
23
lOMoARcPSD|308 972 86
EXAMPLE 4 The following algorithm finds the number of binary digits in the binaryrepresentation
of a positive decimal integer.
• ALGORITHM Binary(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary
representationcount ←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 answershould beabout
log2 n.
The exact formula for the number of times.
The comparison > 1 will be executed is actually ہlog2 n] + 1.
8.ALGORITHM 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.
To accomplish this goal, an algorithm visualization uses graphic elements—points, line segments, two-
or three-dimensional bars, and so on—to represent some “interesting events” in the algorithm’s
operation.
o It 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.
Early efforts in the area of algorithm visualization go back to the 1970s. The watershed event happened
in 1981 with the appearance of a 30-minute color sound film titled Sorting Out Sorting.
This algorithm visualization classic was produced at the University of Toronto by RonaldBaecker with the
assistance of D. Sherman.
The success of Sorting Out Sorting made sorting algorithms a perennial favorite for algorithm animation.
Indeed, the sorting problem lends itself quite naturally to visual presentation via vertical or horizontal
bars or sticks of different heights or lengths, which need to be rearranged according to their sizes.
This presentation is convenient, however, only for illustrating actions of a typical sorting algorithm on
small inputs.
24
lOMoARcPSD|308 972 86
For larger files, Sorting Out Sorting used the ingenious idea of presenting data by a scatterplot of points
on a coordinate plane, with the first coordinate representing an item’s position in the file and the second
one representing the item’s value; with such a representation, the process of sorting looks like a
transformation of a “random” scatterplot of points into the points along a frame’s diagonal .
In addition, most sorting algorithms work by comparing and exchanging two given items at a time—an
event that can be animated relatively easily.
Since the appearance of Sorting Out Sorting, a great number of algorithm animations have been created,
especially after the appearance of Java and the World Wide Web in the 1990s.
They range in scope from one particular algorithm to a group of algorithms for the same problem (e.g.,
sorting) or the same application area (e.g., geometric algorithms) to general purpose animation systems.
At the end of 2010, a catalog of links to existing visualizations, maintained under the NSF-supported
AlgoVizProject, contained over 500links.
Unfortunately, a survey of existing visualizations found most of them to be of low quality, with the content
heavily skewed toward easier topics such as sorting [Sha07].
There are two principal applications of algorithm visualization:
research and
education.
Potential benefits for researchers are based on expectations that algorithm visualizationmay help uncover
some unknown features of algorithms. For example, one researcher used a visualization of the recursive
Tower of Hanoi algorithm in which odd- and even-numbered disks were colored in two different colors.
He noticed that two disks of the same color never came in direct contact during the
algorithm’s execution.
This observation helped him in developing a better non-recursive version of the classicalgorithm.
To give another example, Bentley and McIlroy [Ben93] mentioned using an algorithm animation system
in their work on improving a library implementation of a leading sorting algorithm.
The application of algorithm visualization to education seeks to help students learning algorithms.
The available evidence of its effectiveness is decisively mixed. Although some experiments did register
positive learning outcomes, others failed to do so.
The increasing body of evidence indicates that creating sophisticated software systems is not going to be
enough.
In fact, it appears that the level of student involvement with visualization might be more important than
specific features of visualization software.
In some experiments, low-tech visualizations prepared by students were more effective than passive
exposure to sophisticated software systems.
To summarize, although some successes in both research and education have been reported in the
literature, they are not as impressive as one might expect.
A deeper understanding of human perception of images will be required before the true potential of
algorithm visualization is fulfilled.
25