コード例 #1
0
        public void ClearRootVertex()
        {
            var graph     = new AdjacencyGraph <int, Edge <int> >();
            var algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(graph, _ => 1.0);

            ClearRootVertex_Test(algorithm);
        }
コード例 #2
0
        public void SetRootVertex_Throws()
        {
            var graph     = new AdjacencyGraph <TestVertex, Edge <TestVertex> >();
            var algorithm = new DagShortestPathAlgorithm <TestVertex, Edge <TestVertex> >(graph, _ => 1.0);

            SetRootVertex_Throws_Test(algorithm);
        }
コード例 #3
0
        public void ComputeWithRoot()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVertex(0);
            var algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(graph, _ => 1.0);

            ComputeWithRoot_Test(algorithm);
        }
コード例 #4
0
        public void TryGetDistance()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVertex(1);
            var algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(graph, edge => 1.0);

            TryGetDistance_Test(algorithm);
        }
        public void GetVertexColor_Throws()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVertex(0);
            var algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(graph, edge => 1.0);

            algorithm.Compute(0);

            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <VertexNotFoundException>(() => algorithm.GetVertexColor(1));
        }
コード例 #6
0
        private static void RunDagShortestPathAndCheck <TVertex, TEdge>(
            [NotNull] IVertexListGraph <TVertex, TEdge> graph,
            [NotNull] TVertex root,
            [NotNull] IDistanceRelaxer relaxer)
            where TEdge : IEdge <TVertex>
        {
            var algorithm = new DagShortestPathAlgorithm <TVertex, TEdge>(
                graph,
                _ => 1.0,
                relaxer);

            algorithm.InitializeVertex += vertex =>
            {
                Assert.AreEqual(GraphColor.White, algorithm.VerticesColors[vertex]);
            };

            algorithm.DiscoverVertex += vertex =>
            {
                Assert.AreEqual(GraphColor.Gray, algorithm.VerticesColors[vertex]);
            };

            algorithm.StartVertex += vertex =>
            {
                Assert.AreNotEqual(GraphColor.Black, algorithm.VerticesColors[vertex]);
            };

            algorithm.ExamineVertex += vertex =>
            {
                Assert.AreEqual(GraphColor.Gray, algorithm.VerticesColors[vertex]);
            };

            algorithm.FinishVertex += vertex =>
            {
                Assert.AreEqual(GraphColor.Black, algorithm.VerticesColors[vertex]);
            };

            var predecessors = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

            using (predecessors.Attach(algorithm))
                algorithm.Compute(root);

            Assert.AreEqual(graph.VertexCount, algorithm.VerticesColors.Count);
            foreach (TVertex vertex in graph.Vertices)
            {
                Assert.AreEqual(GraphColor.Black, algorithm.VerticesColors[vertex]);
            }

            CollectionAssert.IsNotEmpty(algorithm.GetDistances());
            Assert.AreEqual(graph.VertexCount, algorithm.GetDistances().Count());

            Verify(algorithm, predecessors);
        }
コード例 #7
0
        public static (bool, IDictionary <string, double>) Get(AdjacencyGraph <string, TaggedEdge <string, string> > graph, string root)
        {
            if (!graph.IsDirectedAcyclicGraph())
            {
                return(false, new Dictionary <string, double>());
            }

            Func <TaggedEdge <string, string>, double> Weights = e => double.Parse(e.Tag);
            var algorithm = new DagShortestPathAlgorithm <string, TaggedEdge <string, string> >(graph, Weights);

            algorithm.Compute(root);
            return(true, algorithm.Distances);
        }
コード例 #8
0
        public static (bool, IDictionary <int, double>) Get(AdjacencyGraph <int, Edge <int> > graph, int root)
        {
            if (!graph.IsDirectedAcyclicGraph())
            {
                return(false, new Dictionary <int, double>());
            }

            Func <Edge <int>, double> Weights = e => 1.0;
            var algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(graph, Weights);

            algorithm.Compute(root);
            return(true, algorithm.Distances);
        }
コード例 #9
0
        public void GetVertexColor()
        {
            var graph = new AdjacencyGraph <int, Edge <int> >();

            graph.AddVerticesAndEdge(new Edge <int>(1, 2));

            var algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(graph, _ => 1.0);

            algorithm.Compute(1);

            Assert.AreEqual(GraphColor.Black, algorithm.GetVertexColor(1));
            Assert.AreEqual(GraphColor.Black, algorithm.GetVertexColor(2));
        }
