0% found this document useful (0 votes)
97 views16 pages

Algorithms Design and Analysis DP Sheet: Year 3 22 20 - Semester 2

This document contains a dynamic programming problem to minimize the probability of a student failing all three of his courses. The student has four hours to study for graphics, English, and algorithms. The probability of failing each course decreases as more hours are spent studying that subject. A probability table is provided based on study hours. The problem is solved using a minimum probability of failure table to systematically calculate the best way to allocate study hours across subjects over four hours to minimize the overall failure probability.

Uploaded by

youssef amr
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
0% found this document useful (0 votes)
97 views16 pages

Algorithms Design and Analysis DP Sheet: Year 3 22 20 - Semester 2

This document contains a dynamic programming problem to minimize the probability of a student failing all three of his courses. The student has four hours to study for graphics, English, and algorithms. The probability of failing each course decreases as more hours are spent studying that subject. A probability table is provided based on study hours. The problem is solved using a minimum probability of failure table to systematically calculate the best way to allocate study hours across subjects over four hours to minimize the overall failure probability.

Uploaded by

youssef amr
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/ 16

Faculty of Computer &

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

FIRST: PROBLEMS and SOLUTIONS1

QUESTION1: Probability of Failure

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

Table: Student failure probabilities.

Hours Graphics English Algorithms

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

4 0.6 0.62 0.5

Answer:
Probability Table → P (H, N):-

Where H means Number of hours and N means Subject Number:

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

4 0.6 0.62 0.5


I.e. P (2, 1) = 0.65

P (0, 3) = 0.9

Minimized Failure Table (Dynamic Programming) → M (H, S):-


Where H means Number of hours and S means Number of Subjects to study.

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)

Means: Probability to study 2


subjects in 1 hours (i.e. subject
1 & 2)

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.

P (0,2) * M (2,1) P (0,3) * M (2,2)


P (1,2) * M (1,1) P (1,3) * M (1,2)
P (2,2) * M (0,1) P (2,3) * M (0,2)
3 P (3,1) Min: Min:
P (0,2) * M (3,1) P (0,3) * M (3,2)
P (1,2) * M (2,1) P (1,3) * M (2,2)
P (2,2) * M (1,1) P (2,3) * M (1,2)
P (3,2) * M (0,1) P (3,3) * M (0,2)
4 P (4,1) Min: Min: Means:
Probability to
P (0,2) * M (4,1) P (0,3) * M (4,2)
study 3 subjects
P (1,2) * M (3,1) P (1,3) * M (3,2) in 4 hours (i.e.
P (2,2) * M (2,1) P (2,3) * M (2,2) subject 1, 2 & 3)

3
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022

P (3,2) * M (1,1) P (3,3) * M (1,2)


P (4,2) * M (0,1) P (4,3) * M (0,2)

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

1- Don’t study subject 3 and study other 2 subjects in 2 hours.


2- Study subject 3 for 1 hour then study 2 subjects in 1 hour.
3- Study subject 3 for 2 hours and don’t study other 2 subjects.
We get probability of the 3 cases then choose the minimum.

How to (Dynamic Programming):-

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

T = P (I, N) * M (H-I, N-1)

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

1 0.7 Min: Min:


.75 * .7 = .525  .9 * .525 = .4725
.7 * .8 = .56 .7 * .6 = .42 
2 0.65 Min: Min:
.75 * .65 = .4875  .9 * .4875 = .4388
.7 * .7 = .49 .7 * .525 = .3675
.67 * .8 = .536 .6 * .6 = .36 
3 0.62 Min: Min:
.75 * .62 = .465 .9 * .465 = .4185
.7 * .65 = .455  .7 * .4875 = .3413
.67 * .7 = .469 .6 * .525 = .315 
.65 * .8 = .52 .55 * .6 = .33
4 0.6 Min: Min:
.75 * .6 = .45 .9 * .434 = .3906
.7 * .62 = .434  .7 * .465 = .3255
.67 * .65 = .4355 .6 * .4875 = .2925
.65 * .7 = .455 .55 * .525 = .2888 
.62 * .8 = .496 .5 * .6 = .3

Minimum Probability of Failure = 0.2888

What Subjects to study and for how long??


We need to backtrack through the Minimized Failure Table and check for the minimum
probability we have chosen earlier.

I.e. Last element we got → P (3, 3) * M (1, 2)

Then when we go to M (1, 2) we find → P (0, 2) * M (1, 1)

Then when we go to M (1, 1) we find → P (1, 1)

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.

That table would contain the minimum item we have chosen.

Complexity then would be O (N) for the process of choosing subjects and study time of each.

QUESTION2: Coin Change


