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 “Subsetsum”, 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.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 UNGOR
 Computer Science, Algorithms, Graph Theory, Adjacency list, negative cycle, nonsubscripted

Click to edit the document details