1
Subset Sum
Given n positive integers
x
1
,
x
2
,…
x
n
and a number
B
Decide whether there is a subset
S
of them such that
For example, if
x
={1,5,7} and
B
=8, then
S
={1,3}
Brute force approach ?
How about greedy approach ?
NPhard problem
We will still try to solve, at least for cases where “B is small”.
191
,{
1
,
,
}
i
iS
x
BS
n
Lecture 16, Nov 18 2010
Subset Sum – continued
We will use Dynamic Programming
How to choose subproblem ?
Subproblem:
»
M(k,b)=
“
true” iff there is a solution to
»
Result in
M(n,B)
»
M(1,b)=
“
true” iff
x
1
=b
, “false” otherwise.
192
'
,'
{
1
,
,
}
i
xS
bk
Goal:Find
{1,
, } .
.
i
Sn
s
t
x
B
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2
Subset Sum – continued
Main recurrence:
»
M(k+1,b)=M(k,b)
or
M(k,bx
k+1
).
»
Explanation: Either use
x
k+1
or do not use it
»
Use “false” for nonexisting elements of
M()
.
»
Running time: Matrix M is [n x B], each element constant time:
O(nB).
This is the end of the preview.
Sign up
to
access the rest of the document.
 '09
 Algorithms, Dynamic Programming, Computational complexity theory, telephone call, $90, Knapsack problem, $110

Click to edit the document details