Group Assignment On Dynamic Problem

You might also like

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

Assignment 02

Group Assignment on Dynamic Problem (3)

Course Code: CSE245


Course Title: Algorithms
Section: 02
Semester: Summer2020

Submitted To:
Riddho Ridwanul Haque
Department of CSE
East West University

Submitted By:
Md. Tanvir Hassan Id: 2018-1-60-236
Md. Aminul Islam Id: 2018-1-68-051
Minhaz Ahmed Id: 2018-1-60-200

Date of Submission: 05.09.2020


Problem : 3

You have n packets of chocolates, which you would like to distribute among two of your
siblings. Each packet consists of a different number of chocolates, and you want to ensure
that both of your siblings get exactly the same number of chocolates in total (otherwise
they will start fighting and complain to your mom about it!). You do not want to open any
of the packets before giving it to them, since they will realize that the packet has been
opened before and start crying thinking that you have given them an old packet. For
example, suppose you have 6 packets containing 1, 2, 2, 7, 4 and 5 packets respectively.
Now, you could distribute the packets among them in the following ways:

a. Give the 2nd and 3rd packets to one sibling (2+2=4 chocolates in total) and
the 5th packet to the other (containing 4 chocolates). The rest of the packets will be
kept by you.

b. Give the first three packets to one sibling (1+2+2=5 chocolates in total) and the 6th
packet to the other (containing 5 chocolates).

c. Give the 1st and 5th packets to one sibling (1+4=5 chocolates in total) and the 6th
packet to the other (containing 5 chocolates).

d. Give the 1st, 2nd, 3rd and 5th packet to one sibling (1+2+2+4=7 chocolates in total)
and the 4th packet to the other (containing 7 chocolates).

You want to ensure that your siblings get as many chocolates as possible.

Design an algorithm that takes the number of chocolates in n packets respectively and
determines the maximum number of chocolates that can be equally distributed among your siblings
while keeping each packet intact.
Summery of the Question:
I have n packets of chocolates and I have to distributed that equally among my two siblings while
each packet intact . If its number adds up odd I can keep the remaining after giving up as maximum
as possible.
Solution:
In a dynamic programming table let’s consider two siblings as the columns and chocolate that can
be kept as the rows.

The state DP[i][j] will denote maximum value of ‘j- chocolate’ considering all values from ‘1 to
ith’. So if we consider ‘Choco’ (chocolate in ‘ith’ row) we can fill it in all columns which have
‘chocolate values > Choco’ . Now two possibilities can take place:

 Fill ‘Choco’ in the given column.


 Do not fill ‘Choco’ in the given column.
Now we have to take a maximum of these two possibilities, formally if we do not fill ‘ith’ chocolate
in ‘jth’ column then DP[i][j] state will be same as DP[i-1][j] but if we fill the chocolate, DP[i][j]
will be equal to the value of ‘Choco’+ value of the column ‘j-Choco’ in the previous row. So we
take the maximum of these two possibilities to fill the current state.

Compare between 1st and 2nd column until it’s 0. And take the best possible way to divide.

Proof of correctness:

We will do proof by induction here: We say pair (i, j) < (i 0 , j0 ) if i < i0 or (i = i 0 and j < j0 ).

For example (0, 0) < (0, 1) < (0, 2) < ... < (1, 0) < (1, 1) < ...

Induction Hypothesis: algorithm is correct for all values of a[i, j] where (i, j) < (i 0 , j0 ). Or in
other words, all previous elements in table are correct. Base Case: a[i, 0] = a[0, j] = 0 for all i, j
Induction Step: When computing a[i 0 , j0 ], by induction hypothesis, we have a[i 0 − 1, j0 ], a[i
0 − 1, j0 − wi 0] are already computed correctly.

Then algorithm considers the optimal value for item i 0 in knapsack as a[i 0 −1, j0 −wi 0]+vi 0
and for item i 0 not in knapsack as a[i 0 −1, j0 ]. Therefore, 2 the value at a[i 0 , j0 ] is correct.
Efficiency of the solution :

Time complexity: O(N*C).


The use of 2D array data structure for storing intermediate states

Description of the solution : We use Dynamic Solutions of 0-1 knapsack to solve this
problem. Where by comparing both columns we can find the best way to divide the chocolate

equality .

We also Memoization Technique

This method is basically an extension to the recursive approach so that we can overcome the
problem of calculating redundant cases and thus increased complexity.

You might also like