Esempio n. 1
0
        private void CheckNode(PrioQueue queue, string destinationNode)
        {
            if (queue.Count == 0)
            {
                return;
            }

            foreach (var route in routes.FindAll(r => r.From == queue.First.Value.Name))
            {
                if (!unvisited.Contains(route.To))
                {
                    continue;
                }

                double travelledDistance = nodeDict[queue.First.Value.Name].Value + route.Cost;

                if (travelledDistance < nodeDict[route.To].Value)
                {
                    nodeDict[route.To].Value        = travelledDistance;
                    nodeDict[route.To].PreviousNode = nodeDict[queue.First.Value.Name];
                }

                if (!queue.HasLetter(route.To))
                {
                    queue.AddNodeWithPriority(nodeDict[route.To]);
                }
            }

            unvisited.Remove(queue.First.Value.Name);
            queue.RemoveFirst();
            CheckNode(queue, destinationNode);
        }
Esempio n. 2
0
        public void ShortestPath(string startNode, string destNode)
        {
            try
            {
                initGraph();
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            nodeDict[startNode].Value = 0;
            var queue = new PrioQueue();

            queue.AddNodeWithPriority(nodeDict[startNode]);
            CheckNode(queue, destNode);
            PrintShortestPath(startNode, destNode);
        }