static void Main(string[] args) { // Intial Graph and add all node Graph graph = new Graph(); graph.AddNode("A"); graph.AddNode("B"); graph.AddNode("C"); graph.AddNode("D"); graph.AddNode("E"); // Bind path weight between node graph.AddConnection("A", "B", 6); graph.AddConnection("A", "D", 1); graph.AddConnection("B", "D", 2); graph.AddConnection("B", "E", 2); graph.AddConnection("B", "C", 5); graph.AddConnection("D", "E", 1); graph.AddConnection("E", "C", 5); DijkstraAlgo algo = new DijkstraAlgo(); // Let A is start node IDictionary <string, NodeRecord> result = algo.CalculateDistances(graph, "A"); // print all shortest path of each node foreach (var item in result) { Console.WriteLine(item.Key + " shortest path:" + item.Value.lowestWeight + " previous node:" + item.Value.previousNode?.name); } Console.ReadLine(); }
public void Test_Calculation_Shortest_Path() { Graph graph = new Graph(); graph.AddNode("A"); graph.AddNode("B"); graph.AddNode("C"); graph.AddNode("D"); graph.AddNode("E"); graph.AddConnection("A", "B", 6); graph.AddConnection("A", "D", 1); graph.AddConnection("B", "D", 2); graph.AddConnection("B", "E", 2); graph.AddConnection("B", "C", 5); graph.AddConnection("D", "E", 1); graph.AddConnection("E", "C", 5); DijkstraAlgo algo = new DijkstraAlgo(); IDictionary <string, NodeRecord> result = algo.CalculateDistances(graph, "A"); Assert.AreEqual(7, result["C"].lowestWeight); }
public void Test_Exception_Node_No_Connection() { Graph graph = new Graph(); graph.AddNode("A"); graph.AddNode("B"); graph.AddNode("C"); graph.AddConnection("A", "B", 6); DijkstraAlgo algo = new DijkstraAlgo(); ArgumentException ex = Assert.ThrowsException <ArgumentException>(() => algo.CalculateDistances(graph, "A")); Assert.AreEqual("all node must have connection", ex.Message); }