Application of the minus technology: generate an arrangement and an idempotent set (implemented in Java)

Source: Internet
Author: User

Just like binary search, this technology is a general algorithm design technology. It is a special form of the divide and conquer method. It is implemented by establishing a recursive relationship between the problem instance P (n) and the problem instance P (n-1; the most typical example is insert sorting. Here, the application of the subtraction Technology in generating and arranging combinations is given.

 

(1) Sorting Problem: generate all arrays of natural numbers 1, 2, and N.

 

Algorithm Description:

Using the subtraction technique, we can establish a recursive relationship between the arrangement of natural numbers 12... n and 12... n-1. Suppose P (n-1) is a natural number 12... all arrays of N-1: P1, P2 ,..., for a set of P (M), P (n) is obtained in the following way ,..., P (M), insert n to the N positions of these arrays to obtain all the arrays. For example, if the values of 12 are 12 and 21, the 123 order is obtained as follows:
1. Insert 3 to 12 to get 312,132,123; 2. Insert 3 to 21 to get 321,231,213. A small example can often provide a way to solve the problem.

"Minimum variation" requirement: Sometimes, in all the arrays generated, the adjacent permutation is only two adjacent locations. For example, 1234 and 1324 are satisfied, while 1234 and 1432 are not satisfied. The 312,132,123,321,231,213,123 and 321 columns generated by the preceding method do not meet this requirement. Solution: When the placement pi is inserted from left to right, the placement PI + 1 is inserted from right to left. For example, in 1, insert 3 to arrange 12 to insert from left to right. In 2, insert 3 from right to left to get 213,231,321. In this way, the minimum change requirement is met with the preceding 312,132,123.

 

Detailed Design:

1. Input: natural number n

2. Output: All sorted sets are represented by a linked list (considering the insert operation). All arrays are represented by a variable list (arraylist) of the linked list. This method is used because the sorting may be taken out for solving, such as the allocation problem. The cost is low space efficiency.

3. Data Structure: a queue is used to store n-1 arrays. Then, each element in the queue is taken out, and N is inserted into the queue to get an arrangement of N.

 

Java code implementation:

Package algorithm. permutation; <br/> Import Java. util. arraylist; <br/> Import Java. util. iterator; <br/> Import Java. util. permission list; <br/>/** <br/> * permutation: <br/> * generate all the permutations of a given number. <br/> * generates a full arrangement of N numbers. <Br/> */<br/> public class permutation {</P> <p>/** <br/> * use a sequence list for each arrangement <integer> to store, <br/> * use the sorted list <integer> list to store all arrays <br/> */</P> <p> private arraylist <sorted list <integer> perms; </P> <p>/** <br/> * use flag as the marker for scanning from left to right or from right to left. <br/>, the minimum variation requirement can be achieved. <Br/> * that is, the two adjacent arrays have only two adjacent positions. <Br/> */</P> <p> private int flag = 1; </P> <p>/** constructor */</P> <p> Public permutation () {</P> <p> If (perms = NULL) <br/> perms = new arraylist <partition list <integer> (); </P> <p >}</P> <p>/** generates a full arrangement of N numbers, and stored in perms */</P> <p> private void createperm (int n) {</P> <p> If (n <= 0) <br/> throw new illegalargumentexception (); </P> <p> If (n = 1) {<br/> upload list <integer> init = new upload list <integer> (); <br/> init. add (1); <br/> perms. add (init); <br/>}< br/> else {<br/> createperm (n-1 ); </P> <p> // insert n to the n possible positions of each n-1 arrangement P (n-1, <br/> // you can obtain n corresponding n elements in the P (n) </P> <p> int length = perms. size (); <br/> for (INT I = 0; I <length; I ++) {<br/> sort list <integer> P = perms. get (I); <br/> If (flag = 0) {</P> <p> // flag = 0: insert from left to right </P> <p> for (Int J = 0; j <= P. size (); j ++) {<br/> partition list <integer> pcopy = copylist (p); <br/> pcopy. add (J, n); <br/> perms. add (pcopy); <br/> flag = 1; <br/>}</P> <p >}< br/> else {</P> <p> // flag = 1: insert from right to left </P> <p> for (Int J = P. size (); j> = 0; j --) {<br/> upload list <integer> pcopy = copylist (p); <br/> pcopy. add (J, n); <br/> perms. add (pcopy); <br/> flag = 0; <br/>}</P> <p >}< br/>}</P> <p> // delete all P (n-1) arrange </P> <p> for (INT I = 0; I <length; I ++) {<br/> perms. remove (0 ); <br/>}</P> <p>/** obtain the full arrangement of n elements */ </P> <p> Public arraylist <partial list <integer> getperm (int n) {</P> <p> createperm (n); <br/> return perms; <br/>}</P> <p>/** copy the list element to another list, returns the list */</P> <p> private partition list <integer> copylist (partition list <integer> list) {</P> <p> shortlist <integer> copylist = new shortlist <integer> (); <br/> iterator iter = List. iterator (); <br/> while (ITER. hasnext () {<br/> integer I = (integer) ITER. next (); <br/> copylist. add (I); <br/>}< br/> return copylist; <br/>}< br/>

 

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

Algorithm Description:

Use the subtraction technique to establish a recursive solution relationship between the problem instance P (n) and the problem instance P (n-1. If P (n-1) is the power set of n-1 of the problematic instance, the Power Set of the problematic instance N is obtained in the following way: powerset (n) = powerset (n-1) + powerset (n-1) returns {n}, that is, returns the Union of each set of N-1's idempotent sets and {n}, and returns the union of the obtained set and n-1's idempotent set. For example, if the power set of {1} is {0}, {1}, the Power Set of {1, 2} is {2} {1}, {0 },
{1}, where {0} indicates an empty set and {0} else {n} = {n}

 

Detailed Design:

1. Input: natural number n

2. Output: The idempotence of {,..., n}. Each subset is represented by a sorted list. The idempotence is represented and stored by the variable list arraylist of sorted list.


Java code implementation:

Package algorithm. permutation; <br/> Import Java. util. arraylist; <br/> Import Java. util. iterator; <br/> Import Java. util. optional list; <br/>/** <br/> * powerset <br/> * generate all the subsets of a given set. <br/> * generates all subsets of a given set. <Br/> */<br/> public class powerset {</P> <p>/** <br/> * Each subset uses an external list <integer> to store, <br/> * use the sorted list <integer> list to store all subsets <br/> */</P> <p> private arraylist <sorted list <integer> powerset; </P> <p>/** constructor */</P> <p> Public powerset () {</P> <p> If (powerset = NULL) <br/> powerset = new arraylist <shortlist <integer> (); </P> <p >}</P> <p>/*** generate {1, 2, 3 ,......, Power Set of n} */</P> <p> private void createpowerset (int n) {</P> <p> If (n <0) <br/> throw new illegalargumentexception (); </P> <p> If (n = 0) {<br/> shortlist <integer> Empty = new shortlist <integer> (); <br/> powerset. add (empty); <br/>}</P> <p> If (n = 1) {<br/> shortlist <integer> Empty = new shortlist <integer> (); <br/> shortlist <integer> init = new shortlist <integer> (); <br/> init. add (1); <br/> powerset. add (empty); <br/> powerset. add (init); <br/>}< br/> else {<br/> createpowerset (n-1); </P> <p> // powerset (N) = powerset (n-1) + powerset (n-1) limit {n} </P> <p> int length = powerset. size (); <br/> for (INT I = 0; I <length; I ++) {<br/> sort list <integer> P = powerset. get (I); <br/> upload list <integer> pcopy = copylist (p); <br/> pcopy. add (P. size (), n); <br/> powerset. add (pcopy ); </P> <p >}< br/>/** obtain the power set of N element sets */ </P> <p> Public arraylist <shortlist <integer> getpowerset (int n) {</P> <p> createpowerset (n); <br/> return powerset; <br/>}< br/>/** copy the list element to another list, returns the list */</P> <p> private partition list <integer> copylist (partition list <integer> list) {</P> <p> shortlist <integer> copylist = new shortlist <integer> (); <br/> iterator iter = List. iterator (); <br/> while (ITER. hasnext () {<br/> integer I = (integer) ITER. next (); <br/> copylist. add (I); <br/>}< br/> return copylist; <br/>}</P> <p >}< br/>

 

Algorithm analysis:

The time complexity of the Subtraction Technique is usually T (n) = T (n-1) + g (n ).

① If G (n) is a constant, T (n) = O (n); (the largest sum of continuous subarrays)

② If G (n) is O (logn), T (n) = O (nlogn); (heap sorting)

③ If G (n) = An + B (! = 0), then t (n) = O (N ^ 2). (insert sort)

Therefore, when G (n) is a constant or logarithm, it is more efficient to subtract one.

 

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.