|
| 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