# Permutations of a set

Discussion in 'C++' started by John Trunek, Apr 5, 2004.

1. ### John TrunekGuest

I have a set of X items, but want permutations of length Y (X > Y). I
am aware of the permutation functions in <algorithm>, but I don't
believe this will do what I want. Is there a way, either through the
STL or some other library to do this, or do I need to write my own
code?

John Trunek, Apr 5, 2004

2. ### BusterGuest

John Trunek wrote:
> I have a set of X items, but want permutations of length Y (X > Y). I
> am aware of the permutation functions in <algorithm>, but I don't
> believe this will do what I want. Is there a way, either through the
> STL or some other library to do this, or do I need to write my own
> code?

standard). A permutation of a set S is a bijective map from S to S.
My guess is that you want the set of images of permutations of Y-
subsets of S (where S is your set of X items, and where a 'Y-subset'
of S is a subset of S which has Y elements).

You need to write your own function for this. It will be easiest
to make the function recursive, at least to begin with, I think.
Post your attempt here and I'm sure you will get help.

--
Regards,
Buster.

Buster, Apr 6, 2004

3. ### BusterGuest

Buster wrote:

> John Trunek wrote:
>
>> I have a set of X items, but want permutations of length Y (X > Y). I
>> am aware of the permutation functions in <algorithm>, but I don't
>> believe this will do what I want. Is there a way, either through the
>> STL or some other library to do this, or do I need to write my own
>> code?

>
> Your terminology is non-standard (and I'm not talking about the C++
> standard). A permutation of a set S is a bijective map from S to S.
> My guess is that you want the set of images of permutations of Y-
> subsets of S (where S is your set of X items, and where a 'Y-subset'
> of S is a subset of S which has Y elements).

s/images of permutations/permutations of images/

> You need to write your own function for this. It will be easiest
> to make the function recursive, at least to begin with, I think.
> Post your attempt here and I'm sure you will get help.
>

--
Regards,
Buster.

Buster, Apr 6, 2004
4. ### BusterGuest

Buster wrote:

> Buster wrote:
>
>> John Trunek wrote:
>>
>>> I have a set of X items, but want permutations of length Y (X > Y). I
>>> am aware of the permutation functions in <algorithm>, but I don't
>>> believe this will do what I want. Is there a way, either through the
>>> STL or some other library to do this, or do I need to write my own
>>> code?

>>
>>
>> Your terminology is non-standard (and I'm not talking about the C++
>> standard). A permutation of a set S is a bijective map from S to S.
>> My guess is that you want the set of images of permutations of Y-
>> subsets of S (where S is your set of X items, and where a 'Y-subset'
>> of S is a subset of S which has Y elements).

>
> s/images of permutations/permutations of images/

No, sorry, let it stand. I meant the images of the permutations,
viewing the permutations as functions, as I said. To be clear, these
sets, the images of the permutations of a set T (say), are what might
be called the 'results of permuting T', or just 'the permutations of T',
speaking less formally.

Another obscurity is the meaning of the word 'set' in this context.
The OP may have been referring to the standard library's 'set' class
template, as well as alluding to the set-theoretic term 'set'.

>> You need to write your own function for this. It will be easiest
>> to make the function recursive, at least to begin with, I think.
>> Post your attempt here and I'm sure you will get help.
>>

>

--
Regards,
Buster.

Buster, Apr 6, 2004
5. ### Alf P. SteinbachGuest

* Buster <> schriebt:
> John Trunek wrote:
> > I have a set of X items, but want permutations of length Y (X > Y). I
> > am aware of the permutation functions in <algorithm>, but I don't
> > believe this will do what I want. Is there a way, either through the
> > STL or some other library to do this, or do I need to write my own
> > code?

>
> Your terminology is non-standard (and I'm not talking about the C++
> standard). A permutation of a set S is a bijective map from S to S.
> My guess is that you want the set of images of permutations of Y-
> subsets of S (where S is your set of X items, and where a 'Y-subset'
> of S is a subset of S which has Y elements).

I think it's more reasonable to assume that the OP wants to generate all
possible passwords of length Y from some character set with X characters.
In which case it's combinations, not permutations. I think.

> You need to write your own function for this. It will be easiest
> to make the function recursive, at least to begin with, I think.
> Post your attempt here and I'm sure you will get help.

