Reference Youtube video:Link
This Java program generates all possible subsets (the power set) of a given array using backtracking. Let's break it down step by step.
Let given array be [1, 2, 3].
Understanding the Code
The program consists of two main functions:
-
subsets(int[] nums):- Initializes the result list (
list) to store all subsets. - Sorts the input array (though sorting is unnecessary in this case).
- Calls the helper function
backtrack()to generate subsets.
- Initializes the result list (
-
backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int start):- Adds the current subset (
tempList) to the result list (list). - Iterates through the numbers in
nums, adding one element at a time totempList, and recursively explores further subsets. - After recursion, removes the last element to backtrack and explore other possibilities.
- Adds the current subset (
Step-by-Step Execution
Let's assume the input is:
nums = [1, 2, 3]
Step 1: Start Execution
-
subsets(nums)is called withnums = [1, 2, 3] -
listis initialized as an empty list:list = [] -
backtrack()is called with:
backtrack(list, tempList = [], nums = [1, 2, 3], start = 0)
Step 2: First Call to backtrack
-
tempList = []is added tolist list = [[]]-
For Loop Iteration 1 (
i = 0):- Add
1totempList → tempList = [1] - Call
backtrack(list, tempList = [1], nums, start = 1)
- Add
Step 3: Second Call to backtrack
-
tempList = [1]is added tolist list = [[], [1]]-
For Loop Iteration 1 (
i = 1):- Add
2totempList → tempList = [1, 2] - Call
backtrack(list, tempList = [1, 2], nums, start = 2)
- Add
Step 4: Third Call to backtrack
-
tempList = [1, 2]is added tolist list = [[], [1], [1, 2]]-
For Loop Iteration 1 (
i = 2):- Add
3totempList → tempList = [1, 2, 3] - Call
backtrack(list, tempList = [1, 2, 3], nums, start = 3)
- Add
Step 5: Fourth Call to backtrack
-
tempList = [1, 2, 3]is added tolist list = [[], [1], [1, 2], [1, 2, 3]]-
start = 3(out of bounds), so the function returns - Backtrack: Remove
3→tempList = [1, 2]
Step 6: Backtrack to Third Call
- Continue loop in Third Call (
i = 2finished) - Backtrack: Remove
2→tempList = [1] -
For Loop Iteration 2 (
i = 2):- Add
3totempList → tempList = [1, 3] - Call
backtrack(list, tempList = [1, 3], nums, start = 3)
- Add
Step 7: Fifth Call to backtrack
-
tempList = [1, 3]is added tolist list = [[], [1], [1, 2], [1, 2, 3], [1, 3]]- Backtrack: Remove
3→tempList = [1] - Backtrack: Remove
1→tempList = []
Step 8: Backtrack to Second Call
-
For Loop Iteration 2 (
i = 1):- Add
2totempList → tempList = [2] - Call
backtrack(list, tempList = [2], nums, start = 2)
- Add
Step 9: Sixth Call to backtrack
-
tempList = [2]is added tolist list = [[], [1], [1, 2], [1, 2, 3], [1, 3], [2]]-
For Loop Iteration 1 (
i = 2):- Add
3totempList → tempList = [2, 3] - Call
backtrack(list, tempList = [2, 3], nums, start = 3)
- Add
Step 10: Seventh Call to backtrack
-
tempList = [2, 3]is added tolist list = [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3]]- Backtrack: Remove
3→tempList = [2] - Backtrack: Remove
2→tempList = []
Step 11: Backtrack to First Call
-
For Loop Iteration 3 (
i = 2):- Add
3totempList → tempList = [3] - Call
backtrack(list, tempList = [3], nums, start = 3)
- Add
Step 12: Eighth Call to backtrack
-
tempList = [3]is added tolist list = [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]- Backtrack: Remove
3→tempList = [] -
All iterations complete. Return
list.
Final Output
[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
Key Takeaways
-
Backtracking Approach:
- We explore subsets recursively, adding and removing elements (backtracking).
- The
tempListis used to build subsets, and each state is stored inlist.
-
Time Complexity:
-
O(2^n) → Since each element can either be included or not, we generate
2^nsubsets.
-
O(2^n) → Since each element can either be included or not, we generate
-
Space Complexity:
-
O(2^n * n) → Each subset requires space, and there are
2^nsubsets, each taking up toO(n)space.
-
O(2^n * n) → Each subset requires space, and there are
-
Recursive Tree Visualization:
- The recursive function creates a tree where each level corresponds to choosing or skipping an element.
This step-by-step breakdown explains how subsets are generated recursively using backtracking. 🚀
Top comments (0)