Problem: Given a finite set, try listing all the elements in its power set.
Workaround 1:to generate all subsets of s,list all 2n bit strings of length n (for instence, in increasing order), and write down T He corresponding subsets.
Workaround 2: Take collection <a,b,c,d> as an example
First step: Add an empty set,
The second step: adding a cell to a single-element subset, and storing it in a Tempbefore string array for conversion to a subset of two elements, the single-element subset in temp is a,b,c,d.
Step three: Add an element to each subset, generating a subset of the N+1 elements, using the Tempbefore and set S generated in the second step.
1 Private Static intChange (string[] tempafter, string[] Tempbefore,2 string[] first) {3 intTempafterlength = 0;4 for(inti = 0;!tempbefore[i].equals ("#"); i++) {5Tempafterlength =Addtempafter (Tempafter, Tempbefore[i], first,6 tempafterlength);7 }8 returntempafterlength;9}View Code
Private Static intAddtempafter (string[] tempafter, string string, string[] First,inttempafterlength) { intLocation =getlaststringlocation (First, string); if(First[location + 1].equals ("#")) { returntempafterlength; /*** For example, if Tempbefore[i] is d!ef and the input string set is "v,<a,v>,d,ef#" * then we know that the subset D!ef cannot be regenerated into a new subset. * */ } for(inti = location + 1;!first[i].equals ("#"); i++) {Tempafter[tempafterlength]= string + "!" +First[i]; Tempafterlength++; } returntempafterlength; }View Code
/** * Gets the position of the last element of the string in the input collection, such as the input set is "v,<a,v>,d,ef#", string v!d * Returns position 2, because element D is subscript 2 in the first array Where Str is taken as the last of the String in this example is D * */private static int getlaststringlocation (string[] first2, string string) {string str = "" ; int i = 0, location = 0;for (int j = 0; J < String.Length (); j + +) {str = str + string.charat (j); if (String.charat (j) = = '! ') {str = "";}} for (i = 0;!first2[i].equals ("#"), i++) {if (First2[i].equals (str)) {location = i;}} return location;}
Discrete Mathematics 2nd Chapter Computer and Exploration 2nd question