Suppose A ={a1,a2,a3,….ak} is a set of distinct coin values (all the ai are positive integers)
available in a particular country. The coin changing problem is defined as follows: Given
integer n find the minimum number of coins from A that add up to n assuming that all coins
have value in A. You should assume that a1=1 so that it is always possible to find some set
that adds up to n. Design a O(nk) dynamic programming algorithm for solving the coin
changing problem, that is, given inputs A and n it outputs the minimum number of coins in A
to add up to n.(It is not necessary to say what the coins are)

Answer
❖ Let’s consider “A” array that consists of the coin values >= 1:-
10 2 5 1

❖ Let “C [P]” be an array that contains minimum number of coins of dominations


needed to make change for “P” cents.
❖ Let “S [P]” be an array that contains index of the optimal coin (In array “A”) needed
to make change for “P” cents.
❖ By using the following recursive formula we guarantee that we calculate the
minimum number of coins of dominations needed to make change for “P” cents:-

0 If p = 0

C [P] =

mini:di<=p{1 + C[p – di]} If p > 0

6
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022

❖ Use this algorithm to fill in the two arrays:-

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

❖ Testing with i.e. Change(A, 4, 8)


❖ Filling Arrays:-

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

QUESTION3: Knapsack Variation


(a) Write the recurrence relation and solve the 0-1 knapsack problem given that you have
four items having weights of 2,3,4,5 and values of 3,4,5,6 respectively. The total weight
capacity equals 5, what is the complexity of your algorithm? Is it polynomial in input size?

(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

Knap (W, N – 1) If w [N] > W Leave it


Knap (W, N) =

Max {Knap (W, N – 1), Knap (W – w [N], N – 1) + b [N]}

0-1 Knapsack Solution:-

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

So we take items 1 and 2, to have maximum total value of 7

Complexity:-

8
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022

O (n * W) →where “n” is the number of items

Polynomial in input size??  I suppose yes ^o) ??

(b) Recurrence Relation:-

If N = 0 No Items or space
0
Knap (W1, W2, N – 1) If w [N] > W1 & w [N] > W2 Leave it

Knap (W1, W2, N) =


Max {Knap (W1, W2, N – 1), Knap (W1
– w [N], W2, N – 1) + b [N], Knap (W1, W2 –
w [N], N – 1) + b [N]}
Complexity:-

O (n * W1 * W2) →where “n” is the number of items

QUESTION4: Exact Sum


You are given a sequence of positive integers a1; a2; :::; an, and a positive integer B. Your goal
is to determine if some subsequence of a1; a2; :::; an sums to exactly B.

Answer
D&C Pseudo Code

Check (A, N, B):-


If A [N] == B Then
Return true
End If
If N == 0 OR B == 0 Then
Return false
End If
If A [N] < B Then
B1 = check (A, N - 1, B – A [N])
B2 = check (A, N - 1, B)
Return (B1 OR B2)
Else
Return check (A, N - 1, B)
End If

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)

Side Note: Example on How Overlapping Can Occur

A = {1, 5, 10, 20, 30, 50, 100}, B = 80

We can reach to a common sub-problem: "exactlySum(3, 30)", which try to find


combination that sum to 30 using first 3 numbers, by two ways:

1. Use 50 and bypass 30 & 20


2. Bypass 50 and use 30 & 20
Both problems need to solve exactlySum(3, 30)

Dynamic Programming Solution:-

Extra storage: 2D Boolean array of size N × B,

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

SECOND: UNSOLVED PROBLEMS

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.

1. Design an efficient algorithm to solve this problem?


2. What’s the complexity of your algorithm?

QUESTION2: Super Thief


You are an amazing super thief. You have scoped out a circle of n, houses that you would like
to rob tonight. However, as the super thief that you are, you've done your homework and
realized that you want to avoid robbing two adjacent houses because this will minimize the
chance that you get caught. You've also scouted out the hood, so for every house i, you
know that the net worth you will gain from robbing that house is vi. Thus, You want to try to
maximize the amount of money that you can rob.

1. Explain why this problem is a good candidate for a DP algorithm?


2. Give a DP algorithm to find the maximum amount of money that you can steal given that
you never rob two houses that are adjacent? state whether your are using the bottom
up approach or the top down approach?
3. Modify your algorithm so that it also returns the best set of houses to rob instead of the
maximum value you can steal?

QUESTION3: Subset Sum


In computer science, the subset sum problem is an important problem in complexity
theory and cryptography. Given a set (or multiset) of integers, is there a non-empty subset
whose sum is zero?

For example, given the set {−7, −3, −2, 5, 8}, the answer is yes because the subset {−3, −2, 5}
sums to zero.

1. Write a recurrence relation?


2. Write a pseudocode of a DP solution for this problem?
3. What will be the complexity of the solution?

QUESTION4: Partitioning Problem


