PHPackages                             alphazygma/combinatorics - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. alphazygma/combinatorics

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

alphazygma/combinatorics
========================

Math libraries for Combinatorics (Combinations, Permutations, ...).

1.0(10y ago)15200.6k↓54.7%2LGPL-3.0+PHPPHP &gt;=5.4

Since Mar 21Pushed 10y ago2 watchersCompare

[ Source](https://github.com/alphazygma/Combinatorics)[ Packagist](https://packagist.org/packages/alphazygma/combinatorics)[ RSS](/packages/alphazygma-combinatorics/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/cb8fe01ca33e95f59079fa006c013eae8b181d2a4f3462f734df1d2c610867a7/68747470733a2f2f706f7365722e707567782e6f72672f616c7068617a79676d612f636f6d62696e61746f726963732f762f737461626c65)](https://packagist.org/packages/alphazygma/combinatorics)[![Build Status](https://camo.githubusercontent.com/4cab4c8c30c22de8901cc30e97d4288e45a41cc5b614220496c5ceb1b0f253a5/68747470733a2f2f7472617669732d63692e6f72672f616c7068617a79676d612f436f6d62696e61746f726963732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/alphazygma/Combinatorics)[![Coverage Status](https://camo.githubusercontent.com/84554d4854e351ec84197814c89f1e566c690cc200afbaa821c81c08ec6012b8/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f616c7068617a79676d612f436f6d62696e61746f726963732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/alphazygma/Combinatorics?branch=master)[![Total Downloads](https://camo.githubusercontent.com/c81bf74ebec85d54962c094a66837d3a7106b24b261edc4d87d9e5e0aa8e167f/68747470733a2f2f706f7365722e707567782e6f72672f616c7068617a79676d612f636f6d62696e61746f726963732f646f776e6c6f616473)](https://packagist.org/packages/alphazygma/combinatorics)[![Latest Unstable Version](https://camo.githubusercontent.com/b00006ac445d036eb81927042258a29d83e5cfe45f064a097ab9036652e8a935/68747470733a2f2f706f7365722e707567782e6f72672f616c7068617a79676d612f636f6d62696e61746f726963732f762f756e737461626c65)](https://packagist.org/packages/alphazygma/combinatorics)[![License](https://camo.githubusercontent.com/f9ee1b11a5227627b69e362cf16fbdc6cebc56b4e1890bb365c9db79b07db6e9/68747470733a2f2f706f7365722e707567782e6f72672f616c7068617a79676d612f636f6d62696e61746f726963732f6c6963656e7365)](https://packagist.org/packages/alphazygma/combinatorics)

COMBINATORICS
=============

[](#combinatorics)

> [Wikipedia](https://en.wikipedia.org/wiki/Combinatorics) : **Combinatorics** is a branch of mathematics concerning the study of finite or countable discrete structures. Aspects of combinatorics include counting the structures of a given kind and size (enumerative combinatorics), deciding when certain criteria can be met, and constructing and analyzing objects meeting the criteria (as in combinatorial designs and matroid theory), finding "largest", "smallest", or "optimal" objects (extremal combinatorics and combinatorial optimization), and studying combinatorial structures arising in an algebraic context, or applying algebraic techniques to combinatorial problems (algebraic combinatorics).

Requirements
------------

[](#requirements)

PHP 5.4+ is required. (*The `[]` short array syntax was introduced on [5.4](http://php.net/manual/en/migration54.new-features.php%3E)*)

Disclosure
----------

[](#disclosure)

The *Combination* and *Permutation* implementation is based on the work of *David Sanders* ().

SourceLinkPEAR[https://pear.php.net/package/Math\_Combinatorics](https://pear.php.net/package/Math_Combinatorics)Pckagist[https://packagist.org/packages/pear/math\_combinatorics](https://packagist.org/packages/pear/math_combinatorics)Github[https://github.com/pear/Math\_Combinatorics](https://github.com/pear/Math_Combinatorics)Changelog
---------

[](#changelog)

- 1.0 Added Permutations code (with unit tests)
- 0.2 Unit Tests added to Combinations library.
- 0.1 Port of the Combinations library (manual tests done, but unit tests, need to be added).

Classes
-------

[](#classes)

### Combinations

[](#combinations)

This version is similar to David's with the difference that the base method will return all possible combinations based on the supplied set.

It also provides with a Static method to access the class as a utility, so if you need this functionality very often, use the Instance approach for you'll get better performance, but if you use it here and there, the static access may prove more readable.

Additionally, it detaches the Pointers functionality into it's own class, providing a bit more clarity into the Combinations code as well as making it better thread-safe as the pointers are no longer an attribute of the class shared across the methods, it is an object created for each run and thus allowing to run multiple combinations in parallel from the same class instance without having them interfere with each other.

**Note**: As with David's implementation, the returned combinations preserve the keys supplied. (However fixes a minor bug where the single element combinations would not preserve its keys)

### Permutations

[](#permutations)

As in Combinations, this code is similar to David's with the difference that both pieces are not mixed in the same class, Permutations is a class of its own and has an internal reference to the Combinations class.

It as well, provides with a Static method to access the class as a utility.

Usage
-----

[](#usage)

*This usage considers that you have an autoloader running*. (see [Install](#Install) for more reference)

The result of the functionality is an `Array of Arrays` in which the Outer Array is a list of combinations and each Inner Array is the combination itself.

##### Retrieving all combinations for a source data set.

[](#retrieving-all-combinations-for-a-source-data-set)

```
$sourceDataSet = ['a' => 5, 'b' => 6, 'c' => 8, 'd' => 10];

// Retrieve all combinations as Utility
$combinationsList = \Math\Combinatorics\Combination::get($sourceDataSet);

// Retrieve all combinations as instance class
$combination      = new \Math\Combinatorics\Combination();
$combinationsList = $combination->getCombinations($sourceDataSet);
```

Here is a detailed version of the expanded array

```
size 1:  [`a` => 5]  [`b` => 6]  [`c` => 8]  [`d` => 10]
size 2:  [`a` => 5,`b` => 6]     [`a` => 5,`c` => 8]
         [`a` => 5,`d` => 10]    [`b` => 6,`c` => 8]
         [`b` => 6,`d` => 10]    [`c` => 8,`d` => 10]
size 3:  [`a` => 5,`b` => 6,`c` => 8]
         [`a` => 5,`b` => 6,`d` => 10]
         [`a` => 5,`c` => 8,`d` => 10]
         [`b` => 6,`c` => 8,`d` => 10]
size 4:  [`a` => 5,`b` => 6,`c` => 8,`d` => 10]

```

##### Retrieving combinations of a given length for a source data set.

[](#retrieving-combinations-of-a-given-length-for-a-source-data-set)

```
$sourceDataSet = ['a' => 5, 'b' => 6, 'c' => 8, 'd' => 10];

// Retrieve all combinations as Utility
$combinationsList = \Math\Combinatorics\Combination::get($sourceDataSet, 3);

// Retrieve all combinations as instance class
$combination      = new \Math\Combinatorics\Combination();
$combinationsList = $combination->getCombinations($sourceDataSet, 3);
```

Here is a detailed version of the expanded array

```
size 3:  [`a` => 5,`b` => 6,`c` => 8]
         [`a` => 5,`b` => 6,`d` => 10]
         [`a` => 5,`c` => 8,`d` => 10]
         [`b` => 6,`c` => 8,`d` => 10]

```

##### Retrieving all permutations for a source data set.

[](#retrieving-all-permutations-for-a-source-data-set)

```
$sourceDataSet = ['z' => 10, 'a' => 50, 'x' => 77];

// Retrieve all combinations as Utility
$permtuationList = \Math\Combinatorics\Permutation::get($sourceDataSet);

// Retrieve all combinations as instance class
$permutation      = new \Math\Combinatorics\Permutation();
$permutationsList = $permutation->getPermutations($sourceDataSet);
```

Here is a detailed version of the expanded array

```
size 1:  ['z' => 10]
         ['a' => 50]
         ['x' => 77]
size 2:  ['z' => 10, 'a' => 50]
         ['a' => 50, 'z' => 10]
         ['z' => 10, 'x' => 77]
         ['x' => 77, 'z' => 10]
         ['a' => 50, 'x' => 77]
         ['x' => 77, 'a' => 50]
size 3:  ['z' => 10, 'a' => 50, 'x' => 77]
         ['z' => 10, 'x' => 77, 'a' => 50]
         ['a' => 50, 'x' => 77, 'z' => 10]
         ['a' => 50, 'z' => 10, 'x' => 77]
         ['x' => 77, 'z' => 10, 'a' => 50]
         ['x' => 77, 'a' => 50, 'z' => 10]

```

##### Retrieving permutations of a given length for a source data set.

[](#retrieving-permutations-of-a-given-length-for-a-source-data-set)

```
$sourceDataSet = ['z' => 10, 'a' => 50, 'x' => 77];

// Retrieve all combinations as Utility
$permtuationList = \Math\Combinatorics\Permutation::get($sourceDataSet, 2);

// Retrieve all combinations as instance class
$permutation      = new \Math\Combinatorics\Permutation();
$permutationsList = $permutation->getPermutations($sourceDataSet, 2);
```

Here is a detailed version of the expanded array

```
size 2:  ['z' => 10, 'a' => 50]
         ['a' => 50, 'z' => 10]
         ['z' => 10, 'x' => 77]
         ['x' => 77, 'z' => 10]
         ['a' => 50, 'x' => 77]
         ['x' => 77, 'a' => 50]

```

Install
-------

[](#install)

The easiest way to install is through [composer](http://getcomposer.org).

Just create a composer.json file for your project:

```
{
    "require": {
        "alphazygma/combinatorics": "~1.0"
    }
}
```

Then you can run these two commands to install it:

```
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install

```

or simply run `composer install` if you have have already [installed the composer globally](http://getcomposer.org/doc/00-intro.md#globally).

Then you can include the autoloader, and you will have access to the library classes:

```
