private IVertexColorizerAlgorithm <int> CreateAlgorithmAndMaybeDoComputation( [NotNull] ContractScenario scenario) { var instantiateAlgorithm = GetAlgorithmFactory(); return(instantiateAlgorithm(scenario)); }
protected IDistancesCollection <T> CreateAlgorithmAndMaybeDoComputation <T>( [NotNull] ContractScenario <T> scenario) { Func <ContractScenario <T>, IDistancesCollection <T> > instantiateAlgorithm = GetAlgorithmFactory <T>(); return(instantiateAlgorithm(scenario)); }
public void ColorReturned_WhenVertexIsAccessibleFromRoot() { var scenario = new ContractScenario { EdgesInGraph = new[] { new Edge <int>(1, 2) }, AccessibleVerticesFromRoot = new[] { 2 }, Root = 1, DoComputation = true }; IVertexColorizerAlgorithm <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario); Assert.IsNotNull(algorithm.GetVertexColor(2)); }
public void NoExceptionThrown_WhenVertexIsAccessibleFromRoot() { var scenario = new ContractScenario <int> { EdgesInGraph = new[] { new Edge <int>(1, 2) }, AccessibleVerticesFromRoot = new[] { 2 }, Root = 1, DoComputation = true }; IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario); Assert.DoesNotThrow(() => { double _ = algorithm.GetDistance(2); }); }
public void ExceptionThrown_WhenVertexDoesNotExistInGraph() { var scenario = new ContractScenario { EdgesInGraph = new[] { new Edge <int>(1, 2) }, AccessibleVerticesFromRoot = new[] { 2 }, Root = 1, DoComputation = true }; IVertexColorizerAlgorithm <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario); Assert.Throws <VertexNotFoundException>(() => algorithm.GetVertexColor(3)); }
public void NoExceptionThrown_WhenVertexExistsButIsInaccessibleFromRoot() { var scenario = new ContractScenario <int> { EdgesInGraph = new[] { new Edge <int>(1, 2) }, SingleVerticesInGraph = new[] { 3 }, AccessibleVerticesFromRoot = new[] { 2 }, Root = 1, DoComputation = true }; IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario); double _ = algorithm.GetDistance(3); }
public void ExceptionThrown_WhenAlgorithmHasNotYetBeenComputed() { var scenario = new ContractScenario <int> { EdgesInGraph = new[] { new Edge <int>(1, 2) }, SingleVerticesInGraph = new int[0], AccessibleVerticesFromRoot = new[] { 2 }, Root = 1, DoComputation = false }; IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario); Assert.Throws <InvalidOperationException>(() => algorithm.TryGetDistance(2, out _)); }
public static UndirectedBreadthFirstSearchAlgorithm <T, Edge <T> > CreateAlgorithmAndMaybeDoComputation <T>( [NotNull] ContractScenario <T> scenario) { var graph = new UndirectedGraph <T, Edge <T> >(); graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new Edge <T>(e.Source, e.Target))); graph.AddVertexRange(scenario.SingleVerticesInGraph); var algorithm = new UndirectedBreadthFirstSearchAlgorithm <T, Edge <T> >(graph); if (scenario.DoComputation) { algorithm.Compute(scenario.Root); } return(algorithm); }
public void DistanceReturned_WhenVertexIsAccessibleFromRoot() { var scenario = new ContractScenario <int> { EdgesInGraph = new[] { new Edge <int>(1, 2) }, AccessibleVerticesFromRoot = new[] { 2 }, Root = 1, DoComputation = true }; IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario); bool distanceFound = algorithm.TryGetDistance(2, out _); Assert.True(distanceFound, "Distance should have been found since the vertex is accessible from root."); }
public void NoDistanceFound_WhenVertexDoesNotExistInGraph() { var scenario = new ContractScenario <int> { EdgesInGraph = new[] { new Edge <int>(1, 2) }, AccessibleVerticesFromRoot = new[] { 2 }, Root = 1, DoComputation = true }; IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario); bool distanceFound = algorithm.TryGetDistance(3, out _); Assert.False(distanceFound, "No distance should have been found since the vertex does not exist."); }
public void ExceptionThrown_WhenTargetVertexIsNull() { var scenario = new ContractScenario <string> { EdgesInGraph = new[] { new Edge <string>("1", "2") }, SingleVerticesInGraph = new string[0], AccessibleVerticesFromRoot = new[] { "2" }, Root = "1", DoComputation = false }; IDistancesCollection <string> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario); // ReSharper disable once AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>(() => { double _ = algorithm.GetDistance(null); }); }
public static BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> > CreateAlgorithmAndMaybeDoComputation( [NotNull] ContractScenario scenario) { var graph = new BidirectionalGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new Edge <int>(e.Source, e.Target))); graph.AddVertexRange(scenario.SingleVerticesInGraph); var algorithm = new BidirectionalDepthFirstSearchAlgorithm <int, Edge <int> >(graph); if (scenario.DoComputation) { algorithm.Compute(scenario.Root); } return(algorithm); }
public static CyclePoppingRandomTreeAlgorithm <T, Edge <T> > CreateAlgorithmAndMaybeDoComputation <T>( [NotNull] ContractScenario <T> scenario) { var graph = new AdjacencyGraph <T, Edge <T> >(); graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new Edge <T>(e.Source, e.Target))); graph.AddVertexRange(scenario.SingleVerticesInGraph); var chain = new NormalizedMarkovEdgeChain <T, Edge <T> >(); var algorithm = new CyclePoppingRandomTreeAlgorithm <T, Edge <T> >(graph, chain); if (scenario.DoComputation) { algorithm.Compute(scenario.Root); } return(algorithm); }
public void EmptyCollectionReturned_WhenAlgorithmHasNotYetBeenRun() { var scenario = new ContractScenario <int> { EdgesInGraph = new[] { new Edge <int>(1, 2) }, SingleVerticesInGraph = new[] { 3 }, AccessibleVerticesFromRoot = new[] { 2 }, Root = 1, DoComputation = false }; IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario); IEnumerable <KeyValuePair <int, double> > distances = algorithm.GetDistances(); CollectionAssert.IsEmpty(distances); }
public void DistancesForAllVerticesInGraphReturnedWhenAlgorithmHasBeenRun() { var scenario = new ContractScenario <int> { EdgesInGraph = new[] { new Edge <int>(1, 2) }, SingleVerticesInGraph = new[] { 3 }, AccessibleVerticesFromRoot = new[] { 2 }, Root = 1, DoComputation = true }; IDistancesCollection <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario); IEnumerable <KeyValuePair <int, double> > distances = algorithm.GetDistances(); CollectionAssert.AreEquivalent(new[] { 1, 2, 3 }, distances.Select(pair => pair.Key)); }
public static TSP <T, EquatableEdge <T>, BidirectionalGraph <T, EquatableEdge <T> > > CreateAlgorithmAndMaybeDoComputation <T>( [NotNull] ContractScenario <T> scenario) { var graph = new BidirectionalGraph <T, EquatableEdge <T> >(); graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new EquatableEdge <T>(e.Source, e.Target))); graph.AddVertexRange(scenario.SingleVerticesInGraph); double Weights(Edge <T> e) => 1.0; var algorithm = new TSP <T, EquatableEdge <T>, BidirectionalGraph <T, EquatableEdge <T> > >(graph, Weights); if (scenario.DoComputation) { algorithm.Compute(scenario.Root); } return(algorithm); }
public static BellmanFordShortestPathAlgorithm <T, Edge <T> > CreateAlgorithmAndMaybeDoComputation <T>( [NotNull] ContractScenario <T> scenario) { var graph = new AdjacencyGraph <T, Edge <T> >(); graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new Edge <T>(e.Source, e.Target))); graph.AddVertexRange(scenario.SingleVerticesInGraph); double Weights(Edge <T> e) => 1.0; var algorithm = new BellmanFordShortestPathAlgorithm <T, Edge <T> >(graph, Weights); if (scenario.DoComputation) { algorithm.Compute(scenario.Root); } return(algorithm); }
public static UndirectedDijkstraShortestPathAlgorithm <int, Edge <int> > CreateAlgorithmAndMaybeDoComputation( [NotNull] ContractScenario scenario) { var graph = new UndirectedGraph <int, Edge <int> >(); graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new Edge <int>(e.Source, e.Target))); graph.AddVertexRange(scenario.SingleVerticesInGraph); double Weights(Edge <int> e) => 1.0; var algorithm = new UndirectedDijkstraShortestPathAlgorithm <int, Edge <int> >(graph, Weights); if (scenario.DoComputation) { algorithm.Compute(scenario.Root); } return(algorithm); }
public static EdmondsKarpMaximumFlowAlgorithm <T, Edge <T> > CreateAlgorithmAndMaybeDoComputation <T>( [NotNull] ContractScenario <T> scenario) { var graph = new AdjacencyGraph <T, Edge <T> >(); graph.AddVerticesAndEdgeRange(scenario.EdgesInGraph.Select(e => new Edge <T>(e.Source, e.Target))); graph.AddVertexRange(scenario.SingleVerticesInGraph); double Capacities(Edge <T> edge) => 1.0; Edge <T> EdgeFactory(T source, T target) => new Edge <T>(source, target); var reverseEdgesAlgorithm = new ReversedEdgeAugmentorAlgorithm <T, Edge <T> >(graph, EdgeFactory); reverseEdgesAlgorithm.AddReversedEdges(); var algorithm = new EdmondsKarpMaximumFlowAlgorithm <T, Edge <T> >(graph, Capacities, EdgeFactory, reverseEdgesAlgorithm); if (scenario.DoComputation) { algorithm.Compute(scenario.Root, scenario.AccessibleVerticesFromRoot.First()); } return(algorithm); }
public void ExceptionThrown_WhenAlgorithmHasNotYetBeenComputed() { var scenario = new ContractScenario { EdgesInGraph = new[] { new Edge <int>(1, 2) }, SingleVerticesInGraph = new int[0], AccessibleVerticesFromRoot = new[] { 2 }, Root = 1, DoComputation = false }; IVertexColorizerAlgorithm <int> algorithm = CreateAlgorithmAndMaybeDoComputation(scenario); Type expectedExceptionType = GetExpectedExceptionType(); Assert.Throws(expectedExceptionType, () => algorithm.GetVertexColor(2)); #region Local function Type GetExpectedExceptionType() { switch (_testedAlgorithm) { case var _ when _testedAlgorithm == typeof(AStarShortestPathAlgorithm <,>): case var _ when _testedAlgorithm == typeof(BellmanFordShortestPathAlgorithm <,>): case var _ when _testedAlgorithm == typeof(DagShortestPathAlgorithm <,>): case var _ when _testedAlgorithm == typeof(DijkstraShortestPathAlgorithm <,>): case var _ when _testedAlgorithm == typeof(UndirectedDijkstraShortestPathAlgorithm <,>): return(typeof(NullReferenceException)); } return(typeof(VertexNotFoundException)); } #endregion }