Algo
Algo
Asymptotic Analysis
LEARNING OBJECTIVES
Analyzing Algorithms
The process of comparing 2 algorithms rate of growth with respect
to time, space, number of registers, network, bandwidth etc is
called analysis of algorithms.
This can be done in two ways
Tower A
1. Priori Analysis: This analysis is done before the execution;
the main principle behind this is frequency count of Tower B
fundamental instruction.
This analysis is independent of CPU, OS and system Tower C
architecture and it provides uniform estimated values. Figure 1 Towers of Hanoi
3.82 | Unit 3 • Algorithms
Assume that the number of disks is ‘n’. To get the largest To move ‘3’ disks from tower A to tower ‘C’ requires 7 disk
disk to the bottom of tower B, we move the remaining (n movements
– 1) disks to tower C and then move the largest to tower B. \ For ‘n’ disks, the number of disk movements required
Now move the disks from tower C to tower B. is 2n – 1 = 23 – 1 = 7
Example:
Time complexity
T(n) = 1 + 2T(n – 1)
3 T(n) = 1 + 2(1 + 2 (T(n – 2)))
2 T(n) = 1 + 2 + 22 T(n – 2)
1 T(n) = 1 + 2 + 22 (1 + 2T(n – 3))
A B C T(n) = 1 + 2 + 22 + 23 + T(n – 3)
T(n) = 1 + 2 + 22 + … + 2i–1 + 2i T(n – i)
n −1
T ( n ) = ∑ 2i
2 i =0
1 3
A B C The time complexity is exponential, it grows as power of 2.
\ T(n) @ O(2n)
Space complexity
The space complexity of an algorithm is the amount of
1 2 3
memory it needs to run to completion. The measure of the
A B C
quantity of input data is called the size of the problem. For
example, the size of a matrix multiplication problem might
be the largest dimension of the matrices to be multiplied.
The size of a graph problem might be the number of edges.
3 The limiting behavior of the complexity as size increases is
1 2 called the asymptotic time complexity.
A B C
• It is the asymptotic complexity of an algorithm which
ultimately determines the size of problems that can be
solved by the algorithm.
• If an algorithm processes inputs of size ‘n’ in time cn2 for
3 some constant c, then we say that the time complexity of
2 1 that algorithm is O(n2), more precisely a function g(n) is
A B C said to be O( f (n)) if there exists a constant c such that
g(n) ≤ c( f (n)) for all but some finite set of non-negative
values for n.
• As computers become faster and we can handle larger
problems, it is the complexity of an algorithm that deter-
3 2 1 mines the increase in problem size that can be achieved
A B C with an increase in computer speed.
• Suppose we have 5 algorithms Algorithm 1 – Algorithm 5
with the following time complexities.
The following figure gives the sizes of problems that can • Main drawback of using adjacency matrix is that it
be solved in one second, one minute, and one hour by each requires |V|2 storage even if the graph has only O(|V|)
of these five algorithms. edges.
• Another representation for a graph is by means of lists.
Maximum
The adjacency list for a vertex v is a list of all vertices
Problem Size
Time W adjacent to V. A graph can be represented by |V| adja-
Algorithm Complexity 1 sec 1 min 1 hour cency lists, one for each vertex.
Algorithm – 1 n 1000 6 × 104 3.6 × 106
Algorithm – 2 n log n 140 4893 2.0 × 105 Example:
Algorithm – 3 n2 31 244 1897
1 2
3
Algorithm – 4 n 10 39 153
Algorithm – 5 2n 9 15 21
4 3
From the above table, we can say that different algorithms
will give different results depending on the input size. Figure 2 Directed graph
Algorithm – 5 would be best for problems of size 2 ≤ n ≤ 9,
Algorithm – 3 would be best for 10 ≤ n ≤ 58, Algorithm – 2
1 2 3 4
would be best for 59 ≤ n ≤ 1025, and Algorithm – 1 is best
for problems of size greater than 1024. 1 0 1 0 1
2 0 0 1 0
3 0 0 0 0
SET REPRESENTATION
4 0 1 1 0
A common use of a list is to represent a set, with this rep-
resentation the amount of memory required to represent a Figure 3 Adjacency matrix
set is proportional to the number of elements in the set. The
amount of time required to perform a set operation depends
on the nature of the operation. Vertex – 1 2 4 0
1 15
6 18
2 13
7 8 16
3 4
9 11 14
17
5 10 12
(c)
Left child Right child Figure 6 (a) Pre-order, (b) Post-order (c) In-order
1 2 6
2 3 4 Post-order traversal
3 0 0 A post-order traversal of T is defined recursively as follows:
4 0 5
5 0 0 1. Visit in post-order the sub trees with roots v1, v2, v3,
6 7 8 … vk in that order.
7 0 0 2. Visit the root r.
8 0 9
9 0 10 In-order Traversal
10 0 0
An in-order traversal is defined recursively as follows:
Figure 5 A binary tree and its representation
1. Visit in in-order the left sub tree of the root ‘r’.
• Vertex 3 is of depth ‘2’, height ‘0’ and the level is 2 2. Visit ‘r’.
(Height of tree - depth of ‘3’ = 4 – 2 = 2). 3. Visit in inorder the right sub tree of r.
• A binary tree is represented by 2 arrays: left child and
right child. Example: Consider the given tree
• A binary tree is said to be complete if for some integer
C
k, every vertex of depth less than k has both a left child
and a right child and every vertex of depth k is a leaf. A D
B
complete binary tree of height k has exactly (2k+1 – 1)
E
vertices.
A
• A complete binary tree of height k is often represented by
a single array. Position 1 in the array contains the root.
What are the pre-order, post-order and in-order traversals of
The left child of the vertex in position ‘i’ is located at
the above tree?
position ‘2i’ and the right child at position ‘2i + 1’.
Solution: Pre-order – CBADE
Tree Traversals Post-order – ABEDC
Many algorithms which make use of trees often traverse the In-order – ABCDE
tree in some order. Three commonly used traversals are pre-
order, postorder and inorder.
DATA STRUCTURE
Pre-order Traversal A data structure is a way to store and organize data in-order
A pre-order traversal of T is defined recursively as follows: to facilitate access and modifications. No single data struc-
ture works well for all purposes, it is important to know the
1. Visit the root.
strengths and limitations of several data structures.
2. Visit in pre-order the sub trees with roots v1, v2 … vk in
that order.
Efficiency
11 Algorithms devised to solve the same problem often differ
18
dramatically in their efficiency. Let us compare efficiencies
16 17 of Insertion sort and merge sort; insertion sort, takes time
12
14
17 16
equal to C1n2 to sort ‘n’ elements, where C1 is a constant
13 15 13 that does not depend on ‘n’. It takes time proportional to
12
n2, merge sort takes time equal to C2nlog n, C2 is another
18
15 constant that also does not depend on ‘n’. Insertion sort has
14 11
a smaller constant factor than merge sort (C1 < C2) constant
(a) (b) factors are far less significant in the running time.
Chapter 1 • Asymptotic Analysis | 3.85
Merge sort has a factor of ‘log n’ in its running time, ASYMPTOTIC NOTATIONS
insertion sort has a factor of ‘n’, which is much larger.
Asymptotic notations are mostly used in computer science
Insertion sort is faster than merge sort for small input sizes,
to describe the asymptotic running time of an algorithm.
once the input size ‘n’ becomes large enough, merge sort
As an example, an algorithm that takes an array of size n
will perform better. No matter how much smaller C1 is than
as input and runs for time proportional to n2 is said to take
C2. There will always be a crossover point beyond which
O(n2) time.
merge sort is faster.
5 Asymptotic Notations:
Example: Consider 2 computers, computer A (faster
computer), B (slower computer). Computer A runs insertion • O (Big-oh)
sort and computer B runs merge sort. Each computer is • q (Theta)
given 2 million numbers to sort. Suppose that computer A • W (Omega)
executes one billion instruction per second and computer B • o (Small-oh)
executes only 10 million instructions per second, computer • w
A is 100 times faster than computer B (C1 = 4, C2 = 50).
How much time is taken by both the computers? How to Use Asymptotic Notation
Solution: Insertion sort takes C1 * n2 time for Algorithm Analysis?
Merge sort takes C2 * n * log n time Asymptotic notation is used to determine rough estimates
C1 = 4, C2 = 50 of relative running time of algorithms. A worst-case anal-
Computer A takes ysis of any algorithm will always yeild such an estimate,
because it gives an upper bound on the running time T(n) of
4 × ( 2 × 106 ) 2 instructions the algorithm, that is T(n) g(n).
≅ 4000 seconds
10 9 instructions/second Example:
is better than B, using the fact that n2(quadratic) is better • The lower order terms of an asymptotically positive func-
than n3(cubic) time, since n2 ∈ O(n3). tion can be ignored in determining asymptotically tight
bounds because they are insignificant for large n.
• A small fraction of the highest order term is enough to
Order of Growth dominate the lower order term. Thus setting C1 to a value
In the rate of growth or order of growth, we consider only that is slightly smaller than the coefficient of the highest
the leading term of a formula. Suppose the worst case run- order term and setting C2 to a value that is slightly larger
ning time of an algorithm is an2 + bn + c for some constants permits the inequalities in the definition of q-notation to
a, b and c. The leading term is an2. We ignore the leading be satisfied. If we take a quadratic function f (n) = an2 +
term’s constant coefficient, since constant factors are less bn + c, where a, b and c are constants and a > 0. Throwing
significant than the rate of growth in determining compu- away the lower order terms and ignoring the constant
tational efficiency for large inputs. Thus we can write, the yields f (n) = q (n2).
worst-case running time is q(n2). • We can express any constant function as q(n0), or q(1) we
We usually consider one algorithm to be more efficient shall often use the notation q(1) to mean either a constant
than another if its worst-case running time has a lower order or a constant function with respect to some variable.
of growth. Due to constant factors and lower order terms,
this evaluation may be in error for small inputs. But for O-Notation
large inputs, q(n2) algorithm will run more quickly in the We use O-notation to give an upper bound on a function,
worst-case than q(n3) algorithm. within a constant factor.
q-Notation Cg(n)
A function f (n) belongs to the set q(g(n)) if there exists a f(n)
positive constant C1 and C2 such that it can be “sand witched”
between C1g(n) and C2g(n) for sufficiently large n. We write
f (n) ∈ q (g(n)) to indicate that f (n) is a member of q (g(n))
or we can write f (n) = q (g(n)) to express the same notation. n0 n
Example 2: Let f (n) = 5.5n2 – 7n, verity whether f (n) is is on or above Cg(n). For any 2 functions f (n) and g(n) we
O(n2) have f (n) = q(g(n)) if f (n) = O(g(n)) and f (n) = W(g(n)).
From the above statement we can say that, an2 + bn + c =
Solution: Let C be a constant such that
q(n2) for any constants a, b and c, where a > 0, immediately
7 implies that
5.5n 2 − 7 n ≤ Cn 2 , or n ≥
c − 5.5 \ an2 + bn + c = W(n2)
0 ≤ Cg(n) ≤ f (n), ∀ n ≥ n0
W (omega)-notation
n0
n0 n
C 2g(n)
The W-notation is used for asymptotically lower bound- 0 ≤ C 2g(n) ≤ f(n) ≤ C 1g(n), ∀ n ≥ n0
ing a function. We would use W(big-omega) notation to n0
represent a set of functions that lower bounds a particular
function. (c) f (n) = q(g(n))
Figure 7 A diagrammatic representation of the asymptotic notations
Definition We say that a function f (n) is big-omega of g(n) O, W and q
written as f (n) = W(g(n)) if there exists positive constants C • W-notation describes a lower bound; it is used to bound
and n0 such that the best-case running time of an algorithm. The best-case
0 ≤ Cg(n) ≤ f (n) , ∀ n ≥ n0 running time of insertion sort is W(n). The running time
of insertion sort falls between W(n) and O(n2), since it
The intuition behind W-notation is shown in the above falls anywhere between a linear function of ‘n’ and a
figure. For all values ‘n’ to the right of n0, the value of f (n) quadratic function of ‘n’.
3.88 | Unit 3 • Algorithms
• When we say that the running time of an algorithm is x − 1 < x ≤ x ≤ x < x + 1 for any integer n,
W(g(n)), we mean that no matter what particular input of
size ‘n’ is chosen for each value of n, the running time on n n
that input is at least a constant times g(n), for sufficiently 2 + 2 = n,
large ‘n’.
For any real number n ≥ 0 and integer a, b > 0
O-notation
n
The asymptotic upper bound provided by O-notation may a n
or may not be asymptotically tight. The bound 2n3 = O(n3) =
is asymptotically tight, but the bound 2n = O(n2) is not. b ab
We use O-notation to denote an upper bound that is not
asymptotically tight.
n
w -notation a n
=
By analogy, w-notation is to W-notation as o-notation is to b ab
O-notation. We use w-notation to denote a lower bound that
is not asymptotically tight.
It is defined as
f (n) ∈ w(g(n)) if and only if g(n) ∈ o(f (n)) Polynomials
Given a non-negative integer k, a polynomial in n of degree
k
‘k’ is a function p(n) of the form p (n) = ∑ ai ni
Comparison of functions i =0
Transitivity Where the constants a0, a1, … ak are the coefficients of
1. f (n) = q(g(n)) and g(n) = q(h(n)) the polynomial and ak ≠ 0.
⇒ f (n) = q(h(n)) For an asymptotically positive polynomial p(n) of degree
2. f (n) = O(g(n)) and g(n) = O(h(n)) k, we have p(n) = q(nk)
⇒ f (n) = O(h(n))
3. f (n) = W(g(n)) and g(n) = W(h(n)) Exponentials
⇒ f (n) = W(h(n))
4. f (n) = o(g(n)) and g(n) = o(h(n)) For all real a > 0, m and n, we have the following identities:
⇒ f (n) = o(h(n)) a0 = 1
5. f (n) = w(g(n)) and g(n) = w(h(n)) a1 = a
⇒ f (n) = w(h(n)) 1
a −1 =
Reflexivity a
1. f (n) = q(f (n)) (am)n = amn
2. f (n) = O(f (n)) (am)n = (an)m
3. f (n) = W(f (n)) aman = am+n
x 2 x3 ∞
xi
Symmetry ex = 1 + x + + +⋯ = ∑
f (n) = q(g(n)) if and only if g(n) = q(f (n)) 2 ! 3! i =0 i !
Substitution Method
log b a n = n log b a
In this method one has to guess the form of the solution.
log c a It can be applied only in cases when it is easy to guess the
log b a =
log c b form of the answer. Consider the recurrence relation
Cn 2
T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1)
The sub-problem size for a node at depth ‘i’ is n/4i, at this Master Method
depth, the size of the sub-problem would be n = 1, when n/4i Let a ≥ 1 and b > 1 be cons-tants, let f (n) be a function
= 1 or i = log4n, the tree has log4n+1 levels. and let T(n) be defined on the non-negative integers by the
• We have to determine the cost at each level of the tree. recurrence
Each level has 3 times more nodes than the level above,
so the number of nodes at depth ‘i’ is 3i. T(n) = aT(n/b) + f (n)
• Sub problem sizes reduce by a factor of ‘4’ for each level T(n) can be bounded asymptotically as follows
we go down from the root, each node at depth i, for i = 0,
1, 2 … log4n–1, has a cost of c(n/4i)2. 1. If f (n) = O(nlogba–∈) for some constant ∈ > 0, then T(n)
= q(nlogba)
Total cost over all nodes at depth i, for i = 0, 1, … log4n–1 2. If f (n) = q(nlogba) then T(n) = q(nlogba. log n)
2 i
n 3 3. If f (n) = W(nlogba+∈) for some constant ∈ > 0, and
= 3i * c i = cn 2
4 16 if af (n/b) ≤ cf (n) for some constant c < 1 and all
The last level, at depth log4n has 3i nodes = 3log4n = nlog43 each sufficiently large n, then T(n) = q (f (n)).
contributing cost T(1), for a total cost of nlog43 T(1), which is
q (nlog43) cost of the entire tree is equal to sum of costs over Note: In the first case, not only must f (n) be smaller than n
all levels. logba, it must be polynomially smaller. That is, f (n) must be
2 asymptotically smaller than nlogba by a factor of n∈, for some
3 2 3 constant ∈ > 0.
T (n) = cn 2 + cn + cn 2 + ⋯ +
16 16 In the third case, not only must f (n) be larger than nlogba,
log 4n −1 it must be polynomially larger and in addition satisfy the
3 regularity condition af (n/b) ≤ Cf (n).
cn 2 + ⋯ + θ (n log 43 )
16
log 4n−1 i Example: Consider the given recurrence relation T(n)
3 2
= ∑
i =0
cn + θ (n 4 )
16
log 3
= 9T(n/3) + n.
To apply master theorem, the recurrence relation must be in
∞
3
i the following form:
< ∑ cn 2 + θ (n log 43 )
i = 0 16 T(n) = aT(n/b) + f (n)
=
1
cn 2 + θ (n log 43 ) a = 9, b = 3, f (n) = n
3
1− nlogba = nlog39 = n2
16
Since f (n) = O(nlog39–∈), where ∈ = 1
16
= 2 cn + θ (n log 43 ) = O(n 2 ) We can apply case 1 of the master theorem and the solution
13 is T(n) = q(n2).
Chapter 1 • Asymptotic Analysis | 3.91
EXERCISES
Practice Problems 1 10. Solve the recurrence relation using master method:
Directions for questions 1 to 15: Select the correct alterna- T(n) = 4T (n/2) + n2
tive from the given choices. (A) q(n log n) (B) q(n2 log n)
(C) q(n )
2
(D) q(n3)
1. What is the time complexity of the recurrence relation
n 11. Arrange the following functions according to their
T (n) = 2T + n 2 ? order of growth (from low to high):
2
(A) q(n2) (B) q(n) (A) 3 n , 0.001n 4 + 3n3 + 1, 3n , 22 n
(C) q(n3) (D) q(n log n) (B) 3n , 22 n , 3 n , 0.001n 4 + 3n3 + 1
2. What is the time complexity of the recurrence relation
n (C) 22 n , 3 n , 3n , 0.001n 4 + 3n3 + 1
by using masters theorem T (n) = 2T + n ?
2 (D) 3
n , 22 n , 3n , 0.001n 4 + 3n3 + 1
(A) q(n2) (B) q(n)
12. The following algorithm checks whether all the ele-
(C) q(n3) (D) q(n log n)
ments in a given array are distinct:
3. What is the time complexity of the recurrence relation
Input: array A[0 … n – 1]
n
by using master theorem, T (n) = 2T + n 0.51 Output: true (or) false
4
(A) q(n2) (B) q(n) For i ← 0 to n – 2 do
(C) q(n3) (D) (n0.51) For j ← i + 1 to n – 1 do
4. What is the time complexity of the recurrence relation if A[i] = A[ j] return false
n
using master theorem, T (n) = 7T + n 2 ? return true
3 The time complexity in worst case is
(A) q(n2) (B) q(n) (A) q(n2) (B) q(n)
(C) q(n3) (D) (log n) (C) q(log n) (D) q(n log n)
5. Time complexity of f (x) = 4x2 - 5x + 3 is 13. The order of growth for the following recurrence rela-
(A) O(x) (B) O(x2) tion is T(n) = 4T(n/2) + n3, T(1) = 1
3/2
(B) O(x ) (D) O(x0.5) (A) q(n) (B) q(n3)
6. Time complexity of f (x) = (x2 + 5 log2 x)/(2x + 1) is (C) q(n )2
(D) q(log n)
(A) O(x) (B) O(x2) n
(C) O(x3/2) (D) O(x0.5) 14. Time complexity of T (n) = 2T + 3 is
4
( )
7. For the recurrence relation, T (n) = 2T n + lg n,
which is tightest upper bound? (A) q ( n log n) (B) q ( n log n )
(A) T(n) = O(n2) (B) T(n) = O(n3)
(C) q ( n ) (D) q(n2)
(C) T(n) = O(log n) (D) T(n) = O(lg n lg lg n)
15. Consider the following three claims
8. Consider T(n) = 9T(n/3) + n, which of the following is
(I) (n + k)m = q(nm), where k and m are constants
TRUE?
(II) 2n + 1 = O(2n)
(A) T(n) = q(n2) (B) T(n) = q(n3)
(III) 22n + 1 = O(2n)
(C) T(n) = W(n ) 3
(D) T(n) = O(n)
Which one of the following is correct?
9. If f (n) is 100 * n seconds and g(n) is 0.5 * n seconds then
(A) I and III (B) I and II
(A) f (n) = g(n) (B) f (n) = W(g(n))
(C) II and III (D) I, II and III
(C) f (n) = w(g(n)) (D) None of these
(A) (i) is true (ii) is false (B) Both are true (C) Investigation of worst case is more complex than
(C) Both are false (D) (ii) is true (i) is false average case.
4. 2n = x (n ), x is which notation?
2 3 (D) None of these
(A) Big-oh (B) Small-oh 11. Time complexity of T(n) = T(n/3) + T(2n/3) + O(n) is
(C) W – notation (D) q – notation (A) O(1)
5. Master method applies to recurrence of the form T(n) (B) O(n log n)
= a T(n/b) + f (n) where (C) O(log n)
(A) a ≥ 1, b > 1 (B) a = 1, b > 1 (D) O(n2)
(C) a > 1, b = 1 (D) a ≥ 1, b ≥ 1 12. Solve the recurrence relation to find T(n): T(n) = 4(n/2)
6. What is the time complexity of the recurrence relation +n
using master method? (A) q(n2) (B) q(log2n)
(C) q(n2 log2n) (D) q(n3)
n
T (n) = 4T + n 13. What is the worst case analysis for the given code?
2 int search (int a[ ], int x, int n)
(A) q(n )2
(B) q(n) {
(C) q(log n) (D) q(n log n) int i;
7. Use the informal definitions of O, q W to determine these for (i = 0 ; i < n; i ++)
assertions which of the following assertions are true. if (a [i] = = x)
(A) n(n + 1)/2 ∈ O(n3) (B) n(n + 1)/2 ∈ O(n2) return i;
(C) n(n + 1)/2 ∈ W(n) (D) All the above return –1;
}
8. Match the following: (A) O(n) (B) O(n log n)
(i) Big-oh (A) ≥ (C) O(log n) (D) O(n2)
(ii) Small-o (B) ≤ 14. Find the time complexity of the given code.
(iii) W (C) = void f (int n)
{
(iv) q (D) <
if (n > 0)
(v) w (E) >
{
(A) (i) – D, (ii) – A, (iii) – C, (iv) -B , (v) – E f (n/2);
(B) (i) – B, (ii) – D, (iii) – A, (iv) – C, (v) – E f (n/2);
(C) (i) – C, (ii) – A, (iii) – B, (iv) – E, (v) – D }
(D) (i) – A, (ii) – B, (iii) – C, (iv) – D, (v) – E }
(A) q(n2)
9. Which one of the following statements is true? (B) q(n)
(A) Both time and space efficiencies are measured as (C) q(n log n)
functions of the algorithm input size. (D) q(2n)
(B) Only time efficiencies are measured as a function
of the algorithm input size. 15. The running time of the following algorithm procedure
(C) Only space efficiencies are measured as a function A(n)
of the algorithm input size. if n ≤ 2
(D) Neither space nor time efficiencies are measured return (1)
as a function of the algorithm input size. else
return ( A( n ))
10. Which of the following is true?
is described by
(A) Investigation of the average case efficiency is con-
siderably more difficult than investigation of the (A) O( n log n)
worst case and best case efficiencies.
(B) O(log n)
(B) Investigation of best case is more complex than
average case. (C) O(log log n)
(D) O(n)
Chapter 1 • Asymptotic Analysis | 3.93
(A) Q(log n) (B) Q(n) executed on input of size n. Which of the following is
(C) Q(n log n) (D) Q(n2) ALWAYS TRUE? [2012]
12. The running time of an algorithm is represented by (A) A(n) = W(W(n)) (B) A(n) = Q(W(n))
the following recurrence relation: [2009] (C) A(n) = O(W(n)) (D) A(n) = o(W(n))
17. The recurrence relation capturing the optimal execu-
n n ≤ 3 tion time of the Towers of Hanoi problem with n discs
T ( n) = n is [2012]
T 3 + cn otherwise (A) T(n) = 2T(n – 2) + 2
(B) T(n) = 2T(n – 1) + n
Which one of the following represents the time com-
(C) T(n) = 2T(n/2) + 1
plexity of the algorithm?
(D) T(n) = 2T(n – 1) + 1
(A) q(n) (B) q(n log n) 18. A list of n strings, each of length n, is sorted into
(C) q(n2) (D) q(n2 log n) lexicographic order using the merge sort algorithm.
13. Two alternative packages A and B are available for The worst-case running time of this computation is
processing a database having 10k records. Package A [2012]
requires 0.0001 n2 time units and package B requires (A) O(n log n) (B) O(n2log n)
10n log10 n time units to process n records. What is (C) O(n2 + log n) (D) O(n2)
the smallest value of k for which package B will be 19. Consider the following function:
preferred over A? [2010]
int unknown (int n) {
(A) 12 (B) 10
(C) 6 (D) 5 int i, j, k = 0;
14. An algorithm to find the length of the longest mono- for (i = n/2; i < = n; i++)
tonically increasing sequence of numbers in an array for (j = 2; j < = n; j = j*2)
A[0 : n - 1] is given below. k = k + n/2;
Let L denote the length of the longest monotonically return (k);
increasing sequence starting at index in the array.
}
Initialize Ln-1 = 1,
The return value of the function is [2013]
For all i such that 0 ≤ i ≤ n - 2 (A) Q(n2) (B) Q(n2log n)
Li {= 1 + Li +1 , if A[i ] < A [i + 1], (C) Q(n3) (D) Q(n3log n)
1 otherwise 20. The number of elements that can be sorted in Q(log n)
time using heap sort is [2013]
Finally the length of the longest monotonically
(A) Q(1)
increasing sequence is Max (L0, L1,…Ln–1)
Which of the following statements is TRUE? [2011] (B) Θ( log n )
(A) The algorithm uses dynamic programming para-
log n
digm. (C) Θ
(B) The algorithm has a linear complexity and uses log log n
branch and bound paradigm. (D) Q(log n)
(C) The algorithm has a non-linear polynomial com-
21. Which one of the following correctly determines the
plexity and uses branch and bound paradigm.
solution of the recurrence relation with T(1) = 1
(D) The algorithm uses divide and conquer paradigm.
n
15. Which of the given options provides the increasing T (n) = 2T + log n ? [2014]
order of asymptotic complexity of functions f1, f2, f3 2
and f4? [2011] (A) q(n) (B) q(n log n)
(C) q(n2) (D) q(log n)
f1(n) = 2n
22. An algorithm performs (log N)1/2 find operations, N
f2(n) = n3/2 insert operations, (log N)1/2 delete operations, and (log
f3(n) = n log2n N)1/2 decrease-key operations on a set of data items
f 4 (n) = n log 2 n with keys drawn from a linearly ordered set. For a
(A) f3, f2, f4, f1 (B) f3, f2, f1, f4 delete operation, a pointer is provided to the record
(C) f2, f3, f1, f4 (D) f2, f3, f4, f1 that must be deleted For the decrease – key opera-
tion, a pointer is provided to the record that has its
16. Let W(n) and A(n) denote respectively, the worst-
key decreased Which one of the following data struc-
case and average-case running time of an algorithm
tures is the most suited for the algorithm to use, if the
Chapter 1 • Asymptotic Analysis | 3.95
34. Consider the recurrence function Which one of the following is the time complexity
of the most time-efficient implementation of enqueue
2T
T (n) =
( n ) +1 n>2 and dequeue, respectively, for this data structure?
[2018]
2,
0<n≤2 (A) q(1), q(1) (B) q(1), q(n)
(C) q(n), q(1) (D) q(n), q(n)
Then T(n) in terms of Q notation is [2017]
(A) Q (log log n) (B) Q (log n) 37. Consider the following C code. Assume that unsigned
long int type length is 64 bits.
(C) Θ ( n) (D) Q (n) unsigned long int fun (unsigned long int n) {
unsigned long int i, j = 0, sum = 0;
35. Consider the following C function.
for (i = n; i > 1. i = i/2) j++;
int fun (int n) {
for (; j > 1; j = j/2) sum++;
int i, j;
for(i = 1; i <= n; i++) { return (sum);
for (j = l; j < n; j += i) { }
printf{“ %d %d”,i, j); The value returned when we call fun with the input 240
} is: [2018]
} (A) 4 (B) 5
(C) 6 (D) 40
Chapter 1 • Asymptotic Analysis | 3.97
ANSWER KEYS
EXERCISES
Practice Problems 1
1. A 2. D 3. D 4. A 5. B 6. A 7. D 8. A 9. A 10. B
11. A 12. A 13. B 14. A 15. B
Practice Problems 2
1. A 2. A 3. A 4. B 5. A 6. A 7. D 8. B 9. A 10. A
11. B 12. A 13. A 14. B 15. C
LEARNING OBJECTIVES
SORTING ALGORITHMS digit is in the set {1, 2,… k}, then radix sort can sort the num-
bers in q (d (n + k)) time. Where ‘d’ is constant. Radix sort runs
Purpose of sorting in linear time.
Sorting is a technique which reduces problem complexity and • Bucket sort, requires knowledge of the probabilistic distribution
search complexity. of numbers in the input array.
• Insertion sort takes q (n2) time in the worst case. It is a fast
inplace sorting algorithm for small input sizes.
• Merge sort has a better asymptotic running time q (n log n), but
it does not operate in inplace. MERGE SORT
• Heap sort, sorts ‘n’ numbers inplace in q (n log n) time, it uses a Suppose that our division of the problem yields ‘a’ sub problems,
data structure called heap, with which we can also implement a
1
priority queue. each of which is th size of the original problem. For merge
• Quick sort also sorts ‘n’ numbers in place, but its worst – case b
running time is q (n2). Its average case is q (n log n). The con- sort, both a and b are 2, but sometimes a ≠ b. If we take D(n) time
stant factor in quick sort’s running time is small, This algorithm to divide the problem into sub problems and C(n) time to combine
performs better for large input arrays. the solutions of the sub problems into the solution to the original
• Insertion sort, merge sort, heap sort, and quick sort are all com- problem. The recurrence relation for merge sort is
parison based sorts; they determine the sorted order of an inpu-
θ (1) if n ≤ c,
tarray by comparing elements. T ( n) =
• We can beat the lower bound of W (n log n) if we can gather aT ( n /b) + D( n) + C ( n) otherwise
information about the sorted order of the input by means other
than comparing elements. Running time is broken down as follows:
• The counting sort algorithm, assumes that the input numbers are Divide: This step computes the middle of the sub array, which
in the set {1, 2, …. k}. By using array indexing as a tool for takes constant time q (1).
determining relative order, counting sort can sort n numbers in
q (k + n) time. Thus counting sort runs in time that is linear in Conquer: We solve 2 sub problems of size (n/2) each recursively
size of the input array. which takes 2T(n/2) time.
• Radix sort can be used to extend the range of counting sort. If Combine: Merge sort procedure on an n-element sub array takes
there are ‘n’ integers to sort, each integer has ‘d’ digits, and each time q (n).
Chapter 2 • Sorting Algorithms | 3.99
• Worst case running time T(n) of merge sort The array is already sorted, but our algorithm does not
know if it is completed. The algorithm needs one whole
0(1) if n ≤1 pass without any swap to know it is sorted.
T ( n) =
aT ( n / 2) + θ ( n) if n > 1 Third pass:
cn
(1 2 4 5 8) → (1 2 4 5 8)
cn
(1 2 4 5 8) → (1 2 4 5 8)
cn cn
cn (1 2 4 5 8) → (1 2 4 5 8)
2
2 (1 2 4 5 8) → (1 2 4 5 8)
Finally the array is sorted, and the algorithm can terminate.
log n cn cn cn cn cn
4 4 4 4 Algorithm
void bubblesort (int a [ ], int n)
{
int i, j, temp;
c c c c c c c c cn
for (i=0; i < n-1; i++)
{
Total: cn log n + cn for (j=0; j < n – 1 – i; j++)
Figure 1 Recurrence tree
if (a [j] > a [j + 1])
{
The top level has total cost ‘cn’, the next level has total cost temp = a [j + 1];
c(n/2) + c(n/2) = cn and the next level has total cost c(n/4) a [j + 1] = a [j];
+ c(n/4) + c(n/4) + c(n/4) = cn and so on. The ith level has a [j] = temp;
total cost 2i c (n/2i) = cn. At the bottom level, there are ‘n’ }
nodes, each contributing a cost of c, for a total cost of ‘cn’. }
The total number of levels of the ‘recursion tree’ is log n + 1. }
There are log n + 1 levels, each costing cn, for a total cost
of cn (log n + 1) = cn log n + cn ignoring the low–order term INSERTION SORT
and the constant c, gives the desired result of q (n log n). Insertion sort is a comparison sort in which the sorted array is
built one entry at a time. It is much less efficient on large lists
than more advanced algorithms such a quick sort, heap sort,
BUBBLE SORT (or) merge sort. Insertion sort provides several advantages.
Bubble sort is a simple sorting algorithm that works by
• Efficient for small data sets.
repeatedly stepping through the list to be sorted, compar-
• Adaptive, i.e., efficient for data set that are already sub-
ing each pair of adjacent items, and swapping them if they
stantially sorted. The complexity is O(n + d), where d is
are in the wrong order. The pass through the list is repeated
the number of inversions.
until no swaps are needed, which indicates that the list is
• More efficient in practice than most other simple quad-
sorted. The algorithm gets its name from the way smaller
ratic, i.e., O(n2) algorithms such as selection sort (or)
elements ‘bubble’ to the top of the list.
bubble sort, the best case is O(n).
Example: Take the array of numbers ‘5 1 4 2 8’and sort the • Stable, i.e., does not change the relative order of elements
array from lowest number to greatest number using bubble with equal keys.
sort algorithm. In each step, elements underlined are being • In-place i.e., only requires a constant amount O(1) of
compared. additional memory space.
First pass: • Online, i.e., can sort a list as it receives it.
(5 1 4 2 8) → (1 5 4 2 8), here algorithm Algorithm
compares the first 2 elements and swaps them
Insertion sort (A)
(1 5 4 2 8) → (1 4 5 2 8), swap (5 > 4)
For (j ← 2) to length [A]
(1 4 5 2 8) → (1 4 2 5 8), swap (5 > 2)
Do key ← A [j]
(1 4 2 5 8) → (1 4 2 5 8), since these ele-
i ←j – 1;
ments are already in order, algorithm does not swap them.
While i > 0 and A [i] > key
Second pass: {
(1 4 2 5 8) → (1 4 2 5 8) Do A [i + 1] ← A [i]
(1 4 2 5 8) → (1 2 4 5 8), swap since (4 > 2) i ← i - 1
(1 2 4 5 8) → (1 2 4 5 8) }
(1 2 4 5 8) → (1 2 4 5 8) A [i + 1] ← key
3.100 | Unit 3 • Algorithms
Read the figure row by row. Elements to the left of A[ j] that BINARY SEARCH TREES
are greater than A[ j] move one position to the right and A[ j] Search trees are data structures that support many
moves into the evacuated position. dynamic, set operations, including SEARCH, MINIMUM,
MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT
SELECTION SORT and DELETE. A search tree can be used as a dictionary and
as a priority Queue. Operations on a binary search tree take
Selection sort is a sorting algorithm, specifically an
time proportional to the height of the tree. For a complete
in-place comparison sort. It has O(n2) complexity, making it
binary tree with ‘n’ nodes, basic operations run in q(log n)
inefficient on large lists.
worst-case time. If the tree is a linear chain of ‘n’ nodes, the
The algorithm works as follows:
basic operations take q(n) worst-case time.
1. Find the minimum value in the list. A binary search tree is organized, in a binary tree such a
2. Swap it with the value in the first position. tree can be represented by a linked data structure in which
3. Repeat the steps above for the remainder of the list each node is an object. In addition to key field, each node
(starting at the second position and advancing each contains fields left, right and P that point to the nodes cor-
time). responding to its left child, its right child, and its parent,
Chapter 2 • Sorting Algorithms | 3.101
respectively. If the child (or) parent is missing, the appropri- the partially sorted array. After removing the largest item, it
ate field contains the value NIL. The root node is the only reconstructs heap, removes the largest remaining item, and
node in the tree whose parent field is NIL. places, it in the next open position from the end of the par-
tially sorted array. This is repeated until there are no items
Binary search tree property left in the heap and the sorted array is full. Elementary
The keys in a binary search tree are always stored in such a implementations require two arrays one to hold the heap
way as to satisfy the binary search tree property. and the other to hold the sorted elements.
Let ‘a’ be a node in a binary search tree. If ‘b’ is a node • Heap sort inserts the input list elements into a binary
in the left sub tree of ‘a’, key [b] ≤ key [a] heap data structure. The largest value (in a max-heap) or
If ‘b’ is a node in the right sub tree of ‘a’ then key [a] ≤ key the smallest value (in a min-heap) is extracted until none
[b]. remain, the value having been extracted in sorted order.
8 Example: Given an array of 6 elements: 15, 19, 10, 7, 17,
10 16, sort them in ascending order using heap sort.
7
14 Steps:
6 9
Figure 2 Binary search tree. 1. Consider the values of the elements as priorities and
build the heap tree.
The binary search tree property allows us to print out all 2. Start delete Max operations, storing each deleted
keys in a binary search tree in sorted order by a simple element at the end of the heap array.
recursive algorithm called an inorder tree.
If we want the elements to be sorted in ascending order, we
Algorithm need to build the heap tree in descending order-the greatest
element will have the highest priority.
INORDER-TREE-WALK (root [T ])
INORDER-TREE-WALK (a) 1. Note that we use only array, treating its parts
differently,
1. If a ≠ NIL 2. When building the heap-tree, part of the array will be
2. Then INORDER-TREE-WALK (left [a]) considered as the heap, and the rest part-the original
3. Print key [a] array.
4. INORDER-TREE-WALK (right [a]) 3. When sorting, part of the array will be the heap and
It takes q(n) time to walk an n-node binary search tree, since the rest part-the sorted array.
after the initial call, the procedure is called recursively twice Here is the array: 15, 19, 10, 7, 17, 6.
for each node in the tree.
Let T(n) denote the time taken by IN-ORDER-TREE-
WALK, when it is called on the root of an n-node subtree. Building the Heap Tree
INORDER-TREE-WALK takes a small, constant The array represented as a tree, which is complete but not
amount of time on an empty sub-tree (for the test x ≠ NIL). ordered.
So T(1) = C for some positive constant C.
For n > 0, suppose that INORDER-TREE-WALK is 15 19 10 7 17 16
15
called on a node ‘a’ whose left subtree has k nodes and
whose right subtree has n – k – 1 nodes. 19 10
The time to perform in order traversal is
T(n) = T(k) + T(n – k – 1) + d.
7 17
For some positive constant ‘d’ that reflects the time to 16
7
7 17 10
Now 10 can be inserted in the hole
As a result:
17
17 15 16 7 10 19
19
19 15 16 7 17 10 15 16
15 16
7 10
7 17 10
Repeat the step B till the array is sorted.
The children of array [2] are greater and item 15 has to be
moved down further, swapped with array [5]. Heap sort analysis
Heap sort uses a data structure called (binary) heap binary,
19 heap is viewed as a complete binary tree. An Array A that
19 17 16 7 15 10
represents a heap is an object with 2 attributes: length [A],
17 16 which is the number of elements in the array and heap size
[A], the number of elements in the heap stored within array A.
7 15
No element past A [heap size [A]], where heap size [A] ≤
10
length [A], is an element of the heap.
There are 2 kinds of binary heaps:
Now the tree is ordered, and the binary heap is built.
1. Max-heaps
Sorting-performing Delete 2. Min-heaps
Max Operations In both kinds the values in the nodes satisfy a heap-property.
Delete the top element Max-heap property A[PARENT (i)] ≥A[i]
Store 19 in a temporary place, a hole is created at the top. The value of a node is almost the value of its parent. Thus the
largest element in a max-heap is stored at the root, and the
sub tree rooted at a node contains values no larger than that
17 16 7 15 10
contained at the node itself.
17 16
19 Min-heap property For every mode ‘i’ other than the root
[PARENT (i)] ≤ A[i]. The smallest element in a min-heap
7 15 10 is at the root.
Max-heaps are used in heap sort algorithm.
Swap 19 with the last element of the heap. As 10 will be Min-heaps are commonly used in priority queues.
adjusted in the heap, its cell will no longer be a part of the
heap. Instead it becomes a cell from the sorted array
Chapter 2 • Sorting Algorithms | 3.103
EXERCISES
7. What is the complexity of the above pseudo code? sorted part of the array. If binary search is used instead
(A) q(log n) (B) q(n2) of linear search to identify the position, the worst case
(C) q(n log n) (D) q(2n) running time would be.
8. Apply Quick sort on a given sequence 6 10 13 5 8 3 2 (A) q (n log n)
11. What is the sequence after first phase, pivot is first (B) q (n2)
element? (C) q (n(log n)2)
(A) 5 3 2 6 10 8 13 11 (D) q (n)
(B) 5 2 3 6 8 13 10 11 13. Consider the process of inserting an element into a
(C) 6 5 13 10 8 3 2 11 max heap where the max heap is represented by an
(D) 6 5 3 2 8 13 10 11 array, suppose we perform a binary search on the path
9. Selection sort is applied on a given sequence: from the new leaf to the root to find the position for
the newly inserted element, the number of comparisons
89, 45, 68, 90, 29, 34, 17. What is the sequence after 2
performed is:
iterations?
(A) q (log n) (B) q (log log n)
(A) 17, 29, 68, 90, 45, 34, 89
(C) q (n) (D) q (n log n)
(B) 17, 45, 68, 90, 29, 34, 89
(C) 17, 68, 45, 90, 34, 29, 89 14. Consider the following algorithm for searching a given
(D) 17, 29, 68, 90, 34, 45, 89 number ‘X’ in an unsorted array A[1 … n] having ‘n’
n distinct values:
10. Suppose there are log n sorted lists of elements
log n (1) Choose an ‘i’ uniformly at random from 1 … n
each. The time complexity of producing sorted lists of (2) If A [i ] = x
all these elements is: (hint: use a heap data structure)
Then stop
(A) q(n log log n) (B) q(n log n)
(C) W(n log n) (D) W(n3/2) else
11. If Divide and conquer methodology is applied on power- goto(1);
ing a Number Xn. Which one the following is correct? Assuming that X is present in A, what is the expected
(A) Xn = Xn/2 ⋅ Xn/2 number of comparisons made by the algorithm before
n −1 n −1 it terminates.
(B) X n = X 2
⋅X 2
. X (A) n (B) n – 1
n +1 n (C) 2n (D) n/2
(C) X n = X ⋅ X 2 2
15. The recurrence equation for the number of additions
(D) Both (A) and (B)
A(n) made by the divide and conquer algorithm on
12. The usual q(n2) implementation of insertion sort to input size n = 2K is
sort an array uses linear search to identify the posi- (A) A(n) = 2A(n/2)+ 1 (B) A(n) = 2A(n/2) + n2
tion, where an element is to be inserted into the already (C) A(n) = 2A(n/4) + n2 (D) A(n) = 2A(n/8) + n2
4. From the recurrence relation. Of merge sort 10. Which one of the following in-place sorting algorithm
T(n) = 2T (n/2) + q(n). needs the minimum number of swaps?
Which option is correct? (A) Quick sort (B) Insertion sort
I. n/2 II. 2T III. q (n) (C) Selection sort (D) Heap sort
(a) Extra work (divide and conquer) 11. As the size of the array grows what is the time com-
(b) Sub-problem size plexity of finding an element using binary search (array
of elements are ordered)?
(c) Number of sub-problems (A) q(n log n) (B) q(log n)
(A) III – b, II – a, I – c (B) I – b, II – c, III – a (C) q(n2) (D) q(n)
(C) I – a, II – c, III – b (D) I – c, II – a, III – b
12. The time complexity of heap sort algorithm is
5. What is the number of swaps required to sort ‘n’ ele- (A) n log n (B) log n
ments using selection sort, in the worst case? (C) n2 (D) None of these.
(A) q(n) (B) q(n2)
13. As part of maintenance work, you are entrusted with
(C) q(n log n) (D) q(n2 log n)
the work of rearranging the library books in a shelf in a
6. In a binary max heap containing ‘n’ numbers, the proper order, at the end of each day. The ideal choices
smallest element can be found in time will be_____.
(A) O(n) (B) O(log n) (A) Heap sort (B) Quick sort
(C) O(log log n) (D) O(1) (C) Selection sort (D) Insertion sort
7. What is the worst case complexity of sorting ‘n’ num- 14. The value for which you are searching is called
bers using quick sort? (A) Binary value
(A) q(n) (B) q(n log n) (B) Search argument
(C) q(n2) (D) q(n !) (C) Key
8. The best case analysis of quick sort is, if partition splits (D) Serial value
the array of size n into 15. To sort many large objects and structures it would be
(A) n/2 : n/m (B) n/2 : n/2 most efficient to _____.
(C) n/3 : n/2 (D) n/4 : n/2 (A) Place them in an array and sort the array
9. What is the time complexity of powering a number, by (B) Place the pointers on them in an array and sort the
using divide and conquer methodology? array
(A) q (n2) (B) q (n) (C) Place them in a linked list and sort the linked list
(C) q(log n) (D) q(n log n) (D) None of the above
(A) q(log n) for both insertion and deletion (A) O(1) (B) O(d) but not O(1)
(B) q(n) for both insertion and deletion (C) O(2d) but not O(d) (D) O(d2d) but not O(2d)
(C) q(n) for insertion and q(log n) for deletion 10. Assume that the algorithms considered here sort the
(D) q(log n) for insertion and q(n) for deletion input sequences in ascending order. If the input is
8. The worst case running times of Insertion sort, Merge already in ascending order, which of the following are
sort and Quick sort, respectively, are: [2016] TRUE? [2016]
(A) Θ(n log n), Θ(n log n),and Θ(n2) I. Quicksort runs in Θ (n2) time
(B) Θ(n2), Θ(n2),and Θ(n log n) II. Bubblesort runs in Θ (n2) time
(C) Θ(n2), Θ(n log n),and Θ(n log n) III. Mergesort runs in Θ (n) time
(D) Θ(n2), Θ(n log n),and Θ(n2) IV. Insertion sort runs in Θ (n) time
9. An operator delete(i) for a binary heap data structure (A) I and II only (B) I and III only
is to be designed to delete the item in the i-th node. (C) II and IV only (D) I and IV only
Assume that the heap is implemented in an array and i
11. A complete binary min - heap is made by including
refers to the i-th index of the array. If the heap tree has
each integer in [1,1023] exactly once. The depth of
depth d (number of edges on the path from the root
a node in the heap is the length of the path from the
to the farthest leaf), then what is the time complexity
root of the heap to that node. Thus, the root is depth 0.
to re-fix the heap efficiently after the removal of the
The maximum depth at which integer 9 can appear is
element? [2016]
_____ . [2016]
ANSWER KEYS
EXERCISES
Practice Problems 1
1. B 2. A 3. A 4. B 5. B 6. C 7. C 8. B 9. A 10. B
11. D 12. A 13. A 14. B 15. A
Practice Problems 2
1. B 2. B 3. B 4. B 5. A 6. A 7. C 8. B 9. C 10. C
11. B 12. A 13. D 14. C 15. B
LEARNING OBJECTIVES
Divide-and-Conquer Examples
• Sorting: Merge sort and quick sort
• Binary tree traversals
• Binary Search A solution to the
• Multiplication of large integers original problem
• Matrix multiplication: Strassen’s algorithm
• Closest-pair and Convex-hull algorithm
Figure 1 Divide-and-conquer technique.
3. Merge the two sorted sub lists back into one sorted list
MERGE SORT 4. The key of merge sort is merging two sorted lists into one,
Merge sort is a sorting algorithm for rearranging lists (or any other such that if we have 2 lists
data structure that can only be accessed sequentially, e.g., file X(x1 ≤ x2 ≤ x3 … ≤ xm) and
streams) into a specified order. Y (y1 ≤ y2 ≤ y3 … ≤ yn) the resulting list is z (z1 ≤ z2 ≤ … ≤ zm+n)
Merge sort works as follows:
1. Divide the unsorted list into two sub lists of about half the size. Example 1: L1 = {3, 8, 9}, L2 = {1, 5, 7}
2. Sort each of the two sub lists. Merge (L1, L2) = {1, 3, 5, 7, 8, 9}
3.108 | Unit 3 • Algorithms
T(N) = 2T(N/2) + N
Figure 2 Tree of recursive calls to quick sort.
= O(N log N)
• Quick sort is a sorting algorithm with worst case run-
QUICK SORT ning time O(n2) on an input array of n numbers. Inspite
Quick sort is an example of Divide-and-conquer strategy. In of this slow worst case running time, quick sort is often
Quick sort we divide the array of items to be sorted into two the best practical choice for sorting because it is effi-
partitions and then call the quick sort procedure recursively cient on the average: its expected running time is O(n
to sort the two partitions, i.e., we divide the problem into log n) and the constants hidden in the O-notation are
two smaller ones and conquer by solving the smaller ones. quite small
The conquer part of the quick sort routine looks like this • Quick sort algorithm is fastest when the median of the array
is chosen as the pivot element. This is because the resulting
<Pivot >Pivot partitions are of very similar size. Each partition splits itself
Low Pivot High in two and thus the base case is reached very quickly.
Chapter 3 • Divide-and-conquer | 3.109
Example: Underlined element is pivot. The recursion tree for this recurrence has cost ‘cn’ at every
level, until a boundary condition is reached at depth log10n =
3 1 4 5 9 2 6 8 7
q (log n). The recursion terminates at depth log10/8n = q(log n).
3 1 4 5 9 2 6 8 7 The total cost of quick sort is O(n log n)
3 1 4 2 5 9 6 8 7
SEARCHING
Two searching techniques are:
3 1 4 2 9 6 8 7
• Linear search
3 1 4 2 9 6 8 7 • Binary search
1 2 4 3 6 7 8 9
Linear Search
4 3 Linear search (or) sequential search is a method for find-
6 8 9
1 ing a particular value in list that consists of checking every
3 4 one of its elements, one at a time and in sequence, until the
1 2 3 4 6 7 8 9
desired one is found. Linear search is a special case of brute
force search. Its worst case cost is proportional to the num-
ber of elements in the list.
1 2 3 4 5 6 7 8 9
step; a binary search finds the median, makes comparison, to Example: Value being searched 123
determine whether the desired value comes before or after it,
and then searches the remaining half in the same manner. A 2 6 7 34 76 123 234 567 677 986
binary search is an example of Divide-and-conquer algorithm.
First (1) mid(5) Last(10)
Implementation 2 6 7 34 76 123 234 567 677 986
function binary search (a, value, left, right)
{ First (6) mid(8) Last(10)
if right < left 2 6 7 34 76 123 234 567 677 986
return not found
mid: = floor ((right –left)/2) + left
First (6) Last(7)
if a [mid] = value Mid (6)
return mid
2 6 7 34 76 123 234 567 677 986
if value < a[mid]
return binary search (a, value, left, mid –1) else return binary search
(a, value, mid + 1, right) First, mid, last (6)
}
EXERCISES
Practice Problems 1 4. What is the depth first search order of the given graph?
Directions for questions 1 to 15: Select the correct alterna-
1 4
tive from the given choices.
1. How many comparisons are required to search an item
89 in a given list, using Binary search? 2
4 8 19 25 34 39 45 48 66 75 89 95
3 5
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
a
(A) 1–2–4–5–3
(B) 1–4–2–5–3
b c (C) 1–2–4–3–5
(D) 1–2–3–4–5
d e f g
6. What is the order of post-order traversal and in-order
traversals of graph given in the above question?
h (A) 4 – 2 – 5 – 1 – 3 and 4 – 5 – 2 – 3 – 1
(B) 4 – 5 – 2 – 3 – 1 and 4 – 2 – 5 – 1 – 3
(A) acbhdefg (B) abcdefgh (C) 4 – 5 – 2 – 1 – 3 and 4 – 2 – 5 – 1 – 3
(C) adbcefgh (D) aebcdfgh (D) 4 – 5 – 2 – 3 – 1 and 4 – 2 – 5 – 3 – 1
Chapter 3 • Divide-and-conquer | 3.111
a c e k l u s v a
d i g m q o
e
b f
j r p w
(A) 12 (B) 13 h
(C) 11 (D) 10
8. Match the following:
g
I. In-order 1. ABCDEFGHI
II. Pre-order 2. DBHEIAFCG
Among the following sequences
III. Post-order 3. ABDEHICFG
IV. Level-order 4. DHIEBFGCA I. a b e g h f II. a b f e h g
For the tree III. a b f h g e IV. a f g h b e
Which are depth first traversals of the above graph?
A (A) I, II and IV only (B) I and IV only
(C) I, III only (D) I, III and IV only
B C 13. The breadth first search algorithm has been imple-
mented using the queue data structure. One possible
order of visiting the nodes is
D E F G
A B C
H I
F E D
(A) I – 2, II – 3, III – 4, IV – 1
(B) I – 3, II – 1, III – 4, IV – 2
(C) I – 1, II – 2, III – 3, IV – 4 (A) A B C D E F (B) B E A D C F
(D) I – 4, II – 3, III – 2, IV – 1 (C) E A B D F C (D) Both (A) and (B)
9. A complete n-array tree in which each node has ‘n’
14. An undirected graph G has ‘n’ nodes. Its adjacency
children (or) no children.
matrix is given by an n × n square matrix.
Let ‘I’ be the number of internal nodes and ‘L’ be the
(i) Diagonal elements are 0’s
number of leaves in a complete n-ary tree.
(ii) Non-diagonal elements are 1’s
If L = 51 and I = 10 what is the value of ‘n’?
Which of the following is true?
(A) 4 (B) 5
(C) 6 (D) Both (A) and (B) (A) Graph G has no minimum spanning tree
(B) Graph G has a unique minimum spanning tree of
10. A complete n-ary tree is one in which every node has 0
cost (n –1)
(or) n children. If ‘X ’ is the number of internal nodes of a
(C) Graph G has multiple distinct minimum spanning
complete n-ary tree, the number of leaves in it is given by
trees, each of cost (n – 1)
(A) X(n – 1) + 1 (B) Xn – 1
(D) Graph G has multiple spanning trees of different cost.
(C) Xn + 1 (D) X(n + 1) + 1
11. The numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in the 15. Which of the following is the breadth first search tree
given order into an initially empty binary search tree. for the given graph?
The binary search tree uses the usual ordering on natu- a b c d
ral numbers. What is the in-order traversal sequence of
the resultant tree?
(A) 7 5 1 0 3 2 4 6 8 9
(B) 0 2 4 3 1 6 5 9 8 7 h g f e
3.112 | Unit 3 • Algorithms
h h
10. Which one of the following is the post-order traversal of Which of the following is the resultant binary search tree
the given tree? after deletion of 33?
a
(A) 22
c
b 11 43
f
d e 10 16 32 44
(A) d e a f c b a (B) d e b f c a
(C) e b d f c a (D) a b c d e f 55
(B) 22
Common data for questions 11 and 12:
11. The pre-order traversal of a tree is a b d h i e c f g. Which
of the following is the correct tree?
11 44
(A) a
d 10 16 43 55
b
e c
h i
45
f g
(C) 22
(B) a
c
b 11 32
f g
d e
10 16 43 44
h i
(C) a
55
b
c (D) 22
f g
d e
h i 11 55
(D) a
c 10 16 43 44
b
f g
e d
43
h i
14. Match the following:
12. Which of the following is in-order traversal of the above
tree? I. Articulation Point 1. An edge whose removal
(A) a b h d e i f g c (B) a b d h e i f g c disconnects graph
(C) h d i b e a f c g (D) i d h b e a f c g II. Bridge 2. A vertex whose removal
disconnects graph
13. Consider the below binary search tree
III. Bi connected 3. Maximal set of edges such
22 component that any two edges in the set
lie on a common simple cycle
ANSWER KEYS
EXERCISES
Practice Problems 1
1. A 2. A 3. B 4. C 5. A 6. B 7. B 8. A 9. C 10. A
11. C 12. D 13. A 14. C 15. A
Practice Problems 2
1. B 2. B 3. C 4. B 5. A 6. D 7. C 8. C 9. A 10. B
11. B 12. C 13. A 14. B 15. A
Previous Years’ Questions
1. C 2. 1 3. B 4. A 5. B 6. 2.2 to 2.4 7. 5 8. C
Chapter 4
Greedy Approach
LEARNING OBJECTIVES
Procedure A A
Procedure for DFS(V) It is at this point that two traversal strategies differ.
Steps Breadth first adds B’s neighbors to the back of READY,
1. push the start vertex ‘V ’ into the stack S depth first adds them to the front.
2. while (S is not empty)
Breadth first
(i) pop a vertex V
(ii) if ‘V ’ is not visited • READY = [E, C, G]
(a) visit the vertex • Process E. READY = [C, G, F]
(b) Store ‘V ’ in visited • Process C. READY = [G, F, D]
(c) push all the adjacent vertices of ‘V ’ in to visited • Process G .READY = [F, D, H]
(iii) End if • Process F. READY = [D, H]
3. End while • Process D. READY = [H]
4. Stop. • Process H. READY = [ ]
Example: Depth First
A A
• READY = [C, G, E]
• Process C. READY = [D, G, E]
• Process D. READY = [G, E]
B D E B D E
• Process G. READY = [H, F, E]
• Process H. READY = [F, E]
• Process F. READY = [E]
C C • Process E. READY = [ ]
A A CONNECTED COMPONENTS
A graph is said to be connected if every pair of vertices
in the graph are connected. A connected component is a
B D E B D E maximal connected sub graph of ‘G’. Each vertex belongs
to exactly one connected component as does each edge.
C C • A graph that is not connected is naturally and obvi-
ously decomposed into several connected components
(Figure 4). Depth first search does this handily. Each restart
A A of the algorithm marks a new connected component.
• The directed graph in (Figure 5) is “Connected” Part of
it can be “Pulled apart” (so to speak, without “breaking”
B D E B D E any edges).
• Meaningful way to define connectivity in directed graph is:
C C ‘Two nodes U and V of a directed graph G = (V, E) con-
nected if there is a path from U to V ’, and one from V to U. This
relation between nodes is reflective, symmetric and transitive.
A As such, it partitions V into disjoint sets, called the strongly
connected components of the graph. In the directed graph of
figure 2 there are four strongly connected components.
B D E
1 2
C
3 5
4
Figure 3 Depth first search
6 8
• Let us compare two traversal orders on the following graph: 7
A B C D 12 13
11
9 10
E F G H
14
Initial steps:
READY = [A]. process A. READY = [B, E ]. process B. Figure 4 Undirected graph.
Chapter 4 • Greedy Approach | 3.121
9 10 a : 47 b : 12 c : 11 d : 14 e : 10 f:6
11
Step III:
100
12
0 1
f:6 e : 10 c : 11 b : 12 d : 14 a : 47
Solution: Two methods are used for compression of data are:
3.122 | Unit 3 • Algorithms
Let x and y be 2 characters in C having the lowest frequen- • A unit – time task is a job, such as a program to be run
cies. Then there exists an optimal prefix code for C in which on a computer, that requires exactly one unit of time to
the code words for x and y have the same length and differ complete.
only in the last bit • Given a finite set S of unit – time tasks, a schedule for S
16
is a permutation of S specifying the order in which these
tasks are to be performed.
0 1
• The first task in the schedule begins at time ‘0’ and fin-
ishes at time 1, the second task begins at time 1 and fin-
f:6 e : 10
ishes at time 2, and so on
• The problem of scheduling unit – time tasks with dead-
53
lines and penalties for a single processor has the follow-
0 1 ing inputs:
1. A set S = {a1, a2, … an} of n unit – time tasks:
23 30 2. A set of n integer deadlines d1, d2, … dn such that each
di satisfies 1 ≤ di ≤ n and task ai is supposed to finish
0 1 0 1
by time di.
c : 11 b : 12 d : 14 16 3. A set of n non-negative weights or penalties w1,
0 1
w2, … wn, such that we incur a penalty of wi if task ai
is not finished by time di and we incur no penalty if a
f:6 e : 10
task finishes by its deadline.
Example: Consider the following 7 tasks, T1, T2, T3, T4, T5
100 T6, T7. Every task is associated with profit and deadline.
0 1 Tasks T1 T2 T3 T4 T5 T6 T7
Deadline 4 2 4 3 1 4 5
a : 47 53 Profit 75 65 55 45 40 35 30
0 1
45 65 55 75 30
T4 T2 T3 T1 T7
0 1 2 3 4 5 6 7
23 30
0 T1 has highest profit, so it will be executed first and the
1 0 1
deadline of T1, is ‘4’ so T1 has to be executed within 4
c : 11 b : 12 d : 14
slots of time, same procedure is applied to other tasks
16
also.
0 1 The tasks which are not executed by CPU are T5 and T6.
j
GRAPH ALGORITHMS
Single Source Shortest Path Solution:
In a shortest-path problem, we are given a weighted • Shortest path from S to a is d(S, a) = W(S, a) = 4 (because
directed graph G = (V, E) with weight function W : E →R there is only one path from ‘S’ to ‘a’)
mapping edges to real-valued weights. The weight of path • Similarly, there is only one path from ‘s’ to ‘b’
P = < VO, V1 … VK > is the sum of the weights of its constitu- d(S, a) = W(S, a) + W(a, b) = 4 + (-5) = -1
ent edges. Shortest-path weight from U to V is defined by • Shortest-path from ‘s’ to ‘c’
3.124 | Unit 3 • Algorithms
There are infinitely many paths from ‘S’ to ‘c’ The algorithm maintains the invariant that Q = V - S at the
1. <S, c> start of each iteration of the while loop. Initially the min -
2. <S, c, d, c> priority queue Q contains all the vertices in V. ( S = ∅).
\
3. <S, c, d, c, d, c > and so on Each time through the while loop, a vertex ‘u’ is extracted
d <S, c> = 6 from Q = V - S and added to set S.
d (S, d, d, c) = 6 + 7- 2 = 11
• Each vertex is extracted from Q and added to S exactly
d (S, c, d, c, d, c) = 6 + 7 – 2 + 7 - 2 = 16
once, so the contents of while loop will be executed
d (S, c, d, c, d, c, d, c)
exactly |v| times.
= 6 + 7 - 2 + 7 - 2 + 7 - 2 = 21
• Dijkstra’s algorithm always chooses the ‘closest’ or
The cycle <c, d, c> has weight = 7 + (-2) = 5 > 0
‘lightest’ vertex in (V - S) to add to set S, we say that it
The shortest path from ‘S’ to ‘c’ is <s, c> with weight
uses a greedy strategy.
d (S, c) = 6 similarly, the shortest-path from ‘S’ to ‘d’ is
<s, c, d>, with weight d (S, d) = w (S, c) + W(c, d) = 13
Example: Consider the following graph, what is the
May there are infinitely paths from ‘S’ to ‘e’ shortest path?
1. <s, e>
a b
2. <s, e f, e> 2
3. <s, e, f, e, f , e> and so on 9 10
Since the cycle <e, f, e> has weight 4 + (-5) = -1 < 0. S 3 4 5 6
However, there is no shortest path from ‘S’ to ‘e’ by traversing
8
the negative-weight cycle <e, f, e> arbitrarily many times, we 6
2 d
can find paths from ‘s’ to ‘e’ with arbitrarily large negative c
weights,
So d(S, e) = -∞ Solution:
Similarly, d(S, f ) = - ∞
S V-S
• The shortest path from ‘S’ to ‘g’:
‘g’ can be reachable from ‘f ; we can also find paths with arbi- S abcd
trarily large negative weights from ‘s’ to ‘g’ and d(s, g) = -∞ Sc abd
• Vertices ‘h’, ‘i’ and ‘j’ also form a negative - weight
Scd ab
cycle. They are not reachable from ‘S’ so, d(S, h ) = d(S,
i) = d(S, j) = ∞ Scda b
Scdab ∅
Dijkstra’s Algorithm
Dijkstra’s algotithm solves the single-source shortest-path Distance from S to all vertices of (V - S)
problem on a weighted, directed graph G = (V, E), for the d[a] = 9
case in which all edge weights are non-negative. d[b] = ∞
d [c] = 6
• The running time of Dijkstra’s algorithm is lower than
d[d] = ∞
that of the Bellman–Ford algorithm.
9, ∞, 6, ∞ values are given to MIN-PRIORITY Queue ‘Q’,
• Dijkstra’s algorithm maintains a set ‘s’ of vertices whose
‘6’ is returned.
final shortest-path weights from the source ‘S’ have
already been determined.
• The algorithm repeatedly selects the vertex u ∈ (V - S)
with the minimum shortest-path estimate, adds ‘u’ to ‘S’
S 0
DIJKSTRA (G, W, S)
INITIALIZE - SINGLE - SOURCE (G, S) 6
6
S←∅ c
S ← V[G]
While Q ≠ 0 Distance from [Sc] to all vertices of (V - S)
do u ← EXTRACT - MIN(Q) d[b] = (S - c - b) = 6 + 10 = 16
S ← S U{u}
d[a] = min{(S - a) = 9, (s - c - a) = 10} = 9
For each vertex v ∈ Adj[u]
do RELAX (u, v, w) d[d] = min{∞, (S - c - d) = 6 + 2 = 8 } = 8
Chapter 4 • Greedy Approach | 3.125
EXERCISES
Practice Problems 1 C 1 2 3 4
1 0 15 20 25
Directions for questions 1 to 14: Select the correct alterna-
2 10 0 14 15
tive from the given choices. 3 11 18 0 17
1. A thief enters a store and sees the following: 4 13 13 14 0
C
A B 1 2
His knapsack can hold 4 pounds, what should he steal (A) 1 - 2 - 4 - 3 - 1 (B) 2 - 3 - 4 - 1 - 2
to maximize profit? (Use 0-1 Knapsack). (C) 1 - 4 - 2 - 3 - 1 (D) 2 - 4 - 3 - 1 - 2
(A) A and B (B) A and C
(C) B and C (D) A, B and C 7. Calculate the maximum profit using greedy strategy,
knapsack capacity is 50. The data is given below:
2. By using fractional Knapsack, calculate the maximum
profit, for the data given in the above question? n=3
(A) 180 (B) 170 (w1, w2, w3) = (10, 20, 30)
(C) 160 (D) 150 (p1, p2, p3) = (60, 100, 120) (dollars)? (0/1 knapsack)
3. Consider the below figure: (A) 180 (B) 220
8 7 (C) 240 (D) 260
b c d
4 7
2 Common data for questions 8 and 9: Given that
4
a 11 i e a b c d e f
14
7 6
10 Frequency 45 13 12 16 9 5
8
g Fixed length code word 000 001 010 011 100 101
h f
1 2
8. Using Huffman code, find the path length of internal
What is the weight of the minimum spanning tree using nodes.
Kruskals algorithm? (A) 8 (B) 100
(A) 34 (B) 35 (C) 100 × 8 (D) 100/8
(C) 36 (D) 38 9. Using above answer, external path length will be
4. Construct a minimum spanning tree for the figure given (A) 18 (B) 108
in the above question, using prim’s algorithm. What are (C) 8 (D) None of these
the first three nodes, added to the solution set respec-
tively (consider ‘a’ as starting node). Common data for questions 10 and 11:
(A) b, c, i (B) h, b, c 10. Item 3
(C) c, i, b (D) h, c, b
Item 2
5. Consider the below graph, calculate the shortest dis- Item 1
50 kg
30 kg
4
A D
18
1 11 9
5 60/kg 100/kg 20/kg knapsack
2 13
S B E T
Using 0-1 knapsack select a subset of the three items
5 shown, whose weight must not exceed 50 kg. What is
16
2
the value?
C F
2 (A) 2220 (B) 2100
(C) 2600 (D) 2180
(A) 23 (B) 9
(C) 20 (D) 22 11. Which of the following gives maximum profit, using
6. Solve the travelling salesman problem, with the given fractional knapsack?
distances in the form of matrix of graph, which of the (A) x1 = 1, x2 = 1, x3 = 0 (B) x1 = 1, x2 = 1, x3 = 2/3
following gives optimal solution? (C) x1 = 1, x2 = 0, x3 = 1 (D) x1 = 1, x2 = 1, x3 = 1/3
Chapter 4 • Greedy Approach | 3.127
a
f d
8
e
Which one of the following cannot be the sequence of
edges added, in that order, to a minimum spanning tree (D) b c
using Kruskal’s algorithm?
a f
(A) (a - b), (d - f ), (b - f ), (d - c), (d - e) d
(B) (a - b), (d - f ), (d - c), (b - f ), (d - e)
e
(C) (d - f ), (a - b), (d - c), (b - f ), (d - e)
(D) (d - f ), (a - b), (b - f ), (d - e), (d - c) 4. Consider the following graph:
2. The worst case height analysis of B-tree is 4
b c
(A) O(n)
(B) O(n2) 3 2 5 6
(C) O(log n)
(D) O(n log n) a d e
7 4
3. Consider the given graph:
1 Find the shortest path using Dijkstra’s algorithm.
3
b c
6 (A) a - b - d - e (B) a - b - c - d
4 4
(C) a - c - d - e (D) a - b - c - e
a 5 5 d
f 5. Which statement is true about Kruskal’s algorithm?
(A) It is a greedy algorithm for the minimum spanning
2
6 8 tree problem.
(B) It constructs spanning tree by selecting edges in
e
increasing order of their weights.
Which of the following is the minimum spanning tree. (C) It does not accept creation of cycles in spanning tree.
(If we apply Kruskal algorithm). (D) All the above
3.128 | Unit 3 • Algorithms
6. Dijkstra’s algorithm bears similarity to which of the (A) I, II, III, IV (B) IV, III, I, II
following for computing minimum spanning trees? (C) IV, II, I, III (D) III, IV, II I
(A) Breadth first search (B) Prim’s algorithm 11. Let V stands for vertex, E stands for edges.
(C) Both (A) and (B) (D) None of these
For both directed and undirected graphs, the adjacency
7. Which of the following algorithm always yields a cor- list representation has the desirable property that the
rect solution for a graph with non-negative weights to amount of memory required is
compute shortest paths? (A) q(V ) (B) q(E)
(A) Prim’s algorithm (B) Kruskal’s algorithm (C) q(V + E ) (D) q(V - E )
(C) Dijkstra’s algorithm (D) Huffman tree
12. Which of the following is false?
8. Let the load factor of the hash table is number of keys (A) Adjacency-matrix representation of a graph per-
is n, cells of the hash table is m then mits faster edge look up.
(B) The adjacency matrix of a graph requires q(v2)
(A) ∝ = n/m (B) ∝ = m/n memory, independent of the number of edges in
m +1 n +1
(C) ∝ (D) ∝ the graph.
n m (C) Adjacency-matrix representation can be used for
9. To implement Dijkstra’s shortest path algorithm on weighted graphs.
unweighted graphs so that it runs in linear time, the (D) All the above
data structure to be used is: 13. Dynamic programming is a technique for solving prob-
(A) Queue lems with
(B) Stack (A) Overlapped sub problems
(C) Heap (B) Huge size sub problems
(D) B-tree (C) Small size sub problems
10. The development of a dynamic-programming algo- (D) None of these
rithm can be broken into a sequence of four steps, 14. The way a card game player arranges his cards, as he
which are given below randomly. picks them up one by one is an example of _____.
I. Construct an optimal solution from computed in- (A) Bubble sort (B) Selection sort
formation. (C) Insertion sort (D) None of the above
II. Compute the value of an optimal solution in a bot- 15. You want to check whether a given set of items is
tom-up fashion. sorted. Which method will be the most efficient if it is
III. Characterize the structure of an optimal solution. already in sorted order?
IV. Recursively defines the value of an optimal solution. (A) Heap sort (B) Bubble sort
The correct sequence of the above steps is (C) Merge sort (D) Insertion sort
(A) (a - b), (d - f), (b - f), (d - c), (d - e) int GetValue (struct CellNode *ptr) {
(B) (a - b), (d - f), (d - c), (b - f), (d - e) int value = 0;
(C) (d - f), (a - b), (d - c), (b - f), (d - e) if (ptr != NULL) {
(D) (d - f), (a - b), (b - f), (d - e), (d - c) if ((ptr->leftChild == NULL) &&
Common data for questions 5 and 6: A 3-ary max-heap (ptr->rightChild == NULL))
is like a binary max-heap, but instead of 2 children, value = 1;
nodes have 3 children. A 3-ary heap can be repre- else
sented by an array as follows: The root is stored in the value = value + GetValue(ptr->leftChild)
first location, a[0], nodes in the next level, from left + GetValue(ptr->rightChild);
to right, is stored from a[1] to a[3]. The nodes from }
the second level of the tree from left to right are stored return(value);
from a[4] location onward. An item x can be inserted
into a 3-ary heap containing n items by placing x in The value returned by GetValue when a pointer to the
the location a[n] and pushing it up the tree to satisfy root of a binary tree is passed as its argument is:
the heap property. (A) The number of nodes in the tree
(B) The number of internal nodes in the tree
5. Which one of the following is a valid sequence of
(C) The number of leaf nodes in the tree
elements in an array representing 3-ary max-heap?
(D) The height of the tree
[2006]
(A) 1, 3, 5, 6, 8, 9 (B) 9, 6, 3, 1, 8, 5 10. Let w be the minimum weight among all edge weights
(C) 9, 3, 6, 8, 5, 1 (D) 9, 5, 6, 8, 3, 1 in an undirected connected graph. Let e be a specific
6. Suppose the elements 7, 2, 10 and 4 are inserted, in edge of weight w. Which of the following is FALSE?
that order, into the valid 3-ary max-heap found in the [2007]
above question, Q-76. Which one of the following is (A) There is a minimum spanning tree containing e.
the sequence of items in the array representing the (B) If e is not in a minimum spanning tree T, then in
resultant heap? [2006] the cycle formed by adding e to T, all edges have
(A) 10, 7, 9, 8, 3, 1, 5, 2, 6, 4 the same weight.
(B) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 (C) Every minimum spanning tree has an edge of
(C) 10, 9, 4, 5, 7, 6, 8, 2, 1, 3 weight w.
(D) 10, 8, 6, 9, 7, 2, 3, 4, 1, 5 (D) e is present in every minimum spanning tree.
7. In an unweighted, undirected connected graph, the 11. The Breadth first search algorithm has been imple-
shortest path from a node S to every other node is mented using the queue data structure. One possible
computed most efficiently, in terms of time complex- order of visiting the nodes of the following graph is
ity, by [2007] [2008]
(A) Dijkstra’s algorithm starting from S. M N O
(B) Warshall’s algorithm
(C) Performing a DFS starting from S.
(D) Performing a BFS starting from S.
8. A complete n-ary tree is a tree in which each node has R
Q P
n children or no children. Let I be the number of inter-
nal nodes and L be the number of leaves in a complete (A) MNOPQR (B) NQMPOR
n-ary tree. If L = 41, and I = 10, what is the value of (C) QMNPRO (D) QMNPOR
n? [2007] 12. G is a graph on n vertices and 2n - 2 edges. The edges
(A) 3 (B) 4 of G can be partitioned into two edge-disjoint span-
(C) 5 (D) 6 ning trees. Which of the following is NOT true for G?
9. Consider the following C program segment where [2008]
CellNode represents a node in a binary tree: [2007] (A) For every subset of k vertices, the induced sub-
struct CellNode { graph has atmost 2k - 2 edges
struct CellNode *leftChild; (B) The minimum cut in G has atleast two edges
int element; (C) There are two edge-disjoint paths between every
struct CellNode *rightChild; pair of vertices
};
(D) There are two vertex-disjoint paths between eve-
ry pair of vertices
3.130 | Unit 3 • Algorithms
22. How many different insertion sequences of the key 24. What will be the cost of the minimum spanning tree
values using the same hash function and linear prob- (MST) of such a graph with n nodes? [2011]
ing will result in the hash table shown above? [2010] 1
(A) 10 (B) 20 (A) (11n 2 − 5n) (B) n2 - n + 1
12
(C) 30 (D) 40
(C) 6n - 11 (D) 2n + 1
23. A max-heap is a heap where the value of each parent
is greater than or equal to the value of its children. 25. The length of the path from V5 to V6 in the MST of
Which of the following is a max-heap? [2011] previous question with n = 10 is [2011]
(A) 11 (B) 25
(A) (C) 31 (D) 41
10
P 0.22 1 3
Q 0.34 4 5
R 0.17
4
S 0.19
ANSWER KEYS
EXERCISES
Practice Problems 1
1. A 2. A 3. B 4. A 5. B 6. A 7. B 8. A 9. A 10. C
11. B 12. A 13. A 14. B
Practice Problems 2
1. D 2. C 3. A 4. A 5. D 6. C 7. C 8. A 9. A 10. D
11. C 12. C 13. A 14. C 15. D
Principle of Optimality 11
It states that whatever the initial state is, remaining decisions must
be optimal with regard to the state following from the first decision. Costs of edges
To solve a problem using dynamic programming strategy, it 1 – 2 → 10
must observe the principle of optimality. 1 – 3 → 20
3.136 | Unit 3 • Algorithms
Example: To map the key 3121 into a hash table of size Open addressing Open addressing hash tables are used to
1000, we square it (3121)2 = 9740641 and extract 406 as stock up the records straight inside the array. This approach
the hash value. is also known as closed hashing. This procedure is based on
probing. Well known probe sequence include:
Folding method • Linear probing: In which the interval between probes is
fixed often at 1.
The folding method breaks up a key into precise segments • Quadratic probing: In which the interval between probes
that are added to form a hash value, and still another tech- increases proportional to the hash value (the interval thus
nique is to apply a multiplicative hash function to each seg- increasing linearly and the indices are described by a
ment individually before folding. quadratic function).
Algorithm H(x) = (a + b + c) mod m. Where a, b, and c • Double hashing: In which the interval between probes is
represent the preconditioned key broken down into three computed by another hash function.
parts, m is the table size, and mod stands for modulo. In other (i) Linear probing: Linear probing method is used for
words: The sum of three parts of the pre conditioned key is resolving hash collisions of values of hash functions
divided by the table size. The remainder is the hash key. by sequentially searching the hash table for a free loca-
Example: tion. The item will be stored in the next available slot
Fold the key 123456789 into a hash table of ten spaces (0 in the table in linear probing. Also an assumption is
through 9) made that the table is not already full.
We are given x = 123456789 and the table size (i.e., m = 10) This is implemented via a linear search for an empty
Since we can break x into three parts any way, we will slot, from the point of collision.
break it up evenly. If the physical end of table is reached during the
Thus a = 123, b = 456 and c = 789 linear search, the search will again get start around to
H(x) = (a + b + c) mod M the beginning of the table and continue from there. The
H(123456789) = (123 + 456 + 789) mod 10 table is considered as full, if an empty slot is not found
= 1368 mod 10 = 8 before reaching the point of collision.
Chapter 5 • Dynamic Programming | 3.141
[3] 43
example. Hash key (89) = 89% 10 = 9 [4]
Hash key = key % table [3] 43
Hash key (18) = 18% 10 = 8 [5]
[4] 36 size [4] 36 Hash key (49) = 49% 10 = 9 (collision)
[6] 49
2 = 10% 8 = (7 − (49% 7)
[5] 5 = 5% 8 [5] 10 = (7 − (0)) [7]
7 = 15% 8 = 7 positions from [9] [8] 18
[6] 6 [6] 6
[9] 89
[7] 7 [7] 7
Figure 3 Double hashing
Soluation: n
+ n = θ (n ) in all. The property of overlapping
2
• In the longest-common-sub sequence problem, we are NP, and a solution to the problem is somehow known, then
given 2 sequences x = <x1, x2, x3 … xm> and y = <y1, y2 … demonstrating the correctness of the solution can always be
yn> and wish to find a maximum length common subse- reduced to a single P (polynomial time) verification. If P
quence of x and y. and NP are not equivalent then the solution of NP-problems
• LCS problem can be solved efficiently using dynamic requires (in the worst case) an exhaustive search.
programming. A problem is said to be NP-hard, if an algorithm for
• A brute force approach to solve the LCS problem is to enu- solving it can be translated into one for solving any other
merate all subsequences of x and check each subsequence NP-problem. It is much easier to show that a problem is NP
to see if it is also a subsequence of y, keeping track of the than to show that it is NP-hard. A problem which is both
longest subsequence found. Each subsequence of x corre- NP and NP-hard is called an NP-complete problem.
sponds to a subset of the indices {1, 2 … m} of x. There are
2m subsequences of x, so this approach requires exponen- P versus NP-problems
tial time, making it impractical for long sequences. The P versus NP problem is the determination of whether
• The classes of sub problems correspond to pairs of ‘pre all NP-problems are actually P-problems, if P and NP are
fixes’ of 2 input sequences: not equivalent then the solution of NP-problem requires an
Given a sequence x = <x1, x2 … xm>, we define the ith exhaustive search, while if they are, then asymptotically
prefix of x, for i = 0, 1, … m, as faster algorithms may exist.
xi = <x1 x2 … xi>
NP-complete problem
Example: If x = <A, B, C, B, D, A, D>, then x4 = <A, B, C,
B> and x0 is the empty sequence. LCS problem has an opti- A problem which is both NP (verifiable in non-deterministic
mal sub-structure property. polynomial time) and NP-hard (any NP-problem can be
translated into this problem). Examples of NP-hard prob-
Optimal Substructure of LCS lems include the Hamiltonian cycle and travelling sales man
Let x = <x1, x2 … xm> and y = <y1, y2 … yn> be sequences problems.
and let z = <z1, z2 … zk> be any LCS of x and y then Example:
1. If xm = yn, then zk = xm = yn and zk–1 is an LCS of xm –1 and Circuit satisfiability is a good example of problem that we
yn – 1. don’t know how to solve in polynomial time. In this prob-
2. If xm ≠ yn, then zk ≠ xm implies that z is an LCS of xm–1 lem, the input is a Boolean circuit. A collection of and, or
and y. and not gates connected by wires. The input to the circuit is
3. If xm ≠ yn, then zk ≠ yn implies that z is an LCS of x and a set of m Boolean (true/false) values x1 … xm. The output
yn – 1. is a single Boolean value. The circuit satisfiability problem
asks, given a circuit, whether there is an input that makes
the circuit output TRUE, or conversely, whether the circuit
NP-HARD AND NP-COMPLETE always outputs FLASE. Nobody knows how to solve this
A mathematical problem for which, even in theory, no short- problem faster than just trying all 2m possible inputs to the
cut or smart algorithm is possible that would lead to a sim- circuit but this requires exponential time.
ple or rapid solution. Instead the only way to find an optimal
solution is a computationally intensive, exhaustive analysis in P, NP, and Co-NP
which all possible outcomes are tested. Examples of NP-hard • P is a set of yes/no problems that can be solved in poly-
problems include the travelling salesman problem. nomial time. Intuitively P is the set of problems that can
be solved quickly.
P-problem • NP is the set of yes/no problems with the following
property: If the answer is yes, then there is a proof of this
A problem is assigned to the P (polynomial time) class if
fact that can be checked in polynomial time. Intuitively
there exists at least one algorithm to solve that problem,
NP is the set of problems where we can verify a YES
such that number of steps of the algorithm is bounded by a
answer quickly if we have the solution in front of us.
polynomial in n, where n is the length of the input.
Example: The circuit satisfiability problem is in NP.
NP-problem If the answer is yes, then any set of m input values that pro-
duces TRUE output is a proof of this fact, we can check the
A problem is assigned to the NP (non-deterministic poly-
proof by evaluating the circuit in polynomial time.
nomial time) class if it is solvable in polynomial time by a
non-deterministic turing machine. • Co-NP is the exact opposite of NP. If the answer to a
A P-problem (whose solution time is bounded by a pol- problem in co-NP is no, then there is a proof of this fact
ynomial) is always also NP. If a problem is known to be that can be checked in polynomial time.
3.144 | Unit 3 • Algorithms
EXERCISES
Practice Problems 1 4. Insert element 14 into the given hash table with double
Directions for questions 1 to 15: Select the correct alternative hashing? h1 (k) = k mod 13, h2 (k) = 1 + (k mod 11). The
from the given choices. element will occupy, which slot?
1. Hash the keys 12, 44, 13, 88, 23, 94, 11, 39, 20 using
0
the hash function with chaining (2 k + 5) mod 11, which
1 79
of the following slots are empty?
2
(A) 0, 1, 2, 3, 4 (B) 0, 2, 3, 4, 8, 10
3
(C) 0, 1, 2, 4, 8, 10 (D) 0, 1, 2, 4, 8
4 69
2. Using linear probing on the list given in the above ques-
5 98
tion with the same hash function, which slots are not
6
occupied? 7 72
(A) 3, 4 (B) 4, 5 8
(C) 3, 6 (D) 4, 6 9
3. In hashing, key value 123456 is hashed to which 10
address using multiplication method (m = 104)? 11 50
(A) 40 (B) 41 12
(C) 42 (D) 44
Chapter 5 • Dynamic Programming | 3.145
(A) 7th (B) 8th 11. Consider the following input (322, 334, 471, 679, 989,
(C) 2nd (D) 9th 171, 173, 199) and the hash function is x mod 10 which
5. Consider the below given keys: statement is true?
257145368, 25842354, 12487654, 248645452. Find I. 679, 989, 199 hash to the same value
the hash values of keys using shift folding method? II. 471, 171, hash to the same value
(A) 770, 221, 153, 345 (B) 221, 770, 153, 345 III. Each element hashes to a different value
(C) 760, 770, 153, 345 (D) 815, 770, 153, 345
IV. All the elements hash to the same value
6. Consider the following two problems on unidirected (A) I Only (B) II Only
graphs. (C) I and II (D) III
β : Given G(V, E), does G have an independent set of 12. For the input 30, 20, 56, 75, 31, 19 and hash function
size |V|-4? h (k) = k mod 11, what is the largest number of key
α : Given G(V, E), does G have an independent set of comparisons in a successful search in the open hash
size 5? table.
Which of the following is true? (A) 4 (B) 3
(A) β is in P and α is in NP-Complete (C) 5 (D) 2
(B) β is in NP-Complete and α is in P 13. The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted into
(C) Both α and β are NP-Complete an empty hash table of length 10 using open address-
(D) Both α and β are in P ing with hash function, h (k) = k mod 10 and linear
7. Let S be an NP-complete problem and Q and R be probing.
two other problems not known to be in NP. Q is pol- Which is the resultant hash table?
ynomial-time reducible to S and S is polynomial-time (A) (B) 0
0
reducible to R. Which one of the following statements 3 1
1
is true?
2 2 12 2
(A) R is NP-Complete (B) R is NP-Hard
(C) Q is NP-Complete (D) Q is NP-Hard 23 3 13 3
13 4 4
8. Let FHAM3 be the problem of finding a Hamiltonian cycle
in a graph G = (V, E) with |V| divisible by 3 and DHAM3 15 5 15 5
be the problem of determining if a Hamiltonian cycle 6 6
exists in such graphs. Which of the following is true? 7 7
(A) Both FHAM3 and DHAM3 are NP-hard 8 8
(B) FHAM3 is NP-hard but DHAM3 is not 9
9
(C) DHAM3 is NP-hard but FHAM3 is not
(D) Neither FHAM3 nor DHAM3 is NP-hard
(C) 0 (D) 0
9. Consider a hash table of size 7, with starting index ‘0’
and a hash function (3x + 4) mod 7. Initially hash table 1 1
is empty. The sequence 1, 3, 8, 10 is inserted into the 12 2 2 2
table using closed hashing then what is the position of 13 3 3 3
element 10? 2 4 12 4
(A) 1st (B) 2nd 3 5 13 5
(C) 6th (D) 0th
23 6 23 6
10. Place the given keys in the hash table of size 13, index 5 7
5 7
from ‘0’ by using open hashing, hash function is h(k)
18 8 18 8
mod 13.
15 9 15 9
Keys: A, FOOL, HIS, AND
(hint : Add the positions of a word’s letters in the alpha- 14. Which one of the following is correct?
bet, take A → 1, B → 2, C → 3. D → 4 … Z → 26). (A) Finding shortest path in a graph is solvable in poly-
Which of the following shows the correct hash nomial time.
addresses of keys? (B) Finding longest path from a graph is solvable in
(A) A – 1, FOOL – 10, HIS – 9, AND – 6 poly-nomial time.
(B) A – 1, FOOL – 9, HIS – 10, AND – 6 (C) Finding longest path from a graph is solvable in
(C) A – 0, FOOL – 6, HIS – 10, AND – 9 polynomial time, if edge weights are very small values.
(D) A – 0, FOOL – 9, HIS – 9, AND – 6 (D) Both (A) and (B) are correct
3.146 | Unit 3 • Algorithms
15. In the following pair of problems (B) II is solvable in polynomial time, I is NP complete
problem.
2 CNF Satisfiability 3 CNF Satisfiability
Vs . (C) Both are solvable in polynomial time
I II (D) None can be solved in polynomial time.
(A) I is solvable in polynomial time, II is NP complete
problem.
hashing, what is the probability that the first 3 slots Match the algorithms to the design paradigms they
are unfilled after the first 3 insertions? [2014] are based on. [2017]
(A) (97 × 97 × 97)/1003 (A) (P) ↔ (ii), (Q) ↔ (iii),(R) ↔ (i)
(B) (99 × 98 × 97)/1003 (B) (P) ↔ (iii), (Q) ↔ (i), (R) ↔ (ii)
(C) (97 × 96 × 95)/1003 (C) (P) ↔ (ii), (Q) ↔ (i), (R) ↔ (iii)
(D) (97 × 96 × 95)/(3! × 1003) (D) (P) ↔ (i), (Q) ↔ (ii), (R) ↔ (iii)
19. Match the following [2014] 25. Assume that multiplying a matrix G1 of dimension p ×
(P) prim’s algorithm for minimum (i) Backtracking q with another matrix G2 of dimension q × r requires
spanning tree pqr scalar multiplications. Computing the product of
(Q) Floyd-Warshall algorithm for (ii) Greedy method n matrices G1G2G3, ..., Gn can be done by parenthesiz-
all pairs shortest paths ing in different ways. Define Gi Gi+1 as an explicitly
(R) Mergesort (iii) Dynamic programming computed pair for a given paranthesization if they are
(S) Hamiltonian circuit (iv) Divide and conquer
directly multiplied. For example, in the matrix multi-
plication chain G1G2G3G4G5G6 using parenthesization
(A) P–iii, Q–ii, R–iv, S–i (G1(G2G3))(G4(G5G6)), G2G3 and G5G6 are the only
(B) P–i, Q–ii, R–iv, S–iii explicitly computed pairs.
(C) P–ii, Q–iii, R–iv, S–i
Consider a matrix multiplication chain
(D) P–ii, Q–i, R–iii, S–iv
F1F2F3F4F5, where matrices F1, F2, F3,
20. Given a hash table T with 25 slots that stores 2000 F4, and F5 are of dimensions 2 × 25,
elements, the load factor ∝ for T is _______ [2015] 25 × 3, 3 × 16, 16 × 1 and 1 × 1000, respectively. In
21. Language L1 is polynomial time reducible to language the parenthesization of F1F2F3F4F5 that minimizes the
L2. Language L3 is polynomial time reducible to L2, total number of scalar multiplications, the explicitly
which in turn is polynomial time reducible to lan- computed pairs is/are: [2018]
guage L4. Which of the following is/are true? [2015] (A) F1F2 and F3F4 only
(1) if L4 ∈ P, then L2 ∈ P (B) F2F3 only
(2) if L1 ∈ P or L3 ∈ P, then L2 ∈ P (C) F3F4 only
(3) L1 ∈ P, if and only if L3 ∈ P (D) F1F2 and F4F5 only
(4) if L4 ∈ P, then L1 ∈ P and L3 ∈ P 26. Consider the weights and values of items listed below.
22. The Floyd - Warshall algorithm for all -pair shortest Note that there is only one unit of each item.
paths computation is based on [2016]
Item no. Weight Value
(A) Greedy paradigm (in Kgs) (in Rupees)
(B) Divide-and-Conquer paradigm 1 10 60
(C) Dynamic Programming paradigm
2 7 28
(D) Neither Greedy nor Divide-and-Conquer nor
3 4 20
Dynamic Programming paradigm.
4 2 24
23. Let A1,A2,A3, and A4 be four matrices of dimensions 10 × 5,
5 × 20, 20 × 10, and 10 ×5, respectively. The minimum The task is to pick a subset of these items such that
number of scalar multiplications required to find the their total weight is no more than 11 kgs and their
product A1 A2 A3 A4 using the basic matrix multiplica- total value is maximized. Moreover, no item may be
tion method is _____ . [2016] split. The total value of items picked by an optimal
24. Consider the following table: algorithm is denoted by Vopt. A greedy algorithm sorts
Algorithms Design Paradigms the items by their value-to-weight ratios in descend-
ing order and packs them greedily, starting from the
(P) Kruskal (i) Divide and Conquer
first item in the ordered list. The total value of items
(Q) Quicksort (ii) Greedy
picked by the greedy algorithm is denoted by Vgreedy.
(R) Floyd-Warshall (iii) Dynamic Programming
The value of Vopt – Vgreedy is ______. [2018]
3.150 | Unit 3 • Algorithms
ANSWER KEYS
EXERCISES
Practice Problems 1
1. B 2. A 3. B 4. D 5. A 6. C 7. C 8. A 9. B 10. B
11. C 12. B 13. C 14. A 15. A
Practice Problems 2
1. B 2. C 3. B 4. D 5. B 6. C 7. A 8. B 9. D 10. C
11. A 12. B 13. B 14. B 15. A