473,666 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combination problem - fast algorithm

I have this problem.

I'm trying to get all the valid combination C(N,K) that pass one
condition.

For example C(5,3) can use in any combination this numbers {1,2,3,4,5}

But let's say that I have the limitations that these pair of numbers
cannot be part of the same combination {2,3} and {4,5}

So the right soolution are only :
{1,2,4}
{1,2,5}
{1,3,4}
{1,3,5}

My sollution for a test of C(20,13) with 7 pairs of numbers took me
about 25s, but it's too much for my needs.
Does anyone has a better algorithm ?

Apr 28 '06 #1
6 4517
aka_eu opined:
I have this problem.

I'm trying to get all the valid combination C(N,K) that pass one
condition.

For example C(5,3) can use in any combination this numbers
{1,2,3,4,5}

But let's say that I have the limitations that these pair of numbers
cannot be part of the same combination {2,3} and {4,5}

So the right soolution are only :
{1,2,4}
{1,2,5}
{1,3,4}
{1,3,5}

My sollution for a test of C(20,13) with 7 pairs of numbers took me
about 25s, but it's too much for my needs.
Does anyone has a better algorithm ?


Most certainly, or at least a faster machine. However, it's impossible
to say with absolute certainty since you didn't show us yours. Or do
you expect us to guess?

In any case, general algorithms are the stuff of comp.programmin g.
Generally, optimisations are not discussed in comp.lang.c, either, but
if you show us some code, someone may bite.

--
If God had intended Man to Watch TV, He would have given him Rabbit
Ears.

<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

Apr 28 '06 #2
My algorithm is the most simply one

for(p1=1;p1<=5; p1++)
for(p2=p1+1;p2< =5;p2++)
for(p3=p2+1;p3< =5;p3++)
if (CondtionIsTrue )
printf(p1,p2,p3 );

Apr 28 '06 #3


aka_eu wrote On 04/28/06 09:49,:
I have this problem.

I'm trying to get all the valid combination C(N,K) that pass one
condition.

For example C(5,3) can use in any combination this numbers {1,2,3,4,5}

But let's say that I have the limitations that these pair of numbers
cannot be part of the same combination {2,3} and {4,5}

So the right soolution are only :
{1,2,4}
{1,2,5}
{1,3,4}
{1,3,5}

My sollution for a test of C(20,13) with 7 pairs of numbers took me
about 25s, but it's too much for my needs.
Does anyone has a better algorithm ?


No. The word "better" implies a comparison between
two things, in this case two algorithms, and since you
have not revealed your algorithm there is no way to compare
another one to it.

Even after you've disclosed your code, there's no way
to be 100% some alternative would be faster other than to
write it up and try it. The language itself has no notion
of the speed of any construct or combination of constructs,
and the actual speed of some piece of code will vary from
one implementation to another, sometimes widely.

That said, your current code gets through only 3100
combinations per second, a rate that might have been pretty
good fifty years ago but looks astonishingly slow today.
There's an excellent chance that your super-secret algorithm
is doing something silly, and that someone will be able to
suggest significant improvements if you take the covers off.

--
Er*********@sun .com

Apr 28 '06 #4
On Fri, 28 Apr 2006 06:49:54 -0700, aka_eu wrote:
I have this problem.

I'm trying to get all the valid combination C(N,K) that pass one
condition.

For example C(5,3) can use in any combination this numbers {1,2,3,4,5}

But let's say that I have the limitations that these pair of numbers
cannot be part of the same combination {2,3} and {4,5}

So the right soolution are only :
{1,2,4}
{1,2,5}
{1,3,4}
{1,3,5}

My sollution for a test of C(20,13) with 7 pairs of numbers took me
about 25s, but it's too much for my needs.
Does anyone has a better algorithm ?


As noted elsewhere, this is off topic.

