如何在QuickGraphDijkstra或A*中设置目标顶点我使用的是QuickGraph3.6版本,我找到了函数SetRootVertex,但是没有SetTagretVertex。我需要这个,因为我在巨大的图表中搜索短路径,这将加快程序的速度。有问题的Clases是DijkstraShortestPathAlgorithm和AStarShortestPathAlgorithm。如果不使用事件,我认为没有办法解决这个问题。您可以将必要的代码包装在扩展方法中以使事情变得清晰,例如:publicstaticclassExtensions{classAStarWrapperwhereTEdge:IEdge{私有AStarShortestPathAlgorithminnerAlgorithm;publicAStarWrapper(AStarShortestPathAlgorithminnerAlgo,TVertexroot,TVertextarget){innerAlgorithm=innerAlgo;this.innerAlgorithm.SetRootVertex(root);this.target=目标;this.innerAlgorithm.FinishVertex+=newVertexAction(innerAlgorithm_FinishVertex);}voidinnerAlgorithm_FinishVertex(TVertexvertex.Equal(vertex,target))this.innerAlgorithm.Abort();}publicdoubleCompute(){this.innerAlgorithm.Compute();返回this.innerAlgorithm.Distances[目标];}}publicstaticdoubleComputeDistanceBetween(thisAStarShortestPathAlgorithmertalgo,TVstart,TVertexend)whereTEdge:IEdge{varwrap=newAStarWrapper(algo,start,end);返回wrap.Compute();}}用法:以上是C#学习教程:HowtosetthetargetinQuickGraphDijkstraorA*Vertex分享的所有内容,如果对你有用还需要进一步了解C#学习和教学希望大家多多关注——varg=newBidirectionalGraph>();g.AddVerticesAndEdge(新边(1,2));g.AddVerticesAndEdge(新边(2,3));g.AddVerticesAndEdge(新边(3,4));g.AddVerticesAndEdge(新边(2,4));varastar=newAStarShortestPathAlgorithm>(g,x=>1.0,x=>0.0);vardist=astar.ComputeDistanceBetween(2,4);本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
