elgigi/hungarian-algorithm

PHP 中 Hungarian 算法的实现。

v1.1.0 2024-01-15 16:26 UTC

This package is auto-updated.

Last update: 2024-09-15 17:49:16 UTC


README

Latest Version Software License Build Status Codacy Grade Total Downloads

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

此分支基于 rpfk 的实现。变更日志中可看到原始存储库的添加。

安装

您可以使用 Composer 安装库。

composer require elgigi/hungarian-algorithm

用法

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

// Define the score matrix as n arrays consisting of n numerical values
$array = [
    [1, ··· ,1],
    ···
    [3, ··· ,0],
];

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

// 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->solve();