In the case that you have k pairs (and no number occurs in more than one
pair) and you want to split the numbers up into one subset of k numbers
and the rest -- as both your examples -- the k element subset must
contain exactly one number from each pair (otherwise we would have a pair
in the same subset). So there are 2^k such splittings; you could get them
all by running through all k bit numbers, and for each such number x, and
i = 0..k-1, choose the the first element of the i'th pair to be in the
k-element subset if the i'th bit of x is clear, otherwise choose the
second element.
Duncan

Apr 28 '06 #5
aka_eu wrote:
My algorithm is the most simply one

for(p1=1;p1<=5; p1++)
for(p2=p1+1;p2< =5;p2++)
for(p3=p2+1;p3< =5;p3++)
if (CondtionIsTrue )
printf(p1,p2,p3 );


Please review the information below.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 28 '06 #6

"aka_eu" <ak****@yahoo.c om> wrote in message
news:11******** **************@ e56g2000cwe.goo glegroups.com.. .
I have this problem.

I'm trying to get all the valid combination C(N,K) that pass one
condition.

For example C(5,3) can use in any combination this numbers {1,2,3,4,5}

But let's say that I have the limitations that these pair of numbers
cannot be part of the same combination {2,3} and {4,5}

So the right soolution are only :
{1,2,4}
{1,2,5}
{1,3,4}
{1,3,5}

My sollution for a test of C(20,13) with 7 pairs of numbers took me
about 25s, but it's too much for my needs.
Does anyone has a better algorithm ?


