繁体   English   中英

在无向图中查找路径

[英]Finding paths in a Undirected Graph

请考虑以下图表:

虚拟图

由以下数组结构表示:

$graph = array
(
    'a' => array(),
    'b' => array('a'),
    'c' => array('a', 'b'),
    'd' => array('a'),
    'e' => array('d'),
    'f' => array('a', 'b', 'c', 'd'),
    'g' => array('d'),
    'h' => array('c'),
    'i' => array('c', 'g'),
    'j' => array(),
);

在没有重复节点的情况下,在任一方向上从节点X到节点Y查找所有路径(不仅是最短路径)的最有效算法是什么? 例如,从节点C到节点A的路径是:

C --> A
C --> B --> A
C --> F --> A
C --> F --> B --> A
C --> F --> D --> A
C --> I --> G --> D --> A

使用节点X的父节点(在节点C的示例中为AB查找所有路径是微不足道的,但是我很难在后代/混合方向上遍历节点。

有人可以帮我吗?


更新 :在@JackManey建议之后,我尝试根据Wikipedia伪代码移植IDDFS (Iterative Deepening Depth-First Search),这或多或少是我的代码的样子:

$graph = directed2Undirected($graph);

function IDDFS($root, $goal) {
    $depth = 0;

    while ($depth <= 2) { // 2 is hard-coded for now
        $result = DLS($root, $goal, $depth);

        if ($result !== false) {
            return $result;
        }

        $depth++;
    }
}

function DLS($node, $goal, $depth) {
    global $graph;

    if (($depth >= 0) && ($node == $goal)) {
        return $node;
    }

    else if ($depth > 0) {
        foreach (expand($node, $graph) as $child) {
            return DLS($child, $goal, $depth - 1);
        }
    }

    else {
        return false;
    }
}

以下是它使用的辅助函数:

function directed2Undirected($data) {
    foreach ($data as $key => $values) {
        foreach ($values as $value) {
            $data[$value][] = $key;
        }
    }

    return $data;
}

function expand($id, $data, $depth = 0) {
    while (--$depth >= 0) {
        $id = flatten(array_intersect_key($data, array_flip((array) $id)));
    }

    return array_unique(flatten(array_intersect_key($data, array_flip((array) $id))));
}

function flatten($data) {
    $result = array();

    if (is_array($data) === true) {
        foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($data)) as $value) {
            $result[] = $value;
        }
    }

    return $result;
}

调用上述内容会产生奇怪或不完整的结果:

var_dump(IDDFS('c', 'a')); // a -- only 1 path?
var_dump(IDDFS('c', 'd')); // NULL -- can't find this path?!

我想我忽略了伪代码中的某些东西,但我不确定它是什么。


我也尝试过在另一个问题中推荐的这个DFS类 ,虽然它似乎总是找到从节点X到节点Y的一条路径,我无法让它返回所有路径(演示C - > AC - > D )。


由于我不需要知道实际采用的路径,只需要有多少路径需要n个步骤才能从节点X到节点Y,我想出了这个函数(使用了上面的directed2Undirected ):

$graph = directed2Undirected($graph);

function Distance($node, $graph, $depth = 0) {
    $result = array();

    if (array_key_exists($node, $graph) === true) {
        $result = array_fill_keys(array_keys($graph), 0);

        foreach (expand($node, $graph, $depth - 1) as $child) {
            if (strcmp($node, $child) !== 0) {
                $result[$child] += $depth;
            }
        }

        $result[$node] = -1;
    }

    return $result;
}

function expand($id, $data, $depth = 0) {
    while (--$depth >= 0) {
        $id = flatten(array_intersect_key($data, array_flip((array) $id)));
    }

    // no array_unique() now!
    return flatten(array_intersect_key($data, array_flip((array) $id)));
}

对于Distance('c', $graph, 0)Distance('c', $graph, 1)Distance('c', $graph, 2)这正确返回C与任何其他节点之间的距离之和。 问题是,使用Distance('c', $graph, 3) 更高 ),它开始重复节点并返回错误的结果:

Array
(
    [a] => 12
    [b] => 9
    [c] => -1
    [d] => 9
    [e] => 3
    [f] => 12
    [g] => 3
    [h] => 3
    [i] => 6
    [j] => 0
)

索引a应该只有6(3 + 3),因为我使用3个步骤从CA的唯一方法是:

C --> F --> B --> A
C --> F --> D --> A

然而,它似乎正在考虑重复节点的两个 (仅?)附加路径:

C --> A --> C --> A
C --> B --> C --> A
C --> F --> C --> A
C --> H --> C --> A
C --> I --> C --> A

当然,索引a不是唯一错误的。 问题似乎是expand()但我不确定如何解决它, array_diff(expand('c', $graph, $i), expand('c', $graph, $i - 2))似乎修复此特定错误,但这不是一个正确的修复...帮助?


虚拟图再次 再次,所以你不必滚动

一般来说,你可以进行深度优先搜索广度优先搜索 ,虽然没有一个优于另一个(因为很容易想出一个优于另一个的例子)。

编辑:在重新阅读的问题和思考了一下,因为你想从所有路径CA ,一个DFS开始C很可能最有意义。 一路上,你必须存储边缘序列并抛弃序列,如果它们不是最终在A

$ stdin = fopen('php:// stdin','r'); $ stdin = fopen('php:// stdin','w');

fscanf(STDIN,"%d\n",$n);
fscanf(STDIN,"%d\n",$e);
$graph = array();


for ($i = 0;$i<$n; $i++)
{
    $graph[$i] = array();
}


for ($i = 0;$i<$e; $i++)
{
    fscanf(STDIN,"%s%s",$x,$y);
    $row = ord($x[0]) - ord('A');
    $row = ord($y[0]) - ord('A');
    echo $row." ".$col;

    $graph[$row][$col] = 1;
    $graph[$col][$row] = 1;
}

for ($i = 0;$i <$n; $i++)
{
    for ($j= 0;$j<$n ;$j++)
    {
      if ($graph[$i][$j]==1)
      {
          echo"1 ";
      }
      else{
        echo"0 "; 
      }
      echo "\n";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM