//compute Dijkstra for each of the boundary nodes public void PrecomputeArcFlags() { for (int i = 0; i < BoundNodes.Count; i++) { Node boundNode = BoundNodes[i]; DijkstraAlgorithm dij = new DijkstraAlgorithm(Grapph); dij.GetShortPath(boundNode.Id, "-1"); //set flag and save the parents into a list Dictionary <string, string> parents = dij.Parents; IEnumerator <string> str = parents.Keys.GetEnumerator(); while (str.MoveNext()) { string currentNodeId = str.Current; string parentNodeId = parents[currentNodeId]; if (parentNodeId != "-1") { Collection <Arc> allarcs = Grapph.AdjacentArcs[currentNodeId]; for (int k = 0; k < allarcs.Count; k++) { Arc arc = allarcs[k]; if (arc.HeadNode.Id == parentNodeId) { arc.ArcFlag = true; } } } } } }
public double ComputeShprtPath(string sourceId, string targetId) { CompBoundNodes(); PrecomputeArcFlags(); DijkstraAlgorithm dij = new DijkstraAlgorithm(Grapph); return(dij.GetShortPath(sourceId, targetId)); }
//dijkstra from a set of nodes //怎样把已经访问过的节点在 public Dictionary <string, double> PreComputLandmarksDistances(string currentLandmarkId) { Dictionary <string, double> costDictionary = new Dictionary <string, double>(); DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(Graph); dijkstra.GetShortPath(currentLandmarkId, "-1"); costDictionary = dijkstra.VisitedNodeMarks; return(costDictionary); }
public double ComputeShortPath(string sourceNodeId, string targetNodeId) { Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")); DijkstraAlgorithm dij = new DijkstraAlgorithm(Graph); dij.Heuristics = GetHeuristicDictionary(targetNodeId); double cost = dij.GetShortPath(sourceNodeId, targetNodeId); dij.ShortPathToString(sourceNodeId, targetNodeId); Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")); return(cost); }
static void Main(string[] args) { RoadNetwork rn = new RoadNetwork(); Node n1 = new Node("1", 0, 0); Node n2 = new Node("2", 0, 0); Node n3 = new Node("3", 0, 0); Node n4 = new Node("4", 0, 0); Node n5 = new Node("5", 0, 0); Node n6 = new Node("6", 0, 0); Arc arc12 = new Arc(n1, n2, 7); Arc arc13 = new Arc(n1, n3, 9); Arc arc16 = new Arc(n1, n6, 14); Arc arc23 = new Arc(n2, n3, 10); Arc arc24 = new Arc(n2, n4, 15); Arc arc34 = new Arc(n3, n4, 11); Arc arc36 = new Arc(n3, n6, 2); Arc arc45 = new Arc(n4, n5, 6); Arc arc65 = new Arc(n6, n5, 9); Arc arc21 = new Arc(n2, n1, 7); Arc arc31 = new Arc(n3, n1, 9); Arc arc61 = new Arc(n6, n1, 14); Arc arc32 = new Arc(n3, n2, 10); Arc arc42 = new Arc(n4, n2, 15); Arc arc43 = new Arc(n4, n3, 11); Arc arc63 = new Arc(n6, n3, 2); Arc arc54 = new Arc(n5, n4, 6); Arc arc56 = new Arc(n6, n5, 9); rn.Nodes.Add(n1); rn.Nodes.Add(n2); rn.Nodes.Add(n3); rn.Nodes.Add(n4); rn.Nodes.Add(n5); rn.Nodes.Add(n6); rn.Arcs.Add(arc12); rn.Arcs.Add(arc13); rn.Arcs.Add(arc16); rn.AdjacentArcs.Add(n1.Id, rn.Arcs); Collection <Arc> aa = new Collection <Arc>(); aa.Add(arc21); aa.Add(arc23); aa.Add(arc24); rn.AdjacentArcs.Add(n2.Id, aa); Collection <Arc> bb = new Collection <Arc>(); bb.Add(arc31); bb.Add(arc32); bb.Add(arc34); bb.Add(arc36); rn.AdjacentArcs.Add(n3.Id, bb); Collection <Arc> cc = new Collection <Arc>(); cc.Add(arc42); cc.Add(arc43); cc.Add(arc45); rn.AdjacentArcs.Add(n4.Id, cc); Collection <Arc> dd = new Collection <Arc>(); dd.Add(arc63); dd.Add(arc65); dd.Add(arc61); rn.AdjacentArcs.Add(n6.Id, dd); Collection <Arc> ee = new Collection <Arc>(); ee.Add(arc56); ee.Add(arc54); rn.AdjacentArcs.Add(n5.Id, ee); rn.MapNodes.Add("1", n1); rn.MapNodes.Add("2", n2); rn.MapNodes.Add("3", n3); rn.MapNodes.Add("4", n4); rn.MapNodes.Add("5", n5); rn.MapNodes.Add("6", n6); DijkstraAlgorithm dij = new DijkstraAlgorithm(rn); double cost = dij.GetShortPath("1", "5"); dij.ShortPathToString("1", "5"); Console.WriteLine("******************************"); LandmarkAlgorithm landmark = new LandmarkAlgorithm(rn); landmark.SelectLandmarks(1); double cost1 = landmark.ComputeShortPath("1", "5"); //Console.WriteLine(cost1); Console.WriteLine("******************************"); Console.ReadLine(); }