0% found this document useful (0 votes)
43 views

Subset Sum Problem Using A Dynamic Programming

This document discusses solving the subset sum problem using a dynamic programming approach. It has a time complexity of O(N*sum) which is faster than exponential approaches. The problem is to find if a subset of numbers from a given set sums to a target value. The document then explains the dynamic programming solution which uses a 2D boolean array to store solutions in a bottom-up manner.

Uploaded by

Han
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Subset Sum Problem Using A Dynamic Programming

This document discusses solving the subset sum problem using a dynamic programming approach. It has a time complexity of O(N*sum) which is faster than exponential approaches. The problem is to find if a subset of numbers from a given set sums to a target value. The document then explains the dynamic programming solution which uses a 2D boolean array to store solutions in a bottom-up manner.

Uploaded by

Han
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Subset Sum problem using a dynamic programming approach will take O(N

* sum) time complexity which is significantly faster than the other


approaches which take exponential time.

Subset sum problem is that given a subset A of n positive integers and a


value sum is given, find whether or not there exists any subset of the given
set, the sum of whose elements is equal to the given value of sum.
Example:

Given the following set of positive numbers:

{ 2, 9, 10, 1, 99, 3}

We need to find if there is a subset for a given sum say 4:

{ 1, 3 }

For 7, there is no subset where the sum of elements equal to 7.

This problem can be solved using following algorithms:

1. Recursive method
2. Backtracking
3. Dynamic Programing
Here, we will solve this using Dynamic Programming.

Dynamic Programming
We create a boolean subset[][] and fill it in bottom up manner.

subset[i][j] denotes if there is a subset of sum j with element at index i-1 as


the last element

subset[i][j] = true if there is a subset with:


* the i-th element as the last element
* sum equal to j
Base cases include:

j=0 then subset[i][0] will be true as the sum for empty set is 0

If i=0, then subset[0][j] will be false, as with no elements, we can get no


sum.

subset[i][0] = true as sum of {} = 0

subset[0][j] = false as with no elements we can get no sum

If element at index i (E1) is greater than j, then subset[i][j] = false as we


cannot get a subset of positive numbers with E1 as a member.

If we include element at index i (E1), we get

subset[i][j] = subset[i-1][j-E1];

where E1 = array[i-1]

As if element E1 is included, then we need to find a subset with the first i-1
elements such that the sum is j - E1.

At this point, we should have the value of subset[i-1][j-E1] and hence, we


compute it instantly.

Dynamic Programming computes [i][j], for each 1 <= i <= n and 1 <= j <=
sum, which is true if subset with sum j can be found using items up to first i
items.

It uses value of smaller values i and j already computed. It has the same
asymptotic run-time as Memoization but no recursion overhead.

Steps:
1.We create a boolean subset[][] and fill it in bottom up manner.
2.The value of subset[i][j] will be true if there is a subset of set[0..j-1] with
sum equal to i., otherwise false.
3.Finally, we return subset[n][sum]

Complexity
Dynamic Programming
 Worst case time complexity: Θ(n*sum)
 Space complexity: Θ(sum)

You might also like