Sunday, July 31, 2016

GENERATE 70 COMBINATIONS IN 1 SECOND ....WHAT YOU SAY 500 MILLION..HALF A BILLION...

 Knuth: Generating All Combinations

I ran into a tricky little problem today: efficiently generating all combinations of k elements from a set of size N. I came up with some ideas but they weren’t efficient enough. I turned to a Knuth Volume 4 preprint on his website, and found all sorts of crazy algorithms for it. Here is a C# implementation I just coded up and tested that people might find useful. It allows you to make a Combination object, and use foreach on it to get all the members.
Note that I had a chance to use the C# 2.0 yield statement; it let me do a fairly direct translation from the pseudocode, although I made a few tiny changes to simplify things. See the comments for a few ways to improve efficiency a tiny bit but it doesn’t affect time complexity. If I understood Knuth, this algorithm runs in O(N choose t) – it’s linear in the number of elements in the output.
Combination class code: Combination.cs
Test class: CombinationsTest.cs
See also: Combinadic on Wikipedia

https://seekwell.wordpress.com/2007/11/17/knuth-generating-all-combinations/ 

 

C Program to Implement Fisher-Yates Algorithm for Array Shuffling

This C program implements Fisher-Yates algorithm for array shuffling. The Fisher–Yates shuffle (named after Ronald Fisher and Frank Yates), also known as the Knuth shuffle (after Donald Knuth), is an algorithm for generating a random permutation of a finite set—in plain terms, for randomly shuffling the set. A variant of the Fisher–Yates shuffle, known as Sattolo’s algorithm, may be used to generate random cycles of length n instead. The Fisher–Yates shuffle is unbiased, so that every permutation is equally likely. The modern version of the algorithm is also rather efficient, requiring only time proportional to the number of items being shuffled and no additional storage space. Here is the source code of the C program to shuffle an array using Fisher-Yates algorithm. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 

http://www.sanfoundry.com/c-program-implement-fisher-yates-algorithm-array-shuffling/ 


No comments: