Skip to content

Create GenerateSubsets #1581

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions Backtracking/GenerateSubsets
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function generateSubsets(nums) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Call the parameter arr, not nums. The implementation doesn't need to make any assumptions about the values in arr.
  2. This should have a doc comment stating what this does (builds and returns a list of all subsets in lexicographic order). Also, consider using a generator rather than building a list; sometimes you want to iterate over all subsets without storing them all temporarily in memory (for example when solving hard optimization problems).

const subsets = [];

function backtrack(start = 0, currentSubset = []) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I would call this backtracking; to me it's just ordinary recursion:

Backtracking is a class of algorithms for finding solutions to some computational problems, notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and abandons a candidate as soon as it determines that the candidate cannot possibly be completed to a valid solution.

// Add the currentSubset to the subsets array
subsets.push([...currentSubset]);

// Explore all possible subsets by backtracking
for (let i = start; i < nums.length; i++) {
currentSubset.push(nums[i]);
backtrack(i + 1, currentSubset);
currentSubset.pop();
}
}

backtrack();
return subsets;
}

const inputSet = [1, 2, 3];
Copy link
Collaborator

@appgurueu appgurueu Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example should be removed from this file (or converted into a JSDoc example).

const allSubsets = generateSubsets(inputSet);

console.log("All Subsets:");
for (const subset of allSubsets) {
console.log(subset);
}
24 changes: 24 additions & 0 deletions Backtracking/tests/GenerateSubsets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// we define a generateSubsets function that uses backtracking to generate all possible subsets of a given set (array of numbers). The backtrack function is a recursive function that explores all possible subsets by considering or excluding each element.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's in the other file. Remove this comment from the test file.


import { generateSubsets } from '../GenerateSubsets'

describe('generateSubsets', () => {
it('should generate all subsets for an array', () => {
const inputSet = [1, 2, 3];
const expectedSubsets = [
[],
[1],
[1, 2],
[1, 2, 3],
[1, 3],
[2],
[2, 3],
[3]
];

const subsets = generateSubsets(inputSet);

// Check if the generated subsets are deep equal to the expected subsets
expect(subsets).to.deep.have.members(expectedSubsets);
});
});