java - Algorithm for combination -


i have designed algorithm in java generate combinations of elements of list . example whereas elements [a, b , c] generates combinations , [a] , [b] , [c] , [ab] , [bc] , [abc] . or based on elements [a, a, b] generates combinations [a] , [b] , [aa] , [ab] , [aab] . code generate combinations .

private list<elemento> combinazionemassima = new arraylist<>();     private log logger = logfactory.getlog(combinazioni3.class);      public combinazioni3(list<elemento> generacombinazionemassima) {         this.combinazionemassima = generacombinazionemassima;     }      public void combine() {         this.findallcombinations(combinazionemassima);     }      private static class node{         int lastindex = 0;     list<elemento> currentlist;     public node(int lastindex, list<elemento> list) {             this.lastindex = lastindex;             this.currentlist = list;     }     public node(node n) {             this.lastindex = n.lastindex;             this.currentlist = new arraylist<elemento>(n.currentlist);     }     }      public void findallcombinations(list<elemento> combinazioni) {         date datainizio = new date();         list<list<elemento>> resultlist = new arraylist<list<elemento>>();         linkedlist<node> queue = new linkedlist<node>();         int n = combinazioni.size();         arraylist<elemento> temp = new arraylist<elemento>();         temp.add(combinazioni.get(0));         queue.add(new node(0, temp));         // add different integers queue once.         for(int i=1;i<n;++i) {                 if(combinazioni.get(i-1) == combinazioni.get(i)) continue;                 temp = new arraylist<elemento>();                 temp.add(combinazioni.get(i));                 queue.add(new node(i, temp));         }         // bfs until have no elements         while(!queue.isempty()) {                 node node = queue.remove();                 if(node.lastindex+1 < n) {                         node newnode = new node(node);                         newnode.lastindex = node.lastindex+1;                         newnode.currentlist.add(combinazioni.get(node.lastindex+1));                         queue.add(newnode);                 }                 for(int i=node.lastindex+2;i<n;++i) {                         if(combinazioni.get(i-1) == combinazioni.get(i)) continue;                         // create copy , add integer                         node newnode = new node(node);                         newnode.lastindex = i;                         newnode.currentlist.add(combinazioni.get(i));                         queue.add(newnode);                 }                 gestoreregole gestoreregole = new gestoreregole();                 gestoreregole.esegui(node.currentlist);         }      } 

but input > 250 program stops , takes long make combinations . how can improve solution ? or there better solution ?

for input=250, there combinations: @ example:

(1) {a}     => {a} (3) {a,b}   => {a}, {b}, {a,b} (7) {a,b,c} => {a}, {b}, {c}, {a,b}, {a,c}, {b,c}, {a,b,c} 

as can see, there 2^n-1 elements

so input=250 - 2^250-1 = large number (1.8*10^75)

too memory used. think number 20 (1048575) make trouble too


Comments

Popular posts from this blog

android - Pass an Serializable object in AIDL -

How to provide Authorization & Authentication using Asp.net, C#? -

How to use Authorization & Authentication in Asp.net, C#? -