473,468 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Permutations

I would like to determine nPr -- that is, the permutation of 'n' items
taken 'r' at a time preferably using the prev_permutation and
next_permutation functions of the STL. I 'googled' but could find nothing
useful.

I would appreciate any guidance or direction toward either algorithms or
source code.

Thanking you in advance.

Ed
Jul 22 '05 #1
7 4591
Ed Neukirch writes:
I would like to determine nPr -- that is, the permutation of 'n' items
taken 'r' at a time preferably using the prev_permutation and
next_permutation functions of the STL. I 'googled' but could find nothing
useful.

I would appreciate any guidance or direction toward either algorithms or
source code.


That's an interesting problem and one that is more and more likely to come
up as time goes by. npr has been kidnapped by a very popular acronym,
National Public Radio and the noise overwhelms the signal. I too was
unsuccessful in finding what you want to know via google. One of the
problems is people posting "my favorite links" pages and these people know
what you want but they also have NPR on their links. As a matter of fact I
have npr in *my* list of links; but so far I have been too shy to think that
anyone else would find my set of links interesting or useful. I am
fascinated by the potential and problems in using Google, which accounts for
this rather long post.

My favorite alternative solution is to look at a book. Your text may have a
section on probabilities and such like. I would avoid using the STL in my
solution unless I had definite contrary advice, from someone with authority,
in my program.

It's kind of cheating google, since I already had, going in, a list of math
pages. But you might try starting here.

http://mathworld.wolfram.com/topics/...tatistics.html
Jul 22 '05 #2
Ed Neukirch wrote:
I would like to determine nPr -- that is, the permutation of 'n' items
taken 'r' at a time preferably using the prev_permutation and
next_permutation functions of the STL. I 'googled' but could find nothing
useful.

I would appreciate any guidance or direction toward either algorithms or
source code.

Thanking you in advance.

Ed

Do you just want the *number* of permutations? Here:

/* Return the number of permutations of n items, taken r at a time. The
* parameters are of type N; they should never be negative. To guard
* against overflow, underflow, etc., use a type N that performs
* validation.
*/
template< typename N /* An unsigned, integer type */ >
N num_permutations( N n, N r )
{
N result = 1;

while( r != 0 )
{
result *= n;
--n;
--r;
}

return result;
}

#include <iostream>

int main( )
{
std::cout << num_permutations( 7, 5 ) << '\n'; // 2520
}

Jul 22 '05 #3
"Ed Neukirch" <eo****@localnet.com> wrote in message
news:vu************@corp.supernews.com...
I would like to determine nPr -- that is, the permutation of 'n' items
taken 'r' at a time preferably using the prev_permutation and
next_permutation functions of the STL. I 'googled' but could find nothing
useful.

I would appreciate any guidance or direction toward either algorithms or
source code.


Well, lets start with:

P(n,r) = n!/(n-r)!

( As distinct from combinations, which is without regard to the order of the
choice ie. C(n,r) = r!*P(n,r) = n!*r!/(n-r)! )

Considerations are:

- factorials get real big, real quick ( exceeding type and machine
limits )
- the order of evaluation can be important. ( truncation, overflow etc )
- you know the answer must be an integer ( use integral types )

Lets look at an example:

P(10,4) = 10!/6! = (10*9*8*7*6*5*4*3*2*1)/(6*5*4*3*2*1)
= 10*9*8*7

That should suggest a certain iterative method ( or recursive if you're
keen ).

I don't know a thing about the 'prev_permutation' and 'next_permutation'
functions of the STL. The next 'n', or the next 'r' ?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #4
"Mike Hewson" <he******@austarnet.com.au> wrote in message
news:bs************@ID-135553.news.uni-berlin.de...
( As distinct from combinations, which is without regard to the order of the choice ie. C(n,r) = r!*P(n,r) = n!*r!/(n-r)! )


Ahem...

C(n,r) = P(n,r)/r! = n!/r!(n-r)!

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #5

"Mike Hewson" <he******@austarnet.com.au> wrote in message
news:bs************@ID-135553.news.uni-berlin.de...
"Ed Neukirch" <eo****@localnet.com> wrote in message
news:vu************@corp.supernews.com...
I would like to determine nPr -- that is, the permutation of 'n' items
taken 'r' at a time preferably using the prev_permutation and
next_permutation functions of the STL. I 'googled' but could find nothing useful.

