Java
Java

 

 

Program Code


import java.util.ArrayList;
import java.util.List;

public class PowerSetGenerator {
    
    /**
     * Generates the power set of a given set of integers.
     * 
     * @param set The input set represented as an array of integers.
     * @return A list of lists representing the power set.
     */
    public static List<List> generatePowerSet(int[] set) {
        List<List> powerSet = new ArrayList<>();
        int setSize = set.length;
        int powerSetSize = 1 << setSize; // 2^setSize

        for (int i = 0; i < powerSetSize; i++) {
            List subset = new ArrayList<>();
            for (int j = 0; j < setSize; j++) {
                // Check if jth element is included in the subset
                if ((i & (1 << j)) > 0) {
                    subset.add(set[j]);
                }
            }
            powerSet.add(subset);
        }
        return powerSet;
    }

    public static void main(String[] args) {
        int[] inputSet = {1, 2, 3}; // Example input set
        List<List> result = generatePowerSet(inputSet);
        
        System.out.println("Power Set:");
        for (List subset : result) {
            System.out.println(subset);
        }
    }
}
    

Program Explanation

The program consists of a class named PowerSetGenerator that contains two main methods: generatePowerSet and main.

Method: generatePowerSet

  • Parameters: It accepts an array of integers set as input.
  • Return Value: It returns a list of lists, where each inner list represents a subset of the power set.
  • Logic:
    • The size of the power set is calculated as 2^n (where n is the size of the input set).
    • A loop iterates over all possible combinations (from 0 to powerSetSize - 1).
    • For each combination, a nested loop checks which elements are included based on the bits of the integer.
    • Included elements are added to a subset, which is then added to the power set.

Method: main

  • This is the entry point of the program.
  • An example input set is defined, and the generatePowerSet method is called.
  • The resulting power set is printed to the console.

Usage

To use this program, you can compile and run it in any Java environment. Modify the inputSet in the main method to test with different sets.

Conclusion

This Java program effectively generates the power set of a given set of integers, showcasing the use of bit manipulation to explore all possible subsets.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)