C++ Program to Generate All Pairs of Subsets Whose Union Make the Set

This is a C++ program to generate all pairs of subsets, whose union make the Set.

Algorithms

Begin
   function UnionSet():
   Arguments:
      a[] = an array.
      n = number of elements.
      Body of the function:
      1) Generate binary code from 0 to 2^(n-1)-1 for all 2^(n-1) pairs.
      2) Print the array element which has 0 or 1 in corresponding indexes in code string for each code.
      3) Print them in a different set, which on the union of both sets gives the super set.
End

Example

#include
#include
#include
using namespace std;
void display(char code[], int a[], int n) //display the pairs
{
   int i;
   cout>n;
   int a[n];
   cout>a[i];
   }
   cout

Output

Enter the number of elements: 4
Enter 1 element: 4
Enter 2 element: 3
Enter 3 element: 2
Enter 4 element: 1
The possible subset pairs which on union generates the superset, are:
{ } { 4 3 2 1 }
{ 1 } { 4 3 2 }
{ 2 } { 4 3 1 }
{ 2 1 } { 4 3 }
{ 3 } { 4 2 1 }
{ 3 1 } { 4 2 }
{ 3 2 } { 4 1 }
{ 3 2 1 } { 4 }
Updated on: 2019-07-30T22:30:26+05:30

323 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements