Skip to content

Commit 60cdcf6

Browse files
authored
Create README.md
1 parent 21bee13 commit 60cdcf6

File tree

1 file changed

+84
-0
lines changed
  • Dynamic Programming/Subset Sum Problem

1 file changed

+84
-0
lines changed
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/)

0 commit comments

Comments
 (0)