If assumption above is correct then the easiest is probably to just
count up; but the easiest programmatically will not be very efficient
in light of some combinations being far more probable than others.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Alf P. Steinbach, Apr 6, 2004
6. ### BusterGuest

Alf P. Steinbach wrote:

> * Buster <> schriebt:
>
>>John Trunek wrote:
>>
>>>I have a set of X items, but want permutations of length Y (X > Y). I
>>>am aware of the permutation functions in <algorithm>, but I don't
>>>believe this will do what I want. Is there a way, either through the
>>>STL or some other library to do this, or do I need to write my own
>>>code?

>>
>>standard). A permutation of a set S is a bijective map from S to S.
>>My guess is that you want the set of images of permutations of Y-
>>subsets of S (where S is your set of X items, and where a 'Y-subset'
>>of S is a subset of S which has Y elements).

>
> I think it's more reasonable to assume that the OP wants to generate all
> possible passwords of length Y from some character set with X characters.
> In which case it's combinations, not permutations. I think.

The original post doesn't suggest that to me. Looks like we'll need a

>>You need to write your own function for this. It will be easiest
>>to make the function recursive, at least to begin with, I think.
>>Post your attempt here and I'm sure you will get help.

>
> If assumption above is correct then the easiest is probably to just
> count up; but the easiest programmatically will not be very efficient
> in light of some combinations being far more probable than others.

All passwords of length Y from a character set with X characters are
equally 'probable' (think 'lottery'). In the obvious thought experiment,
each distinct password has probability 'std:ow (X, -Y);'.

--
Regards,
Buster.

Buster, Apr 6, 2004
7. ### Steven T. HattonGuest

John Trunek wrote:

> I have a set of X items, but want permutations of length Y (X > Y). I
> am aware of the permutation functions in <algorithm>, but I don't
> believe this will do what I want. Is there a way, either through the
> STL or some other library to do this, or do I need to write my own
> code?

If permutations is what you really want, it's actually quite easy. In a
word, /recursion/.
--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org

Steven T. Hatton, Apr 6, 2004
8. ### Alf P. SteinbachGuest

* Buster <> schriebt:
>
> All passwords of length Y from a character set with X characters are
> equally 'probable' (think 'lottery'). In the obvious thought experiment,
> each distinct password has probability 'std:ow (X, -Y);'.

That turns out not to be the case. Some passwords are used far more often
than others. In particular, passwords that contain English words are used
extremely more often than those that do not.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Alf P. Steinbach, Apr 6, 2004
9. ### Alf P. SteinbachGuest

* "Steven T. Hatton" <> schriebt:
> John Trunek wrote:
>
> > I have a set of X items, but want permutations of length Y (X > Y). I
> > am aware of the permutation functions in <algorithm>, but I don't
> > believe this will do what I want. Is there a way, either through the
> > STL or some other library to do this, or do I need to write my own
> > code?

>
> If permutations is what you really want, it's actually quite easy. In a
> word, /recursion/.

You can also enumerate permutations using the factorial number system
plus a bitset.

I once had a letter to the editor about that printed in Computer Language,
the now defunct magazine infamous for its shameless promotion of Jolt Cola.

fundamental ways of generating permutations) "The third way is the charm".

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Alf P. Steinbach, Apr 6, 2004
10. ### bartekGuest

(Alf P. Steinbach) wrote in
news::

> * Buster <> schriebt:
>>
>> All passwords of length Y from a character set with X characters are
>> equally 'probable' (think 'lottery'). In the obvious thought
>> experiment, each distinct password has probability 'std:ow (X,
>> -Y);'.

>
> That turns out not to be the case. Some passwords are used far more
> often than others. In particular, passwords that contain English
> words are used extremely more often than those that do not.

Not necessarily... 'qwerty' 'asdfg' and '1234' are not English words AFAIK.

bartek, Apr 6, 2004
11. ### Rolf MagnusGuest

bartek wrote:

>> That turns out not to be the case. Some passwords are used far more
>> often than others. In particular, passwords that contain English
>> words are used extremely more often than those that do not.

>
> Not necessarily... 'qwerty' 'asdfg' and '1234' are not English words
> AFAIK.
>

Some (hopefully many) systems will at least warn you that your password
is too trivial, so your examples might not be seen that often.

Rolf Magnus, Apr 6, 2004