?
Solved

Create a powerset in Java

Posted on 2011-02-17
6
Medium Priority
?
1,210 Views
Last Modified: 2013-12-14
Hi,

 In Java I want to create a powerset, when i give  a set and a number for the size for e.g.

{1,2,3,4} set:3
------------------
{1,2,3}
{1,2,4}
{1,3,4}
{2,3,4}

or
{1,2,3} set 2
{1,2}
{1,3}
{2,3}

thanks
0
Comment
Question by:kimia_az
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 34922615
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34922622
This implementation is for all sets, I guess it is easy to modify only limiting to
certain size
0
 
LVL 92

Expert Comment

by:objects
ID: 34922645
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 2000 total points
ID: 34922653

This will print only sets of 2 out of {1,2,3}

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SetUtils {
    public static <T> Set<Set<T>> powerSet(Set<T> originalSet) {
        Set<Set<T>> sets = new HashSet<Set<T>>();
        if (originalSet.isEmpty()) {
            sets.add(new HashSet<T>());
            return sets;
        }
        List<T> list = new ArrayList<T>(originalSet);
        T head = list.get(0);
        Set<T> rest = new HashSet<T>(list.subList(1, list.size()));
        for (Set<T> set : powerSet(rest)) {
            Set<T> newSet = new HashSet<T>();
            newSet.add(head);
            newSet.addAll(set);
            sets.add(newSet);
         
            sets.add(set);
        }

       
        return sets;
    }

    public static void main(String[] args){
    Set<Integer> mySet = new HashSet<Integer>();
    mySet.add(1);
    mySet.add(2);
    mySet.add(3);
    for (Set<Integer> s : SetUtils.powerSet(mySet)) {
        if(s.size() == 2)
        System.out.println(s);
    }

    
}
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 34922911

This will print only sets of 3 out of {1,2,3,4}  a little bit nicer:


import java.util.*;

public class SetUtils {
    public static <T> Set<Set<T>> powerSet(Set<T> originalSet) {
        Set<Set<T>> sets = new HashSet<Set<T>>();
        if (originalSet.isEmpty()) {
            sets.add(new HashSet<T>());
            return sets;
        }
        List<T> list = new ArrayList<T>(originalSet);
        T head = list.get(0);
        Set<T> rest = new HashSet<T>(list.subList(1, list.size()));
        for (Set<T> set : powerSet(rest)) {
            Set<T> newSet = new HashSet<T>();
            newSet.add(head);
            newSet.addAll(set);
            sets.add(newSet);
         
            sets.add(set);
        }


        return sets;
    }


        public static <T> Set<Set<T>> powerSet1( Set<T>mySet, int n){
                  Set<Set<T>> sets1 = new  HashSet<Set<T>>();
           Set originalSet =  SetUtils.powerSet(mySet);
            Iterator it = originalSet.iterator();
            while(it.hasNext()) {
                Set s = (Set) it.next();
                if(s.size() == n)sets1.add(s);
            }


            return sets1;
        }


    public static void main(String[] args){
    Set<Integer> mySet = new HashSet <Integer>();
    mySet.add(1);
    mySet.add(2);
    mySet.add(3);
     mySet.add(4);    
    for (Set <Integer> s : SetUtils.powerSet1(mySet, 3)) {
               System.out.println(s);
    }

    
}
}

Open in new window

0
 
LVL 4

Expert Comment

by:msk_apk
ID: 34923511
int setLength = 3;
		int[] array = new int[]{1,2,3,4};
		for(int i=0;i<array.length-2;i++)
		{			
			for(int j=i+1;j<array.length -1;j++)
			{
				for(int k=j+1;k<array.length;k++)
				{
					int[] temp = new int[setLength];
					temp[0] = array[i];
					temp[1] = array[j];
					temp[2] = array[k];
					
					System.out.println(temp[0]+","+temp[1]+","+temp[2]);
				}
			}
		}

Open in new window

0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question