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
Algorithm – 4 n 3
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) = Ω(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 = Ω(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 Ω(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) = Ω(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 Ω-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
ω -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
= ∑ cn + θ (n 4 )
16
log 3
= 9T(n/3) + n.
i =0 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 a = 9, b = 3, f (n) = n
= cn 2 + θ (n log 43 )
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
(B) O(x ) 3/2
(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 = θ(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(n2) (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) Ω (C) = void f (int n)
{
(iv) θ (D) <
if (n > 0)
(v) ω (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) θ(n2)
9. Which one of the following statements is true? (B) θ(n)
(A) Both time and space efficiencies are measured as (C) θ(n log n)
functions of the algorithm input size. (D) θ(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) Θ(n2) (B) Θ(n2log n)
Li {= 1 + Li +1 , if A[i ] < A [i + 1], (C) Θ(n3) (D) Θ(n3log n)
1 otherwise 20. The number of elements that can be sorted in Θ(log n)
time using heap sort is [2013]
Finally the length of the longest monotonically
(A) Θ(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) Θ(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), θ(1) (B) θ(1), θ(n)
(C) θ(n), θ(1) (D) θ(n), θ(n)
Then T(n) in terms of Θ notation is[2017]
(A) Θ (log log n) (B) Θ (log n) 37. Consider the following C code. Assume that unsigned
long int type length is 64 bits.
(C) Θ ( n) (D) Θ (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 16
For some positive constant ‘d’ that reflects the time to
execute in-order (a), exclusive of the time spent in recursive
Start with the right most node at height 1 – the node at posi-
calls T(n) = (c + d) n + c.
tion 3 = size/2. It has one greater child and has to be perco-
For n = 0, we have (c + d) 0 + c = T(0),
lated down.
For n > 0,
T(n) = T(k) + T(n – k – 1) + d
15
= ((c + d)(k + c) + ((c + d) (n – k – 1) + c) + d 15 19 10 7 17 16
= (c + d) n + c – (c + d) + c + d = (c + d)n + c
19 10
Heap Sort
Heap sort begins by building a heap out of the data set, and 7 17 16
then removing the largest item and placing it at the end of
3.102 | Unit 3 • Algorithms
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) θ(log n) (B) θ(n2) of linear search to identify the position, the worst case
(C) θ(n log n) (D) θ(2n) running time would be.
8. Apply Quick sort on a given sequence 6 10 13 5 8 3 2 (A) θ (n log n)
11. What is the sequence after first phase, pivot is first (B) θ (n2)
element? (C) θ (n(log n)2)
(A) 5 3 2 6 10 8 13 11 (D) θ (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) θ (log n) (B) θ (log log n)
(A) 17, 29, 68, 90, 45, 34, 89
(C) θ (n) (D) θ (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) θ(n log log n) (B) θ(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 θ(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) + θ(n). needs the minimum number of swaps?
Which option is correct? (A) Quick sort (B) Insertion sort
I. n/2 II. 2T III. θ (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) θ(n log n) (B) θ(log n)
(A) III – b, II – a, I – c (B) I – b, II – c, III – a (C) θ(n2) (D) θ(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) θ(n) (B) θ(n2)
13. As part of maintenance work, you are entrusted with
(C) θ(n log n) (D) θ(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) θ(n) (B) θ(n log n) (B) Search argument
(C) θ(n2) (D) θ(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) θ (n2) (B) θ (n) (C) Place them in a linked list and sort the linked list
(C) θ(log n) (D) θ(n log n) (D) None of the above
(A) θ(log n) for both insertion and deletion (A) O(1) (B) O(d) but not O(1)
(B) θ(n) for both insertion and deletion (C) O(2d) but not O(d) (D) O(d2d) but not O(2d)
(C) θ(n) for insertion and θ(log n) for deletion 10. Assume that the algorithms considered here sort the
(D) θ(log n) for insertion and θ(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 Q (n2) time
(B) Θ(n2), Θ(n2),and Θ(n log n) II. Bubblesort runs in Q (n2) time
(C) Θ(n2), Θ(n log n),and Θ(n log n) III. Mergesort runs in Q (n) time
(D) Θ(n2), Θ(n log n),and Θ(n2) IV. Insertion sort runs in Q (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
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 b
4
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.
b c (A) a - b - d - e (B) a - b - c - d
3 6
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 he value returned by GetValue when a pointer to the
T
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
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 Vs 3 CNF Satisfiability . (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
Test
17. The following program computes n! 22. Consider the binary search tree
Find the complexity?
31
Input: A non-negative integer
Output: Value of n! 6 40
If n = 0 return 1
Else return F(n – 1) θ n 3 36 81
(A) θ (n) (B) θ (n log n)
(C) θ (n2) (D) θ (n3) Delete node ‘31’, what would be the parent node in the
new binary search tree?
18. Which of the following functions are often referred as (A) 36
‘exponential growth function’? (B) 40
(A) 2n, log n (B) 2n, n! (C) 81
(C) n!, n log n (D) n!, log n (D) 6
19. Consider the following code 23. Consider the given array [4, 6, 7, 8, 21, 9, 3, 10, 13, 16,
sort (a, n) 31] after performing ‘1’ delete max operation, on the
{ max heap. What would be the sequence of elements in
the array?
for i = 1 to n do
(A) 9, 21, 13, 16, 3, 7, 10, 8, 4, 6
{ (B) 21, 9, 13, 16, 7, 3, 10, 8, 4, 6
j = i; (C) 21, 9, 13, 16, 3, 7, 10, 8, 4, 6
for k = i + 1 to n do (D) 21, 9, 13, 16, 7, 3, 10, 4, 8, 6
if (a[k] < a [j]) then j = k; 24. Consider the given Di-graph
t = a[i];
a[i] = a[j]; 1 2 3
a[j] = t;
} How many strongly connected components does the
above graph contain?
}
(A) 1 (B) 2
The above code implements which sorting? (C) 3 (D) many
(A) Merge sort
25. Consider the given graph
(B) selection sort
(C) Insertion sort
(D) Radix sort A
F
20. Assume that the number of disks in a ‘Towers of B C E
Hanoi problem’ is ‘n’, with ‘3’ towers, Initially all G
disks are placed on tower 1, to get the largest disk D H
are placed on tower 1, to get the largest disk to the
bottom of 2nd tower, How many moves are required?
(∴ n = 3) Which of the following shows the adjacency matrix of
(A) n the above graph?
(B) (n – 1)
(C) (n + 1) A B C D E F G H
(D) 2n
A 0 1 1 0 0 0 0 0
21. Each new term in Fibonacci sequence is obtained B 1 0 1 0 0 0 0 0
by taking the sum of the two previous terms. The
C 1 1 0 1 1 0 0 0
first term of the sequence is f0 = 0, and the second
term f1 = 1. Which of the following gives Fibonacci D 0 0 1 0 0 0 0 0
(A)
sequence? E 0 0 1 0 0 1 1 0
(A) fn = fn+1 + fn – 2, n ≥ 2 F 0 0 0 0 1 0 0 0
(B) fn = fn–1 + fn–2, n ≥ 2
G 0 0 0 0 1 0 0 1
(C) fn = fn–1 + fn+1, n ≥ 2
(D) All the above H 0 0 0 0 0 0 1 0
Test | 3.153
A B C D E F G H 1 5
A 0 1 1 0 0 0 0 0
6
B 1 0 1 0 0 0 0 0 (A) 3 2
7
C 1 1 0 1 1 0 0 0
4 8
(B) D 0 0 1 0 0 0 0 0
E 0 0 1 0 0 1 1 0
5
F 0 0 0 0 1 0 0 0 1
G 0 0 0 0 1 0 0 1 6
(B) 2 4
H 0 0 0 0 1 0 1 0 7
3 8
A B C D E F G H
1 5
A 0 1 1 0 0 0 0 0
B 1 0 1 0 0 0 0 0 (C) 3 2 6
C 1 1 0 1 1 0 0 0 7
D 0 0 1 0 0 0 0 0 4 8
(C)
E 0 0 1 0 0 1 1 0
F 0 0 0 0 1 0 0 0 1 5
G 0 0 0 0 0 1 0 1
(D) 2 4 6
H 0 0 0 0 0 0 1 0
7
3 8
A B C D E F G H
27. Which of the following is FALSE?
A 0 1 1 0 0 0 0 0 (A) In dynamic programming an optimal sequence of
B 1 0 1 0 0 0 0 0 decisions is obtained by making explicit appeal to
C 1 1 0 1 1 0 0 0 the principle of optimality
(B) In greedy method only one decision sequence is
(D) D 0 0 1 0 0 0 0 0 generated.
E 0 0 1 0 0 1 1 0 (C) In dynamic programming, many decision se-
F 0 0 0 0 1 0 0 0 quences may be generated.
G 0 0 0 0 1 0 0 1 (D) In greedy method many decision sequences are
generated.
H 0 0 0 0 1 1 0 1
28. Consider an array a[n] of ‘n’ numbers that has ‘n/2’
26. Consider the given adjacency list distinct elements and ‘n/2’ copies of another element,
to identify that repeated element, how many steps are
required in the worst case?
1 3 2 (A) n/2 (B) n/2 + 1
2 4 1 6 (C) n/2 + 2 (D) n
3 1 4 29. Match the following, for a very large value of ‘n’
4 2 3
I. 36n3 + 2n2
5 6
II. 5n2 – 6n
6 2 7 5
III. n1.001 + n log n
7 7 8
7
P. θ (n2)
8
Q. Ω (n3)
R. θ (n1.001)
The above list is representation of which of the follow-
(A) I – P, II – Q, III – R (B) I – Q, II – P, III – R
ing graph?
(C) I – R, II – Q, III – P (D) I – R, II – P, III – R
3.154 | Unit 3 • Algorithms
Answers Keys
1. A 2. C 3. B 4. C 5. A 6. A 7. B 8. D 9. D 10. D
11. C 12. D 13. C 14. B 15. B 16. A 17. A 18. B 19. A 20. C
21. B 22. A 23. B 24. B 25. A 26. C 27. D 28. C 29. B 30. C
Algorithms Test 1
Number of Questions: 35 Section Marks: 30
Directions for questions 1 to 35: Select the correct alterna- (C) Binary Tree Traversal
tive from the given choices. (D) Binary search Tree Traversal
1. Consider the given properties of Asymptotic Notations: 6. How many different binary trees are possible with ‘8’
I. f(n) = θ(g(n)) and g(n) = θ(h(n)) nodes?
⇒ f(n) = θ(h(n)) (A) 256 (B) 128
II. f(n) = θ(g(n)) if and only if g(n) = θ(f(n)) (C) 248 (D) 64
III. f(n) = O(g(n)) if and only if g(n) = Ω(f(n)) 7. A binary search tree is generated by inserting in order
IV. f(n) = O(g(n)) if and only if g(n) = O(f(n)) the following integers 66, 72, 46, 48, 9, 8, 40, 36, 18,
Which of the following are valid? 7, 5, 91, 88, 49, 6. The number of nodes in the Left sub
(A) I, II only (B) I, II, III only tree and Right sub tree of the root respectively.
(C) II, III only (D) I, II, III and IV (A) (8, 6) (B) (9, 5)
2. Consider the given Recurrence Relation (C) (10, 4) (D) (11, 3)
T(n) = 2n T(n/3) + n 8. For a Full Binary tree of height ‘h’, the sum of the
Which of the following is TRUE? heights of all nodes is _______ (‘n’ is number of
(A) Master theorem cannot be applied because ‘a’ is nodes)?
not constant. (A) n – (h – 1) (B) n – (h + 1)
(B) It comes under case 1 of Master theorem. (C) n + (h – 1) (D) n + (h + 1)
(C) It comes under case 2 of Master theorem.
9. A graph ‘G’ has 29 edges and its complement G has
(D) It comes under case 3 of Master theorem.
7 edges, what is the number of vertices present in graph
3. Let ‘n’ be the number of elements in the queue, then G?
What is the Time complexity of following operations (A) 7 (B) 8
respectively, Enqueue( ), Dequeue ( ), IsEmptyQueue (C) 9 (D) 10
( ), Delete Queue( )?
10. Consider the given statements:
(A) O(n), O(1), O(1), O(n)
I. Uses priority queue to store unvisited vertices by
(B) O(1), O(1), O(n), O(1)
distance from source.
(C) O(1), O(1), O(1), O(1)
II. It uses greedy method, means pick the next closest
(D) O(n), O(n), O(n), O(1)
vertex to the source.
4. Which of the following are Applications of Binary III. Does not work with negative weights.
Trees? The above statements describe
I. Huffman coding trees are used in data compres- (A) Bellman Ford Algorithm
sion Algorithms. (B) Dijkstra’s Algorithm
II. Priority Queues support search and deletion of (C) Breadth First search Algorithm
minimum or maximum on ‘n’ number of items in (D) Kruskals Algorithm
(log n) time.
11. A complete bipartite graph km,n is a bipartite graph that
III. Expression trees are used in compilers.
has each vertex from one set is adjacent to each vertex
IV. Binary search Tree supports search, insertion and
to another set, what is the minimum ‘vertex cover’ for
deletion on ‘n’ number of items in (log n) time
km,n graphs?
(average case)
(A) MAX(m, n) (B) MIN(m, n)
(A) I, II only (B) I, III, IV only
(C) m (D) n
(C) I, II, III only (D) I, II, III and IV
12. What is the number of Regions present in the bipartite
5. A Traversal is defined as follows:
graph K5,2?
1. Visit the root
(A) 4 (B) 5
2. While traversing Level ‘i’, keep all the elements at
(C) 6 (D) 7
level ‘i + 1’ in queue.
3. Go to the next Level and visit all the nodes at that 13. Consider the following:
level. Best Case Average Case Worst Case
4. Repeat this until all the levels are completed.
I. O(n) O(n2) O(n2)
The above defined traversal is
(A) Depth First Traversal II. O(n log n) O(n log n) O(n log n)
(B) Level order Traversal
Algorithms Test 1 | 3.79
Which of the following is TRUE? 20. The following sequence of operations is performed
(A) Merge sort-II, Selection sort-II on a stack, PUSH(70), PUSH(50), POP, PUSH(30),
(B) Merge sort-II, Heap sort-II PUSH(50), PUSH(70), POP, POP, PUSH(70), POP,
(C) Heap sort-I, Insertion sort-I POP, what is the sequence of values popped out?
(D) Bubble sort-I, Insertion sort-II (A) 50, 70, 50, 70, 70
14. Which of the following Algorithm uses Divide-and- (B) 50, 50, 70, 30, 70
Conquer strategy? (C) 50, 70, 50, 70, 30
(A) Merge sort (D) 50, 70, 50, 30, 70
(B) Quick sort 21. Assume an algorithm for printing the level order data in
(C) Binary search and strassens multiplication Reverse order, for the Binary tree shown below:
(D) All the above
80
15. For merging two sorted Lists of sizes ‘K’ and ‘L’ into a
sorted list of size K + L, what is the number of compari-
sons required? 60 22
(A) O(K) (B) O(L)
(C) O(K*L) (D) O(K + L)
42 72 81 61
16. Consider the following:
f(n) = n log n
g(n) = log (n!) 77 88
log2n
h(n) = 2
Which of the following is TRUE according to Rate of (A) 80, 60, 22, 42, 72, 81, 61, 77, 88
Growth? (B) 80, 22, 60, 61, 81, 72, 42, 88, 77
(A) g(n) ∈ Ω h(n) and g(n) ∈ O(f(n)) (C) 77, 88, 42, 72, 81, 61, 60, 22, 80
(B) g(n) ∈ O(f(n)) and f(n) ∈ Ω(g(n)) (D) 88, 77, 61, 81, 72, 42, 22, 60, 80
(C) g(n) ∈ Ω(f(n)) and f(n) ∈ Ω(g(n)) 22. Consider the given code:
(D) h(n) ∈ O(g(n)) and g(n) ∈ O(f(n)) int fun(struct BinaryTreeNode *root1,
17. Consider the given code: struct Binary Tree Node *root2)
y = y + z; {
for (i = 1; i < = n; i++) if(root1 = = NULL & & root2 = = NULL)
k = k + 2; return 1;
for (i = 1; i < = n; i++) if(root1 = = NULL || root2 = = NULL)
{ return 0;
for(j = 1; j < = n; j++) return (root1 → data = = root2 →
x = x + 1; data &&
} fun(root1 → left, root2 → left) &&
What is the time complexity of the given code? fun(root1 → right, root2 → right));
(A) O(n3) (B) O(n2) }
(C) O(n log n) (D) O(n2 log n) The above code describes, which of the following task?
18. Consider the given Recurrence Relation (A) Finding the number of nodes with only one child
(B) Finding the number of nodes in a Tree.
T ( n) = 3T + n0.52 .
n
(C) Finding, whether two binary trees are structurally
9 identical or not
What is the Time complexity? (D) Finding the number of Leaf nodes in a tree.
(A) O(n) (B) O(n2) 23. We are given a set of ‘6’ distinct elements and an unla-
(C) O(n0.52) (D) O( n ) belled binary tree with ‘6’ nodes. In how many ways
can we populate the tree with the given set, so that it
19. Consider an empty stack of integers. The numbers 6, 7, becomes a binary search tree?
1, 4, 3, 2, 8, 9 are pushed on to the empty stack in the (A) 58 (B) 720
above given order from Right to Left. Let Z denote a (C) 360 (D) 132
PUSH operation and ‘W’ denote a POP operation. 24. A binary tree is a tree data structure in which each node
Which of the following is the sequence of integers has atmost 2 children, that is, the degree of each node
Popped out after performing ZZZZWWZZWWZW? can be atmost ‘2’. If a binary tree has ‘n’ leaf nodes,
(A) 3, 2, 4, 1, 7 (B) 3, 2, 1, 4, 9 then the number of nodes of degree 2 is?
(C) 3, 2, 1, 4, 8 (D) 3, 2, 1, 4, 7
3.80 | Algorithms Test 1
(A) n – 1 (B) 2n – 1 30. Which of the following MAX-HEAP displays the ele-
n ments in sequence (descending order) if preorder tra-
(C) n + 1 (D)
2 versal is applied on MAX-HEAP?
25. For a 5-ary tree (each node can contain maximum of 7
5 children), what is the maximum possible height with
50 nodes, Assume that height of a single node is ‘0’?
(A) 25 (B) 30 (A) 5 6
(C) 49 (D) 50
26. For a 7-ary tree (each node can contain maximum of 4 3 2 1
7 children), what is the maximum possible height with
7
60 nodes, If we have a restriction that atleast one node
should have 7 children?
(A) 14 (B) 21 (B) 6 5
(C) 49 (D) 53
27. For a K-ary tree (each node can contain maximum of
K children), what is the maximum possible nodes at 4 3 2 1
height ‘h’? 7
(A) Kh+1 (B) Kh
–1
(C) Kh (D) 2Kh
28. Consider any completer Binary tree, what are the mini- (C) 6 3
mum and maximum number of elements interms of
height ‘h’?
5 4 2 1
(A) 2h+1 and 2h+2 (B) 2h and 2h+1 – 1
(C) 2 – 1 and 2 + 1 (D) 2h and 2h+1
h+1 h+1
7
29. Which of the following MIN-HEAP displays the ele-
ments in sequence (ascending order) if preorder tra-
(D) 5 6
versal is applied on MIN-HEAP?
1
3 4 1 2
Common Data for Questions 34 and 35: 34. Which of the following is TRUE?
Consider the given Inorder, preorder, postorder traversals of (A) I is pre order and II is post order
a tree, but it is not known which is what order (B) I is pre order and III is In order
I. ABCDEGFH
(C) I is pre order and II is In order
II. EDGHFCBA
III. EHFGDCBA (D) II is post order and III is In order
35. Which of the following is the correct tree for the tra- A A
versals given in the above question?
A A B B
B B C C
C C (C) D (D) D
(A) D (B) D E F E G
E F E G F
F G H H
G
H H
Answer Keys
1. B 2. A 3. C 4. D 5. B 6. C 7. D 8. B 9. C 10. B
11. B 12. B 13. B 14. D 15. D 16. D 17. B 18. C 19. D 20. C
21. C 22. C 23. D 24. A 25. C 26. D 27. B 28. B 29. B 30. C
31. C 32. D 33. D 34. C 35. D
19. Given Integers \ If both trees are NULL then return true.
6, 7, 1, 4, 3, 2, 8, 9 \ If both trees are not NULL, then compare data and
From Right to Left: recursively check left and right subtree structures.
9, 8, 2, 3, 4, 1, 7, 6 Choice (C)
Z Z Z Z(4 push operations) 23. With ‘6’ distinct elements and 6 unlabelled binary tree
3 nodes, we can have,
2 1
n = 6, × 2ncn
8 n +1
9 1 (2 × 6)! 1 12 × 11× 10 × 9 × 8 × 7
× ×
2 pop operations 6 + 1 (12 − 6)!× 6 ! 7 6 × 5× 4 × 3× 2
⇒ 3, 2
Again 2 push operations = 11 × 2 × 3 × 2 = 132. Choice (D)
24. A binary tree with ‘n’ leaves have (n – 1) internal nodes.
1
So (n – 1) nodes will have degree ‘2’. Choice (A)
4
25. In 5-ary tree each node can contain 0 to 5 children and
8
to get maximum height, we have to keep only one child
9 for each parent, with 50 nodes the maximum possible
2 pop operations height we can get is 49. Choice (C)
⇒ 1, 4 26. If we have a restriction that atleast one node should
One push operation have 7 children, then we keep one node with 7 children
7 and remaining all nodes with 1 child. In this case, with
8 ‘n’ nodes the maximum possible height is (n – 7).
\ 60 – 7 = 53. Choice (D)
9
27. If we want to get minimum height, then we need to fill
One pop operation
all nodes with maximum children.
⇒ 7
Lets take a 4-ary tree.
\ popped integers are 3, 2, 1, 4, 7. Choice (D)
20. Height (h) Maximum Nodes at height, h = 4h
50 0 1
70 1 4
⇒ pop ‘50’ 2 4×4
70 3 4×4×4
50
Choice (B)
30
28. In a complete binary tree, all levels contain full nodes
70
except possibly the lowest level.
⇒ pop 70, 50 Maximum = 2h+1 – 1 elements
Minimum = 2h. Choice (B)
70
30
29. Pre order traversal on the following Min-Heap pro-
duces elements in ascending order
70
⇒ pop 70, 30 1
\ popped sequence = 50, 70, 50, 70, 30.
Choice (C)
21. Level order data in reverse means, 2 5
Bottom level data elements from Left to Right should
be taken first.
Level order Traversal is, 77, 88, 42, 72, 81, 61, 60, 22, 3 4 6 7
80. Choice (C)
\ 1 2 3 4 5 6 7. Choice (B)
22. Given two binary trees,
Returns true if they are structurally identical. 30. Pre order Traversal on the following MAX-HEAP pro-
duces elements in sequence (descending order)
3.84 | Algorithms Test 1
(C) Sort the given array in descending order, in this sort- (B) A, B, C, E, H, G, D, I, F
ed array the first element is the repeated element. (C) A, B, C, D, E, G, H, I, F
(D) Both (A) and (B) (D) A, B, C, E, G, H, D, I, F
17. Consider the given multi stage graph 21. Consider the given graph
6
a b c d
A D
2 8
7 5
7
3 9
S B 3 6 E G
e f g
4 C F 1
1
Implement Depth First search Algorithm on the given
What is the shortest path from node ‘S’ to node ‘G’ us- graph, which of the following cannot be the sequence
ing Greedy Approach? of poped elements?
(A) 16 (B) 6 (A) a, b, c, d, g, f, e (B) c, d, g, f, b, a, e
(C) 7 (D) 19 (C) c, d, g, f, b, e, a (D) f, g, d, c, a, e, b
18. Consider the same graph given in the above question, 22. Consider the graph given in the above Question,
What is the shortest path from node ‘S’ to node ‘G’ Implement Breadth First search on the given graph,
using Dynamic programming? Which of the following cannot be the sequence of
(A) 6 (B) 7 nodes Dequeued?
(C) 16 (D) 19 (A) d, c, g, f, b, a, e (B) g, f, d, c, b, a, e
(C) f, c, g, b, d, a, e (D) e, a, c, d, g, f, b
19. Consider the given Huffman code:
23. The keys 22, 31, 46, 42, 58, 61, 64, 71, 83, 97 are
110 100 11110 110 inserted into an initially empty hash table of length
10 using open addressing with hash function h(k) = k
Huffman code is constructed for the following set of mod 10 and Linear probing. What is the Resultant hash
letters whose frequencies are based on the first 8 Fibo- table?
nacci numbers? (A)
a – 1, b – 1, c – 2, d – 3, e – 5, f – 8, g – 13, h – 21 0 1 2 3 4 5 6 7 8 9
Which of the following sequence of letters correctly 83 31 22 42 61 64 46 71 58 97
matches the given Huffman code? (B)
(A) f g h a d (B) f g h c e
0 1 2 3 4 5 6 7 8 9
(C) f g h d f (D) f g h d e
97 31 22 42 61 64 46 71 58 83
20. Implement Breadth-First search on the graph given
below, starting at vertex A. Assume that the adjacency (C)
lists are in sorted order. Example when exploring ver- 0 1 2 3 4 5 6 7 8 9
tex E, the algorithm considers the edge E – B, before 31 22 83 64 46 97 58
E – C, E – F, E – G, or E – H. Which of the following 61 42
71
is the order of vertices that are enqueued on the FIFO
queue? (D)
C D 0
1 31 61 71
2 22 42
3 83
A B G
F 4 64
5
6 46
7
E H I
8 59
9
(A) A, B, C, E, G, H, I, D, F
Algorithms Test 2 | 3.87
24. What is the number of bits required to store a file of 29. What is the time complexity in executing the given
200 characters, The frequencies of a, b, c, d are as fol- code?
lows a – 25, b – 50, c – 100, d – 25 using Fixed Length (A) O(log n) (B) O(n log n)
Encoding?
(A) 200 bits (B) 800 bits (C) O(n2) (D) O n n ( )
(C) 400 bits (D) 600 bits
Common Data for Questions 30 and 31:
25. Consider the data given in the above question what is Consider the following code:
the number of bits required to store the file using vari- void fun1(int k[ ], int n, int X)
able Length Encoding scheme? {
(A) 200 bits (B) 250 bits for (int i = 0; i < n; i++)
(C) 300 bits (D) 350 bits {
26. Consider the tasks T1, T2, T3, T4, T5. Following table for (j = i; j < n; j++)
shows, Deadlines and profits of the given tasks {
if (k[i] + k[j] = = X)
Task Deadline Profit
{
T1 2 20 printf(“Items found”)
T2 2 50 return;
T3 1 30 }
T4 1 40 }
T5 4 60
}
printf(“Items Not found”);
Which tasks are not executed? }
(A) T1 and T2 (B) T1 and T5 30. The above code performs
(C) T1 and T3 (D) T2 and T5 (A) The sum of 2 array elements is equal to given ‘X’
27. For the data given in the above question, what is the value.
profit made from the tasks executed? (B) Sum of adjacent elements whose sum is equal to
(A) 150 (B) 90 given ‘X’ value.
(C) 120 (D) 130 (C) The sum of adjacent elements if they are equal.
Common Data for questions 28 and 29: (D) The sum of adjacent elements which are equal and
Consider the given code: equal to given ‘X’ value.
int fun (int k[ ], int n) 31. What is the time complexity of given code?
{ (A) O(n log n)
int counter = 0, max = 0; (B) O(n2)
for (int i = 0; i<n; i ++) (C) O(n3)
{ (D) O(log n)
counter = 0; Common Data for Questions 32 and 33:
for (int j = 0; j <n; j++) Consider the given graph,
{ 2
if (k[i] = = k[j])
counter ++; a b
3
}
2 4
if (counter >max)
c 1
max = counter;
1
} 2
return max; d e
} 3
Statement for linked Answer questions 34 and 35: Which of the following can be the sequence of
34. Consider the given graph edges added to minimum spanning tree using
“Prims Algorithm”?
2 6
a b e (A) (a – b), (b – d), (d – c), (d – f ), ( f – g), ( f – e), (e – h)
2 4 (B) (a – b), (b – d), (d – c), (d – f ), ( f – e), (e – g), (e – h)
(C) (e – h), (e – f ), (e – g), ( f – d), (d – b), (b – a), (d – c)
4 1 h 3 g (D) (e – h), (e – f ), (e – g), (d – b), ( f – d), (b – a), (d – c)
3
35. What is the total weight of spanning tree (correct edge
5 sequence) identified in the above question?
c d f (A) 17 (B) 19
2 5
(C) 20 (D) 21
Answer Keys
1. C 2. B 3. B 4. D 5. C 6. D 7. B 8. D 9. 8 10. C
11. A 12. 15 13. B 14. C 15. B 16. B 17. A 18. A 19. C 20. D
21. D 22. D 23. B 24. C 25. D 26. C 27. A 28. B 29. C 30. A
31. B 32. D 33. C 34. C 35. B
24 19. a – 1, b – 1, c – 2, d – 3, e – 5, f – 8, g – 13, h – 21
46
0 1 0 54
1 0 1
h 0 33
24 22 16 8 1
21 20
0 0 0 g 0
1 1 1 1 1
0 13
a b c d e f g h f 0 12 1
11 13 12 10 9 7 5 3 8 7
e 0 1
If there are 8 characters, 7 merging operations would be 5 0 4 1
d
performed. In fixed length coding each character takes 3 2
c 0 1
same number of encoded bits, each character takes
3-bits. 2
‘a b f e g’, 5 characters ⇒ 5 × 3 = 15 bits. a b
13. In Depth First search Algorithm, First all elements 1 1
(V) will be pushed onto stack and followed by (V) pop
Huffman codes
operations.
a – 1111110
∴ Total V * V = 2V.
Choice (B) b – 1111111
c – 111110
14. Since the length of the given string is n, there are ‘n’
d – 11110
sub strings possible (excluding empty string) with first
character. Similarly, (n – 1) substrings possible with e – 1110
second character and so on. f – 110
Choice (C) g – 10
15. K-Regular graph (Kn+1-graph) that is complete graph. h–0
Hence O(n3). Choice (B) Given code 110 10 0 11110 110
f g h d f. Choice (C)
16.
20. Queue
4 2 6 4 2 6
Sorted array: A
2 2 4 4 6 6
dequeue A, and enqueue its neighbours in lexicograph-
In the sorted array First repeating element is ‘2’, but in ic order
the given array 4 is the first repeating element.
Sorted array in descending order: B C E
6 6 4 4 2 2
δ(C , G ) = min {1 + δ( F , G ), 6 + δ( F , G )} G H D
δ( E , G ) = 9
3.90 | Algorithms Test 2
H D I 0 1 2 3 4 5 6 7 8 9
97 31 22 42 61 64 46 71 58 83
Dequeue H
Linear Probing: when collision occurs, search for
D I next empty slot from the place of collision, treat array
as circular array. Choice (B)
24. a – 25, b – 50, c – 100, d – 25
Dequeue D
I F 200
0 1
Dequeue I 75 125
0 0 1
1
F
a c
b d
25 50 100 25
Dequeue F Choice (D)
21. After Encoding
a b c d a – 00(2 bits)
b – 01(2 bits)
c – 10(2 bits)
e f g d – 11(2 bits)
Choice (A) Number of bits required
= 25 × 2 + 50 × 2 + 100 × 2 + 25 × 2
a, b, c, d, g, f, e = 50 + 100 + 200 + 50
= 400 bits. Choice (C)
a
25. Variable Length Encoding is also called “Huffman
pop ‘a’ and push its neighbours coding”
Arrange the characters in increasing order
d g
b c f a – 25, d – 25, b – 50, c – 100
f f
e e e e e e 200
0
pop ‘b’ pop ‘c’ pop ‘d’ pop ‘g’ pop ‘f’ pop ‘e’ 1
100
c
popped elements ⇒ a, b, c, d, g, f, e 0 100
1
In the same way check all the options.
50 b
Choice (D) is incorrect. 0 50
Choice (D) 1
22. Option (A): d, c, g, f, b, a, e a d
25 25
d dequeue ‘d’ and Enqueue its neighbours
After Encoding:
c g dequeue 'c’ and Enqueue its neighbours
a – 000 (3 bits)
g f b dequeue 'g’ and Enqueue its neighbours b – 01 (2 bits)
c – 1 (1 bit)
f b dequeue 'f’
d – 001 (3 bits)
b dequeue 'b’ Number of bits required
= 25 × 3 + 50 × 2 + 25 × 3 + 100 × 1
a dequeue 'a’
= 75 + 100 + 75 + 100
e dequeue 'e’ = 350 bits. Choice (D)
26. Arrange the tasks in decreasing order according to their
In the same way, check all the option, options (D) is
profits.
incorrect. Choice (D)
Algorithms Test 2 | 3.91
T5 T2 T4 T3 T1 dist (b − a) = 3
4 2 1 1 2 dist (c − a) = 6
max =7
60 50 40 30 20 dis ( d − a) = 7
T4 T2 T5
dist (e − a) = 4
Eccentricity of Node ‘b’
T3 and T1 cannot be executed because their deadlines
are 1 and 2 respectively. Choice (C) dist ( a − b) = 2
dist (c − b) = 3
max =7
27. The executed Tasks are T2, T4 and T5 and the profit dist ( d − b) = 4
made is 40 + 50 + 60 = 150. Choice (A)
dist (e − b) = 1
28. The given code performs, For each input element, Eccentricity of Node ‘c’
check whether there is any element with same value
and for each such occurrence, increment the counter. dist ( a − c) = 2
dist (b − c) = 4
Every time, check the current counter with the max and max =8
dist ( d − c) = 8
update it, if this value is greater than counter.
Choice (B) dist (e − c) = 5
29. There are 2 nested loops, each loop will be executed ‘n’ Eccentricity of Node ‘d’ is given in the above solution
times that is ‘6’
∴ n × n = O(n2). Choice (C) Eccentricity of Node ‘e’
30. The given code performs sum of 2 array elements dist ( a − e) = 4
dist (b − e) = 6
which is equal to given ‘X’ value. Choice (A) max =6
dist (c − e) = 2
31. Outer loop executes ‘n + 1’ times
Inner loop executes ‘n – 1’ times dist ( d − e) = 3
∴ O(n2). Choice (B) Center of a graph is min of all eccentricities min (7, 4,
8, 6, 6) = Node. Choice (C)
32. Eccentricity of a node is the maximum of minimum
path from other nodes to the given node. 34. If we start Prims Algorithm with edge (a – b) the
Min path ( a − d ) = 3 sequence will be
Min path (b − d ) = 5 (a – b), (b – d), (d – c), (d – f ), ( f – e), (e – h), (e – g)
max If it starts with edge (e – h) the sequence will be
Min path (c − d ) = 1
(e – h)(e – f )(e – g), ( f – d), (d – b), (b – a), (d – c).
Min path (e − d ) = 6
Choice (C)
Eccentricity = 6 Choice (D)
35. For the correct sequence
33. Center of a graph is a node with minimum eccentricity. (e – h) (e – f ) (e – g), ( f – d) (d – b) (b – a) (d – c)
Eccentricity of Node ‘a’ 2 + 3 + 4 + 5 + 1 + 2 + 2 = 19. Choice (B)
Algorithms Test 3
Number of Questions: 25 Section Marks: 30
Directions for questions 1 to 25: Select the correct alterna- 6. Consider the following:
tive from the given choices. I. If f(n) = O(g(n)) and g(n) = O(h(n)) then
1. Given the following input 564, 816, 726, 903, 1321, f(n) = W(h(n))
1345, 1498 and the Hash function is h(k) = k mod 3. II. The asymptotically tight upper bound for
Which of the following statements are CORRECT? T(n) = T(n – 2) + 1 is O(log n)
I. 564, 816, 726, 903 will hash to same Location. Which of the following is correct?
II. 1321, 1345, 1498 will hash to same Location. (A) I–TRUE, II–TRUE (B) I–TRUE, II–False
III. 816, 1345, 903 will hash to same Location. (C) I–False, II–TRUE (D) I–False, II–False
(A) I and III (B) I and II 7. Let
(C) II and III (D) I, II and III f(n) = 6n + 8n2 + 200n3,
2. Let M be an integer greater than 1. Which of the fol- g(n) = n2 log n
lowing represents the order of growth of the expression Which of the following is valid?
n (A) f(n) = O(g(n)) (B) f(n) = q(g(n))
∑ Mi as a function of ‘n’? (C) f(n) = W(g(n)) (D) g(n) = W(f(n))
i =1
8. If f(n) = n log n then which of the following is FALSE?
(A) q(nM) (B) q(M2n+1) (A) f(n) = O(n2 log n)
(C) q(Mn log n) (D) q(Mn) (B) f(n) = q (n log n)
(C) f(n) = W (n2)
Common Data For Questions 3 and 4: (D) f(n) = W (log n * log n)
To compute the Matrix product M1 M2, where M1 has
9. What is the average time complexity of the sequential
‘p’ rows and ‘q’ columns and M2 has ‘q’ rows and ‘r’ col-
search algorithm where the searched item is sequen-
umns, takes time proportional to ‘pqr’, and result is a matrix
tially compared to each element in the list of ‘n’
of ‘p’ rows and ‘r’ columns. Consider the given 5 matri-
elements?
ces, A1, A2, A3, A4 and A5 and their dimensions are P0 × P1,
n
P1 × P2, P2 × P3, P3 × P4, P4 × P5, respectively. The val- (A) (B) log n
ues are P0 = 10, P1 = 5, P2 = 15, P3 = 20, P4 = 40, 2
P5 = 25. n +1 n −1
(C) (D)
5 1 2 2
4 2 10. Consider the given data:
i
j 3 3 — Let n be the number of elements
Y 4 — Number of levels used in sorting: O(log n)
2 5 — At each level O(n) amount of work
The given data is related to:
1 X
(A) Heap sort (B) Merge sort
A4 A5 (C) Selection sort (D) Bubble sort
A3
A2 11. Consider a graph, with all distinct Edge weights, which
A1
of the following is TRUE?
(A) The shortest path is unique between every pair of
3. MATRIX-CHAIN-ORDER computes the rows from
vertices
bottom to top and from left to right, The value of X
(B) The maximal matching is unique
(where X = M[1, 2]) is ________
(C) The minimum spanning tree is unique
(A) 600 (B) 750
(D) All the above
(C) 800 (D) 900
12. The Hash function h(k) = k mod 7 with Linear probing
4. The value of Y (where Y = M[2, 4]) is ________.
is used to insert the keys 37, 38, 72, 68, 98, 11, 74 into
(A) 1500 (B) 5500
hash table indexed (0 – 6), The location of Key 74 is
(C) 15000 (D) None of the above
_________.
5. Which of the following cannot be the time complex- (A) 1 (B) 2
ity of Quick sort algorithm, under any of the Average, (C) 3 (D) 4
Best, Worst cases?
13. Given an undirected graph G = (V, E) and a posi-
(A) O(n log n) (B) O(n2)
3 tive integer ‘K’, does ‘G’ have ‘K’ vertices that form
(C) O(n ) (D) O(log n)
Algorithms Test 3 | 3.93
a complete sub graph and if it does, then what is the for (int j = 1; j < n; j * = 2){
minimum value of ‘K’? for (int k = 0; k < n; k + = 2){
(A) 2 (B) 3 sum + = (i + j * k);
}
(C) 4 (D) None of the above
}
14. In the Build-Heap algorithm, the Heapify routine }
is invoked ‘n’ times. This indicates that Build-Heap (A) O(n3)
Complexity is _____. (B) O(n2 log n)
(A) O(log n) (B) O (n) (C) O(log n * log n * log n)
(C) O(n log n) (D) O(n2) (D) O(n * log n * log n)
15. Give asymptotically tight Big-O bounds for the follow- 22. Consider the modified binary search algorithm so that
ing recurrence relations. (By using Iterative substitu- it splits the input not into 2 sets of sizes equal sizes, but
tion method) into three sets of sizes approximately one-third. What
T(n) = T(n – 2) + 1 if n > 2 is the recurrence for this ternary search algorithm?
T(n) = 1 if n ≤ 2
(A) T(n) = T + T ( n − 2) + C
n
(A) O(n log n) (B) O(n) 2
(C) O(n2) (D) O(log n)
(B) T(n) = T + C
n
16. Consider the given code:
public static int f2(int n){ 3
(C) T(n) = T + T + C
int x = 0; 3n n
for (int i= 0; i < n; i++)
4 4
for (int j = 0; j < i * i; j++)
(D) T(n) = T + log n
x++; n
return x; 3
}
What is the order of growth of the above code? 23. Sort the following growth rate classes in increasing
(A) O(log n) (B) O(n2) order of time complexity:
3
(C) O(n ) (D) O(2n) Exponential, quadratic, logarithmic, cubic, and facto-
rial.
17. Consider the following:
(A) Logarithmic, quadratic, cubic, exponential, facto-
I. (3n)! = O(n!3) II. log (n!) = q(n log n)
rial
Which of the following is correct?
(B) Logarithmic, quadratic, cubic, factorial, exponen-
(A) I–False, II–False (B) I–False, II–TRUE
tial
(C) I–TRUE, II–False (D) I–TRUE, II–TRUE
(C) Quadratic, cubic, logarithmic, exponential, facto-
18. The Floyd-Warshall all pairs shortest path algorithm rial
for finding the shortest distances between nodes in a (D) Quadratic, cubic, logarithmic, factorial, exponen-
graph is an example of: tial
(A) An iterative based divide - and - conquer
24. Determine if ‘x’ is present in an array of n-elements:
(B) A Dynamic programming formulation
for i = 0 to n
(C) A greedy algorithm if (a[i] = x) return TRUE
(D) A recursive based divide - and - conquer else
19. Which of the following is not having O(n2) complexity? return false
(A) n + 2000n (B) n1.999 What is the worst case, best case time complexities and
n3 space complexity respectively?
(C) 106n + 26n (D)
n (A) O(n), O(1), O(n)
(B) O(n), O(1), O(1)
20. Consider the given two strings str1 and str2. (C) O(n), O(log n), O(1)
Let str1 = < A B R A C A D A B R O A > (D) O(log n), O(log n), O(n)
str2 = < Y A B B A D A B B A D O O B A >
Then the length of longest common subsequence would 25. What is the space complexity of following sorting algo-
be ___________. rithms respectively?
(A) 5 (B) 6 I. Quick sort II. Merge sort
(C) 7 (D) 8 III. Selection sort IV. Insertion sort
(A) O(n), O(n), O(1), O(1)
21. What is the computational complexity of the following (B) O(1), O(n), O(1), O(n)
piece of code: (C) O(1), O(n), O(1), O(1)
for (i = n; i > 0; i/= 2){
(D) O(1), O(n), O(n), O(1)
3.94 | Algorithms Test 3
Answer Keys
1. B 2. D 3. B 4. B 5. D 6. D 7. C 8. C 9. C 10. B
11. C 12. A 13. A 14. D 15. B 16. C 17. A 18. B 19. D 20. D
21. D 22. B 23. A 24. B 25. C
Statement III is not TRUE. Choice (B) (2) M [2, 4] = M[2, 3] + M[4, 4] + P1 P3 P4
k =3
2. Let M value be ‘3’
n We need to get the values of M[3, 4] and M[2, 3]
∑ 3i = 31 + 32 + 33... + 3n M [3, 4] = M[3, 3] + M[4, 4] + P2 P3 P4
k =3
i =1
g(n) = n2 log n
Option (A):
Option (C)
n3 ≤ c * n2 log n (false) For any graph, there will be unique spanning tree.
Option (B): Choice (C)
n3 = c * n2 log n (false) 12. Hash function h(k) = k mod 7
Option (C): Given elements are: 37, 38, 72, 68, 98, 11, 74
n3 ≥ c * n2 log n (TRUE) Choice (C) 37 mod 7 = 2
38 mod 7 = 3
8. Option (A):
72 mod 7 = 2
n log n ≤ c * n2 log n (TRUE) 68 mod 7 = 5
Option (B): 98 mod 7 = 0
n log n = c * n log n (TRUE) 11 mod 7 = 4
Option (C): 74 mod 7 = 4
n log n ≥ c * n2 (False) Choice (C) 98 0
9. In an average case, the probability that ‘X’ is in the 74 1
Kth array slot is 1/n, hence the average number of com- 37 2
parisons is 38 3
n n
K×1 = 1× K
∑ n n ∑ 72 4
i =1 k =1 68 5
1 n( n + 1) n + 1 11 6
= * = Choice (C)
n 2 2 Choice (A)
10. In the worst, average, best cases, merge sort procedure 13.
will have O(log n) levels. A B C
In each level O(n) work is required. Choice (B)
11. Option (A): B
A
b
2 3 It is a complete sub graph, with K = 2. Choice (A)
a 14. Heapify routine takes O(log n) time, if it is invoked ‘n’
c
times then Build Heap takes n * log n ⇒ O(n log n).
1 4
Choice (D)
d
15. Given Recurrence Relation:
\ There are 2 shortest paths From ‘a’ to ‘c’ T(n) = T(n – 2) + 1
1. a – b – c Assume n = 16
2. a – d –c T(16) = T(14) + 1
Option (B): T(14) = T(12) + 1
T(12) = T(10) + 1
T(10) = T(8) + 1
T(8) = T(6) + 1
T(6) = T(4) + 1
T(4) = T(2) + 1
T(2) = 1
Maximal matching 1 T(4) = 2
T(6) = 3
T(8) = 4
T(10) = 5
T(12) = 6
T(14) = 7
3.96 | Algorithms Test 3
⇒ ⇒ O ⇒ O(n)
n n
Choice (B)
2 2 str2 = Y A B B A D A B B A D O O B A
The longest common subsequence would be A B A D
16. Each iteration of the inner loop is quadratic in the outer
A B O A.
loop variable. The simplest way to do this is to realize
The length is ‘8’. Choice (D)
we are just summing Si2, which will just be O(n3), if we
use the integration trick. Choice (C) 21. In the outer for loop, the variable ‘i’ keeps halving, so it
goes a round (log n) times. For each ‘i’ next loop goes
17. Let us assume some value for n
round also (log n) times, because of doubling the vari-
n=5
n
I. (3 × 5)! = 15! = very large number able j. The inner most loop by ‘k’ goes round times.
(5!)3 = 1203 = 1728000 2
Loops are nested, so the bounds may be multiplied to
(3n)! < O(n!3)
give that the algorithm is O(n * log n * log n)
I–false
Choice (D)
II. log(n!) = q(n log n)
lets take n = 8 22. By analogy, we divide the elements instead of 2 sub-
log (n!) = log(8!) = log(40320) = 15.299 sets, we divide them into three subsets.
n log n = 24 n
\ T(n) = T + c Choice (B)
log (n!) < (n log n) 3
II–false Choice (A)
23. logarithmic → log n
18. The Floyd-warshall all pairs shortest path algorithm in cubic → n3
a graph is an example of a dynamic programming for- quadratic → n2
mulation. Exponential → 2n
Choice (B) Factorial → n!
19. Option (A) logn < n2 < n3 < 2n < n!
n + 2000n ≤ c * n2 Logarithmic, quadratic, cubic, exponential, Factorial.
⇒ n ≤ c * n2 (TRUE) Choice (A)
Option (B) 24. The procedure given in problem performs linear search,
n1.999 ≤ c * n2 (TRUE) Worst case ⇒ O(n)
Option (C) Best case ⇒ O(1)
106n + 26n ≤ c * n2 We do not require extra space to search for an element
n ≤ c * n2 (TRUE) in an array, so space complexity is O(1). Choice (B)
Option (D)
25. Quick sort, selection sort, insertion sort are ‘In-place’
n3
≤ c * n2 (False) algorithms means they do not take extra space.
n \ O(1), O(1), O(1)
n2.5 ≤ c * n2(False) Merge sort is Not In-Place algorithm.
\ for a very large value of ‘n’ It needs extra space
Choice (D) \ O(n). Choice (C)
Algorithms Test 4
Number of Questions: 25 Section Marks: 30
Directions for questions 1 to 25: Select the correct alterna- (A) Store the element in an unsorted array and apply
tive from the given choices. linear search.
1. Suppose ‘G’ is an undirected graph, Then which of the (B) Store the element in a hash table and use hashing.
following is TRUE? (C) Store the element in a sorted array and apply bi-
I. If G is a tree, there is a unique path between any nary search.
2 vertices in G. (D) All the above
II. If G = (V, E) is connected and E = V – 1, then G is 9. Which of the following problems have solutions (algo-
a tree. rithms) that run in q(n) time in the worst case?
III. Deleting an edge from a cycle cannot disconnect a (A) Finding the median of ‘n’ integers
graph. (B) Finding the sum of ‘n’ integers
(A) Only II (B) Only III (C) Finding the largest of ‘n’ integers
(C) Only I, II (D) I, II and III (D) All the above
2. Consider an array with ‘n’ values, to return the maxi- 10. Consider the following: (Here h( ) is a hash function).
mum sum of 3 values in an array. What is the time com- I. h(k1) = h(k2) even for k1 ≠ k2
plexity of performing this task? II. h(k1) ≠ h(k2) for k1 < k2 always
(A) O(n) (B) O(n2 log n) III. h(k1) = h(k2) for k1 > k2 always
2
(C) O(n ) (D) O(log n) Which of the following is TRUE?
3. What is the length of longest increasing subsequence of (A) I only (B) I and II
‘6, 4, 5, 2, 7, 11, 8, 12, 13, 9, 10’? (C) II and III (D) I, II and III
(A) 2 (B) 3 11. Let ‘OPT’ be an optimal solution and ‘x’ the solution
(C) 4 (D) 5 we found.
4. Consider 2 sequences, S = gacggattag, and X = gatcg- If OPT equals x then we are done otherwise, we can find
gaatag. What is the length of Longest common another optimal solution that ‘agrees more with x’ which
subsequence? of the following algorithm has in first attempt ‘OPT = x’?
(A) 7 (B) 8 (A) Fractional knapsack
(C) 9 (D) 11 (B) Travelling sales person problem
(C) Kruskals algorithm
5. How does the key in a node compared to the keys of its (D) None of the above
children in
I. Binary search tree 12. Match the following algorithm with its time complexity.
II. A Max HEAP I Dijkstras algorithm P. O(E + V) log V
P. node. key > node.left.key, node.right.key
II Prims algorithm Q. O(E + V)
Q. node.left.key < node.key < node.right.key
Which of the following is correct? III Breadth first search
(A) I–P, II–Q (B) I–P, II–P IV Depth first search
(C) I–Q, II–P (D) I–Q, II–Q
(A) I–P, II–P, III–P, IV–Q
6. A hash table has 11 slots, uses the hash function h(k) = k (B) I–P, II–P, III–Q, IV–Q
mod 11 and collisions are resolved by separate chain- (C) I–Q, II–Q, III–P, IV–P
ing, what is the minimum chain length in the hash table, (D) I–Q, II–P, III–P, IV–Q
after inserting these elements:
13. The distance matrix of a graph with vertices A, B, C,
3, 43, 8, 11, 14, 25
and D is given below:
(A) 0 (B) 1 A B C D
(C) 2 (D) 3 A 0 1 ∞ ∞
7. Which priority queue implementations, with N ele- B ∞ 0 2 4
ments allow for new entries to be inserted in O(1) time?
(A) Sorted array (B) MAX HEAP C 3 ∞ 0 1
(C) MIN HEAP (D) Unsorted array D 1 ∞ ∞ 0
8. In order to search for an element in a dynamic set, The shortest path from B to D consists of edge(s)
which of the following techniques is the asymptoti- ________.
cally most time efficient in the worst case for the search (A) BC and CD
operation? (B) BD
3.98 | Algorithms Test 4
Answer Keys
1. D 2. C 3. B 4. C 5. C 6. A 7. D 8. B 9. D 10. A
11. C 12. B 13. A 14. A 15. B 16. C 17. B 18. C 19. D 20. C
21. D 22. C 23. D 24. C 25. D
B = 1 × 4, Brute Force
(iv)
C = 4 × 3, 5 + 5 + 5 + 5 +1
D = 3 × 6. Choice (C) ⇒ 5 coins. Choice (D)
1 20. Amortized time is q (log n)
17. a = = 0.02
36 Worst case time is q (n log n). Choice (C)
1 21. • Computing a median of ‘n’ elements takes q(n)
b = = 0.02
36 time as it has to traverse ‘n’ elements.
1 • The shortest path between 2 vertices need not be
c = = 0.08 same in the Minimum spanning tree and in graph.
12
Consider following graph G
1
d = = 0.11 A• 1
•B
9
5 1 1
1
e = = 0.13
36
D• C
Huffman tree: 1
1
e 0.23
0 1 D• •C
0.13 1
d 0.12
The shortest path from A to C is 1 in graph ‘G’ where
0.11 0 1
as, it is 2 in MST. Choice (D)
0.04 c
22. 12 39 2 94 23 77 52 9
0 1 0.08
2 12 39 94 23 77 52 9
a b 2 12 39 94 23 77 52 9
0.02 0.02 2 12 23 39 94 77 52 9
Huffman codes 2 12 23 39 77 94 52 9
a = 1100, b = 1101, c = 111, d = 10, e = 0. Choice (B)
2 12 23 39 77 52 9
18. 7 5 6 3 8 12 9 13 14 10 11 2 12 23 39 52 77 94 9
12 + 11 + 22 + 21 = 66
\ 2 9 12 23 39 52 77 94
7 5 6 3 8 12 9 13 14 10 11
\ Insertion sort. Choice (C)
11 + 20 + 27 + 11 = 69. Choice (C)
19. Coin values 1, 5, 6. 23. Given elements 12 39 02 94 23 77 52 09
Value = 20 First sort elements based on least significant digit
Greedy Algorithm 12 02 52 23 94 77 39 09.
(i)
6 + 6 + 6 +1+1 Sort the above elements based on second least signifi-
cant digit.
⇒ 5 coins
02 09 12 23 39 52 77 94
Brute-Force
(ii) \ The above sorting algorithm is ‘Radix–Sort’.
5+5+5+5
Choice (D)
⇒ 4 coins
Value = 21 24. The available elements are [0, 1, 2, ..., n2 – 1]
Greedy Algorithm There are n2 elements.
(iii)
6 + 6 + 6 +1+1+1 Separate chaining has unlimited space, it can save all
⇒ 6 coins the n2 elements.
Algorithms Test 4 | 3.101
Linear probing: 14 5 21 4 16 31 17 8 11
It will have ‘n’ slots, so maximum we can store ‘n’ val-
ues. 5 14 21 4 16 31 17 8 11
Quadratic probing:
5 14 4 21 16 31 17 8 11
Like linear probing, it will also have ‘n’ slots.
We can store only ‘n’ values. 5 14 4 16 21 31 17 8 11
Choice (C)
5 14 4 16 21 17 31 8 11
25. Bubble sort, compares adjacent elements, if not in
order swaps them. 5 14 4 16 21 17 8 31 11
5 14 4 16 21 17 8 11 31.
Choice (D)
Algorithms Test 5
Number of Questions: 25 Section Marks: 30
Directions for questions 1 to 25: Select the correct alterna- 6. If we run Dijkstra’s algorithm starting from ‘S’ to find
tive from the given choices. shortest path ‘T’, consider the following statements:
1. Which of the following is NOT a tree? I. Dijkstra’s algorithm returns shortest path with
minimum total weight.
(A)
II. Dijkstra’s algorithm returns shortest path, with
minimum number of edges.
Which of the following is TRUE?
(A) I only (B) II only
(C) Both I and II (D) None of the above
7. The reason for using Bellman-Ford algorithm for find-
ing the shortest path between 2 vertices in a graph,
(B)
when Dijkstras algorithm does the same thing:
I. Bellman-Ford Algorithm is faster than Dijkstra’s
Algorithm.
II. Bellman-Ford Algorithm works on any directed
graph, with negative weights also.
(C) Which of the following is TRUE?
(A) I only (B) II only
(C) Both I and II (D) None of these
8. Consider the following statements:
I. By applying Breadth First search, on a tree with
(D) None of the above all edges having equal weight; finds minimum dis-
2. What is the number of vertices in a tree with 57 edges? tance from root to any node.
(A) 58 (B) 26 – 4 II. Use of greedy algorithm to solve the knapsack
(C) 56 (D) 57 with fractions is optimal.
3. Consider 2 graphs G1 and G2: Which of the following is TRUE?
I. G1 is connected and every edge is a bridge. (A) I only (B) II only
II. In G2 for any pair of vertices in the graph there is (C) Both I and II (D) None of the above
one and only one path joining them. 9. Consider the set of keys {1, 4, 5, 10, 16, 17, 21}, Draw
Which of the following is TRUE? a binary search tree with height ‘2,’ Then what values
(A) G1 is a tree but G2 is not a tree will appear at internal nodes? [height of root node is
(B) G1 is not a tree but G2 is a tree ‘0’]
(C) Both G1 and G2 are trees (A) 10, 5, 16 (B) 10, 4, 17
(D) Both G1 and G2 are not trees (C) 4, 17, 21 (D) 5, 10, 17
4. How many spanning trees does the following graph 10. Consider the set of keys {2, 5, 6, 11, 17, 18, 22}, Draw
contain, (Assume that all edges have same weight)? a binary search tree with height ‘6’, then, what will be
the child node values (height of root node is 0).
a
(A) 2 (B) 22
(C) Either 2 or 22 (D) None of the above
b c
11. The following is a directed graph
G = (V, E):
d e V = {a, b, c, d, e, f, g}
E = {(a, b), (c, a), (b, c), (c, d), (d, e),
(A) 2 (B) 3 (e, f), (e, g), (f, g), (d, g)}
(C) 4 (D) 5 Identify the correct strongly connected components,
5. Suppose we have a graph where each edge value which of the following is TRUE?
appears atmost twice, then what will be the number (A) There are 3 strongly connected components.
(atmost) of minimum spanning Trees of this graph? (B) There are 4 strongly connected components.
(A) 2 (B) 3 (C) There are 5 strongly connected components.
(C) 4 (D) 6 (D) None of the above
Algorithms Test 5 | 3.103
12. Consider the given graph, with some edges in bold: 16. Consider the given tree:
A 40
9 10
6 39
2
B C
13 18 15 22 38
8
3
4 16 12 11
D E
5
The MAX HEAP property is applied on the above tree,
The bold edges given in the above graph cannot form a (box area), at node with value ‘6’, which of the follow-
spanning Tree because ing is BEST suitable?
(A) The bold edges are not connected (A) First compare 6 and 18 then swap them if required
(B) The bold edges are not least weight edges (B) First compare 6 and 15 then swap them if required.
(C) First compare 18 and 15, then compare the greater
(C) The kruskals algorithm is not implemented on the
element with ‘6’ and swap them if required.
above graph.
(D) Any one of the above
(D) Both (A) and (B)
17. Consider the following statements about Depth First
13. Consider the given statements: traversal:
I. A digraph is a graph with exactly 2 vertices. I. Suppose we run DFS (Depth First search) on an
n
II. A spanning tree of a graph must contain atleast undirected graph and find exactly 15 back edges.
edges always. 2 Then the graph is guaranteed to have at least one
III. The sorted edges algorithm for solving the trav- cycle.
elling salesman problem always gives optimal re- II. DFS on a directed graph with ‘n’ vertices and at
sult. least ‘n’ edges is guaranteed to find at least one
back edge.
Which of the following is TRUE?
Which of the following is TRUE?
(A) I and II (B) II and III (A) I only (B) II only
(C) I and III (D) Only II (C) Both I and II (D) None of the above
14. Consider the given AVL-tree, 18. Suppose ‘G’ is a connected, undirected graph, whose
edges have positive weights. Let M be a minimum
7 spanning Tree of this graph. We modify the graph by
adding ‘6’ to the weight of each edge, which of the fol-
14 lowing is TRUE?
(A) The order of edges added to minimum spanning
5 tree using kruskal’s algorithm, will change.
10 17
(B) The modification adds 6(|V| – 1) to the total weight
3 6 of all spanning trees.
11 (C) The order of edges added to minimum spanning
tree using prim’s algorithm, will change.
If the element with value ‘12’ is inserted into above (D) None of the above
AVL tree, How many rotations are required to balance
the tree? 19. Consider the following graph:
(A) 0 (B) 1 A C E G
(C) 2 (D) 3
15. In which order, the element must be inserted into an
AVL tree, so that, no single Rotation is required, for the
B D F H
given
Elements = {1, 2, 3, 4, 5, 6, 7} Which of the following orderings not a valid topologi-
(A) {4, 2, 6, 1, 3, 5, 7} cal sort of the graph?
(B) {4, 2, 1, 3, 5, 6, 7} (A) BACEDFGH (B) ABCDEFGH
(C) {7, 6, 5, 4, 2, 1, 3} (C) ABCDEGFH (D) BACDEFGH
(D) {4, 1, 2, 3, 5, 6, 7}
3.104 | Algorithms Test 5
20. What is the time complexity to print all the keys of a (A)
binary search tree in sorted order? abef cd
(A) O (log n) (B) q (n)
(C) q (n2) (D) O (n + log n)
21. Let T1, T2, T3, T4, T4, T5, T6 are tasks given along with
their deadlines and profits:
Tasks T1 T2 T3 T4 T5 T6
g h
Deadlines 2 1 3 2 3 1
Profit 6 4 3 7 2 8 (B)
abe cd
Which of the following tasks are not completed?
(A) T6, T4, T3 (B) T6, T1, T2
(C) T1, T2, T5 (D) T4, T5, T6
22. Consider the below program fragment:
for i ← 1 to length (A) – 1
{ f gh
element ← A [i]
pos ← i
(A)
while pos > 0 and A [pos – 1] de
{ abc
A[pos] ← A[pos – 1]
pos ← pos –1
{
A[pos] ← element.
}
This code implements fg h
(A) Bubble sort (B) Insertion sort
(C) Heap sort (D) Bucket sort (D)
abe cd
23. What is the best case time complexity of given algo-
rithm in Q. 22?
(A) q (n2) (B) q (log n)
(C) q (n) (D) q (n log n)
24. Which of the following is an acyclic component graph fg h
of given graph using strongly connected components
algorithm?
25. Consider an undirected graph
a b c d G = (V, E)
V = {r, s, t, u, v, w, x, y}
E = {(r, s), (r, v), (s, w), (w, t), (w, x), (t, u),
(t, x), (u, y), (u, x) (x, y)}
Which node has the highest degree?
e f g h (A) w (B) x
(C) u (D) t
Answer Keys
1. C 2. A 3. C 4. B 5. C 6. A 7. B 8. C 9. B 10. C
11. C 12. A 13. D 14. B 15. A 16. C 17. A 18. B 19. A 20. B
21. C 22. B 23. C 24. D 25. B
Algorithms Test 5 | 3.105
16 21
1 5
10. 2 II–TRUE
22
III. There is no optimal solution for Travelling sales
5 18 person problem.
∴ Only II is TRUE Choice (D)
6 17
14. 7
11
11 14
5 10
6 R
17 10 17
3 6 -2 11
5 ª R
18 11 -1
12
2
22 12 0
7
Tree 1: Child 22 Tree 2: Child 2
14
Choice (C) 11 5
11. 17
12 ª 3 11
10 6
a b c
10 12
2
requires LL rotation
3 for 1st 3 elements
Option (B): Option (D):
5 4
4
5
Choice (A) 2 1
13. I. Digraph:
6
There is no limit on number of vertices, In digraph 1 2
edges will have directions.
7
I–False requires LL rotation 3
II. Example: Spanning tree
for 1st 3 elements
G1 T1
If we have Imbalance, we need to perform Rotation.
Option (A) has no Imbalance. Choice (A)
16. First compare the children of a node. If it is a max heap,
then compare the child with greater value to parent
T2
G2 node, if required swap them. Choice (C)
Algorithms Test 5 | 3.107
17. I. If the graph has a back edge, then it has a cycle. Order of execution of tasks to maximize the profit
II. a 8 7 3
T6 T4 T3
b c
0 1 2 3 4 5
The uncompleted tasks are T1, T2, T5
DFS on the above graph starting at ‘a’ does not Choice (C)
find any back edge. 22. The given code implements insertion sort. In insertion
a
sort, every iteration removes an element from the input
data and insert it into the correct position in the already
sorted list until no input elements remain. Choice (B)
23. The best case input is when the array is already in
b
sorted order. Then time complexity is q(n) because in
each iteration, the first remaining element of input is
only compared with the right-most element of sorted
c list of the array. Choice (C)
24. Strongly connected components of a directed graph
∴ I–TRUE, II–False Choice (A) G = (V, E) is a maximal set of vertices C ⊆ V such that
18. The order of edges added to minimum spanning tree for every pair of vertices u and v in C, vertices u and
does not change, because we are adding same weight to v are reachable from each other. In the given graph the
all the edges. connected components are {a, b, e}, {f, g}, {c, d} and
To the total weight we have to add 6(V – 1), because in {h}.
a spanning tree, there will be (V – 1) edges, For every Hence, the required acyclic component graph will be
edge additional weight ‘6’ is added. Choice (B)
abe cd
19. Option (A):
BACEDFGH
There is an edge from D → E
Where as in the order E appears first and D appears
next. Which is invalid.
Check the other options in the same manner. fg h
Choice (A)
20. We can print all the keys of a binary search tree in
sorted order using inorder traversal which will take Choice (D)
q(n) time. Choice (B) 25. r s t u