943,781 Members | Top Members by Rank

Ad:
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 28th, 2008
0

Re: The fastest combination generator

Hi there,

I have done a bit of reading on this topic and I know there are some research papers on the topic of multiprocessor enumeration optimisation.

for example one is:
'A Parallel Algorithm for Enumerating Combinations', By Martha Torres, Alfredo Goldman,
Junior Barrera

good luck.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
androidandrew is offline Offline
2 posts
since Jan 2008
Jan 28th, 2008
0

Re: The fastest combination generator

oh sorry i forgot to mention that was in

'Proceedings of the 2003 International Conference on Parallel Processing'

i think IEEE Computer Society. I found it using my uni journal sources so if you know someone with access to a similar source then you should be able to find it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
androidandrew is offline Offline
2 posts
since Jan 2008
Jan 30th, 2008
0

Re: The fastest combination generator

Here is a link for the source if someone interesting in paralel processing

Link
wwx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wwx is offline Offline
2 posts
since Dec 2007
Jan 31st, 2008
0

Re: The fastest combination generator

Here's an impressive source of many different combination algorithms. They're documented in a book called "Algorithms for Programmers" by Jörg Arndt. It will be published later this year, but as of now, you can download the PDF.
http://www.jjj.de/fxt/#fxtbook

I will certainly buy this book when it's published!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shaunwilliams is offline Offline
1 posts
since Jan 2008
Feb 1st, 2008
0

Re: The fastest combination generator

Click to Expand / Collapse  Quote originally posted by ithelp ...
It is an NP complete problem , so you can only get some approximation algorithm to get it faster.
No it's not. Learn what NP complete means. Learn what NP means. This problem isn't even in NP.
Reputation Points: 98
Solved Threads: 22
Posting Whiz in Training
sarehu is offline Offline
269 posts
since Oct 2007
Sep 10th, 2008
0

Re: The fastest combination generator

the code is not correct. If u enter n= 5 and k=5 it should generate only one combination
but generating lot many.
nCr = !n/(!r*!(n-r))




Click to Expand / Collapse  Quote originally posted by iamthwee ...
Well unless you have access to a quantum computer I doubt you will be able to cut down on run time. Knuth's work is generally accepted as being the best for algorithms and reducing time complexity.

However, you may be looking at your problem in the wrong way. Take for example the classic travelling salesman problem.

Applying a brute force algorithm to calculate the shortest route for a journey with 100+ pit-stops will take an unfathomable amount of time.

However, if you adopt a heuristic approach you can find a high probabilistic solution,within a much smaller time scale.

Perhaps you need to think about approaching your problem from a different angle.

See this:

http://en.wikipedia.org/wiki/Dynamic_programming

Incase anyone wanted to know here is Knuth's algorithm for producing combinations in C.

 
/* Algorithm by Donald Knuth. */
 
#include <stdio.h>
#include <stdlib.h> 
void main( void)
{
	int i, j=1, k, n, *c, x;
	printf( "Enter n,k: ");
	scanf( "%d,%d", &n, &k);
	c = malloc( (k+3) * sizeof(int));
 
	for (i=1; i <= k; i++) c[i] = i;
	c[k+1] = n+1;
	c[k+2] = 0;
	j = k;
visit:
	for (i=k; i >= 1; i--) printf( "%3d", c[i]);
	printf( "\n");
	if (j > 0) {x = j+1; goto incr;}
	if (c[1] + 1 < c[2])
	 {
	 c[1] += 1;
	 goto visit;
	 }
	j = 2;
do_more:
	c[j-1] = j-1;
	x = c[j] + 1;
	if (x == c[j+1]) {j++; goto do_more;}
	if (j > k) exit(0);
incr:
	c[j] = x;
	j--;
	goto visit;
}


And java
 
//CombinationGenerator.java
//--------------------------------------
// Systematically generate combinations.
//--------------------------------------
import java.math.BigInteger;
public class CombinationGenerator {
private int[] a;
private int n;
private int r;
private BigInteger numLeft;
private BigInteger total;
//------------
// Constructor
//------------
public CombinationGenerator (int n, int r) {
	if (r > n) {
	 throw new IllegalArgumentException ();
	}
	if (n < 1) {
	 throw new IllegalArgumentException ();
	}
	this.n = n;
	this.r = r;
	a = new int[r];
	BigInteger nFact = getFactorial (n);
	BigInteger rFact = getFactorial (r);
	BigInteger nminusrFact = getFactorial (n - r);
	total = nFact.divide (rFact.multiply (nminusrFact));
	reset ();
}
//------
// Reset
//------
public void reset () {
	for (int i = 0; i < a.length; i++) {
	 a[i] = i;
	}
	numLeft = new BigInteger (total.toString ());
}
//------------------------------------------------
// Return number of combinations not yet generated
//------------------------------------------------
public BigInteger getNumLeft () {
	return numLeft;
}
//-----------------------------
// Are there more combinations?
//-----------------------------
public boolean hasMore () {
	return numLeft.compareTo (BigInteger.ZERO) == 1;
}
//------------------------------------
// Return total number of combinations
//------------------------------------
public BigInteger getTotal () {
	return total;
}
//------------------
// Compute factorial
//------------------
private static BigInteger getFactorial (int n) {
	BigInteger fact = BigInteger.ONE;
	for (int i = n; i > 1; i--) {
	 fact = fact.multiply (new BigInteger (Integer.toString (i)));
	}
	return fact;
}
//--------------------------------------------------------
// Generate next combination (algorithm from Rosen p. 286)
//--------------------------------------------------------
public int[] getNext () {
	if (numLeft.equals (total)) {
	 numLeft = numLeft.subtract (BigInteger.ONE);
	 return a;
	}
	int i = r - 1;
	while (a[i] == n - r + i) {
	 i--;
	}
	a[i] = a[i] + 1;
	for (int j = i + 1; j < r; j++) {
	 a[j] = a[i] + j - i;
	}
	numLeft = numLeft.subtract (BigInteger.ONE);
	return a;
}
}

 
/*
* test.java
*
* Created on 14 January 2006, 16:29
*/
/**
*
* @author iamthwee
*/
public class test {
 
