可能な解決策は、グラフをAdjacencyGraph<string, Edge<string>>
としてモデル化し、コストがあなたの距離であるDictionary<Edge<string>, double>
コスト辞書を作成することです。あなたの_graph
// ...
private AdjacencyGraph<string, Edge<string>> _graph;
private Dictionary<Edge<string>, double> _costs;
public void SetUpEdgesAndCosts()
{
_graph = new AdjacencyGraph<string, Edge<string>>();
_costs = new Dictionary<Edge<string>, double>();
AddEdgeWithCosts("A", "D", 4.0);
// snip
AddEdgeWithCosts("C", "B", 1.0);
}
private void AddEdgeWithCosts(string source, string target, double cost)
{
var edge = new Edge<string>(source, target);
_graph.AddVerticesAndEdge(edge);
_costs.Add(edge, cost);
}
は今です:
は、その後、あなたは使用してEへの最短経路を見つけることができます。
private void PrintShortestPath(string @from, string to)
{
var edgeCost = AlgorithmExtensions.GetIndexer(_costs);
var tryGetPath = _graph.ShortestPathsDijkstra(edgeCost, @from);
IEnumerable<Edge<string>> path;
if (tryGetPath(to, out path))
{
PrintPath(@from, to, path);
}
else
{
Console.WriteLine("No path found from {0} to {1}.");
}
}
これはQuickGraph wikiから構成されています。それは印刷:[GitHubの上に実施例を作業
Path found from A to E: A > D > B > E
(https://github.com/serra/QuickgraphExamples/blob/master/src/examples/CalculateDistance.cs) – Marijn