Circular distribution of permutation and combinatorial problems

Source: Internet
Author: User

1. Questions
1.1 Sitting in the clouds
There is a round table, sat a,b,c,d Four people, known, D on the right side of a, C in the opposite of D, ask A,b,c,d, the Shiji?

Answer: The problem is relatively simple, we draw on paper, we can draw their possible position

However, there may be a solution, for example, we turn the a,b,c,d right to a bit, but also to meet the conditions, and as long as they keep their relative position unchanged, turn right to n bits are the solution of the problem, and there is an interesting thing, when they turn around (that is, turn right to 4 bits), they right back to the original

2. Circular distribution
The question above is a circular distribution, so what is the difference between him and the line distribution? What's the connection?
The above article, "Permutation of combinatorial problems," said in the line, when there are 4 of the ball, the possible permutations of a total of 4! = 24 kinds, then we put a,b,c,d four people's seats are marked {0, 1, 2, 3} number, then a,b,c,d four person may sit position is a line arrangement.

Suppose we use a computer to resolve this problem and give a possible distribution. Our train of thought is:
1> List all possible distributions
2> then analyze whether each combination meets the requirements of the topic
Of course, we can also find a combination of each to determine whether or not to meet the requirements of the topic, so that one can exit after finding a, can reduce the complexity of time.
If we are arranged according to the line arrangement, we need to find out 24 kinds of permutations, according to the previous analysis, a certain arrangement and 3 kinds of permutations can satisfy the solution of the problem, we only need a solution to find 24 kinds of comparison fee time, and when the problem is complicated, such as a 16-side table, Given the above-mentioned problem, the worst case scenario altogether needs to list the 16!=20922789888000 species, and if we can remove the duplicates, we can reduce the calculation. If we actually just need to list 24/4 = 6 kinds of distribution

3, how to find out the distribution of different distribution in the outlet type
Let's take a look at the answer to the question: a,b,c,d four people Shiji = {0, 2, 3, 1}
Assuming a is fixed, then B, C, d the possible Shiji is still a linetype distribution, that is, a common 3!=6 species, because a does not move, all b,c,d can not be rotated, so there will be no problem of the above multiple solutions
At this time we each right turn a, then the above 6 kinds of distribution can also get new 6 kinds of line distribution, but they and the distribution before the rotation is the solution of the problem (because their relative position is not changed, and the problem gives the condition is relative position), so for the circular distribution, that is, the first line distribution is connected to a circle, When the circle rotates and their position remains relatively constant, then we only need to ask for the solution of the linear distribution of the a=0,{b,c,d}, which can be used to determine the condition of the problem.

4. Circular distribution algorithm

To reduce the space, create a function of the Linetype distribution Setballnum () see the line arrangement of permutation and combinatorial problems

In order to handle the convenience, reusing code, creating a circular distribution, we are fixed the last one motionless, that is, the position of D is not moving

BOOLCpermutation::createcirclepermutaion (intNnum, std::vector<std::vector<int>>&vectorpermutation) {    //first create a straight line arrangement of nNum-1 bitsstd::vector<int> Vectorballset (Nnum-1,0); Std::vector<int> Vectorball (Nnum-1,0); Setballnum (0, Vectorball, Vectorballset, vectorpermutation); //then add the bits of nNum-1 to the last     for(inti =0; I < vectorpermutation.size (); i++) {vectorpermutation[i].push_back (Nnum-1); }    return    true;}

Description: In order to reduce the code, processing convenience, the algorithm used in the STL vector container, need a little STL container use of relevant knowledge, of course, with arrays can also be implemented, the code will be slightly troublesome

5, how to express the relative position of a,b,c,d
5.1 What is x on the right side of y?
Let's change a picture that looks better:

b on the right side of a, their position is (0, 1), C on the right side of B their position is (1, 2) ....

A, B

B, C

C, D

D, A

Relative position

(0, 1)

(1, 2)

(2, 3)

(3, 0)

By the position before C we can conclude that the so-called X on the right side of Y is f (x) +1 = f (y), where f (x) refers to the position number of X.
But when we get to D, we find that the above formula does not work on the right side of D, but the position is (3, 0), how do you deal with this situation?
We look carefully, when to D, his right position is supposed to be 4, but because it is a circular distribution, the last one and the first to meet, which is actually the same as the clock is full circle is the same, then 4 to become 0, which is actually a modulo operation, take the remaining number can be, namely:
F (x) +1 = f (y)%4 = f (y)% of table side

5.2 What is x on the opposite of y?
Look at the picture above, C is opposite the A, their position is (0, 2), D opposite B, their position is (1, 3), because if 2 people relative, then they are separated by half of the people
So we can sum up the formula: F (Y)-fx (x) = 4/2 = 2
But it can also be said that A is opposite C, f (a)-f (c) =-2, modify the following formula:
|f (Y)-f (x) | = 2 = 4/2 = number of sides of table/2

6. Automatic Judgment algorithm

BOOLCpermutation::testcircledesk () {intNsidenum =4; Std::vector<std::vector<int>>vectorpermutation;    Createcirclepermutaion (Nsidenum, vectorpermutation);    Printpermutation (vectorpermutation); //{ 0, 1, 2, 3} in the corresponding distribution for {A, B, C, D}//Traverse all the distributions     for(inti =0; I < vectorpermutation.size (); i++) {std::vector<int>& vectorcirclepermutation =Vectorpermutation[i]; //Judging by the input criteria//f (x) +1 = f (y)%4//D on the right side of a, i.e. F (0) +1 = f (3)%4//if the condition is not met, continue to the next set of        if((vectorcirclepermutation[0] +1)! = (vectorcirclepermutation[3] %nsidenum)) {            Continue; }        //|f (Y)-f (x) | = 2 = 4/2//C opposite D, i.e. |f (2)-F (3) | = 2 = 4/2        if(ABS (vectorcirclepermutation[2]-vectorcirclepermutation[3]) = = Nsidenum/2) {printf ("Find matched permutation:");  for(intj =0; J < Nsidenum; J + +) {printf ("%d", Vectorcirclepermutation[j]); } printf ("\ n"); return    true; }    }    return    false;}

Note: The algorithm implementation is fixed the last one motionless, that is, the position of D is not moving, so the last one is 3 (that is, D is always in the 3rd position), but the solution is also in line with the original problem.

Circular distribution of permutation and combinatorial problems

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.