2 power of 77 java Powerset -
i have 77 elements n1,n2,n3... etc need calculate superset. used binary mask algorithme number big fit in int. here code:
private static vector powerset(string[] set) { //create empty power set vector power = new vector(); //get number of elements in set int elements = set.length; //the number of members of power set 2^n int powerelements = (int) math.pow(2,elements); //run binary counter number of power elements (int = 0; < powerelements; i++) { //convert binary number string containing n digits string binary = inttobinary(i, elements); //create new set vector innerset = new vector(); //convert each digit in current binary number corresponding element //in given set (int j = 0; j < binary.length(); j++) { if (binary.charat(j) == '1') innerset.add(set[j]); } //add new set power set power.add(innerset); } return power; } private static string inttobinary(int binary, int digits) { string temp = integer.tobinarystring(binary); int founddigits = temp.length(); string returner = temp; (int = founddigits; < digits; i++) { returner = "0" + returner; } return returner; }
i tried remove int using long or double, nothing worked.
you need data structure storing binary digits 77 digits long. use array of 77 ints , manually increment array 1 big binary number each element of array 1 bit in number. wrote increment method below.
int[] num = new int[77]; (int = 0; < 77; i++) num[i]= 0; //create new set (int = 0; < powerelements; i++) { vector innerset = new vector(); (int j = 0; j < 77; j++) { if (num[i] == 1) innerset.add(set[j]); } increment(num); } // increment array of ints if binary number. lsb 77th element in array, index 76. public void increment(int[] num) { int carry = 1; int = 76; while (i > 0) { tmp = int[i] + carry; if (tmp == 0) break; if (tmp == 1) {int[i] = 1; break;} carry = 1; int[i] = 0; i--; } if (carry == 1) throw new exception("overflow"); }
as noted commenters, won't have room store 2^77 sets. , take virtually forever count 2^77.
Comments
Post a Comment