function kuhn_munkres(&$matching, $weight_map)
{
    //计算相等子图
    make_sub_graph($matching, $weight_map);
    //匈牙利算法求最大匹配
    hungary($matching);
    debug($matching);
    if ($matching['max_match'] == count($matching['A'])) {
        return true;
    }
    if (!adjust_mark($matching, $weight_map)) {
        return true;
    }
    return false;
}
示例#2
0
<?php

$boys = array('Peter', 'Allen', 'Jhon', 'Mike', 'Anthony', 'Austin', 'Hugo', 'Howard', 'Jimmy', 'Paul');
$girls = array('Mary', 'Alice', 'Cindy', 'Janet', 'Natasha', 'Peggy', 'Sarah', 'Zoey', 'Mandy', 'Judy');
$favorites = array();
//随机初始化喜好列表
foreach ($boys as $value) {
    make_favorite($value, $girls, $favorites);
}
//print_r($favorites);
$matching = array('search' => array(), 'match' => array(), 'max_match' => 0);
//匈牙利算法
hungary($matching, $boys, $girls, $favorites);
print_r($matching);
function make_favorite($boy, $girls, &$favorites)
{
    //风流指数
    $romantic = rand(1, 5);
    //女孩总数
    $girls_total = count($girls);
    $favorite_girls = array();
    for ($i = 0; $i < $romantic; $i++) {
        $select = rand(0, $girls_total);
        while (in_array($select, $favorite_girls)) {
            $select = rand(0, $girls_total);
        }
        $favorite_girls[] = $select;
    }
    foreach ($girls as $key => $girl) {
        if (in_array($key, $favorite_girls)) {
            $favorites[$boy][$girl] = true;