Skip to content

Commit 818550f

Browse files
Merge pull request #173 from abhisheks008/main
Subset Sum Problem using Dynamic Programming
2 parents 2cad27b + 60cdcf6 commit 818550f

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed
11.8 KB
Loading
11.8 KB
Loading
11.5 KB
Loading
11.6 KB
Loading
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Subset Sum Problem using Dynamic Programming
2+
Language used : **Python 3**
3+
4+
## 🎯 Aim
5+
The aim of this script is to find out if there is a subset of the given set with sum equal to given sum.
6+
7+
## 👉 Purpose
8+
The main purpose of this script is to show the implementation of Dynamic Programming to find out if there is a subset of the given set with sum equal to given sum.
9+
10+
## 📄 Description
11+
Given a set of non-negative integers, and a value sum, determine if there is a subset of the given set with sum equal to given sum.
12+
```
13+
Example:
14+
15+
Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 9
16+
Output: True
17+
There is a subset (4, 5) with sum 9.
18+
19+
Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 30
20+
Output: False
21+
There is no subset that add up to 30.
22+
```
23+
24+
## 📈 Workflow of the script
25+
- `isSubsetSum` - Returns true if there is a subset of set[] with sum equal to given sum.
26+
- `main` - This is the driver code for this python script.
27+
28+
## 📃 Explanation
29+
We will create a 2D array of `size (arr.size() + 1) * (target + 1)` of type boolean. The state `DP[i][j]` will be true if there exists a subset of elements from `A[0….i]` with sum value = `‘j’`. The approach for the problem is:
30+
```
31+
if (A[i-1] > j)
32+
DP[i][j] = DP[i-1][j]
33+
else
34+
DP[i][j] = DP[i-1][j] OR DP[i-1][j-A[i-1]]
35+
```
36+
1. This means that if current element has value greater than ‘current sum value’ we will copy the answer for previous cases
37+
2. And if the current sum value is greater than the `‘ith’` element we will see if any of previous states have already experienced the `sum=’j’` OR any previous states experienced a value `‘j – A[i]’` which will solve our purpose.
38+
39+
## 🧮 Algorithm
40+
The below simulation will clarify the above approach:
41+
```
42+
set[]={3, 4, 5, 2}
43+
target=6
44+
45+
0 1 2 3 4 5 6
46+
47+
0 T F F F F F F
48+
49+
3 T F F T F F F
50+
51+
4 T F F T T F F
52+
53+
5 T F F T T T F
54+
55+
2 T F T T T T T
56+
```
57+
58+
## 💻 Input and Output
59+
- **Test Case 1 :**
60+
61+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Subset%20Sum%20Problem/Images/subset1.png)
62+
63+
- **Test Case 2 :**
64+
65+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Subset%20Sum%20Problem/Images/subset2.png)
66+
67+
- **Test Case 3 :**
68+
69+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Subset%20Sum%20Problem/Images/subset3.png)
70+
71+
- **Test Case 3 :**
72+
73+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Dynamic%20Programming/Subset%20Sum%20Problem/Images/subset4.png)
74+
75+
76+
## ⏰ Time and Space complexity
77+
- **Time Complexity:** `O(sum*n)`, where sum is the ‘target sum’ and ‘n’ is the size of array.
78+
- **Space Complexity:** `O(sum*n)`, as the size of 2-D array is `sum*n`.
79+
80+
---------------------------------------------------------------
81+
## 🖋️ Author
82+
**Code contributed by, _Abhishek Sharma_, 2021 [@abhisheks008](github.com/abhisheks008)**
83+
84+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Subset Sum Problem using Dynamic Programming
2+
# Directory : Dynamic Programming
3+
# Language used : Python 3
4+
# Abhishek S, 2021
5+
6+
# ------------------------------------------------------------------
7+
8+
# Problem Statement : A Dynamic Programming solution for subset
9+
# sum problem Returns true if there is a subset of
10+
# set[] with sun equal to given sum
11+
12+
# ------------------------------------------------------------------
13+
14+
# Solution : Creating the Python script to find out the required output.
15+
16+
17+
18+
# Returns true if there is a subset of set[]
19+
# with sum equal to given sum
20+
def isSubsetSum(set, n, sum):
21+
22+
# The value of subset[i][j] will be
23+
# true if there is a
24+
# subset of set[0..j-1] with sum equal to i
25+
subset =([[False for i in range(sum + 1)]
26+
for i in range(n + 1)])
27+
28+
# If sum is 0, then answer is true
29+
for i in range(n + 1):
30+
subset[i][0] = True
31+
32+
# If sum is not 0 and set is empty,
33+
# then answer is false
34+
for i in range(1, sum + 1):
35+
subset[0][i]= False
36+
37+
# Fill the subset table in bottom up manner
38+
for i in range(1, n + 1):
39+
for j in range(1, sum + 1):
40+
if j<set[i-1]:
41+
subset[i][j] = subset[i-1][j]
42+
if j>= set[i-1]:
43+
subset[i][j] = (subset[i-1][j] or
44+
subset[i - 1][j-set[i-1]])
45+
46+
# uncomment this code to print table
47+
# for i in range(n + 1):
48+
# for j in range(sum + 1):
49+
# print (subset[i][j], end =" ")
50+
# print()
51+
return subset[n][sum]
52+
53+
# Driver code
54+
if __name__=='__main__':
55+
print (" - Subset Sum Problem using Dynamic Programming - ")
56+
print ("--------------------------------------------------")
57+
print ()
58+
set = list(map(int, input("Enter the set of numbers : ").split(" ")))
59+
print ()
60+
sum = int(input("Enter the subset sum : "))
61+
print ()
62+
print ("--------------------------------------------------")
63+
print ()
64+
print ("Calculating the result...")
65+
print ()
66+
print ("The Output is : ")
67+
n = len(set)
68+
if (isSubsetSum(set, n, sum) == True):
69+
print("Found a subset with given sum")
70+
else:
71+
print("No subset with given sum")
72+
73+
74+
# ------------------------------------------------------------------
75+
# Input Cases :
76+
# Enter the set of numbers : 1 5 3 7 4
77+
# Enter the subset sum : 12
78+
79+
# ------------------------------------------------------------------
80+
# Output :
81+
# Calculating the result...
82+
83+
# The Output is :
84+
# Found a subset with given sum
85+
86+
# ------------------------------------------------------------------
87+
88+
# Code contributed by, Abhishek Sharma, 2021

0 commit comments

Comments
 (0)