Optimal weights subset sum using backtracking
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Subset Sum Problem is a classical decision problem in computer science and mathematics. The problem is defined as follows: given a set of integers and a target sum, determine whether there is a subset of the provided set that adds up to the target sum. A quintessential variant of this problem that arises in optimization scenarios is the Optimal Weights Subset Sum, where the goal is to find the subset whose sum is closest to, but not exceeding, a given target.
One effective technique for tackling this problem is backtracking, a methodical way of trying out different sequences, narrowing down possibilities, and pruning branches that do not lead to a solution.
The Backtracking Approach
Backtracking systematically searches for a solution through trial and error. It involves the following steps:
- Start with an empty set: Begin the process with no elements included in the subset.
- Add elements one-by-one: Try to build up the subset by adding elements one-by-one from the given set of integers, checking at each step if the subset still satisfies the problem requirements.
- Check for solution: Each time an element is added, check if the current subset sum is equal to, less than, or greater than the target sum.
- Backtrack upon failure: If the subset sum exceeds the target, remove the last added element and try the next possibility.
- Record the best solution: If a valid subset is found, record its sum if it's better than previously recorded solutions.
Algorithm Implementation
Here's a recursive pseudocode representation for solving the Optimal Weights Subset Sum using backtracking:
• Start with an empty subset and try adding integers from the set. • Upon trying all possibilities, the optimal subset may look like which sums up to 22. • Further attempts will update this optimal subset as summing to 30, which might be the best fit. • Prune if the current subset sum exceeds the target. • Order elements in the set to explore larger figures first, as they potentially offer faster pruning by exceeding the target quickly.
Related reading
- Optimisation of recursive algorithm in Java
- Optimising accuracy for OneClassSVM
- Optimising the drawing of overlapping rectangles
- Optimization from partial solution minimize sum of distances between pairs
- Optimising caret for sensitivity still seems to optimise for ROC
- Optimization Techniques for C
- Optimized argmin an effective way to find an item minimizing a function
- Optimized low-accuracy approximation to rootnx, n

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.