public GraphBalancerAlgorithm(
            IMutableBidirectionalGraph <TVertex, TEdge> visitedGraph,
            VertexFactory <TVertex> vertexFactory,
            EdgeFactory <TVertex, TEdge> edgeFactory,
            TVertex source,
            TVertex sink,
            IDictionary <TEdge, double> capacities)
        {
            Contract.Requires(visitedGraph != null);
            Contract.Requires(vertexFactory != null);
            Contract.Requires(edgeFactory != null);
            Contract.Requires(source != null);
            Contract.Requires(visitedGraph.ContainsVertex(source));
            Contract.Requires(sink != null);
            Contract.Requires(visitedGraph.ContainsVertex(sink));
            Contract.Requires(capacities != null);

            this.visitedGraph = visitedGraph;
            this.source       = source;
            this.sink         = sink;
            this.capacities   = capacities;

            // setting preflow = l(e) = 1
            foreach (var edge in this.VisitedGraph.Edges)
            {
                this.preFlow.Add(edge, 1);
            }
        }
        void IMutableBidirectionalGraph <TVertex, TEdge> .ClearEdges(TVertex v)
        {
            IMutableBidirectionalGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Ensures(!ithis.ContainsVertex(v));
        }
Beispiel #3
0
        public GraphBalancerAlgorithm(
            IMutableBidirectionalGraph <TVertex, TEdge> visitedGraph,
            TVertex source,
            TVertex sink,
            IVertexFactory <TVertex> vertexFactory,
            IEdgeFactory <TVertex, TEdge> edgeFactory
            )
        {
            if (visitedGraph == null)
            {
                throw new ArgumentNullException("visitedGraph");
            }
            if (vertexFactory == null)
            {
                throw new ArgumentNullException("vertexFactory");
            }
            if (edgeFactory == null)
            {
                throw new ArgumentNullException("edgeFactory");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (!visitedGraph.ContainsVertex(source))
            {
                throw new ArgumentException("source is not part of the graph");
            }
            if (sink == null)
            {
                throw new ArgumentNullException("sink");
            }
            if (!visitedGraph.ContainsVertex(sink))
            {
                throw new ArgumentException("sink is not part of the graph");
            }

            this.visitedGraph  = visitedGraph;
            this.vertexFactory = vertexFactory;
            this.edgeFactory   = edgeFactory;
            this.source        = source;
            this.sink          = sink;

            // setting capacities = u(e) = +infty
            foreach (var edge in this.VisitedGraph.Edges)
            {
                this.capacities.Add(edge, double.MaxValue);
            }

            // setting preflow = l(e) = 1
            foreach (var edge in this.VisitedGraph.Edges)
            {
                this.preFlow.Add(edge, 1);
            }
        }
        int IMutableBidirectionalGraph <TVertex, TEdge> .RemoveInEdgeIf(TVertex v, EdgePredicate <TVertex, TEdge> predicate)
        {
            IMutableBidirectionalGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(predicate != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Ensures(ithis.ContainsVertex(v));
            Contract.Ensures(Enumerable.All(ithis.InEdges(v), e => predicate(e)));
            Contract.Ensures(Contract.Result <int>() == Contract.OldValue(Enumerable.Count(ithis.InEdges(v), e => predicate(e))));
            Contract.Ensures(ithis.InDegree(v) == Contract.OldValue(ithis.InDegree(v)) - Contract.Result <int>());

            return(default(int));
        }
Beispiel #5
0
        /// <summary>
        /// Hides the vertex <code>v</code>.
        /// </summary>
        /// <param name="v">The vertex to hide.</param>
        public bool HideVertex(TVertex v)
        {
            if (_graph.ContainsVertex(v) && !hiddenVertices.Contains(v))
            {
                HideEdges(EdgesFor(v));

                //hide the vertex
                _graph.RemoveVertex(v);
                hiddenVertices.Add(v);
                OnVertexHidden(v);
                return(true);
            }

            return(false);
        }
        void IMutableBidirectionalGraph <TVertex, TEdge> .ClearInEdges(TVertex v)
        {
            IMutableBidirectionalGraph <TVertex, TEdge> ithis = this;

            Contract.Requires(v != null);
            Contract.Requires(ithis.ContainsVertex(v));
            Contract.Ensures(ithis.EdgeCount == Contract.OldValue(ithis.EdgeCount) - Contract.OldValue(ithis.InDegree(v)));
            Contract.Ensures(ithis.InDegree(v) == 0);
        }
        /// <inheritdoc />
        public bool HideVertex(TVertex vertex)
        {
            if (!_hiddenVertices.Contains(vertex))
            {
                if (_graph.ContainsVertex(vertex))
                {
                    HideEdges(EdgesFor(vertex));

                    // Hide the vertex
                    _graph.RemoveVertex(vertex);
                    _hiddenVertices.Add(vertex);
                    OnVertexHidden(vertex);
                    return(true);
                }

                throw new VertexNotFoundException();
            }

            return(false);
        }
Beispiel #8
0
        private void PlaceBlock(int modeIndex, LeftRightMode leftRightMode, UpperLowerEdges upperLowerEdges, SugiVertex v)
        {
            if (!double.IsNaN(v.HorizontalPositions[modeIndex]))
            {
                return;
            }

            double delta = Parameters.VertexDistance;

            v.HorizontalPositions[modeIndex] = 0;
            Data w = v;

            do
            {
                SugiVertex wVertex  = w as SugiVertex;
                Segment    wSegment = w as Segment;
                if (_sparseCompactionGraph.ContainsVertex(w) &&
                    ((leftRightMode == LeftRightMode.Left && _sparseCompactionGraph.InDegree(w) > 0) ||
                     (leftRightMode == LeftRightMode.Right && _sparseCompactionGraph.OutDegree(w) > 0)))
                {
                    var edges = leftRightMode == LeftRightMode.Left
                        ? _sparseCompactionGraph.InEdges(w)
                        : _sparseCompactionGraph.OutEdges(w);
                    foreach (var edge in edges)
                    {
                        SugiVertex u    = null;
                        Data       pred = leftRightMode == LeftRightMode.Left ? edge.Source : edge.Target;
                        if (pred is SugiVertex)
                        {
                            u = ((SugiVertex)pred).Roots[modeIndex];
                        }
                        else
                        {
                            var segment = (Segment)pred;
                            u = upperLowerEdges == UpperLowerEdges.Upper ? segment.PVertex.Roots[modeIndex] : segment.QVertex.Roots[modeIndex];
                        }
                        PlaceBlock(modeIndex, leftRightMode, upperLowerEdges, u);
                        if (v.Sinks[modeIndex] == v)
                        {
                            v.Sinks[modeIndex] = u.Sinks[modeIndex];
                        }
                        //var xDelta = delta + (v.Roots[modeIndex].BlockWidths[modeIndex] + u.BlockWidths[modeIndex]) / 2.0;
                        var xDelta = delta + ((wVertex != null ? wVertex.Size.Width : 0.0) + ((pred is SugiVertex) ? ((SugiVertex)pred).Size.Width : u.BlockWidths[modeIndex])) / 2.0;
                        if (v.Sinks[modeIndex] != u.Sinks[modeIndex])
                        {
                            var s = leftRightMode == LeftRightMode.Left
                                ? v.HorizontalPositions[modeIndex] - u.HorizontalPositions[modeIndex] - xDelta
                                : u.HorizontalPositions[modeIndex] - v.HorizontalPositions[modeIndex] - xDelta;

                            u.Sinks[modeIndex].Shifts[modeIndex] = leftRightMode == LeftRightMode.Left
                                ? Math.Min(u.Sinks[modeIndex].Shifts[modeIndex], s)
                                : Math.Max(u.Sinks[modeIndex].Shifts[modeIndex], s);
                        }
                        else
                        {
                            v.HorizontalPositions[modeIndex] =
                                leftRightMode == LeftRightMode.Left
                                    ? Math.Max(v.HorizontalPositions[modeIndex], u.HorizontalPositions[modeIndex] + xDelta)
                                    : Math.Min(v.HorizontalPositions[modeIndex], u.HorizontalPositions[modeIndex] - xDelta);
                        }
                    }
                }
                if (wSegment != null)
                {
                    w = (upperLowerEdges == UpperLowerEdges.Upper) ? wSegment.QVertex : wSegment.PVertex;
                }
                else if (wVertex.Type == VertexTypes.PVertex && upperLowerEdges == UpperLowerEdges.Upper)
                {
                    w = wVertex.Segment;
                }
                else if (wVertex.Type == VertexTypes.QVertex && upperLowerEdges == UpperLowerEdges.Lower)
                {
                    w = wVertex.Segment;
                }
                else
                {
                    w = wVertex.Aligns[modeIndex];
                }
            } while (w != v);
        }
Beispiel #9
0
        private void PlaceBlock(
            int modeIndex,
            LeftRightMode leftRightMode,
            UpperLowerEdges upperLowerEdges,
            [NotNull] SugiVertex v)
        {
            if (!double.IsNaN(v.SlicePositions[modeIndex]))
            {
                return;
            }

            double delta = Parameters.SliceGap;

            v.SlicePositions[modeIndex] = 0;
            Data w = v;

            do
            {
                var wVertex  = w as SugiVertex;
                var wSegment = w as Segment;
                if (_sparseCompactionGraph.ContainsVertex(w) &&
                    (leftRightMode == LeftRightMode.Left && _sparseCompactionGraph.InDegree(w) > 0 ||
                     leftRightMode == LeftRightMode.Right && _sparseCompactionGraph.OutDegree(w) > 0))
                {
                    IEnumerable <IEdge <Data> > edges = leftRightMode == LeftRightMode.Left
                        ? _sparseCompactionGraph.InEdges(w)
                        : _sparseCompactionGraph.OutEdges(w);

                    foreach (IEdge <Data> edge in edges)
                    {
                        SugiVertex u;
                        Data       predecessor = leftRightMode == LeftRightMode.Left ? edge.Source : edge.Target;
                        if (predecessor is SugiVertex vertex)
                        {
                            u = vertex.Roots[modeIndex];
                        }
                        else
                        {
                            var segment = (Segment)predecessor;
                            u = upperLowerEdges == UpperLowerEdges.Upper ? segment.PVertex.Roots[modeIndex] : segment.QVertex.Roots[modeIndex];
                        }

                        PlaceBlock(modeIndex, leftRightMode, upperLowerEdges, u);

                        if (v.Sinks[modeIndex] == v)
                        {
                            v.Sinks[modeIndex] = u.Sinks[modeIndex];
                        }

                        double xDelta = delta
                                        + (
                            (wVertex?.Size.Width ?? 0.0)
                            + (predecessor is SugiVertex sugiVertex
                                                ? sugiVertex.Size.Width
                                                : u.BlockWidths[modeIndex])
                            ) / 2.0;

                        if (v.Sinks[modeIndex] != u.Sinks[modeIndex])
                        {
                            double s = leftRightMode == LeftRightMode.Left
                                ? v.SlicePositions[modeIndex] - u.SlicePositions[modeIndex] - xDelta
                                : u.SlicePositions[modeIndex] - v.SlicePositions[modeIndex] - xDelta;

                            u.Sinks[modeIndex].Shifts[modeIndex] = leftRightMode == LeftRightMode.Left
                                ? Math.Min(u.Sinks[modeIndex].Shifts[modeIndex], s)
                                : Math.Max(u.Sinks[modeIndex].Shifts[modeIndex], s);
                        }
                        else
                        {
                            v.SlicePositions[modeIndex] = leftRightMode == LeftRightMode.Left
                                ? Math.Max(v.SlicePositions[modeIndex], u.SlicePositions[modeIndex] + xDelta)
                                : Math.Min(v.SlicePositions[modeIndex], u.SlicePositions[modeIndex] - xDelta);
                        }
                    }
                }

                if (wSegment != null)
                {
                    w = upperLowerEdges == UpperLowerEdges.Upper ? wSegment.QVertex : wSegment.PVertex;
                }
                // ReSharper disable once PossibleNullReferenceException
                // Justification: If not a segment then it's a vertex
                else if (wVertex.Type == VertexTypes.PVertex && upperLowerEdges == UpperLowerEdges.Upper)
                {
                    w = wVertex.Segment;
                }
                else if (wVertex.Type == VertexTypes.QVertex && upperLowerEdges == UpperLowerEdges.Lower)
                {
                    w = wVertex.Segment;
                }
                else
                {
                    w = wVertex.Aligns[modeIndex];
                }
            } while (w != v);
        }