Skip to content

Commit e966703

Browse files
committed
Implemented the ability to calculate every possible combination of a set.
1 parent 9630262 commit e966703

File tree

4 files changed

+685
-7
lines changed

4 files changed

+685
-7
lines changed

.php_cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Copyright © 2019 PHP Experts, Inc.
77
Author: Theodore R. Smith <theodore@phpexperts.pro>
88
GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
99
https://www.phpexperts.pro/
10-
https://github.com/PHPExpertsInc/Skeleton
10+
https://github.com/PHPExpertsInc/Combinatorics
1111
1212
This file is licensed under the MIT License.
1313
HEADER;

README.md

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
# Combinatorics
22

3-
[![TravisCI]()]()
4-
[![Maintainability]()]()
5-
[![Test Coverage]()]()
3+
[![TravisCI](https://travis-ci.org/phpexpertsinc/Combinatorics.svg?branch=master)](https://travis-ci.org/phpexpertsinc/Combinatorics)
4+
[![Maintainability](https://api.codeclimate.com/v1/badges/cb607de8292ed5b208ae/maintainability)](https://codeclimate.com/github/phpexpertsinc/Combinatorics/maintainability)
5+
[![Test Coverage](https://api.codeclimate.com/v1/badges/cb607de8292ed5b208ae/test_coverage)](https://codeclimate.com/github/phpexpertsinc/Combinatorics/test_coverage)
66

77
Combinatorics is a PHP Experts, Inc., Project meant for the ease of access of Combinatorics algorithms.
88

99
See https://en.wikipedia.org/wiki/Combinatorics
1010

11+
### Memory Consumption
12+
Storing thousands of possibilities can be extremely memory intensive.
13+
14+
However, this project utilizes PHP Generators and uses only a few kilobytes to calculate and output
15+
millions of potential combinations (See the **Benchmarks** section below).
16+
17+
### Generating All Possible Combinations
18+
19+
What are all the possibilities of A, B, C and D? More than you might think! 64.
20+
Add two more letters and you've got 1,956 possibilities.
21+
22+
How many different parking spot orderings can 8 cars occupy on any given day?
23+
Find out in the **Usage** section below!
24+
1125
## Installation
1226

1327
Via Composer
@@ -17,9 +31,20 @@ composer require phpexperts/combinatorics
1731
```
1832

1933
## Usage
34+
```php
35+
$generator = new CombinationsGenerator();
36+
37+
foreach ($generator->generate($styles) as $combination) {
38+
// If you can do what you need to do with the combinations here, without immediately storing
39+
// them into an array, then your memory usage will never exceed the amount needed store one
40+
// combination.
41+
}
42+
```
2043

44+
You can see real-world usage of this project in the test suite of the
45+
**Console Painter project**](https://github.com/PHPExpertsInc/ConsolePainter).
2146

22-
# Use cases
47+
## Use cases
2348

2449
## Testing
2550

@@ -33,9 +58,94 @@ To actually see what's going on -and- run the stress tests, run
3358
phpunit --debug
3459
```
3560

36-
Note: It takes over 4 minutes on an Intel i7 to run all of the stress tests.
61+
Note: It takes over 5 minutes on an Intel i7 to run all of the stress tests.
62+
63+
## Benchmarks
64+
65+
```php
66+
Level 1: Generating combinations for A
67+
[
68+
Number of possibilities => 2
69+
Time (ms) => 0.051975250244141
70+
Time (s) => 5.1975250244141E-5
71+
Memory consumed => 0
72+
Peak Memory (Diff) => 0
73+
]
74+
Level 2: Generating combinations for A, B
75+
[
76+
Number of possibilities => 4
77+
Time (ms) => 0.047922134399414
78+
Time (s) => 4.7922134399414E-5
79+
Memory consumed => 0
80+
Peak Memory (Diff) => 0
81+
]
82+
Level 3: Generating combinations for A, B, C
83+
[
84+
Number of possibilities => 15
85+
Time (ms) => 0.22578239440918
86+
Time (s) => 0.00022578239440918
87+
Memory consumed => 0
88+
Peak Memory (Diff) => 0
89+
]
90+
Level 4: Generating combinations for A, B, C, D
91+
[
92+
Number of possibilities => 64
93+
Time (ms) => 1.0910034179688
94+
Time (s) => 0.0010910034179688
95+
Memory consumed => 0
96+
Peak Memory (Diff) => 0
97+
]
98+
Level 5: Generating combinations for A, B, C, D, E
99+
[
100+
Number of possibilities => 325
101+
Time (ms) => 6.5748691558838
102+
Time (s) => 0.0065748691558838
103+
Memory consumed => 0
104+
Peak Memory (Diff) => 0
105+
]
106+
Level 6: Generating combinations for A, B, C, D, E, F
107+
[
108+
Number of possibilities => 1956
109+
Time (ms) => 47.721147537231
110+
Time (s) => 0.047721147537231
111+
Memory consumed => 0
112+
Peak Memory (Diff) => 0
113+
]
114+
Level 7: Generating combinations for A, B, C, D, E, F, G
115+
[
116+
Number of possibilities => 13699
117+
Time (ms) => 395.29585838318
118+
Time (s) => 0.39529585838318
119+
Memory consumed => 0
120+
Peak Memory (Diff) => 0
121+
]
122+
Level 8: Generating combinations for A, B, C, D, E, F, G, H
123+
[
124+
Number of possibilities => 109600
125+
Time (ms) => 3834.4430923462
126+
Time (s) => 3.8344430923462
127+
Memory consumed => 0
128+
Peak Memory (Diff) => 0
129+
]
130+
Level 9: Generating combinations for A, B, C, D, E, F, G, H, I
131+
[
132+
Number of possibilities => 986409
133+
Time (ms) => 37884.353876114
134+
Time (s) => 37.884353876114
135+
Memory consumed => 320
136+
Peak Memory (Diff) => 0
137+
]
138+
Level 10: Generating combinations for A, B, C, D, E, F, G, H, I, J
139+
[
140+
Number of possibilities => 9864100
141+
Time (ms) => 422606.03785515
142+
Time (s) => 422.60603785515
143+
Memory consumed => 0
144+
Peak Memory (Diff) => 0
145+
]
146+
```
37147

38-
# Contributors
148+
## Contributors
39149

40150
[Theodore R. Smith](https://www.phpexperts.pro/]) <theodore@phpexperts.pro>
41151
GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690

src/CombinationGenerator.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* This file is part of Combinatorics, a PHP Experts, Inc., Project.
5+
*
6+
* Copyright © 2019 PHP Experts, Inc.
7+
* Author: Theodore R. Smith <theodore@phpexperts.pro>
8+
* GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
9+
* https://www.phpexperts.pro/
10+
* https://github.com/PHPExpertsInc/Combinatorics
11+
*
12+
* This file is licensed under the MIT License.
13+
*/
14+
15+
namespace PHPExperts\Combinatorics;
16+
17+
class CombinationsGenerator
18+
{
19+
/**
20+
* Taken from https://stackoverflow.com/a/56598845/430062.
21+
*
22+
* @param array $list
23+
*
24+
* @return \Generator
25+
*/
26+
public function generate(array $list): \Generator
27+
{
28+
// Generate even partial combinations.
29+
$list = array_values($list);
30+
$listCount = count($list);
31+
for ($a = 0; $a < $listCount; ++$a) {
32+
yield [$list[$a]];
33+
}
34+
35+
if ($listCount > 2) {
36+
for ($i = 0; $i < count($list); ++$i) {
37+
$listCopy = $list;
38+
39+
$entry = array_splice($listCopy, $i, 1);
40+
foreach ($this->generate($listCopy) as $combination) {
41+
yield array_merge($entry, $combination);
42+
}
43+
}
44+
} elseif (count($list) > 0) {
45+
yield $list;
46+
47+
if (count($list) > 1) {
48+
yield array_reverse($list);
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)