I read this post which is quite close to the problem I’m having, but couldn’t generalize it.
I’m trying to solve the Traveling Sales Person by searching all paths using multiple CPU’s.
What I need, is a way to encode a path prefix to integer and distribute it to each CPU so it would know what paths it’s supposed to scan.
For example, if the number of cities is 10 one possible 3-prefix (suppose prefix length is fixed and known) is 4-10-3 (there are 10*9*8 prefixes), so the CPU which would receive it, would search all paths that begin with 4-10-3.
Since the number of cities is quite large, I can’t compute n! thus I can’t use the post above.
I read this post which is quite close to the problem I’m having, but
Share
The standard representation of a permutation as a number uses Lehmer codes represented in the factorial number system. The idea is that every permutation of n elements can be mapped to a sequence of n numbers, the first of which is in the range 0 to (n – 1), the second of which is in the range 0 to (n – 2), etc. This sequence of numbers can then be represented as a single integer in the factorial number system.
I believe that it should be possible to adapt this trick to work with prefixes of permutations rather than entire permutations. Suppose that you have n elements and want to choose a permutation of k of them. To do this, start off by computing the Lehmer code for the partial permutation. Instead of getting a sequence of n numbers, you’ll get back a sequence of k numbers. For example, given the partial permutation
c a ddrawn froma b c d e f g, your Lehmer code would be found as follows:cis the second (zero-indexed) element ofa b c d e f gais the zeroth (zero-indexed) element ofa b d e f gdis the first (zero-indexed) element ofb d e f gSo the Lehmer code would be
(2, 0, 1).Once you have this Lehmer code, you can try to encode it as a single integer. To do this, you can use a modified factorial number system encoding. Specifically, you can try doing the following. If you have n elements and want a permutation of k of them, then there will be a total of (n – k + 1) possible choices for the very last element. There are a total of (n – k + 2) possible choices for the second-to-last element, (n – k + 3) possible choices for the third-to-last element, etc. Consequently, you could take your Lehmer code and do the following:
4 …
This produces a unique integer code for the permutation.
For example, our Lehmer code was
(2, 0, 1), n = 7, and k = 3. Therefore, we’d computeTo invert this process, you can take the integer and run it backwards through this procedure to recover the partial Lehmer code. To do this, start off by taking the number and dividing by (n – k + 1)(n – k + 2)…(n – 1) to get back the very first digit of the Lehmer code. Then, mod the number by (n – k + 1)(n – k + 2)…(n – 1) to drop off the first digit. Then, divide the number by (n – k + 1)(n – k + 2)…(n – 2) to get back the second digit of the Lehmer code, then mod by (n – k + 1)(n – k + 2)…(n – 2) to drop off the second digit. Repeat this until all the digits of the Lehmer code have been reconstructed.
For example, given prefix 61, n = 7, and k = 3, we would start off by dividing 61 by 7 × 6 = 30. This gives 2, remainder 1. Thus the first digit of the Lehmer code is 2. Modding by 30, we get back the number 1. Next, we divide by 6. This gives 0, remainder 1. Thus the second digit is 0. Finally, we read off the remaining number, which gives the last digit of the Lehmer code, 1. We have recovered our Lehmer code
(2, 0, 1), from which we can easily reconstruct the permutation.Hope this helps!