100% found this document useful (1 vote)
53 views2 pages

Tut 8 Solution

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
53 views2 pages

Tut 8 Solution

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Birla Institute of Technology and Science-Pilani, Hyderabad Campus

Second Semester 2023-2024


Tutorial-8 Solution
Course No CS F364 Course Title: Design and Analysis of Algorithm
Date:27/3/24

General Instructions: Argue logically. Write it in a manner that explains your logic very
clearly. Do not miss steps in between.
Q1 . For bit strings X = x1 ...xm , Y = y1 ...yn and Z = z1 ...zm+n , we say that Z is an interleaving of X
and Y if it can be obtained by interleaving the bits in X and Y in a way that maintains the left-to-right
order of the bits in X and Y . For example if X = 101 and Y = 01 then x1x2y1x3y2 = 10011 is an
interleaving of X and Y , whereas 11010 is not.

1. Give an efficient algorithm using dynamic programming to determine if Z is an interleaving of X


and Y .

2. Analyze its time complexity as a function m = |X| and n = |Y |.

Solution 1 The general form of the subproblem we solve will be: determine if z1 , ..., zi+j is an interleaving
of x1 , ..., xi and y1 , ...yj for 0 ≤ i ≤ m and 0 ≤ j ≤ n. Let c[i, j] be true if and only if z1 ..., zi+j is an
interleaving of x1 , ..., xi and y1 , ..., yj . We use the convention that if i = 0 then xi = λ (the empty string)
and if j = 0 then yj = λ . The subproblem c[i, j] can be recursively defined as shown (where c[m, n] gives
the answer to the original problem)

Figure 1:

We now argue this recursive definition is correct. First the case where i = j = 0 is when both X and Y
are empty and then by definition Z (which is also empty) is a valid interleaving of X and Y . If xi = zi+j
and yj ̸= zi+j then there could only be a valid interleaving in which xi appears last in the interleaving,
and hence c[i, j] is true exactly when z1 , ..., zi+j−1 is a valid interleaving of x1 , ..., xi−1 and y1 , ...yj which
is given by c[i − 1, j]. Similarly, when xi ̸= zi+j and yj = zi+j then c[i, j] = c[i, j − 1]. Finally, consider
when xi = yj = zi+j . In this case the interleaving (if it exists) must either end with xi (in which case
c[i − 1, j] is true) or must end with yi (in which case c[i, j − 1] is true). Thus returning c[i − 1, j] ∨ c[i, j − 1]
gives the correct answer. Finally, since in all cases the value of c[i, j] comes directly from the answer to
one of the subproblems, we have the optimal substructure property.
The time complexity is clearly O(nm) since there are nṁ subproblems each of which is solved in constant
time. Finally, the c[i, j] matrix can be computed in row major order.
Q2 You are driving from Kanyakumari to Hyderabad. There are petrol pumps along the way at distance
x1 , x2 , . . . , xn from Kanyamukari. Because of different wait times and pump speeds, filling up at petrol
pump xi takes ci minutes (assume petrol costs same everywhere, so don’t worry about it). Your car can
hold enough petrol to go 100 KMs and you start wih a full tank of petrol. If you decide to stop at a petrol

1
pump, you will have to fill your entire tank up. Give a dynamic programming algorithm that minimizes
the amount of time at petrol pump during your trip.

1. Write the recurrence relation for the dynamic program. Make sure you define every symbol used.

2. Explain how you derived the recurrence relation and justify it.

3. Write the memoized non recursive pseudocode for your algorithm, which will return the minimum
cost of the problem.

Solution 2 Lets say we start at a petrol pump in kanyakumari x0 , which has waiting time c0 = 0 and
we end at hyderabad petro pump xn+1 = 0 which has waiting time cn+1 = 0.
Let c[i], 0 ≤ i ≤ n + 1 denote the waiting time at each petrol pump with c[0] = c[n + 1] = 0.
Let OP T [i] denotes the optimal waiting time traveling from x0 to xi . thus essentially we are looking for
OP T [n + 1].
Now note that 
c[i] + OP T [i]
OP T [n + 1] = min
{i||xn+1 −xi |<100} 0 if i = 0

Because in the last 100 Kms before we reach our destination xn+1 . We have to fill the petrol. Thus
all those petrol pumps xi such that |xn+1 − xi | < 100 are feasible choices. Depending, upon any one of
the feasible choices, the waiting time increased is c[i] and we have to solve the remaining subproblem of
reaching to xi in optimal cost.

Algorithm 1
procedure FillPetrolAlgo(C, X)
M [0] ← 0
for i ← 1, (n + 1) do
M [i] ← ∞
for j ← 1, (i − 1) do
if then(|X[i] − X[j]| < 100)
K ← C[j] + M [j]
if then(K < M [i])
M [i] ← K
end if
end if
end for
end for
return M [n + 1] .
end procedure

You might also like