Skip to content

Added backtracking sum of subset #702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions Backtracking/SumOfSubset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
*
* Sum of Subset problem
*
* Given an ordered set W of non-negative integers and a value K,
* determine all possible subsets from the given set W whose sum
* of its elemets equals to the given value K.
*
* More info: https://www.geeksforgeeks.org/subset-sum-backtracking-4/
*/

/*
* @param {number[]} set Original set of numbers
* @param {number[]} subset Subset being evaluated
* @param {number} setIndex Index from set of last element in subset
* @param {number} Sum of elements from subset
* @param {targetSum} The target sum on which the subset sum is compared to
* @returns {number[][]} Subsets whose elements add up to targetSum
*/
const subsetSum = (set, subset, setindex, sum, targetSum) => {
// Base case where the subset sum is equal to target sum
// Evaluation of following subsets on this path will always add up to
// greater than targetSum, so no need to continue
if (sum === targetSum) return [subset];

// This and following subsets on this path will always add up to
// greater than targetSum, so no need to continue
if (sum > targetSum) return [];

// Initialize results array. Will contain only valid subsets
let results = [];

// Slice gets from the set all the elements at the right of the last element
// to be evaluated (last element of subset)
// forEach iterated on the resulting array
set.slice(setindex).forEach((num, index) => {
// The next subset to be evaluated, current subset plus next element
const nextSubset = [...subset, num];

// Next index from the set. Current set index plus iteration index
// index starts at 0, so a + 1 is required
const nextSetIndex = setindex + index + 1;

// Sum of elements from the next subset to be evaluated
const nextSum = sum + num;

// Call recursively the subsetSum for the nextSubset
const subsetResult = subsetSum(
set,
nextSubset,
nextSetIndex,
nextSum,
targetSum
);

// Concat the recursive result with current result arary
results = [...results, ...subsetResult];
});

//Return results
return results;
};

/*
* Example with ordered array
* W = [2, 5, 7, 8, 12, 16, 23, 40]
* K = 25
*
* resulting subsets = [ 2, 7, 16 ], [ 2, 23 ], [ 5, 8, 12 ]
*/

const nums = [2, 5, 7, 8, 12, 16, 23, 40];

const subsets = subsetSum(nums, [], 0, 0, 25);

console.log(subsets);