mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 09:48:40 +00:00
prepare for more algorithms
This commit is contained in:
parent
2c4155b674
commit
27035d8be7
|
|
@ -41,10 +41,10 @@ final class AccountManager implements \Countable
|
|||
/**
|
||||
* Session.
|
||||
*
|
||||
* @var null|SessionInterface
|
||||
* @var SessionInterface
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private ?SessionInterface $session = null;
|
||||
private SessionInterface $session;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
|
|
|
|||
76
Algorithm/CoinMatching/MinimumCoinProblem.php
Normal file
76
Algorithm/CoinMatching/MinimumCoinProblem.php
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package phpOMS\Algorithm\CoinMatching
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\Algorithm\CoinMatching;
|
||||
|
||||
/**
|
||||
* Matching a value with a set of coins
|
||||
*
|
||||
* @package phpOMS\Algorithm\CoinMatching
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class MinimumCoinProblem
|
||||
{
|
||||
private function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the minimum amount of coins that are required to match a value
|
||||
*
|
||||
* @param array $coins Types of coins available (every coin has infinite availablity)
|
||||
* @param int $value Value to match with the coins
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function getMinimumCoinsForValueI(array $coins, int $value) : array
|
||||
{
|
||||
// amount of required coins for different values
|
||||
$table = [0];
|
||||
$usedCoins = [];
|
||||
|
||||
for ($i = 1; $i <= $value; ++$i) {
|
||||
$table[$i] = \PHP_INT_MAX;
|
||||
}
|
||||
|
||||
$m = \count($coins);
|
||||
|
||||
for ($i = 1; $i <= $value; ++$i) {
|
||||
for ($j = 0; $j < $m; ++$j) {
|
||||
if ($coins[$j] <= $i) {
|
||||
if ($coins[$j] === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$subRes = $table[$i - $coins[$j]];
|
||||
|
||||
if ($subRes !== \PHP_INT_MAX
|
||||
&& $subRes + 1 < $table[$i]
|
||||
) {
|
||||
$table[$i] = $subRes + 1;
|
||||
$usedCoins[$i] = \array_merge($usedCoins[$i - $coins[$j]], [$coins[$j]]);
|
||||
$coins[$j] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $usedCoins[$value];
|
||||
}
|
||||
}
|
||||
0
Algorithm/JobScheduling/Job.php
Normal file
0
Algorithm/JobScheduling/Job.php
Normal file
0
Algorithm/JobScheduling/Weighted.php
Normal file
0
Algorithm/JobScheduling/Weighted.php
Normal file
63
Algorithm/Knappsack/Backpack.php
Normal file
63
Algorithm/Knappsack/Backpack.php
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package phpOMS\Algorithm\Knappsack
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\Algorithm\Backpack;
|
||||
|
||||
/**
|
||||
* Matching a value with a set of coins
|
||||
*
|
||||
* @package phpOMS\Algorithm\Knappsack
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Backpack
|
||||
{
|
||||
private $maxCost = 0.0;
|
||||
|
||||
private $value = 0.0;
|
||||
|
||||
private $cost = 0.0;
|
||||
|
||||
private array $items = [];
|
||||
|
||||
public function __construct($maxCost)
|
||||
{
|
||||
$this->maxCost = $maxCost;
|
||||
}
|
||||
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function getCost()
|
||||
{
|
||||
return $this->cost;
|
||||
}
|
||||
|
||||
public function getItems() : array
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
public function addItem(Item $item) : void
|
||||
{
|
||||
$this->items[] = $item;
|
||||
$this->value += $item->getValue();
|
||||
$this->cost += $item->getCost();
|
||||
}
|
||||
}
|
||||
0
Algorithm/Knappsack/Bounded.php
Normal file
0
Algorithm/Knappsack/Bounded.php
Normal file
0
Algorithm/Knappsack/Continuous.php
Normal file
0
Algorithm/Knappsack/Continuous.php
Normal file
40
Algorithm/Knappsack/Item.php
Normal file
40
Algorithm/Knappsack/Item.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package phpOMS\Algorithm\Knappsack
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\Algorithm\Knappsack;
|
||||
|
||||
/**
|
||||
* Matching a value with a set of coins
|
||||
*
|
||||
* @package phpOMS\Algorithm\Knappsack
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class Item
|
||||
{
|
||||
private $value = 0.0;
|
||||
|
||||
private $cost = 0.0;
|
||||
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function getCost()
|
||||
{
|
||||
return $this->cost;
|
||||
}
|
||||
}
|
||||
0
Algorithm/Knappsack/Unbounded.php
Normal file
0
Algorithm/Knappsack/Unbounded.php
Normal file
40
tests/Algorithm/CoinMatching/MinimumCoinProblemTest.php
Normal file
40
tests/Algorithm/CoinMatching/MinimumCoinProblemTest.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package tests
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\Algorithm\CoinMatching;
|
||||
|
||||
use phpOMS\Algorithm\CoinMatching\MinimumCoinProblem;
|
||||
|
||||
require_once __DIR__ . '/../../Autoloader.php';
|
||||
|
||||
/**
|
||||
* @testdox phpOMS\Algorithm\CoinMatching\MinimumCoinProblem: Test coin matching a value problem
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class MinimumCoinProblemTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testMinimumCoins() : void
|
||||
{
|
||||
self::assertEquals(
|
||||
[9, 6, 5, 1],
|
||||
MinimumCoinProblem::getMinimumCoinsForValueI([6, 6, 5], 17)
|
||||
);
|
||||
|
||||
self::assertEquals(
|
||||
[9, 6, 5, 6, 1],
|
||||
MinimumCoinProblem::getMinimumCoinsForValueI([6, 6, 5], 17)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,7 +12,7 @@
|
|||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\tests\Algorithm\Sort;
|
||||
namespace phpOMS\tests\Algorithm\PathFinding;
|
||||
|
||||
use phpOMS\Algorithm\PathFinding\Grid;
|
||||
use phpOMS\Algorithm\PathFinding\MovementType;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user