public void SimpleDijkstraTest(bool directed) { Graph g = new MatrixGraph(7, directed); g.AddEdge(0, 1, 5); g.AddEdge(0, 2, 3); g.AddEdge(0, 3, 0); g.AddEdge(1, 4, 3); g.AddEdge(1, 6, 4); g.AddEdge(2, 4, 2); g.AddEdge(2, 5, 1); g.AddEdge(3, 2, 2); g.AddEdge(3, 6, 15); g.AddEdge(4, 6, 3); g.AddEdge(5, 4, 12); var result = g.Dijkstra(0); Assert.Equal(0, result[0].distance); Assert.Equal(5, result[1].distance); Assert.Equal(2, result[2].distance); Assert.Equal(0, result[3].distance); Assert.Equal(4, result[4].distance); Assert.Equal(3, result[5].distance); Assert.Equal(7, result[6].distance); }
public void TestArrayHeapDijkstra() { Random ran = new Random(); MatrixGraph graph = new MatrixGraph(100, 101); for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { graph.SetUniEdge(i, j, ran.Next(100)); } graph.SetUniEdge(i, i, 0); } double[][] floyd = graph.Floyd(); for (int i = 0; i < 100; i++) { double[] dijkstra = graph.Dijkstra(i, new ArrayHeap <VertexNode>()); for (int j = 0; j < 100; j++) { if (floyd[i][j] != dijkstra[j]) { Console.WriteLine("i:{0} j:{1} floyd:{2} dijkstra:{3}", i, j, floyd[i][j], dijkstra[j]); } Assert.AreEqual(floyd[i][j], dijkstra[j]); } } }
public void TestOneEdgeDijkstra() { MatrixGraph graph = new MatrixGraph(2, 10); graph.SetUniEdge(0, 1, 2); double[] result = graph.Dijkstra(0); Assert.AreEqual(0, result[0]); Assert.AreEqual(2, result[1]); }
public void TestNormalDijkstra() { MatrixGraph graph = new MatrixGraph(3, 10); graph.SetUniEdge(0, 1, 2); graph.SetUniEdge(1, 2, 5); graph.SetUniEdge(0, 2, 6); double[] result = graph.Dijkstra(0); Assert.AreEqual(0, result[0]); Assert.AreEqual(2, result[1]); Assert.AreEqual(6, result[2]); result = graph.Dijkstra(1); Assert.AreEqual(10, result[0]); Assert.AreEqual(0, result[1]); Assert.AreEqual(5, result[2]); result = graph.Dijkstra(2); Assert.AreEqual(10, result[0]); Assert.AreEqual(10, result[1]); Assert.AreEqual(0, result[2]); }