https://github.com/drupol/phpartition
Partition problem for balanced arrays splitting made easy.
https://github.com/drupol/phpartition
karmarkar partition partition-problem php subsets
Last synced: 7 months ago
JSON representation
Partition problem for balanced arrays splitting made easy.
- Host: GitHub
- URL: https://github.com/drupol/phpartition
- Owner: drupol
- Created: 2016-12-17T10:58:46.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-09-11T09:14:13.000Z (over 6 years ago)
- Last Synced: 2025-02-01T20:44:59.731Z (over 1 year ago)
- Topics: karmarkar, partition, partition-problem, php, subsets
- Language: PHP
- Homepage:
- Size: 52.7 KB
- Stars: 8
- Watchers: 5
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
## PHPartition
[](https://travis-ci.org/drupol/phpartition) [](https://scrutinizer-ci.com/g/drupol/phpartition/?branch=master) [](https://scrutinizer-ci.com/g/drupol/phpartition/?branch=master) [](https://www.versioneye.com/user/projects/58551f4d4d6466004c28cc8f)
In number theory and computer science, the partition problem is the task of deciding whether a given multiset of items can be partitioned into multiple balanced subsets.
## Requirements
* PHP >= 5.6,
* (optional) [PHPUnit](https://phpunit.de/) to run tests.
## Examples
```php
setData($data);
$greedy->setSize(3);
$result = $greedy->getResult();
// $result is:
/*
* Array
(
[0] => Array
(
[0] => 9
[1] => 5
[2] => 1
)
[1] => Array
(
[0] => 7
[1] => 6
[2] => 3
)
[2] => Array
(
[0] => 11
[1] => 5
)
)
*/
$simple = new \drupol\phpartition\Algorithm\Simple();
$simple->setData($data);
$simple->setSize(3);
$result = $simple->getResult();
// $result is:
/*
* Array
(
[0] => Array
(
[0] => 5
[1] => 11
)
[1] => Array
(
[0] => 1
[1] => 7
[2] => 3
)
[2] => Array
(
[0] => 5
[1] => 6
[2] => 9
)
)
*/
```
You may also pass objects or array but then, you'll have to define how to access their value.
```php
'anything A',
'weight' => 1,
),
array(
'item' => 'anything B',
'weight' => 2,
),
array(
'item' => 'anything C',
'weight' => 3,
),
array(
'item' => 'anything D',
'weight' => 4,
),
);
$greedy = new \drupol\phpartition\Algorithm\Greedy();
$greedy->setData($data);
$greedy->setSize(2);
$greedy->setItemAccessCallback(function($item) {
return $item['weight'];
});
$result = $greedy->getResult();
// $result is
/*
* Array
(
[0] => Array
(
[0] => Array
(
[item] => anything C
[weight] => 3
)
[1] => Array
(
[item] => anything B
[weight] => 2
)
)
[1] => Array
(
[0] => Array
(
[item] => anything D
[weight] => 4
)
[1] => Array
(
[item] => anything A
[weight] => 1
)
)
)
*/
```
It's also possible to mix the type of object to partition.
## TODO
- Implement Complete Karmarkar-Karp (CKK) algorithm,
- Documentation.