One technology application: generating permutations and Power sets (Java implementation)

Source: Internet
Author: User

Minus one technique, as with binary search, is a general algorithm design technique. It is a special form of divide-and-conquer method, which is realized by establishing the recursive solution relation of the problem instance P (n) and the problem instance P (n-1). The most classic example is the insertion sort. Here, the application of the subtraction technique in generating permutation combinations is given.

(i) Permutation problem: Generate all permutations of the natural number,,,,, N.

Algorithm Description:

A recursive relationship between the permutation of the natural number 12...N and the 12...n-1 is established by using the subtraction technique. Assuming that P (n-1) is a set of all permutations of the natural number 12...n-1 P1, p2,..., p (m), p (n) is obtained by the following means: For all permutations P1, p2, ..., p (m), insert n into the n positions of these permutations, and all the permutations are obtained. For example, the arrangement of 12 is 12, 21, then the arrangement of 123 is obtained by the following way: 1. Insert 3 into 12 to get 312,132,123;2. Insert 3 into 21 and get 321, 231, 213. Small examples are often able to indicate the path to the solution of the problem.

Minimum change requirement: In some cases, all permutations that are required to be generated are only two adjacent to each other in the adjacent arrangement. For example, 1234 and 1324 are satisfied, while 1234 and 1432 are not satisfied. The above method generates permutations of 312, 132,123, 321, 231,213, 123, and 321 that do not meet this requirement. The solution is that when the aligned pi is inserted from left to right, the adjacent permutation pi+1 is inserted from right to left. For example, 1 in the 3 insertion arrangement 12 is inserted from left to right, then 2 should be 3 from right to left, to get 213,231,321, so that with the previous 312,132,123 to meet the minimum change requirements.

Detailed design:

1. Input: Natural number n

2. Output: A collection of all permutations, each of which is represented by a list of links (taking into account the insert operation); All permutations are represented by a mutable listing of the linked lists (ArrayList). The reason for this is that you might want to take out permutations later, such as allocation problems. The cost is very low space efficiency.

3. Data structure: Use the queue to store all the permutations of the n-1, then remove each element in the queue, insert n into it, and get an arrangement of N.

