{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Exam3OnCampusSol - Answer 1(Manna Partitioning the n input...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Answer 1: (Manna) Partitioning the n input numbers into two equal halves is same as finding if the input n numbers have a subset summing S/2 (the sum can be calculated in linear time). So, partitioning is a particular case of the general problem of “Subset-sum”, where one needs to find out if there exists a subset with a sum of some K . First, if not sorted already, sort the input n number. Put them in A [ n ]. Now, create a Boolean array B of n +1 columns and K+ 1 rows. If B [ i , j ] = Y, that means there exists a subset summing i using numbers only from the first j integers among the n available ones (it does not mean that you will have use all j of them – it just means that integers A [ j +1] … A [ n ] are not required for summing i ). So, you can see that if B [ i , j ] = Y, so will B [ i , j +1] … B [ i , n ]. Our initial table would look like something like this (the initializations are trivial) A ° 0 1 2 …. N 0 Y Y Y …. Y 1 N 2 N 3 N 4 N 5 N … … … K -2 N K -1 N K N Now, you fill out the columns one by one (not row by row). So, try to visualize that we are adding the input numbers one by one and watching what new sums we can achieve with this new integer. For each row entry B[ i , j ] in that new column (say column j ), do the following: If ( ( B [ i , j -1] = Y) or ( B [ i - A [ j ], j -1] = Y) ), then B [ i , j ] = Y else N. The first condition has already been explained above. It exploits the fact that if B [ x , j ] = Y for any number x , that means there exists a subset (say S 1 , S 2 , …, S m for some m j ) who add up to x. So, the value of the entries must be Y too for all B [ x S 1 ], B [ x S 2 ] etc.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}