dounabi6295 2014-04-03 10:59
浏览 53
已采纳

用于2D阵列的PHP组合作为Christofides算法解决方案的一部分

I am creating Christofides algorithm for the Traveling Salesman Problem. In part of the algorithm I need to find nodes of a graph that are of odd degree and then calculate the lowest weight. This can be done by the Blossom algorithm, but I am choosing to do it a different way by finding the sum of the possible combinations that you have from a 2D array because I am struggling with the blossom algorithm and do not understand it.

I have 2D array which stores the weights between vertices of odd degree in a graph. For example:

$array = array(
                   0=> array(0, 2, 20,4),
                   1=> array(2,0,7,8),
                   2=> array(20,2,0,12),
                   3=> array(4,8,12,0)
                   )

so between 0 and 1 is of weight 2, if i select the vertices 0 and 1, I am then left with the weight between vertex 2 and 3 because vertex 0 and 1 has already been used. I then need to sum the weights of array[0][1] and array[2][3].

I am struggling with creating an algorithm that returns the combination of possible vertex pairs. For example in the array above the possible pairs are [(0,1)(2,3)],[(0,2)(1,3)],[(0,3)(1,2)]

(0,0),(1,1),(2,2),(3,3) cannot be used as there is no edge weight between them. Also, the reverse of them is not needed([(1,0)(2,3)]).

With these pairs I can then calculate the sum of the weights and choose the smallest.

Any help would be much appreciated.

  • 写回答

2条回答 默认 最新

  • dpmp9359 2014-04-05 20:00
    关注

    You can implement the requirements you lay out pretty quickly using php's array_* functions (which I'll do below), but I would be remiss to not call out that the presented solution limits you to an array of just 4 vertices specifically because of this statement:

    if i select the vertices 0 and 1, I am then left with the weight between vertex 2 and 3 because vertex 0 and 1 has already been used.

    If you have to interact with 5 vertices, the "remaining weight" aspect increases in complexity since you have more than just a left over unused pair. You'll have to define the desired behavior in the case of 5+ vertices to get more assistance if you are not able to modify the code provided below which solves your case of 4.

    <?php
    
    $array = array(
                       0=> array(0, 2, 20,4),
                       1=> array(2,0,7,8),
                       2=> array(20,2,0,12),
                       3=> array(4,8,12,0)
                       );
    
    // Use the keys as the list of vertices.
    $vertices = array_keys($array);
    
    // Filter out nodes without weights (preserves array keys, which are used as index-relations to other nodes)
    $array = array_map('array_filter', $array);
    
    // Create a list of all valid pairs
    $fullPairs = array_reduce(array_map(function($vert1, $otherVerts) use ($vertices) {
            // For each first vertice, create pair combinations using weighted edge and remaining vertices
            return array_map(function($vert2) use ($vert1, $vertices) {
                    // Because reverse combinations are not desired, we sort the pairings to easily identify dupes
                    $vert = array($vert1, $vert2);
                    sort($vert);
                    $vertPair = array($vert, array_values(array_diff($vertices, $vert)));
                    usort($vertPair, function($v1, $v2) { return reset($v1) - reset($v2); });
                    return $vertPair;
            }, array_keys($otherVerts));
    }, $vertices, $array), 'array_merge', array());
    
    // Unique the list using a string representation of the pairs
    $uniqueIndexes = array_unique(array_map('json_encode', $fullPairs));
    
    // Match up the indexes of the unique list against the full pair list to get the pairing structure
    $uniquePairs = array_intersect_key($fullPairs, $uniqueIndexes);
    
    // Print the pairings for verification
    print_r(array_map('json_encode', $uniquePairs));
    
    // Array
    // (
    //    [0] => [[0,1],[2,3]]
    //    [1] => [[0,2],[1,3]]
    //    [2] => [[0,3],[1,2]]
    // )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染