
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
This is C++ Program to Generate All Subsets of a Given Set in the Lexico Graphic Order. This algorithm prints all the possible combination of each length from the given set of array in increasing order. The time complexity of this algorithm is O(n*(2^n)).
Algorithm
Begin For each length ‘i’ GenAllSubset() function is called: 1) In GenAllSubset(), if currLen is more than the reqLen then return. 2) Otherwise, if currLen is equal to reqLen then there will be a new sequence generated, print it. 3) If proceed with a start as ‘true’ and recursively call GenAllSubset() with incremented value of ‘currLen’ and ‘s’. else proceed with a start as ‘false’ and recursively call GenAllSubset() with incremented value of ‘s’. End
Example
#include<iostream> using namespace std; void Sorting(int a[], int n) //array sorting { int i, j, t; for(i = 0; i < n; i++) { for(j = i+1; j < n; j++) { if(a[i] > a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } } } } void GenAllSubset(int a[], int reqLen, int s, int currLen, bool check[], int len) { if(currLen > reqLen) return; else if (currLen == reqLen) { cout<<"\t"; cout<<"{ "; for (int i = 0; i < len; i++) { if (check[i] == true) { cout<<a[i]<<" "; } } cout<<"}\n"; return; } if (s == len) { return; } check[s] = true; GenAllSubset(a, reqLen, s + 1, currLen + 1, check, len); check[s] = false; GenAllSubset(a, reqLen, s + 1, currLen, check, len); } int main() { int i, n; bool ch[n]; cout<<"Enter the number of element array have: "; cin>>n; int arr[n]; cout<<"\n"; for (i = 0; i < n; i++) { cout<<"Enter "<<i+1<<" element: "; cin>>arr[i]; ch[i] = false; } Sorting(arr, n); cout<<"\nThe all subset of the given set in the lexicographic order: \n"; cout<<"\t{ }\n"; for(i = 1; i <= n; i++) { GenAllSubset(arr, i, 0, 0, ch, n); } return 0; }
Output
Enter the number of element array have: 6 Enter 1 element:3 Enter 2 element: 2 Enter 3 element: 1 Enter 4 element:7 Enter 5 element:6 Enter 6 element: 5 The all subset of the given set in the lexicographic order: { } { 1 } { 2 } { 3 } { 5 } { 6 } { 7 } { 1 2 } { 1 3 } { 1 5 } { 1 6 } { 1 7 } { 2 3 } { 2 5 } { 2 6 } { 2 7 } { 3 5 } { 3 6 } { 3 7 } { 5 6 } { 5 7 } { 6 7 } { 1 2 3 } { 1 2 5 } { 1 2 6 } { 1 2 7 } { 1 3 5 } { 1 3 6 } { 1 3 7 } { 1 5 6 } { 1 5 7 } { 1 6 7 } { 2 3 5 } { 2 3 6 } { 2 3 7 } { 2 5 6 } { 2 5 7 } { 2 6 7 } { 3 5 6 } { 3 5 7 } { 3 6 7 } { 5 6 7 } { 1 2 3 5 } { 1 2 3 6 } { 1 2 3 7 } { 1 2 5 6 } { 1 2 5 7 } { 1 2 6 7 } { 1 3 5 6 } { 1 3 5 7 } { 1 3 6 7 } { 1 5 6 7 } { 2 3 5 6 } { 2 3 5 7 } { 2 3 6 7 } { 2 5 6 7 } { 3 5 6 7 } { 1 2 3 5 6 } { 1 2 3 5 7 } { 1 2 3 6 7 } { 1 2 5 6 7 } { 1 3 5 6 7 } { 2 3 5 6 7 } { 1 2 3 5 6 7 }
- Related Articles
- C++ Program to Generate All Pairs of Subsets Whose Union Make the Set
- Find all distinct subsets of a given set in C++
- Python program to get all subsets of given size of a set
- C++ Program to Implement the Binary Counting Method to Generate Subsets of a Set
- Print all subsets of given size of a set in C++
- Python program to get all subsets of a given size of a set
- C++ Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
- How to find all subsets of a set in JavaScript?
- List all the subsets of a set {m , n}
- C++ Program to Generate All Possible Combinations of a Given List of Numbers
- Java Program To Find all the Subsets of a String
- Python Program To Find all the Subsets of a String
- Count number of subsets of a set with GCD equal to a given number in C++
- C++ Program to Generate Random Partition out of a Given Set of Numbers or Characters
- Golang program to find all subsets of a string

Advertisements