public static CompressedSparseRowGraph <TVertex> FromGraph <TEdge>(
            IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph
            )
            where TEdge : IEdge <TVertex>
        {
            Contract.Requires(visitedGraph != null);
            Contract.Ensures(Contract.Result <CompressedSparseRowGraph <TVertex> >() != null);

            var outEdgeStartRanges = new Dictionary <TVertex, Range>(visitedGraph.VertexCount);
            var outEdges           = new TVertex[visitedGraph.EdgeCount];

            int start = 0;
            int end   = 0;
            int index = 0;

            foreach (var vertex in visitedGraph.Vertices)
            {
                end = index + visitedGraph.OutDegree(vertex);
                var range = new Range(start, end);
                outEdgeStartRanges.Add(vertex, range);
                foreach (var edge in visitedGraph.OutEdges(vertex))
                {
                    outEdges[index++] = edge.Target;
                }
                Contract.Assert(index == end);
            }
            Contract.Assert(index == outEdges.Length);

            return(new CompressedSparseRowGraph <TVertex>(
                       outEdgeStartRanges,
                       outEdges));
        }
        public static CompressedSparseRowGraph <TVertex> FromGraph <TEdge>(
            [NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> visitedGraph)
            where TEdge : IEdge <TVertex>
        {
            if (visitedGraph is null)
            {
                throw new ArgumentNullException(nameof(visitedGraph));
            }

            var outEdgeStartRanges = new Dictionary <TVertex, Range>(visitedGraph.VertexCount);
            var outEdges           = new TVertex[visitedGraph.EdgeCount];

            int start = 0;
            int index = 0;

            foreach (TVertex vertex in visitedGraph.Vertices)
            {
                int end   = start + visitedGraph.OutDegree(vertex);
                var range = new Range(start, end);
                outEdgeStartRanges.Add(vertex, range);

                foreach (TEdge edge in visitedGraph.OutEdges(vertex))
                {
                    outEdges[index++] = edge.Target;
                }

                start = end;
                Debug.Assert(index == end);
            }

            Debug.Assert(index == outEdges.Length);

            return(new CompressedSparseRowGraph <TVertex>(outEdgeStartRanges, outEdges));
        }
示例#3
0
        private static void OutDegreeSumEqualsEdgeCount <TVertex, TEdge>(
            [NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> graph)
            where TEdge : IEdge <TVertex>
        {
            int totalOutDegree = 0;

            foreach (TVertex vertex in graph.Vertices)
            {
                totalOutDegree += graph.OutDegree(vertex);
            }

            Assert.AreEqual(graph.EdgeCount, totalOutDegree);
        }
示例#4
0
        private static void RunAStarAndCheck <TVertex, TEdge>(
            [NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> graph,
            [NotNull] TVertex root)
            where TEdge : IEdge <TVertex>
        {
            var distances = new Dictionary <TEdge, double>();

            foreach (TEdge edge in graph.Edges)
            {
                distances[edge] = graph.OutDegree(edge.Source) + 1;
            }

            var algorithm = new AStarShortestPathAlgorithm <TVertex, TEdge>(
                graph,
                e => distances[e],
                v => 0.0);

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

            algorithm.DiscoverVertex += 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.IsNotNull(algorithm.Distances);
            Assert.AreEqual(graph.VertexCount, algorithm.Distances.Count);

            Verify(algorithm, predecessors);
        }
示例#5
0
        private void Dijkstra <TVertex, TEdge>(IVertexAndEdgeListGraph <TVertex, TEdge> g, TVertex root)
            where TEdge : IEdge <TVertex>
        {
            var distances = new Dictionary <TEdge, double>(g.EdgeCount);

            foreach (var e in g.Edges)
            {
                distances[e] = g.OutDegree(e.Source) + 1;
            }

            var algo = new DijkstraShortestPathAlgorithm <TVertex, TEdge>(
                g,
                e => distances[e]
                );
            var predecessors = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

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

            Verify(algo, predecessors);
        }
        public void RunAStarAndCheck <TVertex, TEdge>([NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> graph, [NotNull] TVertex root)
            where TEdge : IEdge <TVertex>
        {
            var distances = new Dictionary <TEdge, double>();

            foreach (TEdge edge in graph.Edges)
            {
                distances[edge] = graph.OutDegree(edge.Source) + 1;
            }

            var algorithm = new AStarShortestPathAlgorithm <TVertex, TEdge>(
                graph,
                e => distances[e],
                v => 0);
            var predecessors = new VertexPredecessorRecorderObserver <TVertex, TEdge>();

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

            Verify(algorithm, predecessors);
        }
示例#7
0
        private static void RunBellmanFordAndCheck <TVertex, TEdge>(
            [NotNull] IVertexAndEdgeListGraph <TVertex, TEdge> graph,
            [NotNull] TVertex root)
            where TEdge : IEdge <TVertex>
        {
            var distances = new Dictionary <TEdge, double>();

            foreach (TEdge edge in graph.Edges)
            {
                distances[edge] = graph.OutDegree(edge.Source) + 1;
            }

            var algorithm = new BellmanFordShortestPathAlgorithm <TVertex, TEdge>(
                graph,
                e => distances[e]);

            algorithm.InitializeVertex += vertex =>
            {
                Assert.AreEqual(GraphColor.White, 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]);
            }

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

            Verify(algorithm, predecessors);
        }
 /// <inheritdoc />
 public int OutDegree(TVertex vertex)
 {
     return(_baseGraph.OutDegree(vertex));
 }