Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

5 Dynamic Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Chapter 5

Dynamic Programming
LEARNING OBJECTIVES

 Dynamic programming  Longest common subsequence


 Multi-stage graph  Optimal substructure of LCS
 All pairs shortest path problem  NP-hard and NP-complete
 Hashing methods  P-problem
 Mid-square method  NP-problem
 Folding method  P, NP, and Co-NP
 Resolving collisions  Cooks theorem
 Matrix chain multiplication  Non-deterministic search

dynAmic ProgrAmming multi-stAge grAPh


Dynamic programming is a method for solving complex problems A multi-stage graph is a graph
by breaking them down into simpler sub problems. It is applicable
• G = (V, E) with V partitioned into K > = 2 disjoint subsets such
to problems exhibiting the properties of overlapping sub problems
that if (a, b) is in E, then a is in Vi, and b is in Vi + 1 for some sub
which are only slightly smaller, when applicable; the method takes
sets in the partition;
far less time than naive method.
• |V1| = |VK| = 1 the vertex S in V1 is called the source; the vertex t
• The key idea behind dynamic programming is to solve a given is called the sink.
problem, we need to solve different parts of the problem (sub prob- • G is usually assumed to be a weighted graph.
lems) then combine the solutions of the sub problems to reach an • The cost of a path from node V to node W is sum of the costs of
overall solution. Often, many of these sub problems are the same. edges in the path.
• The dynamic programming approach seeks to solve each sub • The ‘multi-stage graph problem’ is to find the minimum cost
problem only once, thus reducing the number of computations. path from S to t.
This is especially useful when the number of repeating sub prob-
lems is exponentially large. Example:
• There are two key attributes that a problem must have in order Stage I Stage II Stage III Stage IV Stage V Stage VI
for dynamic programming to be applicable ‘optimal sub struc-
ture’ and ‘overlapping sub-problems’. However, when the over- 8
5
lapping problems are much smaller than the original problem, 2
12
the strategy is called ‘divide-and-conquer’ rather than ‘dynamic
programming’. This is why merge sort-quick sort are not classi- 9
fied as dynamic programming problems. 1
3 13 15
Dynamic programming is applied for: 6
• Multi stage graph 10
• All pairs shortest path
14
4 7

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

