#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Unhappy Returning a set of objects for powerset recursion


    I can implement a powerset generator using an ArrayList but returning a set of Objects is beyond me.

    Here is how I must implement it. MySet is just an implementation of Set (it works).

    Code:
    static MySet<MySet<Object>> powerSet(MySet<Object> s) {
    
    }
    Also there's powerset within the MySet given to me, again I'm not exactly sure what to make of it:

    Code:
    	
    public MySet<MySet<T>> powerset(MySet<T> s) {
    	MySet<MySet<T>>  ps = new MySet<MySet<T>>();
    		if (s.cardinality()==0) {
    			ps.add(new MySet<T>()); 
    			return ps;
    		}
    	T a = s.takeOne();
    	ps = powerset(s);
    	int n = ps.cardinality();
    	for (int i=0; i<n;i++) {
    		MySet<T> t = new MySet<T>(ps.set[i]);
    		t.add(a);
    		ps.add(t);				
    	}
    	return ps;
    }
    Thank you really for any insights
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    If you are getting error messages, please copy the full text of the messages and paste it here.
    Otherwise please explain what the problem is.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    adding an iterator and resetting s to it's inputted form on return with s2 (to account for adding new values) fixed it

    complete result here:

    Code:
    static MySet<MySet<Object>> powerSet(MySet<Object> s) {
    		
    		MySet<MySet<Object>> ps = new MySet<MySet<Object>>();
    		MySet<Object> s2 = new MySet<Object>();
    		
    		//base case
    		if (s.cardinality()==0) {
    			ps.add(new MySet<Object>()); 
    			return ps;
    		}
    		
    		Object a = s.takeOne();
    		s2.add(a);
    		
    		ps = powerSet(s);
    		int n = ps.cardinality();
    		
    		MySet<Object> temp = new MySet<Object>();
    		
    		Iterator<MySet<Object>> itr = ps.iterator();
    		
    		for (int i=0; i<n;i++) {
    			MySet<Object> t = new MySet<Object>(itr.next());
    			t.add(a);
    			ps.add(t);
    		}
    		
    		s.add(s2);
    		return ps;
    	}

IMN logo majestic logo threadwatch logo seochat tools logo