简体   繁体   中英

Segmenting Permutations For Multitasking Java

I have and array that can take any number of points, and the points are given an index.

Then I am running a non recursive solution that creates all permutations

It runs through 1,2,3,4,5,6,7,8,9,0 and ends with 0,9,8,7,6,5,4,3,2,1 this has a time complexity to it

Now, I would like to find the middle point of these permutations so that I can run the pattern from the start and the middle with threads to reduce the time complexity.

How would I find the middle point?.

In Java:

    public Path QuickPerm(int[] points) {
       int N = points.length;
       int a[] = points ;
       int p[] = new int[N];
       Path shortest = null;
       Path current = null;
       int i;
       int j;
       int tmp; // Upper Index i; Lower Index j
       for(int i1 = 0; i1 < N; i1++) {  
          p[i1] = 0;       // p[i] == i controls iteration and index boundaries for i
       }
       //display(a, 0, 0);   // remove comment to display array a[]
       i = 1;   // setup first swap points to be 1 and 0 respectively (i & j)
       while(i < N) {
          if (p[i] < i) {
             j = i % 2 * p[i];   // IF i is odd then j = p[i] otherwise j = 0
             tmp = a[j];         // swap(a[j], a[i])
             a[j] = a[i];
             a[i] = tmp;
                                 //save 
             p[i]++;             // increase index "weight" for i by one
             i = 1;              // reset index i to 1 (assumed)
          } else {               // otherwise p[i] == i
             p[i] = 0;           // reset p[i] to zero
             i++;                // set new index value for i (increase by one)
          } // if (p[i] < i)
       }
       return shortest;// while(i < N)
    } // QuickPerm()

http://en.wikipedia.org/wiki/Lehmer_code describes one way of numbering permutations so that you can decide eg to take a number half way along and find out what permutation it is.

Depending on what your threads do you might find that not all permutations take the same amount of time, so splitting the list of permutations half way along does not balance the work exactly. One way people have handled this is to split the work into a large number of small pieces, and have your threads pick a small piece of work any time they have nothing to do. Then it doesn't matter so much if some pieces of work take longer than others - the threads that get stuck with them will simply end up working on a smaller number of pieces but taking much the same time.

Using a next-permutation algorithm that uses lexico-order would make this a piece of cake, see eg wikipedia: http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order

(Check the middle index for a few values of n, and you'll immediately see the pattern, eg for 1234, element 12 in a zero-indexed sequence would be 1243, and for 12345 the element 60 would be 12354.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM