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

This is a java program to generate and print all the pair of subsets whose union makes the original set.

Here is the source code of the Java Program to Generate All Pairs of Subsets Whose Union Make the Set. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a java program to generate all pair of subsets whose union results the original set
import java.util.Random;
import java.util.Scanner;
 
public class Pair_Subset_Union_Set 
{
    public static int[] binary(int N) 
    {
        int[] binary = new int[(int) Math.pow(2, N)];
        for (int i = 0; i < Math.pow(2, N); i++) 
        {
            int b = 1;
            binary[i] = 0;
            int num = i;
            while (num > 0) 
            {
                binary[i] += (num % 2) * b;
                num /= 2;
                b = b * 10;
            }
        }
        return binary;
    }
 
    public static void main(String args[]) 
    {
        Random random = new Random();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of elements in the set: ");
        int N = sc.nextInt();
        int[] sequence = new int[N];
        for (int i = 0; i < N; i++)
            sequence[i] = Math.abs(random.nextInt(100));
 
        System.out.println("The elements in the set : ");
        for (int i = 0; i < N; i++)
            System.out.print(sequence[i] + " ");
 
        int[] mask = new int[(int) Math.pow(2, N)];
        mask = binary(N);
 
        System.out
                .println("\nThe pair of permutations whose union is original set are: ");
        for (int i = 0; i < (Math.pow(2, N) / 2); i++) 
        {
            System.out.print("{ ");
            for (int j = 0; j < N; j++) 
            {
                if (mask[i] % 10 == 1)
                    System.out.print(sequence[j] + " ");
                mask[i] /= 10;
            }
            System.out.print("} and ");
 
            System.out.print("{ ");
            for (int j = 0; j < N; j++) 
            {
                if (mask[(int) Math.pow(2, N) - 1 - i] % 10 == 1)
                    System.out.print(sequence[j] + " ");
                mask[(int) Math.pow(2, N) - 1 - i] /= 10;
            }
            System.out.println("}");
        }
        sc.close();
    }
}

Output:

$ javac Pair_Subset_Union_Set.java
$ java Pair_Subset_Union_Set
 
Enter the number of elements in the set: 
5
The elements in the set : 
3 47 97 79 8 
The pair of permutations whose union is original set are: 
{ } and { 3 47 97 79 8 }
{ 3 } and { 47 97 79 8 }
{ 47 } and { 3 97 79 8 }
{ 3 47 } and { 97 79 8 }
{ 97 } and { 3 47 79 8 }
{ 3 97 } and { 47 79 8 }
{ 47 97 } and { 3 79 8 }
{ 3 47 97 } and { 79 8 }
{ 79 } and { 3 47 97 8 }
{ 3 79 } and { 47 97 8 }
{ 47 79 } and { 3 97 8 }
{ 3 47 79 } and { 97 8 }
{ 97 79 } and { 3 47 8 }
{ 3 97 79 } and { 47 8 }
{ 47 97 79 } and { 3 8 }
{ 3 47 97 79 } and { 8 }
 
Enter the number of elements in the set: 
3
The elements in the set : 
37 76 87 
The pair of permutations whose union is original set are: 
{ } and { 37 76 87 }
{ 37 } and { 76 87 }
{ 76 } and { 37 87 }
{ 37 76 } and { 87 }

Related posts:

Date Time trong Java 8
Java Switch Statement
Creating a Custom Starter with Spring Boot
Tìm hiểu về Web Service
Java Program to Implement Fermat Factorization Algorithm
Error Handling for REST with Spring
Xây dựng ứng dụng Client-Server với Socket trong Java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Interface trong Java 8 – Default method và Static method
Java InputStream to Byte Array and ByteBuffer
A Guide to Queries in Spring Data MongoDB
REST Web service: Basic Authentication trong Jersey 2.x
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Spring Boot - Scheduling
Java Program to Implement Heap
Tránh lỗi NullPointerException trong Java như thế nào?
The “final” Keyword in Java
Java Program to Describe the Representation of Graph using Adjacency List
Java Program to Implement Max Heap
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Deque và ArrayDeque trong Java
Limiting Query Results with JPA and Spring Data JPA
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Adding Shutdown Hooks for JVM Applications
Java Program to Perform Quick Sort on Large Number of Elements
Life Cycle of a Thread in Java
Java Program to Create the Prufer Code for a Tree
Overview of the java.util.concurrent
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Check Cycle in a Graph using Graph traversal
Array to String Conversions

1 Trackback / Pingback

  1. Java generate all pairs

Comments are closed.