Problem Statement

The Subset Sum Problem is a classic algorithmic problem in computer science. It asks whether a subset of a given set of integers can sum up to a specified target value. This problem is a well-known NP-complete problem.

Approach Explanation

This problem can be solved using dynamic programming. The idea is to build a table that keeps track of which sums can be achieved with subsets of the input set.

Dynamic Programming Structure:

  1. Define a 2D DP table where dp[i][j] represents whether a sum of j can be achieved with the first i items of the input set.
  2. Initialize the first column of the table to True, as a sum of zero can always be achieved with an empty subset.
  3. Iterate through the table, filling it based on whether to include the current item in the subset or not.
  4. The final answer will be found in dp[n][target], where n is the number of items and target is the desired sum.

Time Complexity:

O(n * target), where n is the number of items and target is the target sum.

Space Complexity:

O(n * target) for the DP table.

Python Code


def is_subset_sum(nums, target):
    """
    Function to determine if a subset with a given sum exists.

    Args:
    nums (List[int]): List of integers representing the set.
    target (int): The target sum to check for a subset.

    Returns:
    bool: True if a subset with the given sum exists, False otherwise.
    """
    n = len(nums)
    
    # Create a DP table initialized to False
    dp = [[False] * (target + 1) for _ in range(n + 1)]
    
    # Initialize the first column (sum 0 is always achievable)
    for i in range(n + 1):
        dp[i][0] = True

    # Fill the DP table
    for i in range(1, n + 1):
        for j in range(1, target + 1):
            if nums[i - 1] <= j:
                dp[i][j] = dp[i - 1][j] or dp[i - 1][j - nums[i - 1]]
            else:
                dp[i][j] = dp[i - 1][j]

    return dp[n][target]


# Example usage
numbers = [3, 34, 4, 12, 5, 2]
target_sum = 9
exists = is_subset_sum(numbers, target_sum)

print("Subset with the given sum exists:", exists)
    

Explanation of the Program

Let’s break down the structure of the program:

1. Input:

The input consists of a list of integers and a target sum:

    numbers = [3, 34, 4, 12, 5, 2]
    target_sum = 9

2. DP Table Initialization:

A DP table is created with dimensions (n + 1) x (target + 1), initialized to False. This table is used to store whether a sum can be achieved with subsets of the input set.

3. Base Case Setup:

The first column of the DP table is initialized to True, indicating that a sum of zero is always achievable (using the empty subset).

4. Filling the DP Table:

The program iterates through each number and possible sums. If the current number can be included in the sum, the program updates the DP table to reflect whether the sum can be achieved by either including or excluding the number.

5. Final Result:

The existence of a subset that sums up to the target is indicated in the cell dp[n][target].

Example Execution:

For the provided input, the output will indicate whether a subset with the given sum exists:

Subset with the given sum exists: True

This indicates that there exists a subset of the numbers that adds up to the target sum of 9.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)