• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
August 16, 2024 |220 Views

Dynamic Programming - Subset Sum Problem

  Share   Like
Description
Discussion

Subset Sum Problem | DP-25

The Subset Sum Problem is a fundamental problem in computer science that is often used to introduce dynamic programming concepts. It is a well-known problem that appears in various fields, including cryptography, finance, and resource allocation.

Problem Statement

Given a set of non-negative integers and an integer sum, determine if there is a subset of the given set with a sum equal to the given sum.

Example

Consider the set arr = {3, 34, 4, 12, 5, 2} and the sum 9. The subset {4, 5} sums up to 9, so the output should be True.

Approaches to Solve the Problem

The problem can be solved using various approaches, but the most optimal approach uses dynamic programming (DP).

1. Recursive Approach (Exponential Time Complexity):

  • Check for each element whether to include it in the subset or not.
  • Recursively explore all subsets and check if any of them sum up to the desired value.

This approach has a time complexity of O(2n)O(2^n)O(2n), making it inefficient for large input sizes.

2. Dynamic Programming Approach (Optimal):

  • The idea is to build a 2D DP table dp[][] where dp[i][j] indicates whether a sum j can be achieved using the first i elements of the set.

Dynamic Programming Approach

Define the DP Table:

  • Create a 2D DP table dp[n+1][sum+1], where n is the size of the set.
  • dp[i][j] will be True if a sum j can be achieved using the first i elements.

Initialization:

  • If the sum is 0, then the answer is always True (by selecting an empty subset), so dp[i][0] = True for all i.
  • If the set is empty (i.e., i = 0) and the sum is not 0, then the answer is False, so dp[0][j] = False for all j > 0.

Filling the DP Table:

  • For each element in the set, iterate through the possible sums from 1 to sum.
  • For each sum, check:
    • If the sum can be achieved without the current element (dp[i-1][j]).
    • If the sum can be achieved by including the current element (dp[i-1][j - arr[i-1]]).

Final Result:

  • The value at dp[n][sum] will indicate whether it is possible to achieve the target sum with the given set.

Example Walkthrough

For arr = {3, 34, 4, 12, 5, 2} and sum = 9:

Initialization:

  • dp[0][0] = True
  • dp[0][j] = False for all j > 0.

Filling the Table:

  • After processing all elements, you will determine that dp[6][9] = True, indicating that a subset exists with the sum 9.

Time Complexity

  • The time complexity of this approach is O(n×sum)O(n \times \text{sum})O(n×sum), which is significantly more efficient than the recursive approach.

Space Complexity

  • The space complexity is also O(n×sum)O(n \times \text{sum})O(n×sum).

Applications

  • Knapsack Problem: The subset sum problem is a special case of the 0/1 knapsack problem.
  • Partition Problem: The subset sum problem can be used to determine if a given set can be partitioned into two subsets with equal sums.
  • Cryptography: The problem is related to certain cryptographic algorithms that rely on the difficulty of subset sum problems.

Conclusion

The Subset Sum Problem is a classic dynamic programming problem that introduces key concepts like state definition and recurrence relations. Understanding this problem is essential for tackling more complex problems in dynamic programming and combinatorial optimization.

For a detailed step-by-step guide, check out the full article: https://www.geeksforgeeks.org/subset-sum-problem-dp-25/.