2011-08-26 13 views
9

मेरे पास तत्वों की एक सूची है (1, 2, 3), और मुझे उस सूची के सुपरसेट (पावरसेट) (तत्वों को दोहराए बिना) प्राप्त करने की आवश्यकता है। तो बुनियादी तौर पर मैं सूचियाँ की एक सूची है कि लगता है कि बनाने की जरूरत:सूची के सभी संभावित सबसेट प्रिंट करना

{1} 
{2} 
{3} 
{1, 2} 
{1, 3} 
{2, 3} 
{1, 2, 3} 

सबसे अच्छा क्या है यह लागू करने के लिए जिस तरह से (इस मामले में सादगी> दक्षता, सूची बहुत बड़ा नहीं होगा)? पसंदीदा में जावा में, लेकिन किसी भी भाषा में एक समाधान उपयोगी होगा।

+1

आप उस सूची के सभी सबसेट चाहते हैं। मैं रिकर्सन का सुझाव दूंगा। हालांकि, यदि आप 30-40 से अधिक तत्वों से बात कर रहे हैं, तो आप बड़े (डेटा के 1TB डेटा) से निपटने में सक्षम नहीं होंगे। इसका क्या उपयोग किया जाता है? –

+2

यह डेटा संरचना जिसे आप ढूंढ रहे हैं उसे पावरसेट कहा जाता है (भिन्नता यह है कि इसमें एक खाली सेट भी शामिल है)। एसओ पर पहले से ही चर्चा की जा चुकी है। –

+0

धन्यवाद सही दिशा में मुझे इंगित करने के लिए जेनज़न ... मैंने पाया http://stackoverflow.com/questions/1670862/obtaining-powerset-of-a-set-in-java। – Steve

उत्तर

31

उपयोग bitmasks:

1 = 001 = {1} 
2 = 010 = {2} 
3 = 011 = {1, 2} 
4 = 100 = {3} 
5 = 101 = {1, 3} 
6 = 110 = {2, 3} 
7 = 111 = {1, 2, 3} 

तुम्हें पता है बाइनरी में पहली बिट सबसे दायीं ओर है:

int allMasks = (1 << N); 
for (int i = 1; i < allMasks; i++) 
{ 
    for (int j = 0; j < N; j++) 
     if ((i & (1 << j)) > 0) //The j-th element is used 
      System.out.print((j + 1) + " "); 

    System.out.println(); 
} 

यहाँ सब bitmasks हैं।

+0

यह बहुत रोचक है ... आप स्पष्ट रूप से मेरे मुकाबले बहुत चालाक हैं - मुझे इस बारे में अपना मन लपेटने के लिए कुछ समय दें .... क्या एन मूल सूची में तत्वों की संख्या है? और क्या मैं अपनी सूची में ऑब्जेक्ट्स को पूर्णांक में मैप कर रहा हूं? – Steve

+0

@ स्टेव - हां, 'एन' तत्वों की संख्या है - आपके उदाहरण में' एन = 3' से ऊपर। बाइनरी में सोचें - बिट 0 है यदि तत्व का उपयोग नहीं किया जाता है और यदि तत्व का उपयोग किया जाता है तो बिट 1 होता है। बाइनरी में उदाहरण के लिए 5 = 101। इसका मतलब है '1' और '3' का उपयोग किया जाता है। '= {1, 3}' –

+0

@Steve - मेरे संपादन को देखें। –

