does anyone have a repeat permutation program?

C

Camel

This program generates a list of repeat permutation result. For example, if
given a string "abc", the program prints all the possible permutations like:
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
....

totally has 27 combinations. I know recursion should be used but don't know
how.

Thanks in advance
 
S

Stefan Rybacki

Peter said:
[...]
totally has 27 combinations. I know recursion should be used but don't
know
how.

Just curious...are you and Michalik taking the same class? :)

Sounds like homework to me, too.
As far as the question goes, it would be cheating you to actually give
the exact answer. But consider this:

-- if in your method you pick one character from the string, and
then combine that character with all the permutations of the original
string without that character, you get all the possible permutations.

Not quite. As far as I understand this you mean: "abc" take "a" leaving "bc" to
permutate which leads to:

a bc
a b c
a c b

b ac
b a c
b c a

c ab
c a b
c b a

That would be true in my understanding of permutation as well but since the OP
gave an example where aaa , bbb and so on are also in the list of permutations.
This would actually mean to rephrase your requirement as follows:

-- if in your method you pick one character from the string, and
then combine that character with all the permutations of the original
string INCLUDING that character limiting the permutation result length to the
count of unique characters of the original string, you get all the possible
permutations.


And again, I rather see a permutation of a string like Pete defined than being
basically a permutation of a specific length given a specific alphabet. What is
actually needed is up to the OP.

Stefan
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Camel schreef:
This program generates a list of repeat permutation result. For example, if
given a string "abc", the program prints all the possible permutations like:
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
...

totally has 27 combinations. I know recursion should be used but don't know
how.

If you used Google, you would probably find my code at Roedy’s site. I
won’t give it to you explicitly, makes it too easy :p

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iEYEARECAAYFAkj+3eQACgkQBGFP0CTku6OmnQCgljmhbDsCI7Nsi6a70FEvXbxv
sqIAnRwqnVNeTXenyQuTaY4A0FZ1uYA0
=nW5i
-----END PGP SIGNATURE-----
 
J

John B. Matthews

Hendrik Maryns said:
Camel schreef:
This program generates a list of repeat permutation result. [...] I
know recursion should be used but don't know how.

If you used Google, you would probably find my code at Roedy’s site. I
won’t give it to you explicitly, makes it too easy :p

An exemplary implementation!

My favorite recursive permutation algorithm appeared in the original
edition of Niklaus Wirth's _Algorithms_+_Data_Structures_=_Programs_.
After discussing recursion, exercise 3.2 asks the student to "Write a
procedure that generates all n! permutations of n elements a1, ... , an
in situ, i.e., without the aid of another array." In the following
chapter on tree structures, the sample output of a cross-reference
program is none other than the required permute program itself!

The Oberon version of the text has the same exercise; but alas, the
cross-reference program uses a different example:

<http://www-old.oberon.ethz.ch/WirthPubl/AD.pdf>

An Ada implementation appears on my website, although it's a jumble.
 
T

Tom Anderson

[...]
totally has 27 combinations. I know recursion should be used but don't know
how.

Just curious...are you and Michalik taking the same class? :)

As far as the question goes, it would be cheating you to actually give the
exact answer. But consider this:

-- if in your method you pick one character from the string, and then
combine that character with all the permutations of the original string
without that character, you get all the possible permutations.

-- if you are asked to permute a string that is only one character long,
there is only one permutation

Those two facts together should suggest a possible recursive approach. That
is, you've got one statement that provides a "base case" solution, and
another statement that defines the problem in terms of itself, but reducing
the complexity in a well-defined way before referring to itself. That's
basically recursion in a nutshell. :)

I was thinking about how you could do this non-recursively and without
using a stack. The set of all permutations of a sequence (such as a
string) constitutes a group under composition of permutations. Is there
some bit of group theory we can use to efficiently enumerate the elements
of the group? I was thinking that if there was a generator, you could just
start with that, and compute powers of it to run through the whole group.
However, a symmetric group (which is what the group of all permutations of
a set is called) doesn't have a single generator; it has a generating set
consisting of two permutations - one which swaps the first two elements,
and one which barrel shifts all elements one place. What i don't know is
how you combine these to enumerate all the elements of the group.

Another approach would be to map each permutation onto a number such that
the numbers form a continuous range, then walk through that range and
reverse the mapping. That's easy enough: put the members of the set to be
permuted in order, then go through the permutation, finding the element
picked by each position of the permutation, recording its index in the
ordered set, and then removing it from the set. Then compute the product
of each index with the number of elements in the set before each removal.
That description doesn't make any sense, does it? Consider the
permutation:

<B A D C>

The set is :

{A B C D}

So the first position, B, gets index 1, for 4 elements. You then have
remaining:

{A C D}

So the second position, A, comes out as 0, of 3 elements. The it's:

{C D}

Position three is D, which is 1 of 2. Then it's just:

{C}

And C is 0 of 1. The products are:

(1 * 4) + (0 * 3) + (1 * 2) + (0 * 1) = 6

To decode this to a permutation, you do it all in reverse, modding out
indices with a decrementing modulus starting at the size of the set. Once
you have the indices, you do a draw-without-replacement from the ordered
set to assemble the permutation.

This avoids any recursion, but i can't see a terrifically efficient way to
implement it.

However, this only works up to sets of 12 elements, at which point there
are more than 2**31 permutations, and the number overflows, at least if
you're using ints. Using longs gets you up to 21 elements. Although,
whilst that doesn't seem a lot, you're never going to need more - any plan
which involves going through all possible permutations more than 21
elements is a non-starter, because it would take forever.

(I know the OP wasn't actually asking about permutations - but this is
more interesting!)

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top