Maybe. (I'll post some code below.) I pulled out my probability and
statistics textbook from years ago. If there was a quick answer in there, I
didn't find it.

The first problem I see is how to quickly determine if {2,3}or {4,5} is in
the generated set:

{1,2,3}
{1,2,4}
{1,2,5}
{1,3,4}
{1,3,5}
{1,4,5}
{2,3,4}
{2,3,5}
{2,4,5}
{3,4,5}

{2,3} could be the 2nd & 3rd values or the 1st & 2nd. This gets worse for
C(20,13). {14,15} could be in six(?) different positions. This would
suggest fixing the positions, i.e., 2 always in the 2nd position:

{1,2,3,0,0}
{1,2,0,4,0}
{1,2,0,0,5}
{1,0,3,4,0}
{1,0,3,0,5}
{1,0,0,4,5}
{0,2,3,4,0}
{0,2,3,5,0}
{0,2,0,4,5}
{0,0,3,4,5}

Unfortunately, this would also cause unnecessary additional looping to
search for valid non-zero values, but it has the advantage of easily
determining if {2,3} or {4,5} is in the set since the positions are fixed.
So, can we get the benefits of this without the disadvantages? Yes. We can
use one array with fixed positions as indicators, and another array for the
set of values:

{1,2,3} {1,1,1,0,0}
{1,2,4} {1,1,0,1,0}
{1,2,5} {1,1,0,0,1}
{1,3,4} {1,0,1,1,0}
{1,3,5} {1,0,1,0,1}
{1,4,5} {1,0,0,1,1}
{2,3,4} {0,1,1,1,0}
{2,3,5} {0,1,1,0,1}
{2,4,5} {0,1,0,1,1}
{3,4,5} {0,0,1,1,1}

Now we have one array with the valid values and a quick way to exclude
unwanted sets. This code has an #if directive that allows you to see the
entire computation or just the valid sets (change from '#if 0' to '#if 1').

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

#define N 5
#define K 3

unsigned long val[K];
unsigned char ind[N+1]; /* "boolean" */
void cnk(void)
{
unsigned long p1,p2,p3;
unsigned long i;
unsigned char exclude;

memset(ind,0,N) ;
for(p1=1;p1<=N; p1++)
{
val[0]=p1;
ind[p1]=1;
for(p2=p1+1;p2< =N;p2++)
{
val[1]=p2;
ind[p2]=1;
for(p3=p2+1;p3< =N;p3++)
{
exclude=0;
val[2]=p3;
ind[p3]=1;
#if 0
/* change 0 to 1 for full complete info */
for(i=0;i<K;i++ )
printf("%ld ",val[i]);
printf(" ");
for(i=0;i<N;i++ )
printf("%d ",ind[i]);
if(ind[2]&&ind[3])
printf("*");
if(ind[4]&&ind[5])
printf("*");
printf("\n");
#else
/* only print reduced sets */
if(ind[2]&&ind[3]) /* eliminate {2,3} */
exclude=1;
if(ind[4]&&ind[5]) /* eliminate {4,5} */
exclude=1;
if(!exclude)
{
for(i=0;i<K;i++ )
printf("%ld ",val[i]);
printf("\n");
}
#endif
ind[p3]=0; /* clear indicator */
}
ind[p2]=0; /* clear indicator */
}
ind[p1]=0; /* clear indicator */
}
}

int main(void)
{
cnk();

return(EXIT_SUC CESS);
}
HTH,

Rod Pemberton


Apr 30 '06 #7

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

Similar topics

1
3964
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of size n. There are n!/(s!(n-s)!) ways to do this. Donald E. Knuth gives several methods (algorithms) to generate all the s-combinations in . In such procedure-oriented way, each s-combination is processed while it's being generated. In some
2
8420
by: Ross | last post by:
Is there any combination parsing algorithm/source code available to handle the combination problem of inputting a sequence: YGEQLGMREMVRNCMQDL and generating sequences: YGEQLGMREMVRNCMQDL YGE QLGMREMVRNCMQDL YGE QLGMREMVR NCMQDL
3
3176
by: AsuWoo | last post by:
hi, I want to implement a function that prints all possible combinations of a characters in a string,eg. input "123"into a textbox, add "1","2","3","12","13","23","123",to a listbox,Or "ab" into a textbox,add "a","b","ab"in a listbox,could anyone help me out ?
2
1582
by: Peter Schmitz | last post by:
Hi again, I just need to write the following function to search in a binary buffer for a given pattern: BOOL CheckBufferForPattern(BYTE *buffer,int bufferlen,BYTE *pattern, int patternlen, BOOL casesensitive). What's the best/fastest algorithm for a usual buffer size of 1500bytes or so? Is there any source code available for this?
20
2127
by: pratap | last post by:
Could someone clarify how could one reduce the size of an executable code during compile time. Could one use specific compile time flag with makefile or is it advisable to go for dynamic linking. The idea here is that smaller the code size the faster is the code. Is Dynamically linked executable really faster than a single executable file which is not linked dynamically.? Is there any performance measuring metrics on gcc version 3.2.2
17
8087
by: DanielJohnson | last post by:
how to use the combination function in python ? For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36 Please help, I couldnt find the function through help.
6
4085
by: pj | last post by:
Hi, I 'm currently writing a program that performs transliteration (i.e., converts greek text written using the english alphabet to "pure" greek text using the greek alphabet) as part of my thesis. I have decided to add the capability to convert words using some sort of lookup algorithm as a sidekick to the "normal" conversion algorithm, and here it starts getting interesting. I want to find an algorithm that satisfies these...
19
5410
by: Juha Nieminen | last post by:
If I'm not completely mistaken, the only reason why std::list::size() may be (and usually is) a linear-time operation is because they want std::list::splice() to be a constant-time operation, and if you execute the latter, the size of the resulting lists cannot be known without explicitly counting the sizes of the new lists. I was thinking: What if size() was an O(n) operation only *after* a splice() operation has been performed (and...
9
3747
by: paragpdoke | last post by:
Hello All. I'm looking for some algorithm to build a combination of strings from multiple arrays. Let me explain in detail. - I'm working on VBA (excel). I have functions that accept one string and return a collection. These get listed in columns in excel (one column per call to the function). - This results into arrays of strings in different columns. Unfortunately, the number of strings in an array is unknown at design time (these get...
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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 we have to send another system

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.