Link to home
Start Free TrialLog in
Avatar of TonyNJones
TonyNJones

asked on

r-permutation

I need an algorithm that will generate, in lexicographic order, permutations of 'k' letters from 'n' objects.
e.g. 6 letters from an alphabet of 26
abcdef
abcdeg
abcdeh
.
abcdez
abcdfe
.
abcdze
etc.
Does anyone have access to a source?
Avatar of nietod
nietod

We cannot provide answers to school assignments.  That is grounds for removal from this site.  (for both you and the experts involved.)  We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?
Avatar of ozo
Can you write a routine for k=1?
Given a routine for k=1, can you write a routine for k=2?
or
Given a permutation, can you find the lexicographically next permutation?
can you count in base 26?
Let 'k = 2' and 'n = 4'.  Start off from there and then expand as you go along.
Avatar of TonyNJones

ASKER

To Nietod:
(I'm no student, Nietod, I'm 47 years old! (sometimes going on 15)) I'm trying to write a hill-climbing solution to a cipher problem and I would like to rapidly work through 6, 7, or 8 letter permutations as quickly as possible.  It's easy to write a long-winded routine:
for(a=1;a<=26;a++){
  for(b=1;b<=26.... etc and reject combinations with duplicated letters.  I wanted something more efficient..
This sequence:
BOOL NextPerm(char *OutPut)
{
int i, j, r, s, temp;

    i = 1; OutPut[c+1] = 0;
  for (i=1; i <= c; i++){ OutPut[i-1] = char(n[i]+'A'-1);
/*
      if(n[i] < 9) OutPut[i-1] = char(n[i]+'A');
      else OutPut[i-1] = char(n[i]+'B');
*/
      }
    i = c-1; while (n[i] > n[i+1]) i--;
    j = c;   while (n[i] > n[j]) j--;
    temp = n[i];
    n[i] = n[j];
    n[j] = temp;
    if(i==0) return false;
    else{
       r = c;
       s = i+1;
       while(r > s){
          temp = n[r];
          n[r] = n[s];
          n[s] = temp;
          r--; s++;
          }
       return true;
       }
}

....is excellent for perms of n letters from n, but I can't adapt this for r from n.
To Ozo:
No, no, yes.
To MDarling:
Yes - how does that help?
The above algorithm does not work coz it is based on swapping.

The comment of MDarling is very nice. Just add 1 to a number in 26-base, and check for equal digits. I would go for this one!!

Btw. I dont understand why u dont implement this so-called long-winded routine with for-statements. Makes your live a lot easier. And what about effiency??? You mean your algorithm is quicker or less typing?
ASKER CERTIFIED SOLUTION
Avatar of MDarling
MDarling

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I dont understand your answer....

The elements are from 0 to (n-1)
The permatutation is of length r.
The base of the number is n.

perm[0]=0 ... perm[r-1]=0
perm[r]=0  (carry)

NextPerm()
  do
    add = not Inc()  check for carry
    if (add)
      found = NoEqualDigits()
  while (add && !found)
 
Inc
  perm[0]++
  while (perm[i] > n-1 && i < r)
    perm[i] -= n
    perm[i+1]++
  return perm[r-1] > 0


The rest is open for you to fill in. If you closely look into my result you will notice the internally in the perm-class the n-based-number-class is used with all its methods.
BTW - I hope your not thinking of running through 8! combinations and
decrypting with each one in turn as
that could take a lot (a lot!) of time.

Im writing a Playfair hillclimber.

The way I'm approaching this is to generate an n number of random squares
and decrypt with each square.

Im using a trigraph frequency table
to score each decryption.  If they
meet a minimum standard then they live to the next generation.  Otherwse they
die and are replaced with other random squares.

The ones that passed the scoring test
get perturbed a little by some random
swapping.  Then we begin again hopefully
getting higher scores each generation.

regards,
Mike.

Mike:
Yes, I admit it, #6 of Mr Singh's problems has my attention at the moment. I tried your approach of random squares and keeping the promising ones (I put limits on the frequencies of the rare, medium, and high letters (in English - ?)) to manipulate further.  The trouble was, they seemed to reach a limit very quickly. ie they seemed to be blind-alleys.  Maybe I'll revisit it.
The seven-letter permutations as the keyword took about 36hrs to run using Delphi Pascal. I sort of figured that with a better algorithm in C++, I could squeeze an 8-letter perm into a week(?!). (Just shows how crazy one gets trying to solve these things).
Tony
If you haven't solved stage 6 yet than
I can tell you i solved stage 6 using the playfair.exe which can be found at the cypher challenge files section.

It's quite good once you get past the clunky old fashioned UI.  Start putting
common letter combinations into it
and see if what results looks like it
could be good text.

Don't get bogged down looking for
monogram frequencies or even digraph frequencies.

The best way to score this problem
is to use trigraph frequency tables
(so I'm told by successful hillclimber writers).

You can get etext versions of many books online.  You can use these to
generate trigraph frequency tables.

Put them in a binary tree with their
frequency counts and you've got a good
dictionary for trigraph frequencies.

here's a link to bram stoker's dracula in etext form...

http://sailor.gutenberg.org/by-title/xx444.html

if you need any further assistance or
wish to swap ideas/code you can email
me at mdarling@zoom.co.uk.

regards,
Mike.


Thanks for the ideas. I'll let you know...