1 – 4  → 30 Cost (III, 7) = min {cost(II, 2) + cost (2, 7),


2 – 5  → 10 Cost (II, 3) + cost (3, 7),
2 – 6  → 20 Cost (II, 4) + cost (4, 7)}
2 – 7  → 30 = min {10 + 30, 20 + 50, 30 + 30}
3 – 5  → 40 = 40 → Via the path 1 – 2 – 7
3 – 7  → 50
Step IV:
4 – 6  → 40 Cost (IV, 8) = min {cost (III, 5) + cost (5, 8),
4 – 7  → 30
Cost (III, 6) + cost (6, 8),
5 – 8  → 10
Cost (III, 7) + cost (7, 8)}
5 – 9  → 20
= min {20 + 10, 30 + ∞, 40 + ∞}
5 – 10  → 30
5 – 11  → 40 = 30 → Via path 1 – 2 – 5 – 8
6 – 9  → 20 Cost (IV, 9) = min {cost (III, 5) + cost (5, 9),
6 – 10  → 30 Cost (III, 6) + cost (6, 9),
7 – 10  → 30 Cost (III, 7) + cost (7, 9)}
7 – 11  → 20 = min {20 + 20, 30 + 20, 40 + ∞}
8 – 12  → 10 = 40 → Via the path 1 – 2 – 5 – 9
8 – 13  → 20
Cost (IV, 10) = min {cost (III, 5) + cost (5, 10),
8 – 14  → 30
9 – 13  → 20 Cost (III, 6) + cost (6, 10),
9 – 14  → 10 Cost (III, 7) + cost (7, 10}
10 – 13  → 10 = min {20 + 30, 30 + 30, 40 + 30}
10 – 14  → 20 = 50 → Via the path 1 – 2 – 5 – 10
11 – 13  → 10 Cost (IV, 11) = min {cost (III, 5) + cost (5, 11)
11 – 14  → 30
Cost (III, 6) + cost (6, 11),
12 – 15  → 20
Cost (III, 7) + cost (7, 11)}
13 – 15  → 10
= min {20 + 40, 30 + ∞, 40 + 20}
14 – 15  → 30
= 60 → Via the path 1 – 2 – 5 – 11
Solution Using Backward Cost or Via the path 1 – 2 – 7 – 11
Format: COST (Stage, node) = minimum cost of travelling
Step V:
to the node in stage from the source node (node 1)
Cost (V, 12) = min {cost (IV, 8) + cost (8, 12)
Step I: Cost (IV, 9) + cost (9, 12),
Cost (I, 1) = 0 Cost (IV, 10) + cost (10, 12),
Step II: Cost (IV, 11) + cost (11, 12)}
= min {30 + 10, 40 + ∞, 50 + ∞, 60 + ∞}
Cost (II, 2) = cost (I, 1) + cost (1, 2) = 0 + 10 = 10
Cost (II, 3) = cost (I, 1) + cost (1, 3) = 0 + 20 = 20 = 40 → Via the path 1 - 2 - 5 – 8 – 12
Cost (II, 4) = cost (I, 1) + cost (1, 4) = 0 + 30 = 30 Cost (V, 13) = min {cost (IV, 8) + cost (8, 13)
Step III: Cost (IV, 9) + cost (9, 13),
Cost (IV, 10) + cost (10, 13),
Cost (III, 5) = min {cost (II, 2) + cost (2, 5),
Cost (IV, 11) + cost (11, 13)}
cost (II, 3) + cost (3, 5),
= min {30 + 20, 40 + 20, 50 + 10, 60 + 10}
cost (II, 4) + cost (4, 5)
= 50 → Via the path 1 – 2 – 5 – 8 – 13
= min {10 + 10, 20 + 40, 30 + ∞}
= 20 → Via path 1 – 2 – 5 Cost (V, 14) = min {cost (IV, 8) + cost (8, 14)
Cost (IV, 9) + cost (9, 14),
Cost (III, 6) = min {cost (II, 2) + cost (2, 6),
cost (II, 3) + cost (3, 6), Cost (IV, 10) + cost (10, 14),
cost (II, 4) + cost (4, 6)} Cost (IV, 11) + cost (11, 14)}
= min {10 + 20, 20 + ∞, 30 + 40} = min {30 + 30, 40 + 10, 50 + 20, 60 + 30}
= 30 → via the path 1 – 2 – 6 50 → Via the path 1 – 2 – 5 – 9 – 14
Chapter 5  •  Dynamic Programming  |  3.137

Step VI: Example:


1
Cost (VI, 15) = min {cost (V, 12) + cost (12, 15), V1 V2
Cost (V, 13) + cost (13, 15), 9
3
5
Cost (V, 14) + cost (14, 15)} V5
2 3
1
= min {40 + 20, 50 + 10, 50 + 30}
3
= 60 → Via the path 1 – 2 – 5 - 8 – 13 – 15 2
V3
V4
(or) 1 – 2 – 5 – 8 – 12 – 15 4
The weight matrix will be
All Pairs Shortest Path Problem 1 2 3 4 5
(Floyd–Warshall Algorithm) 1 0 1 ∞ 1 5
A weighted graph is a collection of points (vertices) con- 2 9 0 3 2 ∞
nected by lines (edges), where each edge has a weight (some 3 ∞ ∞ 0 4 ∞
real number) associated with it. 4 ∞ ∞ 2 0 3
Example:  A graph in the real world is a road map. Each 5 3 ∞ ∞ ∞ 0
location is a vertex and each road connecting locations is an
edge. We can think of the distance travelled on a road from Let D(K) [i, j] = weight of a shortest path from vi to vj using
one location to another as the weight of that edge. only vertices from {v1, v2, … vk} as intermediate vertices in
the path.
•• T he Floyd–Warshall algorithm determines the shortest •• D(0) = W
path between all pairs of vertices in a graph. •• D(n) = D which is the goal matrix.
•• The vertices in a graph be numbered from 1 to n. Consider
the subset {1, 2, … K} of these n vertices. How to compute D(K) from D(K–1)?
•• Finding the shortest path from vertex i to vertex j that uses Case I:  A shortest path from vi to vj restricted to using only
vertex in the set {1, 2, … K} only. There are two situations. vertices from {v1, v2, … vK} as intermediate verti-
ces does not use VK.
1. K is an intermediate vertex on the shortest path. Then D(K) [i, j] = D(K-1) [i, j]
2. K is not an intermediate vertex on the shortest path.
Case II: A shortest path from vi to vj restricted to using only
In the first situation, we can break down our shortest path vertices from {v1, v2 … vK} as intermediate vertices
into two paths: i to K and then K to j. Note that all the ver- does use VK. Then D(K) [i, j] = D(K-1) [i, K] + D(K-1)
tices from i to K are from the set {1, 2, … K – 1} and that [K, j]
all the intermediate vertices from K to j are from the set {1, Since D(K) [i, j] = D(K-1) [i, j]
2, … K –1}. Also in the second situation, we simply have
or D(K) [i, j] = D(K-1) [i, K] + D(K –1) [K, j]
that all intermediate vertices are from the set {1, 2, ... K –
We conclude: D(K) [i, j] = min{D(K-1) [i, j], D(K –1) [i,
1}. Now define the function D for a weighted graph with the
K] + D(K–1) [K, j]}
vertices {1, 2, … n} as follows.
D (i, j, K) = the shortest distance from vertex i to vertex j Example: 1
using the intermediate vertices. In the set {1, 2, … K}
Using the above idea, we can recursively define the func- 1 5
tion D.
4
2 3
D(i, j, K) = W(i, j) if K = 0
min (D(i, j, K – 1), D(i, K, K –1) + D(K, j, K – 1)) if K > 0 −3
2
•• The first line says that if we do not allow intermediate verti-
ces, then the shortest path between two vertices is the weight 1 2 3
of the edge that connects them. If no such weightexists, we 1 0 4 5
usually define this shortest path to be of length infinity.
W = D° = 2 2 0 ∞
•• The second line pertains to allowing intermediate vertices. It
3 ∞ –3 0
says that the minimum path from i to j through vertices {1,
2, … K} is either the minimum path from i to j through ver- 1 2 3
tices {1, 2, … K – 1} OR the sum of the minimum path from 1 0 0 0
vertex i to K through {1, 2, … K – 1} plus the minimum path P= 2 0 0 0
from vertex K to j through {1, 2, … K – 1}. Since this is the 3 0 0 0
case, we compute both and choose the smaller of these.
3.138 | Unit 3  •  Algorithms

K = 1, vertex 1 can be intermediate node Example 2:


D1 [2, 3] = min (D°[2, 3], D°[2, 1] + D°[1, 3])
= min (∞, 7) 1
3 5
=7 1
1
D1 [3, 2] = min (D°[3, 2], D°[3, 1] + D°[1, 2]) 5
7
2
= min (−3, ∞) 6
3 1 4
= –3 50
4 3
2
1 2 3
D1 = 1 0 4 5
The final distance matrix and P
2 2 0 7
3 ∞ –3 0
1 2 3 4 5 6
1 0 2(6) 2(6) 4(6) 3 1
1 2 3
2 2(6) 0 2(6) 4(6) 5(6) 1
P= 1 0 0 0
D =
6
3 2(6) 2(6) 0 2 5(4) 1
2 0 0 1
3 0 0 0 4 4(6) 4(6) 2 0 3 3(3)
5 3 5(4) 5(4) 3 0 4(1)
K = 2, vertices 1, 2 can be intermediate nodes, 6 1 1 1 3(3) 4(1) 0
D2 [1, 3] = min (D′[1, 3], D′[1, 2] + D′[2, 3])
= min (5, 4 + 7) = 5 The values in parenthesis are the non-zero P values.
D2 [3, 1] = min (D′[3, 1], D′[3, 2] + D′[2, 1])
= min (∞, –3 + 2) Table 1  Divide and conquer vs dynamic programming.
= –1 1. 
This design strategy 1. 
This design strategy
divides the problem into chooses an optimal solu-
1 2 3 sub problems, conquer the tion for the problem, by
D2 = 1 0 4 5 each sub problem recur- recursively defining the
2 2 0 7 sively, finally combine all value of optimal solution,
the sub problem solutions, these values are computed
3 –1 –3 0
for the original problem. in bottom up fashion or top
down fashion.
1 2 3 2. each sub problem is solved 2. 
Each sub problem is
P= 1 0 0 0 recursively, and consumes solved only once and is
more time at each sub stored in table
2 0 0 1
problem
3 2 0 0
3. 
Sub problems are inde- 3. 
The sub problems are
pendent of each other e.g., dependent e.g., Traveling
K = 3 vertices 1, 2, 3 can be intermediate Binary search sales person problem
D3[1, 2] = min (D2[1, 2], D2[1, 3] + D2[3, 2])
= min (4, 5 + (–3))
=2 Dynamic Programming
D3[2, 1] = min (D2[2, 1], D2[ 2, 3] + D2[3, 1]) vs Greedy Method
= min (2, 7 + (–1)) The main difference between greedy method (GM) and
=2 dynamic programming (DP) methodology is, DP consid-
ers all possible solutions for the given problem and picks
1 2 3 the optimal one. Where as greedy, considers only one set of
1 0 2 5 solutions to the problem.
D3 = 2 2 0 7 The other difference between GM and DP is that, GM
3 –1 -3 0 considers the choice, which is best at that step, which is done
at each level of the sub problem. That is, it won’t reconsider
1 2 3 its choice. The choices reflect only present, won’t consider
1 0 3 0 the future choices, where as DP tries out all the best alterna-
P= 2 0 0 1 tives and finds the optimal solution. It implements principle of
3 2 0 0 optimality. At each stage of the problem, it decides based on
the previous decision made in the previous stage.
Chapter 5  •  Dynamic Programming  |  3.139

Hashing Methods h(i) = i


h(i + 1) = i + 1 (mod M)
Uniform Hash Function
h(i + 2) = i + 2 (mod M)
If the keys, K, are integers randomly distributed in [0, r] .
then hash function H(K) is given as
.
 mk  .
H (K ) =  
 r  While this ensures that consecutive keys do not collide, it
does not mean that consecutive array locations will be occu-
H(K) is a uniform hash function pied. We will see that in certain implementations this can
Uniform hashing function should ensure lead to degradation in performance.
1
ΣP ( K ) = ΣP ( K ) =  Σ P ( K ) = Multiplication method
K |h ( K ) = 0 K | h ( K ) =1 K |h ( K ) = 0 m
A variation on the middle-square method that alleviates its
P(K) = probability that a key K, occurs that is the number of deficiencies is called, multiplication hashing method. Instead
keys that map to each slot is equal. of multiplying the key x by itself, we multiply the key by a
carefully chosen constant ‘a’ and then extract the middle k
bits from the result. In this case, the hashing function is
Division method
Hashing an integer x is to divide x by M and then to use the M 
h( x) =  (ax mod W )) 
remainder modulo M. This is called the division method of  W 
hashing. In this case the hash function is
h(x) = x mod M if we want to avoid the problems that the middle-square
method encounters with keys having a large number of
Generally this approach is quite good for just about any leading (or) trailing zero’s then we should choose an ‘a’ that
value of M. However, in certain situations some extra care is has neither leading nor trailing zero’s.
needed in the selection of a suitable value for M. For exam- Furthermore, if we, choose an ‘a’ that is relatively prime
ple, it is often convenient to make M an even number. But to W, then there exists another number ‘a’ such that aa′ = 1
this means that h(x) is even if x is even, and h(x) is odd of x (mod W). Such a number has the nice property that if we
is odd. If all possible keys are equiprobable, then this is not take a key x, and multiply it by ‘a’ to get ax, we can recover
a problem. However, if say even keys are more likely than the original key by multiplying the product again by a′,
odd keys, the function h(x) = x mod M will not spread the since a × a′ = aa′x = 1x.
hashed values of those keys evenly. The multiplication method for creating a hash function
•• Let M be a power of two, i.e., M = 2k for some integer k > 1. operates in two steps:
In this case, the hash function h(x) = x mod 2k simply Step 1: Multiply the key K by a constant A in the
extracts the bottom k-bits of the binary representation of range 0 < A < 1 and extract the fractional part of KA.
x. While this hash function is quite easy to compute, it is Step 2: Multiply this value by M and take the floor of the
not a desirable function because it does not depend on all result.
the bits in the binary representation of x.
•• For these reasons M is often chosen to be a prime number. In short the hash function is
Suppose there is bias in the way the keys are created that h(k ) =  M ⋅ ( KA mod 1) 
makes it more likely for a key to be a multiple of some
small constant, say two or three. Then making M a prime Where (KA mod 1) denotes the fractional part of KA, that
increases the likelihood that those keys are spread out is KA  KA
evenly. Also if M is a prime number, the division of x by
that prime number depends on all the bits of x, not just the Example:
bottom k-bits, for some small constant k. Let m = 10000, K = 123456 and A = 5 − 1
2
Example:  Hash table size = 10
Key value = 112 = 0.618033
Hash function = h(k) = k mod M Then h(k ) = 10000 ⋅ (123456 ⋅ 0.61803 mod 1) 
= 112 mod 10 = 2
Disadvantage: A potential disadvantage of the division = 10000 ⋅ (76300.00412 mod 1) 
method is due to the property that consecutive keys map to
consecutive hash values. = 10000 ⋅ 0.00412  = 41
3.140 | Unit 3  •  Algorithms

Practical issues 123456789 are inserted into the table at address 8.


•• Easy to implement The folding method is distribution independent.
–On most machines multiplication is faster than division.
Resolving collisions In collision resolution strategy
–We can substitute one multiplication by shift operation.
algorithms and data structures are used to handle two hash
–We don’t need to do floating-point operations.
keys that hash to the same hash keys. There are a number of
•• If successive keys have a large interval, A = 0.6125423371
collision resolution techniques, but the most popular are open
can be recommended.
addressing and chaining.
Mid-square method •• Chaining: An array of linked list, Separate chaining
A good hash function to use with integer key values is the •• Open Addressing: Array based implementation:
mid-square method. The mid-square method squares the key – Linear probing (Linear Search)
value, and then takes out the middle ‘r’ bits of the result, giv- – Quadratic probing (non-linear search)
ing a value in the range 0 to 2r – 1. This works well because – Double hashing (use two hash functions)
most (or) all bits of the key value contribute to the result. Separate chaining Every linked list has each element
Example: that collides to the similar slot. Insertion need to locate the
Consider records whose keys are 4-digit numbers in base accurate slot and appending to any end of the list in that slot
10. The goal is to hash these key values to a table of size wherever, deletion needs searching the list and removal.
100(i.e., a range of 0 to 99).
This range is equivalent to two digits in base 10.
Hash key = key % table size [0] 72
That is r = 2. If the input is the number 4567, squaring
4 = 36% 8 [1]
yields an 8-digit number, 20857489. The middle two digits of 2 = 18% 8 [2] 10 18
this result are 57. All digits of the original key value (equiva- 0 = 72% 8
[3] 43
lently, all bits when the number is viewed in binary) contribute 3 = 43% 8
6 = 6% 8 [4] 36
to the middle two digits of the squared value. Thus, the result is [5]
2 = 10% 8 5
not dominated by the distribution of the bottom or the top digit 5 = 5% 8 [6] 6
of the original key value. Of course, if the key values all tend 7 = 15% 8 [7] 15
to be small numbers, then their squares will only affect the low
order digits of the hash value. Figure 1  Separate chaining

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

[0] 72 [0] 72 [0]


Table size = 10 elements [1]
[1] [1] 5 Hash1(key) = key %10
[2]
Add the keys 10, 5 and Hash 2(key) = 7 − (key %7)
[2] 18 15 to the previous [2] 18 Insert keys: 89, 18, 49, 58 and 69 [3]
example. Hash key (89) = 89% 10 = 9 [4]
[3] 43 [3] 43
Hash key = key % table Hash key (18) = 18% 10 = 8 [5]
[4] 36 size [4] 36 Hash key (49) = 49% 10 = 9 (collision)
[6] 49
2 = 10% 8 = (7 − (49% 7)
[5] 5 = 5% 8 [5] 10 = (7 − (0)) [7]
7 = 15% 8 = 7 positions from [9] [8] 18
[6] 6 [6] 6
[9] 89
[7] 7 [7] 7
Figure 3  Double hashing

Figure 2  Linear probing

Limitation: A problem with linear probe method is


Insert keys = 58, 69
primary clustering. In primary clustering blocks of data Hash key (58) = 58% 10 = 8 a collision!
may possibly be able to form collision. Several attempts = (7 − (58% 7) = (7 − 2 ) = 5 positions from [8]
may be required by any key that hashes into the cluster Hash key (69) = 69% 10 = 9 a collision!
to resolve the collision. = (7 − (69 % 7)) = (7 − 6) = 1 position from [9]
(ii) Quadratic probing: To resolve the primary clustering [0] 69
problem, quadratic probing can be used. With quadratic [1]
probing, rather than always moving one spot, move i2 [2]
spots from the point of collision where i is the number [3] 58
of attempts needed to resolve the collision. [4]
[5]
[0] 49 [6] 49
[1] [7]
89% 10 = 9 [8] 18
[2] 58
18% 10 = 8 [9] 89
[3] 69 49% 10 = 9 → 1 attempt needed → 12 = 1 spot
[4] 58% 10 = 8 → 2 attempts needed → 22 = 4 spot
[5] 69% 10 = 9 → 2 attempts needed → 22 = 4 spot Figure 4  Double hashing
[6]
[7]
[8] 18
Matrix-chain Multiplication
[9] 89
We are given a sequence of n matrices m1, m2 … mn to be mul-
tiplied. If the chain matrices is < m1, m2, m3, m4>, the product
m1, m2, m3, m4 can be fully parenthesized in 5 distinct ways:
Limitation: Maximum half of the table can be used
as substitute locations to resolve collisions. Once the 1. (m1 (m2 (m3 m4)))
table gets more than half full, its really hard to locate 2. (m1 ((m2 m3) m4))
an unfilled spot. This new difficulty is recognized as 3. ((m1 m2) (m3 m4))
secondary clustering because elements that hash to the 4. ((m1 (m2 m3)) m4)
same hash key will always probe the identical substi- 5. (((m1 m2) m3) m4)
tute cells.
The way we parenthesize a chain of matrices can have a
(iii) Double hashing: Double hashing uses the idea of dramatic impact on the cost of evaluating the product. We
applying a second hash function to the key when a col- can multiply 2 matrices A and B only if they are compatible
lision occurs, the result of the second hash function i.e., the number of columns of A must equal the number
will be the number of positions from the point of col- of rows of B. If A is a (p × q) matrix and B is a (q × r)
lision to insert. There are some requirements for the matrix, the resulting matrix C is a (p × r) matrix. The time
second function: to compute C is the number of scalar multiplications, which
1. It must never evaluate to zero is (pqr).
2. Must make sure that all cells can be probed. Example:  Consider the problem of a chain <m1, m2, m3> of
A popular second hash function is: three matrices. Suppose that the dimensions of the matrices
Hash(key) = R-(Key mod R) where R is a prime num- are (6 × 8), (8 × 14), (14 × 20) respectively. Which parenthe-
ber smaller than the size of the table. sization will give least number of multiplications?
3.142 | Unit 3  •  Algorithms

Soluation: n
  + n = θ (n ) in all. The property of overlapping
2

(i) ((m1 m2) m3) 2


sub problems is the second hallmark of the applicability of
[m1]6 × 8 × [m2]8 × 14 = [m1 m2]6 × 14
dynamic programming.
Number of multiplications performed The first hall mark being optimal substructure.
= 6 × 8 × 14 = 672 Algorithm
[m1 m2]6 × 14 × [m3]14 × 20 = ((m1 m2) m3)6 × 20  1. n ← length [p] – 1
Number of multiplications performed  2. for i ← 1 to n
 3. do m[i, i] ← 0
= 6 × 14 × 20 = 1680
 4. for i ← 2 to n
Total number of multiplications  5. do for i ← 1 to n – i + 1
= 672 + 1680 = 2352  6. do j ← i + i – 1
(ii) (m1(m2 m3))  7. m[i, j] ← ∞
 8. for k ← i to j – 1
[m2]8 × 14 × [m3]14 × 20 = [m2 m3]8 × 20
 9. do q ← m [i, k] + m [k + 1, j] + Pi – 1 Pk Pj
Number of multiplications performed 10. if q < m [i, j]
= 8 × 14 × 20 = 2240 11. then m [i, j] ← q
[m1]6 × 8 × [m2 m3]8 × 20 = (m1 (m2 m3))6 × 20 12.  S[i, j] ← k
13. return m and S
Number of multiplications performed
= 6 × 8 × 20 = 960 It first computes m[i, j] ← 0 for i = 1, 2 … n (the minimum
Total number of multiplications = 960 + 2240 = 3200 costs for chains of length 1). To compute m[i, i + 1] for i = 1,
∴ ((m1 m2) m3) gives least number of multiplications. 2, … n – 1(the minimum costs for chains of length l = 2 and
so on). At each step, the m[i, j] cost computed depends only
We need to define the cost of an optimal solution recur-
on table entries m[i, k] and m[k + 1, j] already computed. An
sively in terms of the optimal solutions to sub problems. For
entry m[i, j] is computed using the products Pi–1 Pk Pj for k
Matrix-chain multiplication problem, we pick as our sub
= i, i + 1, … j – 1. A simple inspection of the nested loop
problem the problems of determining the minimum cost of
structure of the above algorithm yields a running time of
a parenthesization of Ai Ai+1 … Aj for 1 ≤ i ≤ j ≤ n let m[i, j]
O(n3) for the algorithm.
be the minimum number of scalar multiplications needed to
compute the matrix Ai … j; for the full problem, the cost of
a cheapest way to compute A1 … N would be m[1, n]. We can
define m[i, j] recursively as follows:
Longest Common Subsequence
A sub sequence of a given sequence is just the given
m [i, j] = m [i, k] + m [k + 1, j] + Pi-1 Pk Pj sequence with 0 or more elements left out. Formally, given a
sequence x = <x1, x2 … xm>, another sequence z = <z1, z2 …
If i = j, the problem is trivial. The chain consists of just one zk > is a subsequence of x if there exists a strictly increasing
matrix Ai … i = Ai, so that no scalar multiplications are nec- sequence <i1, i2 … ik> of indices of x such that for all j = 1,
essary to compute the product. 2 … k, we have xij = zj
Minimum cost of parenthesizing the product Ai Ai+1 …
Aj becomes Example: z = < B, C, D, B > is a subsequence of x = <A,
B, C, B, D, A, B> with corresponding index sequence <2,
0 if i = j 3, 5, 7>

m[i, j ] = min{m[i, k ] + m[k + 1, j ] Example: Given 2 sequences x and y, we say that a sequence
+ p p p } if i < j , i ≤ k < j z is a common sub sequence of x and y if z is a sub sequence
 i −1 k i
of both x and y.
The m[i, j] values give the costs of optimal solutions to sub If x = <A, B, C, B, D, A, B>
problems. y = <B, D, C, A, B, A>
At this point, to write a recursive algorithm based on The sequence <B, C, A> is a common subsequence of both
recurrence to compute the minimum cost m[1, n] for mul- x and y.
tiplying A1 A2 … An. However, this algorithm takes expo- The sequence <B, C, A> is not a longest common sub-
nential time, which is not better than the brute force method sequence (LCS) of x and y since it has length ‘3’ and the
of checking each way of parenthesizing the product. The sequence <B, C, B, A>, which is also common to both x
important observation we can make at this point is that we and y, has length 4. The sequence <B, C, B, A> is an LCS
have relatively few sub problems, one problem for each of x and y, as is the sequence <B, D, A, B>, since there is no
choice of i and j satisfying 1 ≤ i ≤ j ≤ n (or) common subsequence of length 5 or greater.
Chapter 5  •  Dynamic Programming  |  3.143

•• 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

•• π is NP-hard ⇒ if π can be solved in polynomial time, Cooks Theorem


then P = NP. Cook’s theorem states that CNFSAT is NP-Complete
This is like saying that if we could solve one particular It means, if the problem is in NP, then the deterministic
NP-hard problem quickly, then we could solve any problem Turing machine can reduce the problem in polynomial time.
whose solution is easy to understand, using the solution to The inference that can be taken from these theorems is,
that one special problem as a subroutine. NP-hard problems if deterministic polynomial time algorithm exists for solv-
are atleast as hard as any problem in NP. ing satisfiability, then to all problems present in NP can be
solved in polynomial time.
•• Saying that a problem is NP-hard is like saying ‘If I own
a dog, then it can speak fluent English’. You probably
don’t know whether or not I own a dog, but you’re prob- Non-deterministic Search
ably pretty sure that I don’t own a talking dog. Nobody Non-deterministic algorithms are faster, compared to
has a mathematical proof that dogs can’t speak English. deterministic ones. The computations are fast as it always
The fact that no one has ever heard a dog speak English chooses right step
is evidence as per the hundreds of examinations of dogs The following functions are used to specify these algorithms
that lacked the proper mouth shape and brain power, but 1. Choice (A), which chooses a random element from set A
mere evidence is not a proof nevertheless, no sane person 2. Failure (A), specifies failure
would believe me if I said I owned a dog that spoke fluent 3. Success ( ), Specifies success
English. So the statement ‘If I own a dog then it can speak
fluent English’ has a natural corollary: No one in their The non-deterministic search is done as follows.
right mind should believe that I own a dog ! Likewise if Let us consider an array S[1 … n], n ≥ 1 we need to get
a problem is NP-hard no one in their right mind should the indice of ‘i’ such that S[i] = t (or) i = 0. The algorithm
believe it can be solved in polynomial time. is given below.
Steps:
NP−hard
1. i = Choice (1, n);
2. if S[i] = t, then
(i) Print (i);
(ii) Success ( );
Co−NP NP 3. Print (0)
failure
4. Stop.
P
If the search is successful it returns the indice of array ‘S’,
NP−complete
otherwise it returns ‘0’, the time complexity is Ω(n).

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.

Practice Problems 2 9. Which of the following is TRUE?


(A) All NP-complete problems are NP-hard.
Directions for questions 1 to 15:  Select the correct alterna-
(B) If an NP-hard problem can be solved in polyno-
tive from the given choices.
mial time, then all NP-complete problems can be
1. For NP-complete problems solved in polynomial time.
(A) Several polynomial time algorithms are available (C)  NP-hard problems are not known to be NP-complete.
(B) No polynomial time algorithm is discovered yet (D) All the above
(C) Polynomial time algorithms exist but not discovered
10. If a polynomial time algorithm makes polynomial num-
(D) Polynomial time algorithms will not exist, hence
ber of calls to polynomial time subroutines, then the
cannot be discovered
resulting algorithm runs in
2. In the division method for creating hash functions, we (A) Polynomial time (B) No-polynomial time
map a key k into one of m slots by taking the remainder (C) Exponential time (D) None of these
of k divided by m. That is, the hash function is
11. If a polynomial time algorithm makes atmost constant
(A) h(k) = m mod k (B) h(k) = m mod m/k
number of calls to polynomial time subroutines, then
(C) h(k) = k mod m (D) h(k) = mk mod k
the resulting algorithm runs in
3. In the division method for creating hash function, which (A) Polynomial time (B) No-polynomial time
of the following hash table size is most appropriate? (C) Exponential time (D) None of these
(A) 2 (B) 7
12. When a record to be inserted maps to an already occu-
(C) 4 (D) 8
pied slot is called
4. Which of the following techniques are commonly used to (A) Hazard
compute the probe sequence required for open addressing? (B) Collision
(A) Linear probing (B) Quadratic probing (C) Hashing
(C) Double hashing (D) All the above (D) Chaining
5. Which of the following problems is not NP-hard? 13. Worst-case analysis of hashing occurs when
(A) Hamiltonian circuit problem (A) All the keys are distributed
(B) The 0/1 knapsack problem (B) Every key hash to the same slot
(C) The graph coloring problem (C) Key values with even number, hashes to slots with
(D) None of these even number
6. For problems x and y, y is NP-complete and x reduces (D) Key values with odd number hashes to slots with
to y in polynomial time. Which of the following is true? odd number
(A) If x can be solved in polynomial time, then so can y 14. Main difference between open hashing and closed
(B) x is NP-hard hashing is
(C) x is NP-complete (A) Closed hashing uses linked lists and open hashing
(D) x is in NP, but not necessarily NP-complete does not.
7. If P1 is NP-complete and there is a polynomial time (B) Open hashing uses linked list and closed hashing
reduction of P1 to P2, then P2 is does not
(A) NP-complete (C) Open hashing uses tree data structure and closed
(B) Not necessarily NP-complete uses linked list
(C) Cannot be NP-complete (D) None of the above
(D) None of these 15. The worst case scenario in hashing occurs when
8. A problem is in NP, and as hard as any problem in NP. (A) All keys are hashed to the same cell of the hash table
The given problem is (B) The size of hash table is bigger than the number of
(A) NP hard keys
(B) NP complete (C) The size of hash table is smaller than the number
(C) NP of keys
(D) NP-hard ∩ NP-complete (D) None of the above
Chapter 5  •  Dynamic Programming  |  3.147

Previous Years’ Questions


1. Consider a hash table of size seven, with starting index and linear probing. What is the resultant hash table?
zero, and a hash function (3x + 4) mod7. Assuming  [2009]
the hash table is initially empty, which of the follow- (A) 0 (B) 0
ing is the contents of the table when the sequence 1, 3, 1 1
8, 10 is inserted into the table using closed hashing? 2 12 2 12
Note that − denotes an empty location in the table. 3 23 3 13
 [2007] 4 4
5 15 5 5
(A) 8, −, −, −, −, −, 10 (B) 1, 8, 10, −, −, −, 3
6 6
(C) 1, −, −, −, −, −, 3 (D) 1, 10, 8, −, −, −, 3 7 7
8 18 8 18
Common data for questions 2 and 3: Suppose the let- 9 9
1 1 1 1 1 1
ters a, b, c, d, e, f have probabilities , , , , , , (C) 0 (D)
2 4 8 16 32 32 0
respectively. 1 1
2 12 2 12,2
2. Which of the following is the Huffman code for the 3 13 3 13,3,23
letter a, b, c, d, e, f  ? [2007] 4 2 4
(A) 0, 10, 110, 1110, 11110, 11111 5 3 5 5,15
(B) 11, 10, 011, 010, 001, 000 6 23 6
(C) 11, 10, 01, 001, 0001, 0000 7 5 7
(D) 110, 100, 010, 000, 001, 111 8 18 8 18
9 15 9
3. What is the average length of the correct answer to
above question?
Common data for questions 7 and 8: A sub-sequence
[2007]
of a given sequence is just the given sequence with some
(A) 3 (B) 2.1875 elements (possibly none or all) left out. We are given two
(C) 2.25 (D) 1.9375 sequences X[m] and Y[n] of lengths m and n, respectively,
4. The subset-sum problem is defined as follows: Given with indexes of X and Y starting from 0.
a set S of n positive integers and a positive integer W,
determine whether there is a subset of S whose ele- 7. We wish to find the length of the longest common sub-
ments sum to W. sequence (LCS) of X[m] and Y[n] as l(m, n), where an
An algorithm Q solves this problem in O(nW) incomplete recursive definition for the function l(i, j)
time. Which of the following statements is false? to compute the length of the LCS of X[m] and Y[n] is
 [2008] given below:
I(i, j) = 0, if either i = 0 or j = 0
(A)  Q solves the subset-sum problem in polynomial
= expr1, if i, j > 0 and X[i - 1] = Y[ j - 1]
time when the input is encoded in unary
= expr2, if i, j > 0 and X[i - 1] ≠ Y [ j - 1]
(B)  Q solves the subset-sum problem in polynomial
time when the input is encoded in binary Which one of the following options is correct? [2009]
(C) The subset sum problem belongs to the class NP (A) expr1 ≡ I(i - 1, j) + 1
(D) The subset sum problem is NP-hard (B) expr1 ≡ I(i, j - 1)
(C) expr2 ≡ max(I(i - 1, j), I(i, j - 1))
5. Let πA be a problem that belongs to the class NP. Then
(D) expr2 ≡ max(I(i -1, j - 1), I(i, j))
which one of the following is TRUE?
 [2009] 8. The values of l(i, j) could be obtained by dynamic
(A) There is no polynomial time algorithm for πA. programming based on the correct recursive defini-
(B) If πA can be solved deterministically in polyno- tion of l(i, j) of the form given above, using an array
mial time, then P = NP. L[M, N], where M = m + 1 and N = n + 1, such that
(C) If πA is NP-hard, then it is NP-complete. L[i, j] = l(i, j).
(D) πA may be undecidable. Which one of the following statements would be
TRUE regarding the dynamic programming solution
6. The keys 12, 18, 13, 2, 3, 23, 5 and 15 are inserted
for the recursive definition of l(i, j)? [2009]
into an initially empty hash table of length 10 using
(A) All elements of L should be initialized to 0 for
open addressing with hash function h(k) = k mod 10
the values of l(i, j) to be properly computed.
3.148 | Unit 3  •  Algorithms

(B) The values of l(i, j) may be computed in a row (NPC)? [2014]


major order or column major order of L(M, N). (A) (B)
(C) The values of l(i, j) cannot be computed in either P NP P NP
row major order or column major order of L(M, N). NPC
(D)  L[p, q] needs to be computed before L[r, s] if NPC
either p < r or q < s. P = NP (D) P = NP = NPC
(C)
9. The weight of a sequence a0, a1, …, an-1 of real num- NPC
bers is defined as a0 + a1/2 + … + an-1/2n-1. A subse-
quence of a sequence is obtained by deleting some
elements from the sequence, keeping the order of the 14. Consider a hash, table with 9 slots. The hash func-
remaining elements the same. Let X denote the maxi- tion is h(K) = K mod 9. The collisions are resolved
mum possible weight of a subsequence of a0, a1, …., by chaining. The following 9 keys are inserted in the
an-1. Then X is equal to [2010] order: 5, 28, 19, 15, 20, 33, 12, 17, 10. The maximum,
(A) max (Y, a0 + Y) (B) max (Y, a0 + Y/2) minimum, and average chain lengths in the hash table,
(C) max (Y, a0 + 2Y) (D) a0 + Y/2 respectively, are  [2014]
(A) 3, 0 and 1 (B) 3, 3 and 3
10. Four matrices M1, M2, M3 and M4 of dimensions p × (C) 4, 0 and 1 (D) 3, 0 and 2
q, q × r, r × s and s × t respectively, can be multiplied
15. Consider two strings A = ‘qpqrr’ and B = ‘pqprqrp’.
in several ways with different number of total scalar
Let x be the length of the longest common subsequence
multiplications. For example when multiplied as ((M1
(not necessarily contiguous between A and B and let y
× M2) × (M3 × M4)), the total number of scalar multipli-
be the number of such longest common subsequences
cations is pqr + rst + prt. When multiplied (((M1 × M2)
between A and B. then x + 10y = –––––––– [2014]
× M3) × M4) the total number of scalar multiplications
is pqr + prs + pst. 16. Suppose you want to move from 0 to 100 on the
If p = 10, q = 100, r = 20, s = 5 and t = 80, then the number line. In each step, you either move right by
minimum number of scalar multiplications needed is a unit distance or you take a shortcut. A shortcut is
 [2011] simply a pre-specified pair of integers i, j with i <
j. Given a shortcut i, j if you are at position i on the
(A) 248000
number line, you may directly move to j. Suppose
(B) 44000
T(k) denotes the smallest number of steps needed to
(C) 19000
move from k to 100. Suppose further that there is at
(D) 25000
most 1 shortcut involving any number, and in particu-
11. Assuming P ≠ NP, which of the following is TRUE? lar from 9 there is a shortcut to 15. Let y and z be
 [2012] such that T(9) = 1 + min(T(y), T(z)). Then the value of
(A) NP-complete = NP the product yz is _______ [2014]
(B) NP-complete ∩ P = ∅
(C) NP-hard = NP 17. Consider the decision problem 2CNFSAT defined as
(D) P = NP-complete follows: [2014]
12. Which of the following statements are TRUE? {φ | φ is a satisfiable propositional formula in CNF
(i) The problem of determining whether there exists with at most two literals per clause}
a cycle in an undirected graph is in P. For example, φ = (x1 ∨ x2) ∧ (x1 ∨ x3) ∧ (x2 ∨ x4) is a
(ii) The problem of determining whether there exists Boolean formula and it is in 2CNFSAT.
a cycle in an undirected graph is in NP.
The decision problem 2CNFSAT is
(iii) If a problem A is NP-Complete, there exists a non-
deterministic polynomial time algorithm to solve A. (A) NP-complete
[2013] (B) Solvable in polynomial time by reduction to di-
(A) 1, 2 and 3 (B) 1 and 2 only rected graph reach ability.
(C) 2 and 3 only (D) 1 and 3 only (C) Solvable in constant time since any input in-
13. Suppose a polynomial time algorithm is discov- stance is satisfiable.
ered that correctly computes the largest clique in (D) NP-hard, but not NP-complete
a given graph. In this scenario, which one of the
following represents the correct Venn diagram of 18. Consider a hash table with 100 slots. Collisions are
the complexity classes P, NP and NP-complete resolved using chaining. Assuming simple uniform
Chapter 5  •  Dynamic Programming  |  3.149

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

Previous Years’ Questions


1. B 2. A 3. D 4. B 5. C 6. C 7. C 8. B 9. C 10. B
11. B 12. A 13. D 14. A 15. 34 16. 150 17. B 18. A 19. C 20. 80
21. C 22. C 23. 1500 24. C 25. C 26. 16

You might also like