考虑下图:
由以下数组结构表示:
$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 的父节点查找所有路径(A 和 B,在节点 C<>) 是微不足道的,但我很难在后代/混合方向上遍历节点。
有人可以帮帮我吗?
更新:按照@JackManey 的建议,我尝试移植 IDDFS (迭代深化深度优先搜索)基于维基百科伪代码,这或多或少是我的代码的样子:
$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?!
我想我从伪代码中忽略了一些东西,但我不确定它是什么。
我也试过this DFS class这是在另一个问题中推荐的,虽然它似乎总能找到从节点 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)));
}
对于 Distance('c', $graph, 0) , Distance('c', $graph, 1)和 Distance('c', $graph, 2)这会正确返回 C 和任何其他节点之间的距离总和。问题是,Distance('c', $graph, 3) ( and higher ) 它开始重复节点并返回错误的结果:
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 从 C 到 A 的唯一方法步骤是:
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)) 似乎修复了这个特定错误,但这不是正确的修复...帮助?
再次强调,因此您无需滚动
最佳答案
一般来说,你可以做一个depth-first search或 breadth-first search ,尽管两者都不优于另一个(因为很容易举出一个优于另一个的示例)。
编辑:在重新阅读问题并稍作思考后,由于您想要从 C 到 A 的所有路径,因此 DFS 从 C 可能最有意义。在此过程中,您必须存储边序列并丢弃序列(如果它们没有以 A 结束)。
关于php - 在无向图中查找路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10804148/
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我需要在RubyonRails中实现无向图G=(V,E)并考虑构建一个Vertex和一个Edge模型,其中Vertex有_多条边。由于边恰好连接两个顶点,您将如何在Rails中执行此操作?您是否知道任何有助于实现此类图表的gem或库(对重新发明轮子不感兴趣;-))? 最佳答案 不知道有任何现有库在ActiveRecord之上提供图形逻辑。您可能必须实现自己的Vertex、EdgeActiveRecord支持的模型(请参阅Rails安装的rails/activerecord中的vertex.rb和edge.rb/test/fixtur
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
如何使此根路径转到:“/dashboard”而不仅仅是http://example.com?root:to=>'dashboard#index',:constraints=>lambda{|req|!req.session[:user_id].blank?} 最佳答案 您可以通过以下方式实现:root:to=>redirect('/dashboard')match'/dashboard',:to=>"dashboard#index",:constraints=>lambda{|req|!req.session[:user_id].b
我需要根据字符串路径的长度将字符串路径数组转换为符号、哈希和数组的数组给定以下数组:array=["info","services","about/company","about/history/part1","about/history/part2"]我想生成以下输出,对不同级别进行分组,根据级别的结构混合使用符号和对象。产生以下输出:[:info,:services,about:[:company,history:[:part1,:part2]]]#altsyntax[:info,:services,{:about=>[:company,{:history=>[:part1,:pa
我有一个应用需要发送用户事件邀请。当用户邀请friend(用户)参加事件时,如果尚不存在将用户连接到该事件的新记录,则会创建该记录。我的模型由用户、事件和events_user组成。classEventdefinvite(user_id,*args)user_id.eachdo|u|e=EventsUser.find_or_create_by_event_id_and_user_id(self.id,u)e.save!endendend用法Event.first.invite([1,2,3])我不认为以上是完成我的任务的最有效方法。我设想了一种方法,例如Model.find_or_cr
Organization和Image具有一对一的关系。Image有一个名为filename的列,它存储文件的路径。我在Assets管道中包含这样一个文件:app/assets/other/image.jpg。播种时如何包含此文件的路径?我已经在我的种子文件中尝试过:@organization=...@organization.image.create!(filename:File.open('app/assets/other/image.jpg'))#Ialsotried:#@organization.image.create!(filename:'app/assets/other/i
我想找到给定字符串中的所有匹配项,包括重叠匹配项。我怎样才能实现它?#Example"a-b-c-d".???(/\w-\w/)#=>["a-b","b-c","c-d"]expected#Solutionwithoutoverlappedresults"a-b-c-d".scan(/\w-\w/)#=>["a-b","c-d"],but"b-c"ismissing 最佳答案 在积极的前瞻中使用捕获:"a-b-c-d".scan(/(?=(\w-\w))/).flatten#=>["a-b","b-c","c-d"]参见Rubyde
这应该是一个简单的问题,但我找不到任何相关信息。给定一个Ruby中的正则表达式,对于每个匹配项,我需要检索匹配的模式$1、$2,但我还需要匹配位置。我知道=~运算符为我提供了第一个匹配项的位置,而string.scan(/regex/)为我提供了所有匹配模式。如果可能,我需要在同一步骤中获得两个结果。 最佳答案 MatchDatastring.scan(regex)do$1#Patternatfirstposition$2#Patternatsecondposition$~.offset(1)#Startingandendingpo
我安装了ruby、yeoman,当我运行我的项目时,出现了这个错误:Warning:Running"compass:dist"(compass)taskWarning:YouneedtohaveRubyandCompassinstalledthistasktowork.Moreinfo:https://github.com/gruUse--forcetocontinue.Use--forcetocontinue.我有进入可变session目标的路径,但它不起作用。谁能帮帮我? 最佳答案 我必须运行这个:geminstallcom