Backtracking to find all subsets
Given a set of positive integers, find all its subsets.
Examples:
Input : 1 2 3
Output : // this space denotes null element.
1
1 2
1 2 3
1 3
2
2 3
3
Input : 1 2
Output :
1
2
1 2
We have already discussed iterative approach to find all subsets. This article aims to provide a backtracking approach.
Idea is that if we have n number of elements inside an array, we have exactly two choices for each of the elements. Either we include that element in our subset or we do not include it.
C++
// CPP program to find all subsets by backtracking. #include <bits/stdc++.h> using namespace std; // In the array A at every step we have two // choices for each element either we can // ignore the element or we can include the // element in our subset void subsetsUtil(vector<int>& A, vector<vector<int> >& res, vector<int>& subset, int index) { res.push_back(subset); for (int i = index; i < A.size(); i++) { // include the A[i] in subset. subset.push_back(A[i]); // move onto the next element. subsetsUtil(A, res, subset, i + 1); // exclude the A[i] from subset and triggers // backtracking. subset.pop_back(); } return; } // below function returns the subsets of vector A. vector<vector<int> > subsets(vector<int>& A) { vector<int> subset; vector<vector<int> > res; // keeps track of current element in vector A; int index = 0; subsetsUtil(A, res, subset, index); return res; } // Driver Code. int main() { // find the subsets of below vector. vector<int> array = { 1, 2, 3 }; // res will store all subsets. // O(2 ^ (number of elements inside array)) // because at every step we have two choices // either include or ignore. vector<vector<int> > res = subsets(array); // Print result for (int i = 0; i < res.size(); i++) { for (int j = 0; j < res[i].size(); j++) cout << res[i][j] << " "; cout << endl; } return 0; } |
Python3
# Python3 program to find all subsets
# by backtracking.
# In the array A at every step we have two
# choices for each element either we can
# ignore the element or we can include the
# element in our subset
def subsetsUtil(A, subset, index):
print(*subset)
for i in range(index, len(A)):
# include the A[i] in subset.
subset.append(A[i])
# move onto the next element.
subsetsUtil(A,subset, i + 1)
# exclude the A[i] from subset and
# triggers backtracking.
subset.pop(-1)
return
# below function returns the subsets of vector A.
def subsets(A):
global res
subset = []
# keeps track of current element in vector A
index = 0
subsetsUtil(A, subset, index)
# Driver Code
# find the subsets of below vector.
array = [1, 2, 3]
# res will store all subsets.
# O(2 ^ (number of elements inside array))
# because at every step we have two choices
# either include or ignore.
subsets(array)
# This code is contributed by SHUBHAMSINGH8410
Output:
1 1 2 1 2 3 1 3 2 2 3 3
Time Complexity : O(2 ^ n)
Recommended Posts:
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Find all distinct subsets of a given set
- Sudoku | Backtracking-7
- Rat in a Maze | Backtracking-2
- Backtracking | Introduction
- Subset Sum | Backtracking-4
- Magnet Puzzle | Backtracking-9
- Rat in a Maze | Backtracking using Stack
- m Coloring Problem | Backtracking-5
- Hamiltonian Cycle | Backtracking-6
- N Queen Problem | Backtracking-3
- Solving Cryptarithmetic Puzzles | Backtracking-8
- The Knight's tour problem | Backtracking-1
- Word Break Problem using Backtracking
- Top 20 Backtracking Algorithm Interview Questions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