	/** Creates a new instance of test */
	public test() {
	}
 
	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		String[] elements = {"a", "b","c","d"};
int[] indices;
CombinationGenerator x = new CombinationGenerator (elements.length, 2);
StringBuffer combination;
while (x.hasMore ()) {
combination = new StringBuffer ();
indices = x.getNext ();
for (int i = 0; i < indices.length; i++) {
	combination.append (elements[indices[i]]);
}
System.out.println (combination.toString ());
}
 
	}
 
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bhavanaindia is offline Offline
2 posts
since Aug 2008
Dec 31st, 2008
-1

Re: The fastest combination generator

Honored sir,
I am intersted in Your problem :-)) Long ago I have solved >>Traveling salesman<< problem when I realized that contrary to overall opinion there is only one optimal path between cities, and difference is only in starting and ending city. Implementing one kind of fractall geometry (Sierpinski Curve) as basis for adressing cities, I was able to get 1000 cities solution under one hour on ATARI TT which worked on 60 Mhz.
Now, my question is, do You just need all combinations in order and eventually listed on file?
But, I suppose You have some good use for this combinations, so if You explain exactly what it is supposed to be, perhaps I could find solution in a way You dont expect.
Last of all, if my solution satisfies Your requirements and You can get results fast, how much it would be worth to You?
Sorry that I have to ask this, but I am poor man from poor country and I have to fight for survival just to stay alive (I am retired)....
I am also married to one woman from India, who would like to visit her sick father who need heart operation and pacemaker, and I cannot buy even airoplane tickets..........
Regards from Zagreb, the capitol of Croatia, Europe!
Marijan Pollak, IT SA/SE 1st. Class, Instructor & Team Leader
Reputation Points: 7
Solved Threads: 0
Newbie Poster
henrik14 is offline Offline
5 posts
since Dec 2008
26 Days Ago
-1

A recursive program to generate nCk

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

int nCk(int n,int loopno,int ini,int *a,int k)
{
	static int count=0;
	int i;
	loopno--;
	if(loopno<0)
	{
		a[k-1]=ini;
		for(i=0;i<k;i++)
		{
			printf("%d,",a[i]);
		}
		printf("\n");
		count++;
		return 0;
	}
	for(i=ini;i<=n-loopno-1;i++)
	{
		a[k-1-loopno]=i+1;
		nCk(n,loopno,i+1,a,k);
	}
	if(ini==0)
	return count;
	else
	return 0;
}

void main()
{
	int n,k,*a,count;
	printf("Enter the value of n and k\n");
	scanf("%d %d",&n,&k);
	a=(int*)malloc(k*sizeof(int));
	count=nCk(n,k,0,a,k);
	printf("No of combinations=%d\n",count);
}
Last edited by Ezzaral; 26 Days Ago at 3:40 pm. Reason: Added code tags. Please use them to format any code that you post.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
overtomanu is offline Offline
2 posts
since Jan 2012
26 Days Ago
0

A recursive C program thats generates all of the combinations nCk in a fast possible

Here is a recursive c program thats generates all of the combinations nCk in a fast possible way.
This code works for nC0 also.

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

int nCk(int n,int loopno,int ini,int *a,int k)
{
	if(k==0)
	return 1;
	static int count=0;
	int i;
	loopno--;
	if(loopno<0)
	{
		a[k-1]=ini;
		for(i=0;i<k;i++)
		{
			printf("%d,",a[i]);
		}
		printf("\n");
		count++;
		return 0;
	}
	for(i=ini;i<=n-loopno-1;i++)
	{
		a[k-1-loopno]=i+1;
		nCk(n,loopno,i+1,a,k);
	}
	if(ini==0)
	return count;
	else
	return 0;
}

void main()
{
	int n,k,*a,count;
	printf("Enter the value of n and k\n");
	scanf("%d %d",&n,&k);
	a=(int*)malloc(k*sizeof(int));
	count=nCk(n,k,0,a,k);
	printf("No of combinations=%d\n",count);
	getch();
}
Last edited by overtomanu; 26 Days Ago at 3:50 pm. Reason: Added note for bug fix
Reputation Points: 10
Solved Threads: 0
Newbie Poster
overtomanu is offline Offline
2 posts
since Jan 2012
Message:
Previous Thread in Computer Science Forum Timeline: How to know what to do?
Next Thread in Computer Science Forum Timeline: Question... About a question?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC