static void Main(string[] args) { try { initGraph(); } catch (FileNotFoundException ex) { Console.WriteLine(ex.Message); return; } Console.Clear(); PrintOverview(); var(startNode, destNode) = GetStartAndEnd(); // Set our start node. The start node has to have a value // of 0 because we're already there. nodeDict[startNode].Value = 0; var queue = new PrioQueue(); queue.AddNodeWithPriority(nodeDict[startNode]); // Do the calculations to find the shortest path to every node // in the graph starting from our starting node. CheckNode(queue, destNode); // Print out the result PrintShortestPath(startNode, destNode); }
// Called for each node in the graph and iterates over its directly // connected nodes. The function always handles the node that // currently has the highest priority in our queue. // So this function checks any directly connected node and compares // the value it currently holds (the shortest path we know to it) is // bigger than the distance of the path through the node we're // currently checking. // If it is, we just found a shorter path to it and we update its // 'shortest path' value and also update its previous node to the // one we're currently processing. // Every directly connected node that we find we also add to the queue // (which is sorted by distance), if it's not already in the queue. // After we're finished private static void CheckNode(PrioQueue queue, string destinationNode) { // If there are no nodes left to check in our queue, we're done. if (queue.Count == 0) { return; } foreach (var route in routes.FindAll(r => r.From == queue.First.Value.Name)) { // Skip routes to nodes that have already been visited. if (!unvisited.Contains(route.To)) { continue; } double travelledDistance = nodeDict[queue.First.Value.Name].Value + route.Distance; // We only look at nodes we haven't visited yet and we only // update the node's values if the distance of the path we're // currently checking is shorter than the one we found before. if (travelledDistance < nodeDict[route.To].Value) { nodeDict[route.To].Value = travelledDistance; nodeDict[route.To].PreviousNode = nodeDict[queue.First.Value.Name]; } // We don't add the 'to' node to the queue if it has already been // visited and we don't allow duplicates. if (!queue.HasLetter(route.To)) { queue.AddNodeWithPriority(nodeDict[route.To]); } } unvisited.Remove(queue.First.Value.Name); queue.RemoveFirst(); CheckNode(queue, destinationNode); }