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

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









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 generate the powerset of a given set
Java generate the powerset of a given set In this section, you will learn how to generate the powerset of a given set of values. As you have already know...') set.add(st[j]); }  and at last we get the powerset of the given set
Advertisements
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
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
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
write a java program for inserting a substring in to the given main string from a given position
write a java program for inserting a substring in to the given main string from a given position  write a java program for inserting a substring in to the given main string from a given position
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
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
Various Commands that are used in java are given below
is a computer program or set of programs which translate java source code into java... Various Commands that are used in java are given below...:\ appletviewer classname.html Applet viewer is a command line program to run Java
i need a program that will generate daily base log file dynamically - Log4J
i need a program that will generate daily base log file dynamically  i need a program that will generate daily base log file dynamically For example: if suppose today date is 2 sep 2009, when i execute the program it ll
to generate barcharts - Java Beginners
) If you want to generate bar chart using JFreeChart in java, try the following...to generate barcharts  Please kindly suggest: to generate a bar chart which should take data from backend(JDBC),in which Y-axis should represent
Set Different Color to different row in Java Program
Java Set Different Color to different row In this section, you will learn how to set different color to table row using java swing.For this purpose, we have.... This class provides the method setBackground() which allow us to set different
Write a program to calculate factorial of any given number
program to calculate factorial of any given number. First of all define a class... in the range of double data type in java. Here is the code of program:ADS... Factorial Examples - Java Factorial Example to calculate factorial of any
java code for given query
java code for given query  i am not able to display to display graph in all browsers it is only displaying in ie using java
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
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
Program to check whether two given words pronouncing same or not?
Program to check whether two given words pronouncing same or not?  Program to check whether two given words pronouncing same or not? example: raju and rajoo pronouncing same please help me out
Day for the given Date in Java
Day for the given Date in Java  How can i get the day for the user input data ?   Hello Friend, You can use the following code:ADS_TO_REPLACE_1 import java.util.*; import java.text.*; class CheckDay{ public static
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
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
how to generate code - Java Beginners
how to generate code   hi friends, this is poornima i want to generate code about online shopping by using servlets,jsp,html,jdbc and eclipse (IDE) could you please tell me about what are steps included. Thanks in advance
How to Generate Reports in Java - Java Beginners
How to Generate Reports in Java  How to Display and Generate Reports in Java? Give Me Full Sample Code  Hi Friend, What do you want to display on reports.Please elaborate it. Thanks
Java set example
Java set example In this section you will learn about set interface in java. In java set is a collection that cannot contain duplicate element. The set... collection. Example of java set interface. import java.util.Iterator; import
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
Add Content of an iText PDF File using java code
to generate pdf file dynamically. The iText library can be used to generate PDF documents from Java program. The iText is a open source library that provide... Add Content of an iText PDF File using java code  
Result=Set - Java Beginners
result set, first move the pointer from first record to last record and get
How to run a Java program in CMD
a notepad or any other editor and write a small program in Java as given below... the program. Run the given command. C:\myproject> java HelloWorldADS_TO_REPLACE_2... discussed how to write and run Java program in CMD. Tips to run Java program
generate java code and html code - Java Beginners
generate java code and html code  create a web application for museum....a)the first page wil be a login page for administrators.. passwords for all acounts should be stored in the database and checked on login... on success ogin
How to generate random number in java
How to generate random number in java In this section you will learn how to generate random number in java. Java API  provide a random class...() method is most convenient way to generate random number in java which return
how to generate random questions - Java Beginners
how to generate random questions  i am designing a test engine, i... friend, Plz specify the technology with full details like Java,JSP,Servlet,ASP and .net etc... See the below code in Java to help in solving the problem
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
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 Set iterator with example
Java Set Interface keeps the data without duplicate value. Its one subtype... sorted data. It uses iterator() method to traverse the data Example of Java Set Iterator import java.util.*; public class setiterator { public static
Java Set Iterator
data. It uses iterator() method to traverse the data Java Set Iterator... Set Interface keeps the data without duplicate value. Its one subtype...(String[] args) { Set s = new HashSet(); s.add("car
java program for
java program for   java program for printing documents,images and cards
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  A Java Program that print the data on the printer but buttons not to be printed
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  write a program to print 1234 567 89 10
java program
java program  Develop the program calculateHeight, which computes the height that a rocket reaches in a given amount of time. If the rocket accelerates at a constant rate g, it reaches a speed of g â?¢ t in t time units
java program
java program  Develop the program calculateHeight, which computes the height that a rocket reaches in a given amount of time. If the rocket accelerates at a constant rate g, it reaches a speed of g ? t in t time units

Ads