1
import java.io.*; 
import java.util.*; 
class subsets 
{ 
    static String list[]; 
    public static void process(int n) 
    { 
     int i,j,k; 
     String s=""; 
     displaySubset(s); 
     for(i=0;i<n;i++) 
     { 
      for(j=0;j<n-i;j++) 
      { 
       k=j+i; 
       for(int m=j;m<=k;m++) 
       { 
        s=s+m; 
       } 
       displaySubset(s); 
       s=""; 
      } 
     } 
    } 
    public static void displaySubset(String s) 
    { 
     String set=""; 
     for(int i=0;i<s.length();i++) 
     { 
      String m=""+s.charAt(i); 
      int num=Integer.parseInt(m); 
      if(i==s.length()-1) 
       set=set+list[num]; 
      else 
       set=set+list[num]+","; 
     } 
     set="{"+set+"}"; 
     System.out.println(set); 
    } 
    public static void main() 
    { 
     Scanner sc=new Scanner(System.in); 
     System.out.println("Input ur list"); 
     String slist=sc.nextLine(); 
     int len=slist.length(); 
     slist=slist.substring(1,len-1); 
     StringTokenizer st=new StringTokenizer(slist,","); 
     int n=st.countTokens(); 
     list=new String[n]; 
     for(int i=0;i<n;i++) 
     { 
      list[i]=st.nextToken(); 
     } 
     process(n); 
    } 
} 
+0

कार्यक्रम सरल है। सबसे पहले, हम 1,2,3 की प्रत्येक सूची के अंतिम अंक तक 1-अंकों की संख्याओं के सभी संभावित संयोजन प्राप्त करने का प्रयास करते हैं .... n अंकों की संख्या n है। और फिर प्रत्येक संयोजन के साथ अपने प्रत्येक वर्ण (यानी संख्या) निकालने के लिए और इस वर्ण (संख्या) द्वारा निर्दिष्ट सेल इंडेक्स में संग्रहीत सबसेट का तत्व प्रदर्शित करें। –

+0

टिप्पणियों की तुलना में उत्तर में कोड का विवरण जोड़ना बेहतर होगा। कोड के साथ उत्तर सामान्य रूप से समुदाय द्वारा अच्छा जवाब नहीं माना जाता है, यहां तक ​​कि कोड सही ढंग से प्रश्न का उत्तर देता है। –

0

एक जावा समाधान Petar Minchev समाधान के आधार पर -

public static List<List<Integer>> getAllSubsets(List<Integer> input) { 
    int allMasks = 1 << input.size(); 
    List<List<Integer>> output = new ArrayList<List<Integer>>(); 
    for(int i=0;i<allMasks;i++) { 
     List<Integer> sub = new ArrayList<Integer>(); 
     for(int j=0;j<input.size();j++) { 
      if((i & (1 << j)) > 0) { 
       sub.add(input.get(j)); 
      } 
     } 
     output.add(sub); 
    } 

    return output; 
} 
0

मैंने देखा है कि उत्तर स्ट्रिंग सूची पर ध्यान केंद्रित कर रहे हैं। नतीजतन, मैंने अधिक सामान्य जवाब साझा करने का फैसला किया। आशा है कि यह सहायक हो जाएगा। (soultion एक और समाधान मैंने पाया पर आधारित है, मैं इसे एक सामान्य algorithem के लिए संयुक्त।)

/** 
* metod returns all the sublists of a given list 
* the method assumes all object are different 
* no matter the type of the list (generics) 
* @param list the list to return all the sublist of 
* @param <T> 
* @return list of the different sublists that can be made from the list object 
*/ 
public static <T> List<List<T>>getAllSubLists(List<T>list) 
{ 
    List<T>subList; 
    List<List<T>>res = new ArrayList<>(); 
    List<List<Integer>> indexes = allSubListIndexes(list.size()); 
    for(List<Integer> subListIndexes:indexes) 
    { 
     subList=new ArrayList<>(); 
     for(int index:subListIndexes) 
      subList.add(list.get(index)); 
     res.add(subList); 
    } 
    return res; 
} 
/** 
* method returns list of list of integers representing the indexes of all the sublists in a N size list 
* @param n the size of the list 
* @return list of list of integers of indexes of the sublist 
*/ 
public static List<List<Integer>> allSubListIndexes(int n) { 
    List<List<Integer>> res = new ArrayList<>(); 
    int allMasks = (1 << n); 
    for (int i = 1; i < allMasks; i++) 
    { 
     res.add(new ArrayList<>()); 
     for (int j = 0; j < n; j++) 
      if ((i & (1 << j)) > 0) 
       res.get(i-1).add(j); 

    } 
    return res; 
} 
संबंधित मुद्दे