/// <summary>
        /// Initializes the private _graph field which stores the graph that
        /// we operate on.
        /// </summary>
        private void InitTheGraph()
        {
            //make a copy of the original graph
            _graph = new BidirectionalGraph <SugiVertex, SugiEdge>();

            //copy the vertices
            foreach (var vertex in VisitedGraph.Vertices)
            {
                Size size = new Size();
                if (_vertexSizes != null)
                {
                    _vertexSizes.TryGetValue(vertex, out size);
                }

                var vertexWrapper = new SugiVertex(vertex, size);
                _graph.AddVertex(vertexWrapper);
                _vertexMap[vertex] = vertexWrapper;
            }

            //copy the edges
            foreach (var edge in VisitedGraph.Edges)
            {
                var edgeWrapper = new SugiEdge(edge, _vertexMap[edge.Source], _vertexMap[edge.Target]);
                _graph.AddEdge(edgeWrapper);
            }
        }
        protected static void InEdges_Test(
            IMutableBidirectionalGraph <int, Edge <int> > graph)
        {
            var edge12 = new Edge <int>(1, 2);
            var edge13 = new Edge <int>(1, 3);
            var edge14 = new Edge <int>(1, 4);
            var edge24 = new Edge <int>(2, 4);
            var edge32 = new Edge <int>(3, 2);
            var edge33 = new Edge <int>(3, 3);

            graph.AddVertex(1);
            AssertNoInEdge(graph, 1);
            AssertNoOutEdge(graph, 1);

            graph.AddVerticesAndEdgeRange(new[] { edge12, edge13, edge14, edge24, edge32, edge33 });

            AssertHasOutEdges(graph, 1, new[] { edge12, edge13, edge14 });
            AssertHasOutEdges(graph, 2, new[] { edge24 });
            AssertHasOutEdges(graph, 3, new[] { edge32, edge33 });
            AssertNoOutEdge(graph, 4);

            AssertNoInEdge(graph, 1);
            AssertHasInEdges(graph, 2, new[] { edge12, edge32 });
            AssertHasInEdges(graph, 3, new[] { edge13, edge33 });
            AssertHasInEdges(graph, 4, new[] { edge14, edge24 });
        }
        protected static void InEdge_Throws_Test(
            IMutableBidirectionalGraph <int, Edge <int> > graph)
        {
            const int vertex1 = 1;
            const int vertex2 = 2;

            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            Assert.Throws <VertexNotFoundException>(() => graph.InEdge(vertex1, 0));

            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);
            AssertIndexOutOfRange(() => graph.InEdge(vertex1, 0));

            graph.AddEdge(new Edge <int>(1, 2));
            AssertIndexOutOfRange(() => graph.InEdge(vertex1, 5));
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
Beispiel #4
0
        public bool UnhideVertex(TVertex v)
        {
            //if v not hidden, it's an error
            if (!IsHiddenVertex(v))
            {
                return(false);
            }

            //unhide the vertex
            _graph.AddVertex(v);
            hiddenVertices.Remove(v);
            OnVertexUnhidden(v);
            return(true);
        }
        /// <inheritdoc />
        public bool UnhideVertex(TVertex vertex)
        {
            // If the vertex is not hidden, it's an error
            if (!IsHiddenVertex(vertex))
            {
                return(false);
            }

            // Unhide the vertex
            _graph.AddVertex(vertex);
            _hiddenVertices.Remove(vertex);
            OnVertexUnhidden(vertex);

            return(true);
        }
        protected static void Degree_Test(
            [NotNull] IMutableBidirectionalGraph <int, Edge <int> > graph)
        {
            var edge1 = new Edge <int>(1, 2);
            var edge2 = new Edge <int>(1, 3);
            var edge3 = new Edge <int>(1, 4);
            var edge4 = new Edge <int>(2, 4);
            var edge5 = new Edge <int>(3, 2);
            var edge6 = new Edge <int>(3, 3);

            graph.AddVerticesAndEdgeRange(new[] { edge1, edge2, edge3, edge4, edge5, edge6 });
            graph.AddVertex(5);

            Assert.AreEqual(3, graph.Degree(1));
            Assert.AreEqual(3, graph.Degree(2));
            Assert.AreEqual(4, graph.Degree(3)); // Self edge
            Assert.AreEqual(2, graph.Degree(4));
            Assert.AreEqual(0, graph.Degree(5));
        }
        public static void MergeVertices <TVertex, TEdge>(this IMutableBidirectionalGraph <TVertex, TEdge> graph,
                                                          IEnumerable <TVertex> verticesToMerge,
                                                          TVertex replacement,
                                                          Func <TEdge, TVertex, TEdge> mergeInFunc,
                                                          Func <TVertex, TEdge, TEdge> mergeOutFunc,
                                                          IEqualityComparer <TVertex> comparer = null,
                                                          bool checkExistence = false)
            where TEdge : IEdge <TVertex>
        {
            var verticies   = (checkExistence ? graph.Vertices.Intersect(verticesToMerge, comparer) : verticesToMerge).ToList();
            var allInEdges  = verticies.SelectMany(graph.InEdges);
            var allOutEdges = verticies.SelectMany(graph.OutEdges);
            var newInEdges  = allInEdges.Select(x => mergeInFunc(x, replacement));
            var newOutEdges = allOutEdges.Select(x => mergeOutFunc(replacement, x));

            verticies.ForEach(x => graph.RemoveVertex(x));
            graph.AddVertex(replacement);
            graph.AddEdgeRange(newInEdges);
            graph.AddEdgeRange(newOutEdges);
        }
        /// <summary>
        /// Initializes the <see cref="_graph"/> field which stores the graph that we operate on.
        /// </summary>
        private void CopyToWorkingGraph()
        {
            // Make a copy of the original graph
            _graph = new BidirectionalGraph <SugiVertex, SugiEdge>();

            // Copy the vertices
            foreach (TVertex vertex in VisitedGraph.Vertices)
            {
                var size = default(Size);
                _verticesSizes?.TryGetValue(vertex, out size);

                var vertexWrapper = new SugiVertex(vertex, size);
                _graph.AddVertex(vertexWrapper);
                _verticesMap[vertex] = vertexWrapper;
            }

            // Copy the edges
            foreach (TEdge edge in VisitedGraph.Edges)
            {
                var edgeWrapper = new SugiEdge(edge, _verticesMap[edge.Source], _verticesMap[edge.Target]);
                _graph.AddEdge(edgeWrapper);
            }
        }