Given an array of positive integers, decide whether or not it can be partitioned into two
subsets S1 and S2 such that the sum of the numbers in S1 equals the sum of the numbers in S2

1. Write a recurrence relation?


2. Write a pseudocode of a DP solution for this problem?
3. What will be the complexity of the solution?

11
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022

QUESTION5: String Alignment


Two strings are aligned together if we can change one of them to the other. Moreover,
same character may be appeared several consecutive times in one or both strings with
different number of repetition, as shown below:

In this problem, it is allowed ONLY to change the 2nd string to be aligned with the 1st one.

Three possible changes are allowed:

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?

QUESTION6: Rabbit Crossing a River


The Rabbit is going to cross the river. There is a straight chain of tiny isles across the flow
and the animal should jump from one to another. At each of the isles there are some
candies. When the Rabbit arrives to the new isle, it collects all the candies here.
However, the Rabbit could not jump directly to the next isle in the chain - it just is too strong
to make short jumps. So, instead, it can jump over the one or two isles (i.e. from the 1st for
example to 3rd or 4th but not to 2nd or 5th and further). Also the Rabbit could not jump
back.

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

QUESTION7: Ali Baba in a Cave


Ali Baba and his thieves enter a cave that contains a set of n types of items from which they
are to select some number of items to be theft. Each item type has a weight, a profit and a
number of available instances. Their objective is to choose the set of items that fits the
possible load of their Camels and maximizes the profit. Note the following:
1- Each item should be taken as a whole (i.e. they can’t take part of it)
2- They can take the same item one or more time (according to its number of
instances).
Given n triples where each triple represents the (weight, profit, number of instances) of an
item type and the Camels possible load, find maximum profit that can be loaded on the
Camels by the OPTIMAL WAY.

Complexity
Your algorithm should take polynomial time

Example

Max Profit = 20$, as follows:


1. one instance from item2 (profit 8$)
2. two instances from item3 (profit 6$ × 2 = 12$)

THIRD: ONLINE PROBLEMS

1. Diving for Gold @ UVa

2. Vacations @codeforces

3. Dividing coins @ UVa

13
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022

FOURTH: Other Questions


QUESTION1: 0-1 Knapsack Tracing
In a 0-1 Knapsack problem, it’s required to fill a knapsack of weight W with the maximum
total cost from N items. Each item has only one instance with weight w[i] and cost c[i].
Following is an example:
Knapsack weight (W) = 5; Items (weight, cost): (1,2), (2,3), (3, 4), (4, 5).
Answer: maximum total cost is 7. This is achieved by taking 2nd and 3rd item.

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

QUESTION2: Rod-Cut Tracing


In a rod-cutting problem, it’s required to cut a steel rod into piece(s) to maximize the total
revenue. Thus, given a steel rod of length N and a price table containing the price of each
length; it’s required to find the optimal cuts that result in maximum total revenue. Following
is an example:
Rod length (N) = 4; Price table (length, price): (1,1), (2,5), (3, 8), (4, 9).
Answer: Optimal cut is two pieces of length two. This results total revenue of 5 + 5 = 10

Given the following price table:


Length 1 2 3 4 5 6 7 8 9 10
Price 3 5 8 10 16 17 19 24 29 30
Find the optimal solutions (i.e. max revenues) to cut a rod of each of the following lengths: 3,
4, 5, 6, 7, 8, 9, 10? (i.e. Fill up the following dynamic-programming solution table)
Length 0 1 2 3 4 5 6 7 8 9 10
Optimal Revenue 0 3 6

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

QUESTION3: Convert D&C to DP (bottom-up)


1. Convert the following D&C solution to a DP with building table (bottom-up)?
2. What’s the complexity of the DP solution?
FUN(I)
if I = N + 1 return 0
m = ∞
for J = I to N
m = Min(m, AUX(I,J) + FUN(J+1))
return m

main()
return FUN(1)

AUX(i,j): is an auxiliary function that calculate a specific value in O(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]

𝜽(𝑵𝟐 )

QUESTION4: Convert D&C to DP (top-down)


1. Convert the following D&C solution to a DP with memoization (top-down)?
2. What’s the complexity of the DP solution?

15
Faculty of Computer &
Information Sciences
Algorithms Design and Analysis
Ain Shams University DP Sheet
3rd Year 2nd Semester - 2022

//V is an input 1D array of size N (1-based)


ST(N)
if (N = 0)
return 0
if (N = 1)
return V[1]
return max(V[N] + ST(N-2), ST(N-1))

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]

return R[N] = max(V[N]+ST(N-2), ST(N-1))

𝜽(𝑵)

QUESTION5: Convert D&C to DP (bottom-up)


3. Convert the following D&C solution to a DP with building table (bottom-up)?
4. What’s the complexity of the DP solution?

16

You might also like