arthurkushman/hungarian

此包已被废弃且不再维护。未建议替代包。

PHP中的匈牙利算法实现。

0.1.2 2018-09-03 06:41 UTC

This package is auto-updated.

Last update: 2020-10-27 13:48:59 UTC


README

PHP中的匈牙利算法实现。匈牙利算法可以用于在给定的成本矩阵下,在两种类型的实体之间找到最优(最小成本)的分配。匈牙利算法也称为Kuhn–Munkres算法或Munkres分配算法。

使用Composer安装

在bash/zsh中执行以下命令

composer require arthurkushman/hungarian

示例用法

为匈牙利类定义一个包含分数作为输入的方阵。方阵必须是一个由n个数组(行)组成的数组,每个数组包含n个分数。行数组中每个元素的键必须等于列的键。

<?php
use Hungarian\Hungarian;
// Define the score matrix as n arrays consisting of n numerical values
$matrix = [
    [1, 2, 3, 0, 1],
    [0, 3, 12, 1, 1],
    [3, 0, 1, 13, 1],
    [3, 1, 1, 12, 0],
    [3, 1, 1, 12, 0],
];

// Create a new Hungarian problem using the score matrix as input
$hungarian  = new Hungarian($matrix);

// Solve the problem using the Hungarian algorithm and get the solution as an array with the row and column as key and value, respectively
$allocation = $hungarian->solveMin();