我有一张图表,我试图找到最长的路径,但我不确定如何去做。我使用Data.Graph
中的标准Graph
和Edge
类型,并且使用buildG
函数生成图表。我最初的想法是使用dfs
来执行深度优先搜索,但是这产生了一个Forest
对象,我不确定如何操作。在Haskell中查找通过DFS森林的最长路径
如果它在所有帮助(我很抱歉我不能漂亮的印刷本,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中,但它们只适用于Tree
s,而不是Forest
s,我不确定是否有可能在两者之间进行转换。
谢谢!
你能详细阐述_longest路径graph_?你的意思是_2所有顶点对中距离最长的顶点_? – Shersh
@Shersh排序的;从根开始,我需要距离根最远的顶点以及需要到达的节点路径。 –
“,对不起,我不能打印这个,'showForest'显然只打印字符串” - 如果你的意思是['drawForest']](https://hackage.haskell.org/package/containers-0.5。 8.1/docs/Data-Tree.html#v:drawForest),你可以使用'drawForest。fmap(fmap show)' – duplode