【发布时间】:2016-11-17 05:22:32
【问题描述】:
我有一个图表,我正在尝试找到最长的路径,但我不确定如何去做。我正在使用来自Data.Graph 的标准Graph 和Edge 类型,并且该图是使用buildG 函数生成的。我最初的想法是使用dfs 执行深度优先搜索,但这产生了一个我不确定如何操作的Forest 对象。
如果它有帮助(很抱歉我不能很好地打印这个,showForest 显然只打印字符串),这是我图表上dfs 命令的输出:
[Node {rootLabel = 87, subForest = [Node {rootLabel = 82, subForest = [Node {rootLabel = 70, subForest = []},Node {rootLabel = 83, subForest = [Node {rootLabel = 66, subForest = [Node {rootLabel = 72, subForest = [Node {rootLabel = 88, subForest = []}]}]},Node {rootLabel = 79, subForest = [Node {rootLabel = 69, subForest = [Node {rootLabel = 85, subForest = []},Node {rootLabel = 84, subForest = [Node {rootLabel = 86, subForest = []},Node {rootLabel = 73, subForest = [Node {rootLabel = 81, subForest = []}]}]}]}]}]},Node {rootLabel = 89, subForest = []}]}]}]
我发现了一些不同的函数来查找通过树的最长路径,例如在这个 answer 中,但它们仅适用于 Trees,而不适用于 Forests,我不确定它是否甚至可以在两者之间转换。
谢谢!
【问题讨论】:
-
你能详细说明一下最长路径图吗?你的意思是所有顶点对中距离最长的2个顶点?
-
@Shersh 之类的;从根开始,我需要距离根最远的顶点,以及到达它所需的节点路线。
-
"很抱歉我不能很好地打印这个,
showForest显然只打印字符串" -- 如果你的意思是drawForest,你可以使用drawForest . fmap (fmap show) -
“但它们只适用于树木,而不适用于森林,我不确定是否可以在两者之间进行转换。” --
Forest只是Trees 的列表,所以如果你有一个与Trees 一起使用的函数,你至少可以简单地将fmap放在Forest之上。