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

Home Posts Topics Members FAQ

permutations and combinations

Hi.

I realize this might be more of a "figure out the algorithm" thing than
strictly an std question.. sorry if it is off topic? It certainly was in the
other group!

Also, I'm pretty old, not in school.. just trying to figure this out., so it
isn't an assignment.
I see you can use next_permutation to find the permutations of a string of a
given length.

Right now I'm using a string, sorting it and passing it repeated into
next_permutation.

Is there any slick way to find all of them of any length, i.e. given a string
"dear" to get all of all lengths including:

a
ra
are
ear
dear
read
dare

I'm trying to figure out an algorithm to find all possible words from a set of
strings, without using any letters any more than they are in the string, i.e.
given lott you would not come up with "loot"

Thanks..
Jeff Kish
Jul 23 '05 #1
9 3116
Jeff Kish wrote:
I realize this might be more of a "figure out the algorithm" thing than
strictly an std question.. sorry if it is off topic? It certainly was in the
other group!

Also, I'm pretty old, not in school.. just trying to figure this out., so it
isn't an assignment.
I see you can use next_permutation to find the permutations of a string of a
given length.

Right now I'm using a string, sorting it and passing it repeated into
next_permutation.
Does it work?

Is there any slick way to find all of them of any length, i.e. given a string
"dear" to get all of all lengths including:

a
ra
are
ear
dear
read
dare

I'm trying to figure out an algorithm to find all possible words from a set of
strings, without using any letters any more than they are in the string, i.e.
given lott you would not come up with "loot"


There is no standard function for it, so you'd need to write a simple "get
me the next M from the set of N using the previous M" function. After you
have the subset, use std::next_permutation to enumerate all possibilities.
So, you'll have nested loops, one for lengths from 1 to strlen(yourstr),
and the other inside for the subsets, and inside that -- for permutations.

And, yes, it's not strictly a C++ question.

V
Jul 23 '05 #2
In article <tm********************************@4ax.com>,
Jeff Kish <je*******@mro.com> wrote:
Hi.

I realize this might be more of a "figure out the algorithm" thing than
strictly an std question.. sorry if it is off topic? It certainly was in the
other group!

Also, I'm pretty old, not in school.. just trying to figure this out., so it
isn't an assignment.
I see you can use next_permutation to find the permutations of a string of a
given length.

Right now I'm using a string, sorting it and passing it repeated into
next_permutation.

Is there any slick way to find all of them of any length, i.e. given a string
"dear" to get all of all lengths including:

a
ra
are
ear
dear
read
dare

I'm trying to figure out an algorithm to find all possible words from a set of
strings, without using any letters any more than they are in the string, i.e.
given lott you would not come up with "loot"


There's a closely related article in the Feb. '05 issue of C/C++ Users
Journal by John Dibling titled "Extending the STL".

In this article he defines a generic algorithm:

template <class BidirectionalIterator, class Function, class Size>
Function
for_each_permutation(BidirectionalIterator first,
BidirectionalIterator last,
Size k,
Function fn);

The algorithm is documented to come from "The Art of Computer
Programming," Vol. 1, p. 45, Method 1.

for_each_permutation calls fn for each permutation of last-first items
taken k at a time:

fn(first, last)

I think this is a great algorithm, except that I think it would be
better if the Function were called with only the first k elements of the
permuted sequence, else you have to store k in fn:

fn(first, first+k)

Inspired by John's article, I wrote my own version of
for_each_permutation, which uses a slightly modified copy of
std::next_permutation to take into account that you sometimes only want
the items taken k at a time. However one could easily modify John's
publicly available listing to call fn(first, first+k) instead of
fn(first, last). Note, you might want to use std::advance where I've
used "+" to avoid requiring random access iterators.

At any rate, once you have this tool, your original problem can be
solved quite elegantly:

Create a printing functor:

class print
{
public:
print() : i_(0) {}

template <class It>
void operator()(It first, It last)
{
typedef typename std::iterator_traits<It>::value_type value_type;
std::ostream_iterator<value_type> out(std::cout, " ");
std::cout << ++i_ << "\t: ";
std::copy(first, last, out);
std::cout << '\n';
}
private:
unsigned i_;
};

And then call for_each_permutation with this functor and for each
permutation length you're interested in (1 to 4):

print p;
for (unsigned k = 1; k <= size_array; ++k)
p = for_each_permutation(array, array + size_array, k, p);

The printing functor keeps a simple state which is just the line number.
Note that by passing the same functor back in, and catching it from the
return, the line number is kept up to date. My output looks like:

1 : a
2 : d
3 : e
4 : r
5 : a d
6 : a e
7 : a r
8 : d e
....

Kudos to John Dibling for suggesting the for_each_permutation algorithm.
I think for_each_combination would also be a valuable tool in the box:

template <class BidirectionalIterator, class Size,
class Functor>
Functor
for_each_combination(BidirectionalIterator first,
BidirectionalIterator last, Size k,
Functor f);

template <class BidirectionalIterator, class Size,
class Functor, class Compare>
Functor
for_each_combination(BidirectionalIterator first,
BidirectionalIterator last, Size k,
Functor f, Compare comp);

-Howard
Jul 23 '05 #3

"Howard Hinnant" <hi*****@metrowerks.com> wrote in message news:hi***************************@syrcnyrdrs-01-ge0.nyroc.rr.com...
[snip]
There's a closely related article in the Feb. '05 issue of C/C++ Users
Journal by John Dibling titled "Extending the STL".

In this article he defines a generic algorithm:

