473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Combinations problem

Hello all,
I have an array of values which I would like to generate all possible
combinations for. And example would be the values "a,b,c". This would
produce the following:

a
ab
abc
ac
b
bc
c

Does anybody know of the algorithm to produce this? I've seen the topic
discussed, but never for strings or anything of the such. Most discussion
is regarding calculating the number of combinations...which I don't care
about.

Thanks in advance,
Tom Cameron
tom<at>drdabbles<dot>us
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Nov 14 '05 #1
15 2375
what you need is a permutation algorithm. If you have a large set of
items then that means a long wait. but if your set is small then its
all wood. Just looking at the adds on the left you could check out
www.cs.sunysb.edu to learn more. Then pass your NULL terminated ( of
course) char array to the function U create. If you need more help,
just ask.....

Nov 14 '05 #2
Thomas Cameron wrote:

Hello all,
I have an array of values which I would like to generate all possible
combinations for. And example would be the values "a,b,c". This would
produce the following:

a
ab
abc
ac
b
bc
c

Does anybody know of the algorithm to produce this? I've seen the topic
discussed, but never for strings or anything of the such. Most discussion
is regarding calculating the number of combinations...which I don't care
about.

Thanks in advance,
Tom Cameron
tom<at>drdabbles<dot>us
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Nov 14 '05 #3
Thomas Cameron wrote:
I have an array of values which I would like to generate all possible
combinations for. And example would be the values "a,b,c". This would
produce the following:
a
ab
abc
ac
b
bc
c
Does anybody know of the algorithm to produce this?


I'm pretty sure this must be covered in Knuth, if not earlier
then in one of the preliminary facsicles for Volume 4.

Anyway, it is easy to figure out. Let's assume that all the
array values are distinct, or if not that you want the
duplicates that will be produced. (Otherwise, first sort
the array then walk it and collapse it down to one instance
each.) Then what you want can be characterized by: sets
such that whether or not each element is present in the set
varies through all possible combinations. That should ring
a bell: presence = 1, absence = 0 => binary integer of width
N where N = # elements. Thus all you have to do is enumerate
the distinct possibilities for an N-bit integer, which can be
done simply by counting, starting at 0. For each value of
the counter, slide a 1-bit past it, ANDing to determine
whether or not the corresponding position denotes the presence
or the absence of that element. If present, output the token;
if absent, do nothing. After the 1-bit has been slid N places,
output a newline and increment the counter. Note that this
will also output the empty string (no element present), which
is a valid combination but which you might want to eliminate
(so start the counter at 1 instead).
Nov 14 '05 #4
Thomas Cameron wrote:

Hello all,
I have an array of values which I would like to generate all possible
combinations for. And example would be the values "a,b,c". This would
produce the following:

a
ab
abc
ac
b
bc
c

Does anybody know of the algorithm to produce this? I've seen the topic
discussed, but never for strings or anything of the such. Most discussion
is regarding calculating the number of combinations...which I don't care
about.


Sounds like you need to count in binary from 1 to 2**N-1
001 = c
010 = b
011 = bc
100 = a
101 = ac
110 = ab
111 = abc
---
Fred J. Tydeman Tydeman Consulting
ty*****@tybor.com Testing, numerics, programming
+1 (775) 358-9748 Vice-chair of J11 (ANSI "C")
Sample C99+FPCE tests: http://www.tybor.com
Savers sleep well, investors eat well, spenders work forever.
Nov 14 '05 #5
I don't know if there is any classic algorithm for this, but a
quick-and-dirty approach I used once at a programming contest was to
use binary numbers as flags into the array of symbols. For the case you
mentioned:
a, b, c = 3 symbols = pow(2,3) = 8. Mathematically, the first
combination would be empty, so that would leave us 8 - 1 printable
combinations:

abc
001 = c
010 = b
011 = bc
100 = a
101 = ac
110 = ab
111 = abc

Nov 14 '05 #6
Thomas Cameron wrote:
Hello all,
I have an array of values which I would like to generate all possible combinations for. And example would be the values "a,b,c". This would
produce the following:

a
ab
abc
ac
b
bc
c

Does anybody know of the algorithm to produce this? I've seen the topic discussed, but never for strings or anything of the such. Most discussion is regarding calculating the number of combinations...which I don't care about.

Thanks in advance,
Tom Cameron
tom<at>drdabbles<dot>us
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. Sorry.


If you don't mind a recursive solution (blows up for big input sets),
here is one. This is permuting characters, as in your example, but it
could permute anything suitable modified...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void printcombos(const char *letters, int total_letters,
int current_letter, const char *prefix)
{
char *buf;
int i;

buf = malloc(strlen(letters)+ 1);
if (buf == NULL) {
puts("malloc failure");
exit(EXIT_FAILURE);
}

if (strlen(prefix) > 0) {
printf("%s\n", prefix);
}

for (i = current_letter; i < total_letters; i++) {
sprintf(buf, "%s%c", prefix, letters[i]);
printcombos(letters, total_letters, i+1, buf);
}
free(buf);
}

