subset-sum


Subset sum with dynamic programming


Following code checks if there is sum
public static bool isSubsetSumDP(int[] s, int n, int sum)
{
bool[, ] dp = new bool[n + 1, sum + 1];
for (int i = 0; i <= n; i++)
{
dp[i, 0] = true;
}
for (int i = 1; i <= sum; i++)
{
dp[0, i] = false;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= sum; j++)
{
dp[i, j] = dp[i - 1,j];
if (j >= s[i - 1])
{
dp[i,j] = dp[i,j] || dp[i - 1,j - s[i - 1]];
}
}
}
return dp[n,sum];
}
For example, I have input set[] = {3, 34, 4, 12, 5, 2} and sum = 9
Output: True
I need to modify this code to not only show if there is a subset but also print them (using dynamic programming approach), then output would be: true, Subset(4, 5)

Related Links

Subset sum with dynamic programming
Reducing Subset Sum Problm
Clarification on subset sum
Subset whose sum is the smallest sum over a specific threshold

Categories

HOME
yii2
client
gitlab
cloud
pypi
c#-4.0
youtube-dl
framework7
rsync
yahoo-oauth
jgroups
v8
android-youtube-api
amazon-cloudformation
vifm
fancybox-3
reveal.js
postgres-xl
worldwind
systemc
pugjs
propel
textfield
intentfilter
extjs5
mustache.php
microsoft-r
icloud-api
blazemeter
qhull
brunch
maquette
delicious-api
pim
lxd
rotational-matrices
amazon-kinesis-kpl
hexo
wpf-controls
dartium
bitbucket-pipelines
x11-forwarding
code-search-engine
theano.scan
sqlite2
ensembles
google-closure
http-redirect
flashair
slick-3.0
qwt
smartcontracts
strptime
domain-model
pebble-watch
dropbox-php
windows-iot-core-10
pintos
medium.com
firmata
log4c
home-directory
azure-virtual-network
phishing
fody
pcf
apachebench
rtbkit
t4mvc
python-3.2
libressl
varargs
sframe
android-listview
event-bubbling
sailfish-os
c3
dd
camanjs
terminfo
navigationservice
comexception
uv-mapping
page-layout
dir
dexterity
spring-io
coverflow
quartz-graphics
qt-faststart
rabl
frameset
bubble-chart
buildr
dotnethighcharts
amazon-appstore
horizontal-accordion
rfc1123
javax.script
table-footer
zend-translate
modelstate
data-driven
yagni
nerddinner
w3wp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App