-
Notifications
You must be signed in to change notification settings - Fork 2
feat(algorithms, backtracking): find subsets #175
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
+175
−75
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...scading_subsets/test_cascading_subsets.py → ...scading_subsets/test_cascading_subsets.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
algorithms/backtracking/subsets/find_all_subsets/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| from typing import List | ||
|
|
||
|
|
||
| def find_all_subsets(nums: List[int]) -> List[List[int]]: | ||
| n = len(nums) | ||
|
|
||
| if n == 0: | ||
| return [] | ||
|
|
||
| subsets = [] | ||
|
|
||
| def backtrack(first, curr): | ||
| # Add the current subset to the output | ||
| subsets.append(curr[:]) | ||
| # Generate subsets starting from the current index | ||
| for i in range(first, n): | ||
| curr.append(nums[i]) | ||
| backtrack(i + 1, curr) | ||
| curr.pop() | ||
|
|
||
| backtrack(0, []) | ||
| return subsets | ||
|
|
||
|
|
||
| def find_all_subsets_with_duplicates(nums: List[int]) -> List[List[int]]: | ||
| n = len(nums) | ||
|
|
||
| if n == 0: | ||
| return [] | ||
|
|
||
| # Sort in place to ensure that duplicate elements are next to each other, which will be used to skip over the duplicate | ||
| # elements in the iteration. | ||
| # Time complexity here is O(n log(n)) and space complexity is O(n) due to Timsort requiring space to handle sorting | ||
| # in place | ||
| nums.sort() | ||
|
|
||
| subsets: List[int] = [] | ||
| result: List[List[int]] = [] | ||
|
|
||
| def backtrack(idx: int) -> None: | ||
| """ | ||
| Backtrack to create subsets from the given current index of the element being considered | ||
| Args: | ||
| idx(int): index of the element being considered. | ||
| """ | ||
| # Base case, if we reach the end of the list, we include the current subset to the result | ||
| if idx == n: | ||
| result.append(subsets[:]) | ||
| return | ||
|
|
||
| # Add the current subset to the output | ||
| # Include the current element to the subset and move to the next element | ||
| subsets.append(nums[idx]) | ||
| backtrack(idx + 1) | ||
| # After recursion, remove it to backtrack and explore the excluded path | ||
| subsets.pop() | ||
|
|
||
| # Skip the current element(exclude path), but skip duplicates too. If there are duplicate elements, skip them all | ||
| # when choosing exclude path. This ensures that we only take one unique subset for each duplicate group | ||
| while idx + 1 < n and nums[idx] == nums[idx + 1]: | ||
| idx += 1 | ||
| backtrack(idx + 1) | ||
|
|
||
| backtrack(0) | ||
| return result | ||
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added
BIN
+23.9 KB
...king/subsets/find_all_subsets/images/examples/find_all_subsets_ii_example_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+21.9 KB
...king/subsets/find_all_subsets/images/examples/find_all_subsets_ii_example_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+24.8 KB
...king/subsets/find_all_subsets/images/examples/find_all_subsets_ii_example_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions
87
algorithms/backtracking/subsets/find_all_subsets/test_find_all_subsets.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import unittest | ||
| from typing import List | ||
| from parameterized import parameterized | ||
| from algorithms.backtracking.subsets.find_all_subsets import ( | ||
| find_all_subsets, | ||
| find_all_subsets_with_duplicates, | ||
| ) | ||
|
|
||
| FIND_ALL_SUBSETS_TEST_CASES = [ | ||
| ([], []), | ||
| ([9], [[], [9]]), | ||
| ([1], [[], [1]]), | ||
| ([0], [[], [0]]), | ||
| ([2, 4], [[], [2], [4], [2, 4]]), | ||
| ([1, 2], [[], [1], [2], [1, 2]]), | ||
| ([2, 5, 7], [[], [2], [5], [2, 5], [7], [2, 7], [5, 7], [2, 5, 7]]), | ||
| ( | ||
| [1, 2, 3, 4], | ||
| [ | ||
| [], | ||
| [1], | ||
| [2], | ||
| [1, 2], | ||
| [3], | ||
| [1, 3], | ||
| [2, 3], | ||
| [1, 2, 3], | ||
| [4], | ||
| [1, 4], | ||
| [2, 4], | ||
| [1, 2, 4], | ||
| [3, 4], | ||
| [1, 3, 4], | ||
| [2, 3, 4], | ||
| [1, 2, 3, 4], | ||
| ], | ||
| ), | ||
| ([3, 6, 9], [[], [3], [3, 6], [3, 6, 9], [3, 9], [6], [6, 9], [9]]), | ||
| ([1, 2, 3], [[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]), | ||
| ] | ||
|
|
||
| FIND_ALL_SUBSETS_WITH_DUPLICATES_TEST_CASES = [ | ||
| ([0], [[], [0]]), | ||
| ([-1, 1], [[], [-1], [-1, 1], [1]]), | ||
| ([1, 2, 2], [[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]), | ||
| ([0, 0], [[], [0], [0, 0]]), | ||
| ([-2, -2, 1], [[], [-2], [-2, -2], [-2, -2, 1], [-2, 1], [1]]), | ||
| ([1, 1], [[], [1], [1, 1]]), | ||
| ( | ||
| [4, 4, 4, 1, 4], | ||
| [ | ||
| [], | ||
| [1], | ||
| [1, 4], | ||
| [1, 4, 4], | ||
| [1, 4, 4, 4], | ||
| [1, 4, 4, 4, 4], | ||
| [4], | ||
| [4, 4], | ||
| [4, 4, 4], | ||
| [4, 4, 4, 4], | ||
| ], | ||
| ), | ||
| ] | ||
| FIND_ALL_SUBSETS_WITH_DUPLICATES_TEST_CASES.extend(FIND_ALL_SUBSETS_TEST_CASES) | ||
|
|
||
|
|
||
| class FindAllSubsetsTestCase(unittest.TestCase): | ||
| @parameterized.expand(FIND_ALL_SUBSETS_TEST_CASES) | ||
| def test_find_all_subsets(self, nums: List[int], expected: List[List[int]]): | ||
| actual = find_all_subsets(nums) | ||
| expected_sorted = sorted(expected) | ||
| actual_sorted = sorted(actual) | ||
| self.assertEqual(expected_sorted, actual_sorted) | ||
|
|
||
| @parameterized.expand(FIND_ALL_SUBSETS_WITH_DUPLICATES_TEST_CASES) | ||
| def test_find_all_subsets_with_duplicates( | ||
| self, nums: List[int], expected: List[List[int]] | ||
| ): | ||
| actual = find_all_subsets_with_duplicates(nums) | ||
| expected_sorted = sorted(expected) | ||
| actual_sorted = sorted(actual) | ||
| self.assertEqual(expected_sorted, actual_sorted) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
algorithms/subsets/find_all_subsets/test_find_all_subsets.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty input returns
[]instead of[[]].Mathematically, the power set of an empty set is
{∅}, i.e.,[[]]. Returning[](no subsets at all) deviates from the standard definition. The same applies tofind_all_subsets_with_duplicatesat lines 28-29. If this is intentional, consider adding a comment explaining the design choice.🤖 Prompt for AI Agents