Package Algorithm.permutation;import Java.util.arraylist;import Java.util.iterator;import java.util.LinkedList;/** * Permutation: * Generate all the permutations of a given number. * * Generates a full array of n numbers.  * */public class Permutation {/** * each arrangement uses a linkedlist<integer> to store, * using Linkedlist<integer> 's  List to store all permutations * */private arraylist<linkedlist<integer>> perms;/** * Use flag as alternate left-to-right and right-to-left scan flags * In this way, the requirement to arrange the "minimum change" can be achieved. * i.e.: Each adjacent two permutations, only two adjacent positions are different. * */private int flag = 1;/** constructor */public permutation () {if (perms = null) perms = new Arraylist<linkedlist<intege R>> ();}  /** generates a full array of n numbers and is stored in perms */private void createperm (int n) {if (n <= 0) throw new IllegalArgumentException (); if (n = = 1) {linkedlist<integer> init = new linkedlist<integer> (); Init.add (1);p erms.add (init);}        else {createperm (n-1); For each n-1 permutation P (n-1), n is inserted into the n possible position of the permutation P (n-1), and//can get n corresponding n element permutation p (n) int length = PERMS.SIze ();       for (int i=0; i < length; i++) {linkedlist<integer> p = perms.get (i); if (flag = = 0) {//flag = 0: Scan left-to-right insert for (int j=0; J <= P.size (); + j) {Linkedlist&lt      ;integer> pcopy = Copylist (p);      Pcopy.add (j, N);      Perms.add (pcopy);       flag = 1; }} else {//flag = 1: Right-to-left scan insert for (int j=p.size (); J >=0; j--) {LINKEDLIST&L   t;integer> pcopy = Copylist (p);   Pcopy.add (j, N);   Perms.add (pcopy);    Flag = 0;    }}}//delete all P (n-1) permutations for (int i=0; i < length; i++) {perms.remove (0); }}}/** gets the full array of n elements */public arraylist<linkedlist<integer>> getperm (int n) {createperm (n); return perms;} /** copies the elements of the list to another list and returns the list */private linkedlist<integer> copylist (linkedlist<integer> list) {linkedlist& Lt;integer> copylist = new linkedlist<integer> () Iterator iter = List.iterator (); while (Iter.hasnext ()) {Integer i = (integer) iter.next (); Copylist.add (i);} return copylist;}}

  

(ii) Generating a power set of the given set {,..., n}, that is, given the natural number 3, to generate its power set: {{0}, {1}, {2}, {}, {3}, {1,3},{2,3},{1,2,3}}

Algorithm Description:

Using the subtraction technique, a recursive solution relationship between the problem instance P (n) and the problem instance P (n-1) is established. If P (n-1) is the power set of the problem instance n-1, then the power set of the problem instance n is obtained by: Powerset (n) = Powerset (n-1) + powerset (n-1) ∪{n}, that is, for each set of N-1 's power set and {n}. Then the resulting set and the power set of the n-1 are set. For example, {1} has a power set of {{0}, {1}}, and {{2} {{0}}, {1}, {$}}, {0} = {n} {0}∪{n}

Detailed design:

1. Input: Natural number n

2. Output: A power set of {,.., n}, each subset is represented by a linkedlist, and the power set is represented and stored using LinkedList's variable list arraylist.

Java Code Implementation:

Package Algorithm.permutation;import Java.util.arraylist;import Java.util.iterator;import java.util.LinkedList;/** * PowerSet * Generate All the subsets of a given set. * * Generates all subsets of a given set. * */public class PowerSet {/** * Each subset uses a linkedlist<integer> to store, * Use the list of linkedlist<integer> to store    All subsets * */private arraylist<linkedlist<integer>> powerset; /** Constructor */public PowerSet () {if (PowerSet = = null) PowerSet = new arraylist<linkedlist<integer>> ();} /** generates a power set of {,......, n} */private void Createpowerset (int n) {if (n < 0) throw new IllegalArgumentException (); if (n = = 0) {linkedlist<integer> empty = new linkedlist<integer> ();p owerset.add (empty);} if (n = = 1) {linkedlist<integer> empty = new linkedlist<integer> (); linkedlist<integer> init = new linkedlist<integer> () Init.add (1);p owerset.add (empty);p Owerset.add (init );}   else {createpowerset (n-1);//Powerset (n) = Powerset (n-1) + powerset (n-1) ∪{n}     int length = Powerset.size ();       for (int i=0; i < length; i++) {linkedlist<integer> p = powerset.get (i);   linkedlist<integer> pcopy = Copylist (p);   Pcopy.add (P.size (), n);            Powerset.add (pcopy); }}}/** gets the power set of N elements collection */public arraylist<linkedlist<integer>> getpowerset (int n) {Createpower Set (n); return powerset;} /** copies the elements of the list to another list and returns the list */private linkedlist<integer> copylist (linkedlist<integer> list) {linkedlist& Lt;integer> copylist = new linkedlist<integer> () Iterator iter = List.iterator (); while (Iter.hasnext ()) { Integer i = (integer) iter.next (); Copylist.add (i);} return copylist;}}

  

Algorithm Analysis:

The time complexity of reducing a technique is usually: t (n) = t (n-1) + G (n).

① if G (n) is constant, then T (n) = O (n); (Maximum and number of contiguous sub-arrays)

② if G (n) is O (logn), then T (n) = O (Nlogn); (Heap sort)

③ if G (n) = An+b (a!=0), then T (n) = O (n^2). (Insert sort)

Therefore, when G (n) is constant or logarithmic, the reduction technique is more efficient.

One technology application: generating permutations and Power sets (Java implementation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.