0
0
DSA Cprogramming~10 mins

Generate All Subsets Powerset in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Generate All Subsets Powerset
Start with empty subset
Pick next element
Two choices: Include or Exclude
Include element
Add subset to result if at end
Backtrack to previous step
Back to Pick next element
Start with an empty subset, then for each element, choose to include or exclude it, recursively building all subsets.
Execution Sample
DSA C
void generateSubsets(int arr[], int n, int index, int subset[], int subsetSize) {
    if (index == n) {
        printSubset(subset, subsetSize);
        return;
    }
    generateSubsets(arr, n, index + 1, subset, subsetSize);
    subset[subsetSize] = arr[index];
    generateSubsets(arr, n, index + 1, subset, subsetSize + 1);
}
This code recursively generates all subsets by excluding or including each element.
Execution Table
StepOperationCurrent IndexSubset Being BuiltAction TakenVisual State of Subsets
1Start0[]Begin recursion[]
2Exclude element 11[]Exclude arr[0]=1[]
3Exclude element 22[]Exclude arr[1]=2[]
4Exclude element 33[]Exclude arr[2]=3[]
5Add subset3[]Reached end, add [][[]]
6Include element 33[3]Include arr[2]=3[[], [3]]
7Backtrack to index 22[]Backtrack[[], [3]]
8Include element 22[2]Include arr[1]=2[[], [3]]
9Exclude element 33[2]Exclude arr[2]=3[[], [3], [2]]
10Add subset3[2]Reached end, add [2][[], [3], [2]]
11Include element 33[2,3]Include arr[2]=3[[], [3], [2], [2,3]]
12Backtrack to index 11[]Backtrack[[], [3], [2], [2,3]]
13Include element 11[1]Include arr[0]=1[[], [3], [2], [2,3]]
14Exclude element 22[1]Exclude arr[1]=2[[], [3], [2], [2,3]]
15Exclude element 33[1]Exclude arr[2]=3[[], [3], [2], [2,3], [1]]
16Add subset3[1]Reached end, add [1][[], [3], [2], [2,3], [1]]
17Include element 33[1,3]Include arr[2]=3[[], [3], [2], [2,3], [1], [1,3]]
18Backtrack to index 22[1]Backtrack[[], [3], [2], [2,3], [1], [1,3]]
19Include element 22[1,2]Include arr[1]=2[[], [3], [2], [2,3], [1], [1,3]]
20Exclude element 33[1,2]Exclude arr[2]=3[[], [3], [2], [2,3], [1], [1,3], [1,2]]
21Add subset3[1,2]Reached end, add [1,2][[], [3], [2], [2,3], [1], [1,3], [1,2]]
22Include element 33[1,2,3]Include arr[2]=3[[], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3]]
23End3[]All subsets generated[[], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3]]
💡 Index reaches array length (3), recursion ends and all subsets are generated.
Variable Tracker
VariableStartAfter Step 5After Step 11After Step 17After Step 22Final
index033333
subset[][][2,3][1,3][1,2,3][]
subsetSize002230
subsets_collected[][[]][[], [3], [2], [2,3]][[], [3], [2], [2,3], [1], [1,3]][[], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3]][[], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3]]
Key Moments - 3 Insights
Why do we call the recursive function twice at each step?
Because at each index, we have two choices: exclude or include the current element. The execution_table rows 2 and 13 show these two calls for index 0.
How do we know when to add the current subset to the result?
When the index reaches the length of the array (step 5, 10, 16, 21), we add the current subset because we have considered all elements.
Why does the subset array change after including an element but not after excluding?
Excluding does not add the element, so subset stays the same (see steps 2, 3, 4). Including adds the element at subsetSize position (steps 6, 11).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 11. What is the subset being built?
A[]
B[3]
C[2,3]
D[1,3]
💡 Hint
Check the 'Subset Being Built' column at step 11 in the execution_table.
At which step does the recursion first add the empty subset to the result?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look for the first 'Add subset' action with an empty subset in the execution_table.
If we change the input array to have 2 elements instead of 3, how many subsets will be generated?
A4
B3
C2
D8
💡 Hint
Recall that the number of subsets is 2^n, where n is the number of elements.
Concept Snapshot
Generate All Subsets (Powerset):
- Use recursion with index to track position
- At each step, choose to include or exclude current element
- When index == n, add current subset to result
- Total subsets = 2^n
- Backtracking builds all combinations
Full Transcript
This concept shows how to generate all subsets of an array using recursion. We start with an empty subset and at each element, we decide to include or exclude it. This creates two recursive calls per element. When we reach the end of the array, we add the current subset to the result. The execution table tracks each step, showing the current index, subset being built, and the subsets collected so far. The variable tracker shows how index and subset change over time. Key moments clarify why two recursive calls happen and when subsets are added. The visual quiz tests understanding of subset states and counts. The snapshot summarizes the approach and key rules.