在VS里用Nuget下載"Dijkstra.NET"庫(kù),然后就可以在需要的地方使用了。
首先,這是一個(gè)簡(jiǎn)單的graph,5個(gè)節(jié)點(diǎn),7條邊。
移動(dòng)端看不清楚的可以看下面的:
Graph<int, string> graph = new Graph<int, string>(); graph.AddNode(1); // 添加節(jié)點(diǎn)1 graph.AddNode(2); // 添加節(jié)點(diǎn)2 graph.AddNode(3); graph.AddNode(4); graph.AddNode(5); graph.Connect(1, 2, 4, "some custom information in edge"); // 添加節(jié)點(diǎn)1和2的邊長(zhǎng)(即成本)5,并添加描述信息 graph.Connect(2, 4, 1, "some custom information in edge"); graph.Connect(1, 4, 2, "some custom information in edge"); graph.Connect(2, 3, 4, "some custom information in edge"); graph.Connect(3, 4, 1, "some custom information in edge"); graph.Connect(3, 5, 3, "some custom information in edge"); graph.Connect(4, 5, 7, "some custom information in edge"); // graph.Connect(4, 3, 1, "some custom information in edge"); // 注釋這一句就會(huì)變成1 4 5 不注釋就是 1 4 3 5 // 證明這個(gè)算法只能算有向圖 ShortestPathResult result = graph.Dijkstra(1, 5); // 從圖中獲取節(jié)點(diǎn)1和2的最短路徑 IEnumerable<uint> path = result.GetPath();// 返回所需走過的節(jié)點(diǎn)列表 foreach (var item in path) { System.Console.WriteLine(item);// 結(jié)果是 1 4 5 -> 因?yàn)?~3沒有距離 }
基本不需要解釋了
第一,這個(gè)算法只能解決有向圖的最短路徑;
第二,計(jì)算結(jié)果返回的是節(jié)點(diǎn)號(hào)?不太清楚
第三,沒有提供鄰接矩陣的接口,輸入數(shù)據(jù)也比較麻煩
聯(lián)系客服