/// <summary> /// Init the specified currentNode. /// </summary> /// <param name='currentNode'> /// Current node. /// </param> public static TravelingNode Init(string currentNode) { TravelingNode node = new TravelingNode (); node.currentNode = currentNode; node.totalDistance = 0.0F; node.iteration = 0; return node; }
private string GetShortestPath(TravelingNode tNode, string destination) { // All travel nodes found the destinationa already, time to return the shortest path if (tNode == null) { return GetShortDistanceNode (tNodes, true).path; } // Get start point connected nodes first List<Node> nodes; data.TryGetValue (tNode.currentNode, out nodes); // Make sure we won't get duplicate node if (!traveledNodes.Contains (tNode.currentNode)) traveledNodes.Add (tNode.currentNode); // Remove existing travel node since we will have a new one soon if ( tNodes.Contains ( tNode ) ) { tNodes.Remove ( tNode ); } foreach (Node node in nodes) { // Avoid node that already visited. if (traveledNodes.Contains (node.nodeName)) { continue; } TravelingNode newTNode = TravelingNode.Init (node.nodeName); newTNode.path = tNode.path + node.nodeName; newTNode.iteration = tNode.iteration + 1; newTNode.totalDistance += tNode.totalDistance + node.weight; // Mark it as done when we found the destination if (node.nodeName == destination) { newTNode.isDone = true; } tNodes.Add (newTNode); } // Once it is done, travel using shortest path TravelingNode sdNode = GetShortDistanceNode (tNodes); return GetShortestPath (sdNode, destination); }