Ali*_*xel 9 php algorithm graph graph-algorithm
请考虑以下图表:

由以下数组结构表示:
$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(),
);
Run Code Online (Sandbox Code Playgroud)
在没有重复节点的情况下,在任一方向上从节点X到节点Y 查找所有路径(不仅是最短路径)的最有效算法是什么?例如,从节点到节点的路径是:CA
C --> A
C --> B --> A
C --> F --> A
C --> F --> B --> A
C --> F --> D --> A
C --> I --> G --> D --> A
Run Code Online (Sandbox Code Playgroud)
使用节点的父节点X(A以及B在节点的示例中C)查找所有路径是微不足道的,但是我很难在后代/混合方向上遍历节点.
有人可以帮我吗?
更新:在@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;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是它使用的辅助函数:
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;
}
Run Code Online (Sandbox Code Playgroud)
调用上述内容会产生奇怪或不完整的结果:
var_dump(IDDFS('c', 'a')); // a -- only 1 path?
var_dump(IDDFS('c', 'd')); // NULL -- can't find this path?!
Run Code Online (Sandbox Code Playgroud)
我想我忽略了伪代码中的某些东西,但我不确定它是什么.
我也尝试过在另一个问题中推荐的这个DFS类,虽然它似乎总是找到从节点X到节点Y的一条路径,我无法让它返回所有路径(演示为C- >A和C- >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)));
}
Run Code Online (Sandbox Code Playgroud)
对于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
)
Run Code Online (Sandbox Code Playgroud)
该指数a应该只有6(3 + 3),因为唯一的方法,我可以从中获取C到A使用3个步骤是:
C --> F --> B --> A
C --> F --> D --> A
Run Code Online (Sandbox Code Playgroud)
然而,它似乎正在考虑重复节点的两个(仅?)附加路径:
C --> A --> C --> A
C --> B --> C --> A
C --> F --> C --> A
C --> H --> C --> A
C --> I --> C --> A
Run Code Online (Sandbox Code Playgroud)
当然,索引a不是唯一错误的.问题似乎是,expand()但我不知道如何解决它,array_diff(expand('c', $graph, $i), expand('c', $graph, $i - 2))似乎解决了这个特殊的错误,但这不是一个正确的修复...帮助?
再次,所以你不必滚动