public void BellmanFordNotReachable() { var run = new MoveTypeEdgeData { moveType = MoveType.Run }; var jump = new MoveTypeEdgeData { moveType = MoveType.Jump }; var wallJump = new MoveTypeEdgeData { moveType = MoveType.WallJump }; var myGraph = new DirectedWeightedGraph <char, MoveTypeEdgeData>(); myGraph.AddNodes('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); myGraph.AddEdges( ('a', 'b', 2, run), ('a', 'c', 3, run), ('a', 'e', 4, wallJump), ('b', 'd', 2, jump), ('b', 'e', 1, run), ('c', 'g', 4, jump), ('c', 'h', 11, run), ('d', 'c', 3, jump), ('d', 'h', 3, wallJump), ('f', 'd', 2, run), ('f', 'h', 6, wallJump), ('g', 'h', 7, run) ); myGraph .BellmanFordShortestPath('a', 'f') .Should() .BeEmpty(); }
public void BellmanFordSingleSourceShortestPathNegativeCycle() { var run = new MoveTypeEdgeData { moveType = MoveType.Run }; var jump = new MoveTypeEdgeData { moveType = MoveType.Jump }; var wallJump = new MoveTypeEdgeData { moveType = MoveType.WallJump }; var myGraph = new DirectedWeightedGraph <char, MoveTypeEdgeData>(); myGraph.AddNodes('a', 'b', 'c'); myGraph.AddEdges( ('a', 'b', -2, run), ('b', 'c', -3, run), ('c', 'a', -1, jump) ); myGraph .Invoking(x => x.BellmanFordSingleSourceShortestPath('a').Count()) .Should(). Throw <NegativeCycleException>(); }
public void DijkstraNegativeWeight() { var run = new MoveTypeEdgeData { moveType = MoveType.Run }; var jump = new MoveTypeEdgeData { moveType = MoveType.Jump }; var wallJump = new MoveTypeEdgeData { moveType = MoveType.WallJump }; var myGraph = new DirectedWeightedGraph <char, MoveTypeEdgeData>(); myGraph.AddNodes('a', 'b', 'c', 'd'); myGraph.AddEdges( ('a', 'b', -2, run), ('a', 'c', 3, run), ('b', 'd', 2, jump), ('d', 'c', -3, jump) ); myGraph .Invoking(x => x.DijkstraShortestPath('a', 'd').Count()) .Should() .Throw <NegativeWeightException>(); }
public void BellmanFordSingleSourceShortestPath() { var run = new MoveTypeEdgeData { moveType = MoveType.Run }; var jump = new MoveTypeEdgeData { moveType = MoveType.Jump }; var wallJump = new MoveTypeEdgeData { moveType = MoveType.WallJump }; var myGraph = new DirectedWeightedMultiGraph <char, MoveTypeEdgeData>(); myGraph.AddNodes('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); var edgeA = myGraph.AddEdge('a', 'b', 2, run); var edgeB = myGraph.AddEdge('a', 'c', 1, jump); var edgeC = myGraph.AddEdge('b', 'd', 2, jump); var edgeD = myGraph.AddEdge('b', 'e', 1, run); var edgeE = myGraph.AddEdge('d', 'f', 2, run); var edgeF = myGraph.AddEdge('c', 'g', 2, run); var edgeG = myGraph.AddEdge('d', 'h', 3, wallJump); myGraph.AddEdges( ('a', 'c', 3, run), ('a', 'e', 4, wallJump), ('b', 'd', 5, run), ('c', 'g', 4, jump), ('c', 'h', 11, run), ('d', 'c', 3, jump), ('e', 'f', 5, run), ('f', 'd', 2, run), ('f', 'h', 6, wallJump), ('g', 'h', 7, run), ('h', 'f', 1, jump), ('a', 'a', 3, jump) // cheeky lil self-edge ); myGraph .BellmanFordSingleSourceShortestPath('a') .Should() .Contain(('b', edgeA, 2)).And .Contain(('c', edgeB, 1)).And .Contain(('d', edgeC, 4)).And .Contain(('e', edgeD, 3)).And .Contain(('f', edgeE, 6)).And .Contain(('g', edgeF, 3)).And .Contain(('h', edgeG, 7)).And .HaveCount(7); // have to call Count() because otherwise the lazy evaluation wont trigger myGraph.Invoking(x => x.BellmanFordSingleSourceShortestPath('z').Count()).Should().Throw <System.ArgumentException>(); }
public void BellmanFordShortestPath() { var run = new MoveTypeEdgeData { moveType = MoveType.Run }; var jump = new MoveTypeEdgeData { moveType = MoveType.Jump }; var wallJump = new MoveTypeEdgeData { moveType = MoveType.WallJump }; var myGraph = new DirectedWeightedMultiGraph <char, MoveTypeEdgeData>(); myGraph.AddNodes('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); var edgeA = myGraph.AddEdge('a', 'b', 2, run); var edgeB = myGraph.AddEdge('a', 'c', 1, jump); var edgeC = myGraph.AddEdge('b', 'd', 2, jump); var edgeD = myGraph.AddEdge('b', 'e', 1, run); var edgeE = myGraph.AddEdge('d', 'f', 2, run); var edgeF = myGraph.AddEdge('c', 'g', 2, run); var edgeG = myGraph.AddEdge('d', 'h', 3, wallJump); myGraph.AddEdges( ('a', 'c', 3, run), ('a', 'e', 4, wallJump), ('b', 'd', 5, run), ('c', 'g', 4, jump), ('c', 'h', 11, run), ('d', 'c', 3, jump), ('e', 'f', 5, run), ('f', 'd', 2, run), ('f', 'h', 6, wallJump), ('g', 'h', 7, run), ('h', 'f', 1, jump), ('a', 'a', 3, jump) // cheeky lil self-edge ); myGraph .BellmanFordShortestPath('a', 'h') .Select(edgeID => myGraph.EdgeData(edgeID)) .Should() .ContainInOrder( run, jump, wallJump ) .And .HaveCount(3); myGraph.Invoking(x => x.BellmanFordShortestPath('a', 'z').Count()).Should().Throw <System.ArgumentException>(); }
public void BellmanFordSingleSourceShortestPathNegative() { var run = new MoveTypeEdgeData { moveType = MoveType.Run }; var jump = new MoveTypeEdgeData { moveType = MoveType.Jump }; var wallJump = new MoveTypeEdgeData { moveType = MoveType.WallJump }; var myGraph = new DirectedWeightedMultiGraph <char, MoveTypeEdgeData>(); myGraph.AddNodes('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); var edgeA = myGraph.AddEdge('a', 'b', 2, run); var edgeB = myGraph.AddEdge('a', 'c', 1, jump); var edgeC = myGraph.AddEdge('b', 'd', -1, jump); var edgeD = myGraph.AddEdge('b', 'e', 1, run); var edgeE = myGraph.AddEdge('d', 'f', 2, run); var edgeF = myGraph.AddEdge('c', 'g', 2, run); var edgeG = myGraph.AddEdge('d', 'h', 3, wallJump); myGraph.AddEdges( ('a', 'c', 3, run), ('a', 'e', 4, wallJump), ('b', 'd', 5, run), ('c', 'g', 4, jump), ('c', 'h', 11, run), ('d', 'c', 3, jump), ('e', 'f', 5, run), ('f', 'd', 2, run), ('f', 'h', 6, wallJump), ('g', 'h', 7, run), ('h', 'f', 1, jump), ('a', 'a', 3, jump) // cheeky lil self-edge ); myGraph .BellmanFordSingleSourceShortestPath('a') .Should() .Contain(('b', edgeA, 2)).And .Contain(('c', edgeB, 1)).And .Contain(('d', edgeC, 1)).And .Contain(('e', edgeD, 3)).And .Contain(('f', edgeE, 3)).And .Contain(('g', edgeF, 3)).And .Contain(('h', edgeG, 4)).And .HaveCount(7); }
public void AStarShortestPath() { var run = new MoveTypeEdgeData { moveType = MoveType.Run }; var jump = new MoveTypeEdgeData { moveType = MoveType.Jump }; var wallJump = new MoveTypeEdgeData { moveType = MoveType.WallJump }; var myGraph = new DirectedWeightedMultiGraph <char, MoveTypeEdgeData>(); myGraph.AddNodes('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); myGraph.AddEdges( ('a', 'b', 2, run), ('a', 'c', 1, jump), ('a', 'c', 3, run), ('a', 'e', 4, wallJump), ('b', 'd', 2, jump), ('b', 'd', 5, run), ('b', 'e', 1, run), ('c', 'g', 2, run), ('c', 'g', 4, jump), ('c', 'h', 11, run), ('d', 'c', 3, jump), ('d', 'f', 2, run), ('d', 'h', 3, wallJump), ('e', 'f', 5, run), ('f', 'd', 2, run), ('f', 'h', 6, wallJump), ('g', 'h', 7, run), ('h', 'f', 1, jump), ('a', 'a', 3, jump) // cheeky lil self-edge ); myGraph .AStarShortestPath('a', 'h', (x, y) => 1) .Select(id => myGraph.EdgeData(id)) .Should() .ContainInOrder( run, jump, wallJump ) .And .HaveCount(3); // have to call Count() because otherwise the lazy evaluation wont trigger myGraph.Invoking(x => x.AStarShortestPath('a', 'z', (x, y) => 1).Count()).Should().Throw <System.ArgumentException>(); }
public void DijkstraSingleSourceShortestPath() { var run = new MoveTypeEdgeData { moveType = MoveType.Run }; var jump = new MoveTypeEdgeData { moveType = MoveType.Jump }; var wallJump = new MoveTypeEdgeData { moveType = MoveType.WallJump }; var myGraph = new DirectedWeightedGraph <char, MoveTypeEdgeData>(); myGraph.AddNodes('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); myGraph.AddEdges( ('a', 'b', 2, run), ('a', 'c', 3, run), ('a', 'e', 4, wallJump), ('b', 'd', 2, jump), ('b', 'e', 1, run), ('c', 'g', 4, jump), ('c', 'h', 11, run), ('d', 'c', 3, jump), ('d', 'f', 2, run), ('d', 'h', 3, wallJump), ('e', 'f', 5, run), ('f', 'd', 2, run), ('f', 'h', 6, wallJump), ('g', 'h', 7, run), ('h', 'f', 1, jump) ); myGraph .DijkstraSingleSourceShortestPath('a') .Should() .Contain(('b', 'a', 2)).And .Contain(('c', 'a', 3)).And .Contain(('d', 'b', 4)).And .Contain(('e', 'b', 3)).And .Contain(('f', 'd', 6)).And .Contain(('g', 'c', 7)).And .Contain(('h', 'd', 7)).And .HaveCount(7); // have to call Count() because otherwise the lazy evaluation wont trigger myGraph.Invoking(x => x.DijkstraSingleSourceShortestPath('z').Count()).Should().Throw <System.ArgumentException>(); }
public void BellmanFordShortestPath() { var run = new MoveTypeEdgeData { moveType = MoveType.Run }; var jump = new MoveTypeEdgeData { moveType = MoveType.Jump }; var wallJump = new MoveTypeEdgeData { moveType = MoveType.WallJump }; var myGraph = new DirectedWeightedGraph <char, MoveTypeEdgeData>(); myGraph.AddNodes('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); myGraph.AddEdges( ('a', 'b', 2, run), ('a', 'c', 3, run), ('a', 'e', 4, wallJump), ('b', 'd', 2, jump), ('b', 'e', 1, run), ('c', 'g', 4, jump), ('c', 'h', 11, run), ('d', 'c', 3, jump), ('d', 'f', 2, run), ('d', 'h', 3, wallJump), ('e', 'f', 5, run), ('f', 'd', 2, run), ('f', 'h', 6, wallJump), ('g', 'h', 7, run), ('h', 'f', 1, jump) ); myGraph .BellmanFordShortestPath('a', 'h') .Select(pair => myGraph.EdgeData(pair.Item1, pair.Item2)) .Should() .ContainInOrder( run, jump, wallJump ) .And .HaveCount(3); // have to call Count() because otherwise the lazy evaluation wont trigger myGraph.Invoking(x => x.BellmanFordShortestPath('a', 'z').Count()).Should().Throw <System.ArgumentException>(); }
public void BellmanFordSingleSourceShortestPathWithNegative() { var run = new MoveTypeEdgeData { moveType = MoveType.Run }; var jump = new MoveTypeEdgeData { moveType = MoveType.Jump }; var wallJump = new MoveTypeEdgeData { moveType = MoveType.WallJump }; var myGraph = new DirectedWeightedGraph <char, MoveTypeEdgeData>(); myGraph.AddNodes('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'); myGraph.AddEdges( ('a', 'b', 2, run), ('a', 'c', 3, run), ('a', 'e', 4, wallJump), ('b', 'd', -1, jump), ('b', 'e', 1, run), ('c', 'g', 4, jump), ('c', 'h', 11, run), ('d', 'c', 3, jump), ('d', 'f', 2, run), ('d', 'h', 3, wallJump), ('e', 'f', 5, run), ('f', 'd', 2, run), ('f', 'h', 6, wallJump), ('g', 'h', 7, run), ('h', 'f', 1, jump) ); myGraph .BellmanFordSingleSourceShortestPath('a') .Should() .Contain(('b', 'a', 2)).And .Contain(('c', 'a', 3)).And .Contain(('d', 'b', 1)).And .Contain(('e', 'b', 3)).And .Contain(('f', 'd', 3)).And .Contain(('g', 'c', 7)).And .Contain(('h', 'd', 4)).And .HaveCount(7); }