コード例 #10
0
        public void Constructor()
        {
            Func <Edge <int>, double> Weights = _ => 1.0;

            var graph     = new AdjacencyGraph <int, Edge <int> >();
            var algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(graph, Weights);

            AssertAlgorithmProperties(algorithm, graph, Weights);

            algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(graph, Weights, DistanceRelaxers.CriticalDistance);
            AssertAlgorithmProperties(algorithm, graph, Weights, DistanceRelaxers.CriticalDistance);

            algorithm = new DagShortestPathAlgorithm <int, Edge <int> >(null, graph, Weights, DistanceRelaxers.CriticalDistance);
            AssertAlgorithmProperties(algorithm, graph, Weights, DistanceRelaxers.CriticalDistance);

            #region Local function

            void AssertAlgorithmProperties <TVertex, TEdge>(
                DagShortestPathAlgorithm <TVertex, TEdge> algo,
                IVertexListGraph <TVertex, TEdge> g,
                Func <TEdge, double> eWeights = null,
                IDistanceRelaxer relaxer      = null)
                where TEdge : IEdge <TVertex>
            {
                AssertAlgorithmState(algo, g);
                Assert.IsNull(algo.VerticesColors);
                if (eWeights is null)
                {
                    Assert.IsNotNull(algo.Weights);
                }
                else
                {
                    Assert.AreSame(eWeights, algo.Weights);
                }
                CollectionAssert.IsEmpty(algo.GetDistances());
                if (relaxer is null)
                {
                    Assert.IsNotNull(algo.DistanceRelaxer);
                }
                else
                {
                    Assert.AreSame(relaxer, algo.DistanceRelaxer);
                }
            }

            #endregion
        }
コード例 #11
0
        private static void Search <TVertex, TEdge>(
            [NotNull] IVertexListGraph <TVertex, TEdge> graph,
            [NotNull] TVertex root,
            [NotNull] IDistanceRelaxer relaxer)
            where TEdge : IEdge <TVertex>
        {
            var algorithm = new DagShortestPathAlgorithm <TVertex, TEdge>(
                graph,
                e => 1,
                relaxer);
            var predecessors = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

            using (predecessors.Attach(algorithm))
                algorithm.Compute(root);

            Verify(algorithm, predecessors);
        }
コード例 #12
0
        public static DagShortestPathAlgorithm <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 DagShortestPathAlgorithm <T, Edge <T> >(graph, Weights);

            if (scenario.DoComputation)
            {
                algorithm.Compute(scenario.Root);
            }
            return(algorithm);
        }
コード例 #13
0
 private static void Verify <TVertex, TEdge>(
     [NotNull] DagShortestPathAlgorithm <TVertex, TEdge> algorithm,
     [NotNull] VertexPredecessorRecorderObserver <TVertex, TEdge> predecessors)
     where TEdge : IEdge <TVertex>
 {
     // Verify the result
     foreach (TVertex vertex in algorithm.VisitedGraph.Vertices)
     {
         if (!predecessors.VertexPredecessors.TryGetValue(vertex, out TEdge predecessor))
         {
             continue;
         }
         if (predecessor.Source.Equals(vertex))
         {
             continue;
         }
         Assert.AreEqual(algorithm.Distances[vertex], algorithm.Distances[predecessor.Source] + 1);
     }
 }
 private static void Verify <TVertex, TEdge>(
     DagShortestPathAlgorithm <TVertex, TEdge> algorithm,
     VertexPredecessorRecorderObserver <TVertex, TEdge> predecessors)
     where TEdge : IEdge <TVertex>
 {
     // Verify the result
     foreach (TVertex vertex in algorithm.VisitedGraph.Vertices)
     {
         if (!predecessors.VerticesPredecessors.TryGetValue(vertex, out TEdge predecessor))
         {
             continue;
         }
         if (predecessor.Source.Equals(vertex))
         {
             continue;
         }
         Assert.AreEqual(
             algorithm.TryGetDistance(vertex, out double currentDistance),
             algorithm.TryGetDistance(predecessor.Source, out double predecessorDistance));
         Assert.AreEqual(predecessorDistance + 1, currentDistance);
     }
 }
コード例 #15
0
        public static TryFunc <TVertex, IEnumerable <TEdge> > ShortestPathsDag <TVertex, TEdge>
            (this IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph,
            Func <TEdge, double> edgeWeights,
            TVertex source
            )
            where TEdge : IEdge <TVertex>
        {
            Contract.Requires(visitedGraph != null);
            Contract.Requires(edgeWeights != null);
            Contract.Requires(source != null);

            var algorithm           = new DagShortestPathAlgorithm <TVertex, TEdge>(visitedGraph, edgeWeights);
            var predecessorRecorder = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

            using (predecessorRecorder.Attach(algorithm))
                algorithm.Compute(source);

            var predecessors = predecessorRecorder.VertexPredecessors;

            return(delegate(TVertex v, out IEnumerable <TEdge> edges)
            {
                return EdgeExtensions.TryGetPath(predecessors, v, out edges);
            });
        }