I would appreciate any guidance or direction toward either algorithms or
source code.
Well, lets start with:

P(n,r) = n!/(n-r)!

( As distinct from combinations, which is without regard to the order of

the choice ie. C(n,r) = r!*P(n,r) = n!*r!/(n-r)! )

Considerations are:

- factorials get real big, real quick ( exceeding type and machine
limits )
- the order of evaluation can be important. ( truncation, overflow etc ) - you know the answer must be an integer ( use integral types )

Lets look at an example:

P(10,4) = 10!/6! = (10*9*8*7*6*5*4*3*2*1)/(6*5*4*3*2*1)
= 10*9*8*7

That should suggest a certain iterative method ( or recursive if you're
keen ).

I don't know a thing about the 'prev_permutation' and 'next_permutation'
functions of the STL. The next 'n', or the next 'r' ?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal


Thanks for your response, but to clarify my request for information, I am
not looking for the number of permutations, but for a method to determine
the permutations themselves; for example, nPr for n = 4, r = 2 four
items taken two at a time -- string = 'abcd' would produce 'ab', 'ac',
'ad', 'ba' .... etc.

Ed
Jul 22 '05 #6
Ed Neukirch wrote:
I would like to determine nPr -- that is, the permutation of 'n' items
taken 'r' at a time preferably using the prev_permutation and
next_permutation functions of the STL. I 'googled' but could find
nothing useful.
Thanks for your response, but to clarify my request for information, I am
not looking for the number of permutations, but for a method to determine
the permutations themselves; for example, nPr for n = 4, r = 2 four
items taken two at a time -- string = 'abcd' would produce 'ab', 'ac',
'ad', 'ba' .... etc.

Perhaps you could find a function to step through all possible
combinations (nCr), then step through each combination with
std::next_permutation. Do you think this approach would be valid?

Jul 22 '05 #7
"Ed Neukirch" <eo****@localnet.com> wrote in message
news:vu************@corp.supernews.com...
Thanks for your response, but to clarify my request for information, I am
not looking for the number of permutations, but for a method to determine
the permutations themselves; for example, nPr for n = 4, r = 2 four
items taken two at a time -- string = 'abcd' would produce 'ab', 'ac',
'ad', 'ba' .... etc.


Hmmm...yes. Try the following approach ( pseuodcode ):

main_string is "abcd";

For letter_in_position_1 from start_of(main_string) thru end_of(main_string)
{
// letter_in_position_1 is 'a',b','c',d'.
sub_string1 is main_string minus letter_in_position_1
// First time thru sub_string1 is "bcd".
// Second time thru sub_string1 is "acd".
// Third time thru sub_string1 is "abd".
// Fourth time thru sub_string1 is "abc".
For letter_in_position_2 from start_of(sub_string1) thru
end_of(sub_string1)
{
sub_string2 is sub_string1 minus letter_in_position_2;
print letter_in_position_1 next to letter_in_position_2;
}
}

That's fine as it goes for the example quoted, but it doesn't readily
generalise. You need 'r' loops ( with nesting ), presumably not known at
compile time. I feel deja vu ( in other words recursion ) coming on.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
20
by: John Trunek | last post by:
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...
2
by: Alex Vinokur | last post by:
Does STL contain algorithms which generate (enable to generate) exponents, permutations, arrangements, and combinations for any numbers and words? -- Alex Vinokur mailto:alexvn@connect.to...
11
by: Girish Sahani | last post by:
Hi guys, I want to generate all permutations of a string. I've managed to generate all cyclic permutations. Please help :) def permute(string): l= l.append(string) string1 = '' for i in...
20
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
7
by: Christian Meesters | last post by:
Hi, I'd like to hack a function which returns all possible permutations as lists (or tuples) of two from a given list. So far, I came up with this solution, but it turned out to be too slow for...
1
by: JosAH | last post by:
Greetings, last week we talked a bit about generating permutations and I told you that this week will be about combinations. Not true; there's a bit more to tell about permutations and that's...
5
by: Shraddha | last post by:
Suppose we are having 3 variables...a,b,c And we want to print the permutations of these variables...Such as...abc,acb,bca...all 6 of them... But we are not supposed to do it mannually... I...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
82
by: Bill Cunningham | last post by:
I don't know if I'll need pointers for this or not. I wants numbers 10^16. Like a credit card 16 digits of possible 10 numbers, so I guess that would be 10^16. So I have int num ; These are of...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.