private void InitializeGraph(Graph graph, string startingNode)
        {
            foreach (Node node in graph.Nodes.Values)
            {
                node.DistanceFromStart = double.PositiveInfinity;
            }

            graph.Nodes[startingNode].DistanceFromStart = 0;
        }
        public IDictionary<string, double> CalculateDistances(Graph graph, string startingNode)
        {
            if (!graph.Nodes.Any(n => n.Key == startingNode))
                throw new ArgumentException("Starting node must be in graph.");

            InitialiseGraph(graph, startingNode);
            ProcessGraph(graph, startingNode);
            return ExtractDistances(graph);
        }
        public static void Main()
        {
            var graph = new Graph();

            // Nodes
            graph.AddNode("A");
            graph.AddNode("B");
            graph.AddNode("C");
            graph.AddNode("D");
            graph.AddNode("E");
            graph.AddNode("F");
            graph.AddNode("G");
            graph.AddNode("H");
            graph.AddNode("I");
            graph.AddNode("J");
            graph.AddNode("Z");

            // Connections
            graph.AddConnection("A", "B", 14, true);
            graph.AddConnection("A", "C", 10, true);
            graph.AddConnection("A", "D", 14, true);
            graph.AddConnection("A", "E", 21, true);
            graph.AddConnection("B", "C", 9, true);
            graph.AddConnection("B", "E", 10, true);
            graph.AddConnection("B", "F", 14, true);
            graph.AddConnection("C", "D", 9, false);
            graph.AddConnection("D", "G", 10, false);
            graph.AddConnection("E", "H", 11, true);
            graph.AddConnection("F", "C", 10, false);
            graph.AddConnection("F", "H", 10, true);
            graph.AddConnection("F", "I", 9, true);
            graph.AddConnection("G", "F", 8, false);
            graph.AddConnection("G", "I", 9, true);
            graph.AddConnection("H", "J", 9, true);
            graph.AddConnection("I", "J", 10, true);

            var calculator = new DistanceCalculator();
            var distances = calculator.CalculateDistances(graph, "G");  // Start from "G"

            foreach (var d in distances)
            {
                Console.WriteLine("{0}, {1}", d.Key, d.Value);
            }
        }
 private void ProcessGraph(Graph graph, string startingNode)
 {
     bool finished = false;
     var queue = graph.Nodes.Values.ToList();
     while (!finished)
     {
         Node nextNode = queue.OrderBy(n => n.DistanceFromStart).FirstOrDefault(
             n => !double.IsPositiveInfinity(n.DistanceFromStart));
         if (nextNode != null)
         {
             ProcessNode(nextNode, queue);
             queue.Remove(nextNode);
         }
         else
         {
             finished = true;
         }
     }
 }
 private IDictionary<string, double> ExtractDistances(Graph graph)
 {
     return graph.Nodes.ToDictionary(n => n.Key, n => n.Value.DistanceFromStart);
 }