/// <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);
            }
        }
Esempio n. 2
0
        private void PutBackIsolatedVertices()
        {
            _sparseCompactionGraph.AddVertexRange(_isolatedVertices);
            _graph.AddVertexRange(_isolatedVertices);

            // Null if no edge exist (only isolated vertices graph)
            if (_sparseCompactionByLayerBackup != null)
            {
                int        layer = 0;
                SugiVertex prevIsolatedVertex = null;
                foreach (SugiVertex isolatedVertex in _isolatedVertices)
                {
                    _layers[layer].Add(isolatedVertex);
                    isolatedVertex.LayerIndex = layer;
                    isolatedVertex.Position   = _layers[layer].Count - 1;

                    Edge <Data> lastOnLayer = _sparseCompactionByLayerBackup[layer].LastOrDefault();
                    var         edge        = lastOnLayer is null
                        ? new Edge <Data>(_layers[layer][0], isolatedVertex)
                        : new Edge <Data>(lastOnLayer.Target, isolatedVertex);

                    _sparseCompactionByLayerBackup[layer].Add(edge);
                    _sparseCompactionGraph.AddEdge(edge);

                    if (layer > 0 && prevIsolatedVertex != null)
                    {
                        _graph.AddEdge(new SugiEdge(default(TEdge), prevIsolatedVertex, isolatedVertex));
                    }

                    layer = (layer + 1) % _layers.Count;
                    prevIsolatedVertex = isolatedVertex;
                }
            }
        }
Esempio n. 3
0
        private void PutbackIsolatedVertices()
        {
            _sparseCompactionGraph.AddVertexRange(_isolatedVertices.OfType <Data>());
            _graph.AddVertexRange(_isolatedVertices);
            int        layer = 0;
            SugiVertex prevIsolatedVertex = null;

            foreach (var isolatedVertex in _isolatedVertices)
            {
                var lastOnLayer = _sparseCompactionByLayerBackup[layer].LastOrDefault();
                _layers[layer].Add(isolatedVertex);
                isolatedVertex.LayerIndex = layer;
                isolatedVertex.Position   = _layers[layer].Count - 1;
                if (lastOnLayer != null)
                {
                    var edge = new Edge <Data>(lastOnLayer.Target, isolatedVertex);
                    _sparseCompactionByLayerBackup[layer].Add(edge);
                    _sparseCompactionGraph.AddEdge(edge);
                }
                if (layer > 0 && prevIsolatedVertex != null)
                {
                    _graph.AddEdge(new SugiEdge(default(TEdge), prevIsolatedVertex, isolatedVertex));
                }
                layer = (layer + 1) % _layers.Count;
                prevIsolatedVertex = isolatedVertex;
            }
        }
Esempio n. 4
0
        public LayeredTopologicalSortAlgorithm(IVertexAndEdgeListGraph <TVertex, TEdge> g)
            : base(g)
        {
            tmpGraph = new BidirectionalGraph <TVertex, TEdge>();

            //create a copy from the graph
            tmpGraph.AddVertexRange(g.Vertices);
            foreach (var e in g.Edges)
            {
                tmpGraph.AddEdge(e);
            }
        }
Esempio n. 5
0
        public bool UnhideEdge(TEdge e)
        {
            if (IsHiddenVertex(e.Source) || IsHiddenVertex(e.Target) || !IsHiddenEdge(e))
            {
                return(false);
            }

            //unhide the edge
            _graph.AddEdge(e);
            hiddenEdges.Remove(e);

            GetHiddenEdgeListOf(e.Source).Remove(e);
            GetHiddenEdgeListOf(e.Target).Remove(e);

            OnEdgeUnhidden(e);
            return(true);
        }
        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
        }
Esempio n. 7
0
        /// <inheritdoc />
        public bool UnhideEdge(TEdge edge)
        {
            // If edge is not hidden or has at least one of its vertex hidden => does nothing
            if (!IsHiddenEdge(edge) || IsHiddenVertex(edge.Source) || IsHiddenVertex(edge.Target))
            {
                return(false);
            }

            // Unhide the edge
            _graph.AddEdge(edge);
            _hiddenEdges.Remove(edge);

            // ReSharper disable PossibleNullReferenceException
            // Justification: The list must exists (at least empty) if unhiding an edge
            // because it has been created on HideEdge call
            GetHiddenEdgeListOf(edge.Source, false).Remove(edge);
            GetHiddenEdgeListOf(edge.Target, false).Remove(edge);
            // ReSharper restore PossibleNullReferenceException

            OnEdgeUnhidden(edge);
            return(true);
        }
        /// <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);
            }
        }