0% found this document useful (0 votes)
4 views

Dynamicprogramming

The document discusses two approaches, brute force and dynamic programming, to solve the subset sum problem. The brute force approach generates all possible subsets but has exponential time complexity, while the dynamic programming approach solves the problem optimally in polynomial time by storing solutions to subproblems, trading off additional space for improved time complexity.

Uploaded by

Aliza Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Dynamicprogramming

The document discusses two approaches, brute force and dynamic programming, to solve the subset sum problem. The brute force approach generates all possible subsets but has exponential time complexity, while the dynamic programming approach solves the problem optimally in polynomial time by storing solutions to subproblems, trading off additional space for improved time complexity.

Uploaded by

Aliza Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Subset Sum Problem: Brute Force and Dynamic

Programming Approaches
Muhammad Tayyab, Faizan Rasool, Muhammad Shafqat,
Muhammad Ehtisham, Kiran Waheed, Aimen Nawaz

1 Problem Statement
Given a set of positive integers and a target sum, determine if there is a subset
of the given set with a sum equal to the target sum.

2 Constraints
• Each number in the set can be used only once.
• The subset sum should be exactly equal to the target sum.

3 Input
• An integer n representing the number of elements in the set.
• An integer target_sum representing the target sum.

• A list of integers representing the set.

4 Output
• A boolean indicating whether a subset with the target sum exists.

• Optionally, the subset itself if it exists.

5 Brute Force Approach


5.1 Solution Process
1. Generate Subsets: Generate all possible subsets of the given set. This
involves considering whether each element is included or excluded from
the subset, resulting in a power set.

1
2. Check Sum: For each subset generated, calculate the sum of its elements.
3. Compare Sum: Compare the sum of each subset with the target sum.
4. Return Result: If any subset has a sum equal to the target sum, return
True; otherwise, return False.

5.2 Analysis
• Time Complexity: Generating all subsets requires examining 2n possi-
ble combinations, where n is the number of elements in the set. Calcu-
lating the sum for each subset takes O(n) time. Thus, the overall time
complexity is O(2n · n).
• Space Complexity: The brute force approach does not require addi-
tional space proportional to the input size beyond the input set itself, so
the space complexity is O(n).

6 Dynamic Programming Approach


6.1 Solution Process
1. Define DP Table: Create a 2D DP table where DP[i][j] represents
whether it is possible to obtain the sum j using elements up to the ith
element of the set.
2. Initialization: Initialize the first row of the DP table considering an
empty set, where only DP[0][0] is True.

3. State Transition: For each element of the set, iterate through the target
sum and update the DP table based on the inclusion or exclusion of the
current element.
4. Final Result: The value of DP[n][target_sum] indicates whether the
target sum can be achieved using all elements of the set.

6.2 State Transition


DP[i][j] = DP[i − 1][j] (if we do not include the ith element)
DP[i][j] = DP[i − 1][j] ∨ DP[i − 1][j − set[i − 1]] (if we include the ith element)

6.3 Analysis
• Time Complexity: Constructing the DP table involves iterating through
all elements of the set (O(n)) and all possible sums up to the target sum
(O(target_sum)). Thus, the time complexity is O(n · target_sum).

Tayyab
• Space Complexity: The DP table requires space proportional to the
product of the number of elements in the set and the target sum, leading
to a space complexity of O(n · target_sum).

7 Conclusion
• Brute Force vs. Dynamic Programming: While the brute force
approach exhaustively explores all possible subsets, resulting in exponen-
tial time complexity, the dynamic programming approach optimally solves
the problem by leveraging solutions to subproblems, leading to polynomial
time complexity.

• Efficiency: Dynamic programming trades space for time, making it more


efficient in scenarios where the brute force approach becomes impractical
due to its exponential time complexity. However, it requires additional
space for storing solutions to subproblems.

• Trade-offs: The choice between brute force and dynamic programming


depends on the problem size and available resources. For smaller prob-
lem instances, brute force may suffice, but for larger instances, dynamic
programming offers a more scalable solution.

Tayyab

You might also like