Introduction
It doesn’t happen very often, but every once in awhile one needs to iterate over all of the combinations or permutations of a set of objects. Or more specifically, given a set of N objects, you want to consider k of them at a time (for each combination or permutation).
Practically, combinations and permutations algorithms can be used as a brute-force approach to solve certain problems, by trying every possible combination or permutation, until the optimal solution is found.
The circular k-permutations algorithm can be used in real world to solve parts of the Traveling Salesman Problem (TSP) or other similar problems like finding the currencies candidate for Triangular Arbitrage in Forex.
Below is a solution to each of these problems and more. The following generic algorithms permit to visit every combination or permutation of a sequence of length N, taken k items at time.
Performance Considerations
Every effort has been made to make this API library as fast as possible. Many algorithms and libraries for combinations/permutations were reviewed, and lot of benchmarks, code profiling and micro-optimizations were performed.
The Heap’s algorithm in this library was re-implemented in a more efficient way that provides faster performance than its standard implementation.