int main(int argc, char **argv)
{
if (argc != 2) {
puts("Need a single argument -- letters to be permuted");
exit(EXIT_FAILURE);
}

printcombos(argv[1], strlen(argv[1]), 0, "");

return 0;
}

Sample run:

temp(566)$ gcc -ansi -pedantic -g -Wall -O -o permute permute.c
temp(567)$ permute abcd
a
ab
abc
abcd
abd
ac
acd
ad
b
bc
bcd
bd
c
cd
d

-David

Nov 14 '05 #7
On Tue, 05 Apr 2005 11:38:15 -0700, David Resnick wrote:

....
Sample run:

temp(566)$ gcc -ansi -pedantic -g -Wall -O -o permute permute.c
temp(567)$ permute abcd
a
ab
abc
abcd
abd
ac
acd
ad
b
bc
bcd
bd
c
cd
d


Note you are generating a list of combinations, not permutations. You
might argue that the lines above are all permutations but as a permutation
list it is highly incomplete. You typically use all or a specific number
of the elements in a permutation list and the order matters e.g. abcd and
dcba are distinct permutations. As the subject line indicates what
you have are combinations.

Lawrence
Nov 14 '05 #8
I don't know if there's a classic optimized algorithm for this problem
but a quick and dirty approach I used once in a programming contest
consisted in using sequences of binary numbers as flags into the symbol
array.
Suppose you have an array [a, b, c], like the one you mentioned.
The number of combinations in your case is given by pow(2, 3) wich will
be equal to 8, because, mathematically, one of the combinations will be
empty, that leaves us with 7 printable combinations:
abc
1 001 = c
2 010 = b
3 011 = bc
4 100 = a
5 101 = ac
6 110 = ab
7 111 = abc

Nov 14 '05 #9
I don't know if there's a classic optimized algorithm for this problem
but a quick and dirty approach I used once in a programming contest
consisted in using sequences of binary numbers as flags into the symbol
array.
Suppose you have an array [a, b, c], like the one you mentioned.
The number of combinations in your case is given by pow(2, 3) wich will
be equal to 8, because, mathematically, one of the combinations will be
empty, that leaves us with 7 printable combinations:
abc
1 001 = c
2 010 = b
3 011 = bc
4 100 = a
5 101 = ac
6 110 = ab
7 111 = abc
Thomas Cameron wrote:
Hello all,
I have an array of values which I would like to generate all possible combinations for. And example would be the values "a,b,c". This would
produce the following:

a
ab
abc
ac
b
bc
c

Does anybody know of the algorithm to produce this? I've seen the topic discussed, but never for strings or anything of the such. Most discussion is regarding calculating the number of combinations...which I don't care about.

Thanks in advance,
Tom Cameron
tom<at>drdabbles<dot>us
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. Sorry.


Nov 14 '05 #10
Lawrence Kirby wrote:
On Tue, 05 Apr 2005 11:38:15 -0700, David Resnick wrote:

...
Sample run:

temp(566)$ gcc -ansi -pedantic -g -Wall -O -o permute permute.c
temp(567)$ permute abcd
a
ab
abc
abcd
abd
ac
acd
ad
b
bc
bcd
bd
c
cd
d
Note you are generating a list of combinations, not permutations. You
might argue that the lines above are all permutations but as a

permutation list it is highly incomplete. You typically use all or a specific number of the elements in a permutation list and the order matters e.g. abcd and dcba are distinct permutations. As the subject line indicates what
you have are combinations.

Lawrence


Yep, I used the wrong word. I meant "combinations". btw, I assumed
that the code I posted would behave badly -- as in deep recursion --
for long strings, but it is OK. The recursion depth is only the length
of the string, as I discovered by printing the current depth during a
run, guess some examination would have shown that. So it isn't a bad
solution, though the bitwise approach proposed by others seem like it
may be easier/faster. For long strings the number of combinations is
large (2^n - 1, or 2^n if the empty string is a combination). It takes
a bit over 5 minutes on my workstation to do all combinations of the
alphabet this way (a bit of a pokey machine).

-David

Nov 14 '05 #11
Eden wrote:

I don't know if there's a classic optimized algorithm for this problem
but a quick and dirty approach I used once in a programming contest
consisted in using sequences of binary numbers as flags into the symbol
array.
Suppose you have an array [a, b, c], like the one you mentioned.
The number of combinations in your case is given by pow(2, 3) wich will
be equal to 8, because, mathematically, one of the combinations will be
empty, that leaves us with 7 printable combinations:
abc
1 001 = c
2 010 = b
3 011 = bc
4 100 = a
5 101 = ac
6 110 = ab
7 111 = abc


Are you enjoying reposting this time after time, and ignoring all
the replies?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #12
Thomas Cameron <to*@drdabbles.us> writes:
I have an array of values which I would like to generate all
possible combinations for. And example would be the values
"a,b,c". This would produce the following:

a
ab
abc
ac
b
bc
c Does anybody know of the algorithm to produce this? I've seen the
topic discussed, but never for strings or anything of the such.


You mean a char array? Is it null-terminated? If so, this may do
want you want:

void f(char *s)
{
if (!*s) {
putchar('\n');
return;
f(s+1);
putchar(*s);
f(s+1);
}
urs
Nov 14 '05 #13
Thomas Cameron wrote:

Hello all,
I have an array of values which I would like to generate all possible
combinations for. And example would be the values "a,b,c". This would
produce the following:

a
ab
abc
ac
b
bc
c

Does anybody know of the algorithm to produce this?


If all the items are distinct, it is straight-forward. Assume you
have n items. Count from 0 to 2 ^ n in binary. Assign each bit
position with an item from your set. For each binary number, generate
the subset with the items corresponding to 1 bits in the number.

Thad
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Nov 14 '05 #14
Thomas Cameron wrote:
Hello all,
I have an array of values which I would like to generate all possible
combinations for. And example would be the values "a,b,c".
I hope you mean "abc" or {'a','b','c'}, otherwise some more complex
string handling will be required.
This would
produce the following:

a
ab
abc
ac
b
bc
c
what about cba, ba, cb, bca, etc? I assume you want only combinations
in which the output elements are in the order they appeared in the
input. I also assume that duplicate detection isn't required, meaning
that "aaa" => { "a", "a", "a", "aa", "aa", "aa", "aaa" }.
Does anybody know of the algorithm to produce this? I've seen the topic
discussed, but never for strings or anything of the such. Most discussion
is regarding calculating the number of combinations...which I don't care
about.


There are two common ways to design this type of algorith. One way is
to define a rule for ordering the elements of the set you wish to
produce: once you work that out it is often easy to define a production
rule which will emit the successor to any given element.

The other way is to take an easy-to-calculate series and define a
one-to-one mapping from that series to the desired output. I've used
this method here.

This C99 code will emit what you seem to be asking for, although not in
the order you showed above:

#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

void
print_combos ( size_t numelems,
const char *input
)
{
char outbuf[numelems + 1];
size_t idx;
// selector is an expensive implementation of a numelems-bit binary
// integer.
bool selector[numelems];
// We want to start at zero
memset(selector, false, numelems);
for (;;)
{
// Implement binary increment with overflow detection
bool carry;
idx = numelems;
do
{
--idx;
carry = selector[idx];
selector[idx] = !selector[idx];
} while (carry && (idx > 0));
// carry will only be true if the preceding increment overflowed
if (carry)
break;
// now copy the selected elements into the output buffer
size_t outpos = 0;
for (idx = 0; idx < numelems; ++idx)
if (selector[idx])
outbuf[outpos++] = input[idx];
// NUL-terminate it
outbuf[outpos] = '\0';
// and print it
puts(outbuf);
}
}

C90-fication is left as an exercise for the interested reader...
Nov 14 '05 #15
Thad Smith wrote:
Thomas Cameron wrote:
Hello all,
I have an array of values which I would like to generate all possible
combinations for. And example would be the values "a,b,c". This would
produce the following:

a
ab
abc
ac
b
bc
c

Does anybody know of the algorithm to produce this?

If all the items are distinct, it is straight-forward. Assume you
have n items. Count from 0 to 2 ^ n in binary. Assign each bit
position with an item from your set. For each binary number, generate
the subset with the items corresponding to 1 bits in the number.


Shuhel wrote (via email):
Hello group,
You misaddressed. Please reply to the newsgroup.
I am new here. even in programming.. please make your answer clear to
me. what you ment by distinct?


It is good form to quote the part of the post you are referring to.

By distinct, I mean that each element of the set generating the
combination is different, e.g., {a,b,c}. If there are repetitions
(elements are not distinct), such as {a,b,b,c}, you don't get 2^n
combinations, and the generation is slightly more complicated.

Thad
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Nov 14 '05 #16

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

Similar topics

0
by: suzy | last post by:
Hi, i'm from belgium, europe, and my english is not perfect, so my excuses. I ame looking for someone ho can help me with my problem. I want to compare combinations in an imputfile, and if they...
36
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
2
by: deancoo | last post by:
I've seen an algorithm for permutations, but I need to find combinations. Is there an STL equivalent to next_permutation for combinations? The material I'm finding is kinda light, if anyone would...
7
by: Micheal Artindale | last post by:
I am looking at creating list of letter combinations. letters a-h 6 letters per combination letter can repeat its self in the combination, but not next to its self and, a series of letter can...
1
by: M a n i s h | last post by:
i have been trying to build a program to find various combinations of a string. the problem is that if there are multiple similar characters in the string then the program displays multiple similar...
19
by: Thomas Cameron | last post by:
Hello all, I have an array of values which I would like to generate all possible combinations for. And example would be the values "a,b,c". This would produce the following: a ab abc ac
16
by: akameswaran | last post by:
Ok, this is really irritating me. I'm sure there are different ways of doing this - I'm interested in the algo, not the practical solution, I'm more trying to play with iterators and recursion. I...
2
by: zgfareed | last post by:
Can anyone suggest an algorithm or function to generate combinations/ permutations of a group of substrings stored in a vector. The substrings consists of 3 letters and the resulting string...
4
by: rbr | last post by:
Hello all, I have an odd requirement. I have a column with a system generated username that is a 6 character, alphanumeric, field. These usernames are randomly generated by code. I need to create a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.