a program in java to generate the powerset of a given set

Ads
 

a program in java to generate the powerset of a given set

Hello :)

am a fresher to java
i am in need of a java code which generates the powerset of a given set
i.e
suppose the set S be {a,b,c}
the power set would be
{}
{a}
{b}
{c}
{a,b}
{a,c}
{b,c}
{a,b,c}
and also the size of the set may vary

kindly do the needfull

thanks and regards
Shilpa.R
View Answers

March 5, 2010 at 11:31 AM

Hi Friend,

Try the following code:

import java.util.*;
public class PowerSet{
public static void main(String[] args) {
String st[] = {"a", "b", "c"};
LinkedHashSet hashSet = new LinkedHashSet();
int len = st.length;
int elements = (int) Math.pow(2,len);
for (int i = 0; i < elements; i++) {
String str = Integer.toBinaryString(i);
int value = str.length();
String pset = str;
for (int k = value; k < len; k++) {
pset = "0" + pset;
}
LinkedHashSet set = new LinkedHashSet();
for (int j = 0; j < pset.length(); j++) {
if (pset.charAt(j) == '1')
set.add(st[j]);
}
hashSet.add(set);
}
System.out.println(hashSet.toString());
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
a program in java to generate the powerset of a given set - Java Beginners
a program in java to generate the powerset of a given set  Hello :) am a fresher to java i am in need of a java code which generates the powerset of a given set i.e suppose the set S be {a,b,c} the power set would
Java program to generate the total count
Java program to generate the total count  I need a java program which would take a text file as an input and scans through it to generate the output. For Ex. : If the text file contains the following information: There is/are 3
Advertisements
program on factorial of a given number in java
program on factorial of a given number in java  please tell me, how to write program on factorial of a given number in java
program on factorial of a given number in java
program on factorial of a given number in java  please tell me, how to write program on factorial of a given number in java
Please provide the java code for the given program.
Please provide the java code for the given program.   We need an application for managing an educational institute. That application should provide...: StudentAddr: StudentDob: Email: Course ALL Java C C++ Oracle Fees: Duration: Faculty
Write a java program to display the season given the month using switch case
Write a java program to display the season given the month using switch case  Write a java program to display the season given the month using switch case
Write a program that converts any given year to Roman Numeral in Java
Write a program that converts any given year to Roman Numeral in Java  Write a program that converts any given year to Roman Numeral
program to create student report using java applet,read the input using text boxesand generate the grades..?
program to create student report using java applet,read the input using text boxesand generate the grades..?   sir plz give me java applet codes for above question
ModuleNotFoundError: No module named 'Powerset'
ModuleNotFoundError: No module named 'Powerset'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'Powerset' How to remove the ModuleNotFoundError: No module named 'Powerset
Various Commands that are used in java are given below
Various Commands that are used in java are given below   1.javac c:\ javac classname.java A Java Compiler javac is a computer program or set of programs which translate java source code into java byte code
please help me in coding this given program
please help me in coding this given program  consider the below... are the prime numbers in ascending order write a program to find the nth term in this series. (i m unable to code this program..please provide solution
please help me in coding this given program
please help me in coding this given program  consider the below... are the prime numbers in ascending order write a program to find the nth term in this series. (i m unable to code this program..please provide solution
How to store unique values in Java using Set?
Store unique values in Java using Set interface In this section we will discuss about the Set interface in Java which is designed to store unique values. We will show you how to store unique values in Java using Set? The Set interface
How to store unique values in Java using Set?
Store unique values in Java using Set interface In this section we will discuss about the Set interface in Java which is designed to store unique values. We will show you how to store unique values in Java using Set? The Set interface
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  Write a java program to do matrix addition operation On two given matrices
java program
java program  hi friends how to make a java program for getting non prime odd numbers in a given series
Java code for set...!!!
Java code for set...!!!  Create 2 classes in same package Product.java productId, name, price ProductImpl.java create a set in this and try... not allow the duplicate products to be added into the set(equal productId). Hint
Prime number program in java
Prime number program in java In this example you will learn how to write a program to generate and check  prime number  in java. As we know prime...,stc; String c; do { System.out.println("Press 1 for generate prime
java program for
java program for   java program for printing documents,images and cards
Generate random numbers in Java
Generate random numbers in Java - How to use the java.util.Random class to generate desired random number in Java program? In this tutorial I will teach you how to write Java code using the java.util.Random class to generate single
How to run a Java program in CMD
a notepad or any other editor and write a small program in Java as given below... command at your command prompt. set path=%path%;C:\Program Files\Java\jdk1.5.0_09... discussed how to write and run Java program in CMD. Tips to run Java program
Java: Some problems in given condition
Java: Some problems in given condition   SIR, I want to get the values from the table( database), if any one of the column of the table value is 0. Then one alert should b printed in application using Java. If(att.getdata1()==0
java program
java program  . Develop a program that computes the distance a boat travels across a river, given the width of the river, the boat's speed perpendicular to the river, and the river's speed. Speed is distance/time
Java Program
Java Program  A Simple program of JSP to Display message, with steps to execute that program   Hi Friend, Follow these steps: 1)Go to the Control Panel>>System>>Environment Variable And the set
a Java program
a Java program    Write a Java program to print even numbers from 2 to 1024? Write a Java program to print ? My Name is Mirza? 100 times? Write a Java program to print Fibonacci Series? Write a Java program to reverse a number
java program
java program  how to write an addition program in java without using arithematic operator
java program
java program  write java program for constructor,overriding,overriding,exception handling
Java Program
Java Program  java program to insert row in excel sheet after identifying an object
java program
java program  java program to implement the reflection of a particular class details like constructor,methods and fields with its modifiers

Ads