template <class BidirectionalIterator, class Function, class Size>
Function
for_each_permutation(BidirectionalIterator first,
BidirectionalIterator last,
Size k,
Function fn);

The algorithm is documented to come from "The Art of Computer
Programming," Vol. 1, p. 45, Method 1.
Is there any link to that?

[snip]
Inspired by John's article, I wrote my own version of
for_each_permutation


Could one see your algorithm?

[snip]

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Jul 23 '05 #4
In article <d0**********@news.wplus.net>,
"Alex Vinokur" <al****@big-foot.com> wrote:
"Howard Hinnant" <hi*****@metrowerks.com> wrote in message
news:hi***************************@syrcnyrdrs-01-ge0.nyroc.rr.com...
[snip]
There's a closely related article in the Feb. '05 issue of C/C++ Users
Journal by John Dibling titled "Extending the STL".

In this article he defines a generic algorithm:

template <class BidirectionalIterator, class Function, class Size>
Function
for_each_permutation(BidirectionalIterator first,
BidirectionalIterator last,
Size k,
Function fn);

The algorithm is documented to come from "The Art of Computer
Programming," Vol. 1, p. 45, Method 1.
Is there any link to that?


Yes: www.cuj.com

You'll have to navigate through their website to find code downloads. A
direct link to their downloads page will just redirect you back to their
homepage.
[snip]
Inspired by John's article, I wrote my own version of
for_each_permutation


Could one see your algorithm?


Sorry, no. For now it must be considered my employer's IP. And
besides, I'm not at all sure the algorithm can't be improved over what I
did, and I'd hate to stifle such work.

The most I can say about it is what I already did: It was a minor
modification of the std::next_permutation that ships with Metrowerks.
And that algorithm is very close to the original HP algorithm which is:

rfind the first pair of adjacent elements in sorted order, and mark the
first of that pair with iterator i.

If no such adjacent elements are found, reverse the list and return
false (no more permutations).

rfind first element greater than *i, mark it with iterator j.

Exchange the elements referred to by i and j, and then reverse the
subrange [i+1, last). Return true.

-Howard
Jul 23 '05 #5
On Mon, 07 Mar 2005 16:28:12 GMT, Howard Hinnant <hi*****@metrowerks.com>
wrote:
In article <tm********************************@4ax.com>,
Jeff Kish <je*******@mro.com> wrote:
Hi.

I realize this might be more of a "figure out the algorithm" thing than
strictly an std question.. sorry if it is off topic? It certainly was in the
other group!

Also, I'm pretty old, not in school.. just trying to figure this out., so it
isn't an assignment.
I see you can use next_permutation to find the permutations of a string of a
given length.

Right now I'm using a string, sorting it and passing it repeated into
next_permutation.

Is there any slick way to find all of them of any length, i.e. given a string
"dear" to get all of all lengths including:

a
ra
are
ear
dear
read
dare

I'm trying to figure out an algorithm to find all possible words from a set of
strings, without using any letters any more than they are in the string, i.e.
given lott you would not come up with "loot"
<snip>Programming," Vol. 1, p. 45, Method 1.

for_each_permutation calls fn for each permutation of last-first items
taken k at a time:

fn(first, last)

I think this is a great algorithm, except that I think it would be
better if the Function were called with only the first k elements of the
permuted sequence, else you have to store k in fn:

fn(first, first+k)

Inspired by John's article, I wrote my own version of
for_each_permutation, which uses a slightly modified copy of
std::next_permutation to take into account that you sometimes only want
the items taken k at a time. However one could easily modify John's
publicly available listing to call fn(first, first+k) instead of
fn(first, last). Note, you might want to use std::advance where I've
used "+" to avoid requiring random access iterators.

At any rate, once you have this tool, your original problem can be
solved quite elegantly:
<snip>

-Howard

Thanks.
I don't know if I'm up to extending the stl though. My skills are lacking a
bit in this area.
I appreciate the information, and I'll look it over.

Jeff Kish
Jul 23 '05 #6
While we're on the subject;

Does anyone know where to find a spell checker complete with dictionary in
c/c++ source or LIB files?

Thanks a bunch.
Jul 23 '05 #7
DHOLLINGSWORTH2 wrote:
While we're on the subject;

Does anyone know where to find a spell checker complete with dictionary in
c/c++ source or LIB files?


The web?
Jul 23 '05 #8
DHOLLINGSWORTH2 wrote:
Does anyone know where to find a spell checker complete with dictionary in
c/c++ source or LIB files?


maybe those are useful:

aspell
ispell

Michael


Jul 23 '05 #9
re: aspell, ispell

Cool, Thanks.

Dan
Jul 23 '05 #10

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. ...
1
by: kpp9c | last post by:
Greetings, I am working on a program to produce patterns. What would like is for it to exhaustively produce all possible permutations of a sequence of items but for each permutation produce...
4
by: Bernard A. | last post by:
hello, i'm looking for a way to have all possible length fixed n-uples from a list, i think generators can help, but was not able to do it myself, maybe some one could point me out to an idea to...
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...
1
by: Jose | last post by:
Hi all, I have a select list that can contain between 1 and N items. I would like to get all possible permutations of the list, i.e. combinations that a user might select. So for example if my...
1
by: brendan | last post by:
Hi, I'm not at all competent in ms-sql, nor vb, as we work in Oracle and Mysql .... however, we need to port a couple of db queries to ms-access (2000) and I'm having a heck of a time trying to...
0
by: JosAH | last post by:
Greetings, last week I stated that a lot of topics in a lot of forums mention all sorts of sorting problems. Last week's tip gave an answer to quite a bit of those sorting problems. Another...
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...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.