private static void PrintResults(Bellman_Ford bellman) { Console.WriteLine("Path:"); var previous = bellman.Previous; var secondToLast = previous[609]; var stack = new Stack <int?>(); do { stack.Push(secondToLast); secondToLast = previous[(int)secondToLast]; } while (secondToLast != 109); Console.Write("[ 109 "); while (stack.Count != 0) { Console.Write(stack.Pop()); Console.Write(" "); } Console.WriteLine("609 ]"); Console.WriteLine("Distance:"); Console.WriteLine(bellman.ShortestPathTable[609]); Console.Write("["); for (int i = 0; i < bellman.Previous.Length; i++) { Console.Write(" " + bellman.ShortestPathTable[i] + " "); } Console.Write("]"); Console.WriteLine(); }
public void BadCase() { int V = 5, E = 8; //number of vertices and number of edges in graph Bellman_Ford graph = new Bellman_Ford(V, E); graph.edge[0].source = 0; graph.edge[0].destination = 1; graph.edge[0].weight = -1; graph.edge[1].source = 0; graph.edge[1].destination = 2; graph.edge[1].weight = 4; graph.edge[2].source = 1; graph.edge[2].destination = 2; graph.edge[2].weight = 3; graph.edge[3].source = 1; graph.edge[3].destination = 3; graph.edge[3].weight = 2; graph.edge[4].source = 1; graph.edge[4].destination = 4; graph.edge[4].weight = 2; graph.edge[5].source = 3; graph.edge[5].destination = 2; graph.edge[5].weight = 5; graph.edge[6].source = 3; graph.edge[6].destination = 1; graph.edge[6].weight = 1; graph.edge[7].source = 4; graph.edge[7].destination = 3; graph.edge[7].weight = -3; int[,] result = graph.BellmanFord(graph, 0); int[,] expectedList = new int[5, 2] { { 0, 1 }, { 1, -1 }, { 2, 2 }, { 3, -2 }, { 4, 1 } }; for (int i = 0; i < V; i++) { if (result[i, 1] != expectedList[i, 1]) { Assert.AreNotEqual(result[i, 1], expectedList[i, 1]); break; } } }