Algorithms Design and Analysis DP Sheet: Year 3 22 20 - Semester 2
Algorithms Design and Analysis DP Sheet: Year 3 22 20 - Semester 2
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
Table of Contents
Table of Contents .........................................................................................................1
FIRST: PROBLEMS and SOLUTIONS ..................................................................................2
QUESTION1: Probability of Failure ...............................................................................2
QUESTION2: Coin Change...........................................................................................6
QUESTION3: Knapsack Variation .................................................................................8
QUESTION4: Exact Sum ..............................................................................................9
SECOND: UNSOLVED PROBLEMS .................................................................................. 11
QUESTION1: N Stairs ............................................................................................... 11
QUESTION2: Super Thief .......................................................................................... 11
QUESTION3: Subset Sum .......................................................................................... 11
QUESTION4: Partitioning Problem ............................................................................. 11
QUESTION5: String Alignment................................................................................... 12
QUESTION6: Rabbit Crossing a River .......................................................................... 12
QUESTION7: Ali Baba in a Cave ................................................................................. 13
THIRD: ONLINE PROBLEMS .......................................................................................... 13
1. Diving for Gold @ UVa .................................................................... 13
2. Vacations @codeforces .................................................................. 13
3. Dividing coins @ UVa ...................................................................... 13
FOURTH: Other Questions ........................................................................................... 14
QUESTION1: 0-1 Knapsack Tracing ............................................................................ 14
QUESTION2: Rod-Cut Tracing.................................................................................... 14
QUESTION3: Convert D&C to DP (bottom-up) ............................................................. 15
QUESTION4: Convert D&C to DP (top-down) .............................................................. 15
QUESTION5: Convert D&C to DP (bottom-up) ............................................................. 16
1
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
A student is currently taking three courses. It is important that he does not fail all of them. If
the probability of failing Graphics is P1, the probability of failing English is P2, and the
probability of failing Algorithms is P3, then the probability of failing all of them is P1P2P3. He
has left himself with four hours to study. How should he minimize his probability of failing all
his courses? (use dynamic programming)
The following gives the probability of failing each course given he studies for a certain
number of hours on that subject, as shown in the following table
Answer:
Probability Table → P (H, N):-
Hours/Subj. No 1 2 3
0 0.8 0.75 0.9
1 0.7 0.7 0.7
2 0.65 0.67 0.6
3 0.62 0.65 0.55
1
These problems originally prepared by Dr. Essam Atta. Solutions are prepared by Shady A.
Elyasaki 2008-2009 and revised by Ahmed Salah
2
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
P (0, 3) = 0.9
We are going to check the probability of failure when studying certain number of subjects
respectively in a time limit.
I.e. if we want to know the minimum probability of failure when studying the 3 subjects in 4
hours we use → M (4, 3)
Hours\No. of 1 2 3
subject To study
Means: 0 P (0,1) P (0,2) * M (0,1) P (0,3) * M (0,2)
Probability to
study one
subject in 0
hours (i.e.
1 P (1,1) Min: Min: Notice every time
subject 1)
P (0,2) * M (1,1) P (0,3) * M (1,2) Hours increment,
P (1,2) * M (0,1) P (1,3) * M (0,2) we get H + 1
2 P (2,1) Min: Min: equations.
3
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
Explanation
Let’s consider M (2, 3). We want to get the minimum probability of failure when studying 3
subjects in 2 hours. So we got 3 cases:-
For (I = 1 → N) If H =0
M (H, N) T *= P (0, I)
If N =1
P (H, 1)
For (I = 0 → H) If H >0
Minimum
T()
Complexity
❖ Creating the Minimized Failure Table takes H * N times
❖ Filling each cell takes H + 1 times
❖ Therefore total complexity → O (H*N * H) → O (H^2 * N)
4
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
Filling Values
Hours\No. of 1 2 3
subject To study
0 0.8 Min: Min:
.75 * .8 = .6 .9 * .6 = .54
Therefore to minimize the probability of failure that student should Study Subject 3 for 3
hours, subject 1 for 1 hour and don’t study subject 2 at all!!
5
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
N.B. To achieve this we could create another table while filling the Minimized Failure Table.
Complexity then would be O (N) for the process of choosing subjects and study time of each.
Answer
❖ Let’s consider “A” array that consists of the coin values >= 1:-
10 2 5 1
0 If p = 0
C [P] =
6
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
Change (d, k, n)
C[0] 0
for p 1 to n
min ∞
for i 1 to k
if A[i] <= p then O (n * k)
if 1 + C[p − d[i]] < min then
min 1 + C[p − d[i]]
coin i
C[p] min
S[p] coin
return C and S
0 1 2 3 4 5 6 7 8
C 0 1 1 2 2 1 2 2 3
S
0 4 2 2 2 3 3 3 2
A 0 10 2 5 1
❖ Use this algorithm to extract minimum coins needed for the change (Which is not
actually needed in the question):-
Make-Change (S, A, n)
while n > 0
Print A[S[n]]
O (n)
n n − A[S[n]]
❖ Output of Make-Change(S,A, 8):-
n=8
>> 2
n=6
>> 5
n=1
>> 1
7
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
(b) Suppose you are given a set of n objects. The weights of the objects are w1, w2, . . . ,wn,
and the values of the objects are v1, v2, . . . , vn. You are given two knapsacks each of weight
capacity C. Write a recurrence relation to determine the maximum value of objects that can
be placed into the two knapsacks. What will be the complexity in this case?
Answer
(a) Recurrence Relation:-
No Items
0 If N = 0 or space
Weight\Item 0 1 2 3 4 _-_-Items-_-_
0 0 0 0 0 0 (Weight, Value)
1 0 0 0 0 0
1- (2, 3)
2 0 3 3 3 3
2- (3, 4)
3 0 3 4 4 4 3- (4, 5)
4 0 3 4 5 5 4- (5, 6)
5 0 3 7 7 7
Complexity:-
8
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
If N = 0 No Items or space
0
Knap (W1, W2, N – 1) If w [N] > W1 & w [N] > W2 Leave it
Answer
D&C Pseudo Code
D&C Complexity:-
9
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
This algorithm uses recursion, so find its recurrence and solve it:
T(N) = 2 × T(N – 1) + Θ(1)
Solve by iteration method ➔ Θ(2N)
For I = 0 to N
For J = 0 to B
If A [I] == J Then
Check[I, J] = true
Else if I == 0 Then
Check[I, J] = false
Else if A [I] < J Then
B1 = Check[I - 1, J – A[I]]
B2 = Check[I - 1, J]
Check[I, J] = (B1 OR B2)
Else
Check[I, J] = Check[I - 1, J]
End If
End For
End For
DP Complexity: Θ(N × B)
10
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
QUESTION1: N Stairs
Suppose we are walking up n stairs. At each step, you can go up 1 stair, 2 stairs or 3 stairs.
Our goal is to compute how many different ways there are to get to the top (level n) starting
at level 0. We can’’t go down and we want to stop exactly at level n.
For example, given the set {−7, −3, −2, 5, 8}, the answer is yes because the subset {−3, −2, 5}
sums to zero.
11
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
In this problem, it is allowed ONLY to change the 2nd string to be aligned with the 1st one.
1. Replace a character from the 2nd string by the one from the 1st string
2. Repeat one character in 2nd string (only if it's matched with the one from 1st string).
Allowing it to be stretched. (e.g. red case in the above example)
3. Skip one character from 2nd string (only if it's matched with the one from 1st string).
Allowing it to be shrunk. (e.g. green case in the above example)
Thus, given two strings, s1 and s2, it's required to find the min number of changes that
should be applied to s2 in order to align it with s1?
The amount of 34 candies. Obviously he could do better if the path is chosen more wisely.
Your task is to choose the best path for Rabbit over the given chain of isles - i.e. to maximize
the amount of the candies collected. Note that Rabbit starts from 1st isle and finishes either
on the Nth or (N-1)th where N is the total number of isles (because from these two it will
necessarily jump immediately to the other bank).
Examples
Input Output
11 5 3 17 2 13 19 7 48
9 7 12 7 16 3 7 17 14 13 4 6 11 6 3 3 5 4 11 3 15 12 14 2 15 19 11 12 157
12
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
Complexity
Your algorithm should take polynomial time
Example
2. Vacations @codeforces
13
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
Given a knapsack of weight W = 3, and the following 4 items, fill-up the DP solution table
and answer the questions:
item index (i) 1 2 3 4
weight (w[i]) 1 2 4 1
cost (c[i]) 30 20 80 30
1. What’s the final total cost in this case? What’s the remaining weight in the knapsack (if
any)?
2. From the filled DP table, find the optimal total cost in each of the following cases:
(knapsack weight, items count) (0,0) (0,3) (3,0) (2,1) (3,2) (3,3) (2,4)
Optimal total cost 0
Answer
1. 60, 1
2.
(knapsack weight, items count) (0,0) (0,3) (3,0) (2,1) (3,2) (3,3) (2,4)
Optimal total cost 0 0 0 30 50 50 60
14
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
Answer
Length 0 1 2 3 4 5 6 7 8 9 10
Optimal Revenue 0 3 6 9 12 16 19 22 25 29 32
main()
return FUN(1)
Answer
FUN(N)
for I = N + 1 downto 1
if (I = N + 1) R[I] = 0
else
m = ∞
for J = I to N
m = Min(m, AUX(I,J) + R[J+1])
R[I] = m
return R[1]
𝜽(𝑵𝟐 )
15
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022
Answer
ST(N)
If (R[N] ≠ NiL) return R[N]
if (N = 0)
return R[N] = 0
if (N = 1)
return R[N] = V[1]
𝜽(𝑵)
16