public void FloydWarshall_WhenNegativeEdge_ThenEdgeIncluded() { // given double[,] distances = new double[, ] { { 0.0, 4.0, inf, 9.0, 11.0, 12.0, 16.0, 14.0, 14.0, 24.0 }, { 8.0, 0.0, inf, 5.0, 7.0, 8.0, 12.0, 10.0, 10.0, 20.0 }, { 7.0, 11.0, 0.0, 4.0, 6.0, 7.0, 8.0, 9.0, 9.0, 19.0 }, { 3.0, 7.0, inf, 0.0, 14.0, 7.0, 11.0, 5.0, 9.0, 19.0 }, { 1.0, 5.0, inf, -2.0, 0.0, 1.0, 5.0, 3.0, 3.0, 13.0 }, { 0.0, 4.0, inf, -3.0, 11.0, 0.0, 4.0, 2.0, 2.0, 12.0 }, { 7.0, 11.0, inf, 4.0, 18.0, 7.0, 0.0, 9.0, 9.0, 19.0 }, { 2.0, 6.0, inf, -1.0, 13.0, 2.0, 6.0, 0.0, 4.0, 14.0 }, { -2.0, 2.0, inf, -5.0, 9.0, 2.0, 6.0, 0.0, 0.0, 10.0 }, { 10.0, 14.0, inf, 7.0, 21.0, 10.0, 3.0, 12.0, 12.0, 0.0 } }; Dictionary <(Vertex <int>, Vertex <int>), double> expected = fromMatrix(undirectedGraph, distances); directedGraph.AddEdgeBetween(directedGraph[8], directedGraph[3], new Weighted(-5.0)); // when Dictionary <(Vertex <int>, Vertex <int>), double> result = ShortestPaths.FloydWarshall(directedGraph); // then result.Should().BeEquivalentTo(expected); }
public void Dijkstra_WhenNegativeEdge_ThenIllegalStateException() { // given directedGraph.AddEdgeBetween(directedGraph[8], directedGraph[3], new Weighted(-5.0)); // when Action action = () => ShortestPaths.Dijkstra(directedGraph, directedGraph[1]); // then action.Should().Throw <InvalidOperationException>(); }
public void BellmanFord_WhenNegativeCycle_ThenIllegalStateException() { // given directedGraph.AddEdgeBetween(directedGraph[8], directedGraph[3], new Weighted(-20.0)); // when Action action = () => ShortestPaths.BellmanFord(directedGraph, directedGraph[1]); // then action.Should().Throw <InvalidOperationException>(); }
public void BellmanFord_WhenDirectedGraph_ThenShortestPathsLengths() { // given var distances = new List <double> { 20.0, 0.0, inf, 17.0, 7.0, 8.0, 12.0, 12.0, 10.0, 20.0 }; Dictionary <Vertex <int>, double> expected = fromList(directedGraph, distances); // when Dictionary <Vertex <int>, double> result = ShortestPaths.BellmanFord(directedGraph, directedGraph[1]); // then result.Should().BeEquivalentTo(expected); }
public void Dijkstra_WhenUndirectedGraph_ThenShortestPathsLengths() { // given var distances = new List <double> { 4.0, 0.0, inf, 7.0, 7.0, 8.0, inf, 10.0, 10.0, inf }; Dictionary <Vertex <int>, double> expected = fromList(undirectedGraph, distances); // when Dictionary <Vertex <int>, double> result = ShortestPaths.Dijkstra(undirectedGraph, undirectedGraph[1]); // then result.Should().BeEquivalentTo(expected); }
public void BellmanFord_WhenNegativeEdge_ThenEdgeIncluded() { // given var distances = new List <double> { 8.0, 0.0, inf, 5.0, 7.0, 8.0, 12.0, 10.0, 10.0, 20.0 }; Dictionary <Vertex <int>, double> expected = fromList(directedGraph, distances); directedGraph.AddEdgeBetween(directedGraph[8], directedGraph[3], new Weighted(-5.0)); // when Dictionary <Vertex <int>, double> result = ShortestPaths.BellmanFord(directedGraph, directedGraph[1]); // then result.Should().BeEquivalentTo(expected); }
public void FloydWarshall_WhenUndirectedGraph_ThenAllShortestPathsLengths() { // given double[,] distances = new double[, ] { { 0.0, 4.0, inf, 3.0, 11.0, 10.0, inf, 8.0, 12.0, inf }, { 4.0, 0.0, inf, 7.0, 7.0, 8.0, inf, 10.0, 10.0, inf }, { inf, inf, 0.0, inf, inf, inf, 8.0, inf, inf, 11.0 }, { 3.0, 7.0, inf, 0.0, 8.0, 7.0, inf, 5.0, 9.0, inf }, { 11.0, 7.0, inf, 8.0, 0.0, 1.0, inf, 3.0, 3.0, inf }, { 10, 8, inf, 7.0, 1.0, 0.0, inf, 2.0, 2.0, inf }, { inf, inf, 8.0, inf, inf, inf, 0.0, inf, inf, 3.0 }, { 8.0, 10.0, inf, 5.0, 3.0, 2.0, inf, 0.0, 4.0, inf }, { 12.0, 10.0, inf, 9.0, 3.0, 2.0, inf, 4.0, 0.0, inf }, { inf, inf, 11.0, inf, inf, inf, 3.0, inf, inf, 0.0 } }; Dictionary <(Vertex <int>, Vertex <int>), double> expected = fromMatrix(undirectedGraph, distances); // when Dictionary <(Vertex <int>, Vertex <int>), double> result = ShortestPaths.FloydWarshall(undirectedGraph.AsDirected()); // then result.Should().BeEquivalentTo(expected); }