コード例 #1
0
        /// <inheritdoc />
        protected override void InternalCompute()
        {
            ICancelManager cancelManager = Services.CancelManager;

            while (_heap.Count != 0)
            {
                if (cancelManager.IsCancelling)
                {
                    return;
                }

                TVertex vertex = _heap.Dequeue();
                int     degree = Degrees[vertex];
                // 0 => isolated vertex
                // 1 => single adjacent edge
                if (degree != 0 && degree != 1 && !AllowCyclicGraph)
                {
                    throw new NonAcyclicGraphException();
                }

                _sortedVertices.Add(vertex);
                OnVertexAdded(vertex);

                // Update the count of its adjacent vertices
                UpdateAdjacentDegree(vertex);
            }

            SortedVertices = _sortedVertices.ToArray();

            #region Local function

            void UpdateAdjacentDegree(TVertex vertex)
            {
                foreach (TEdge edge in VisitedGraph.AdjacentEdges(vertex).Where(e => !e.IsSelfEdge()))
                {
                    TVertex other = edge.GetOtherVertex(vertex);
                    --Degrees[other];

                    if (Degrees[other] < 0 && !AllowCyclicGraph)
                    {
                        throw new InvalidOperationException("Degree is negative, and cannot be.");
                    }

                    if (_heap.Contains(other))
                    {
                        _heap.Update(other);
                    }
                }
            }

            #endregion
        }
        /// <inheritdoc />
        protected override void InternalCompute()
        {
            ICancelManager cancelManager = Services.CancelManager;

            InitializeInDegrees();

            while (_heap.Count != 0)
            {
                if (cancelManager.IsCancelling)
                {
                    return;
                }

                TVertex vertex = _heap.Dequeue();
                if (Degrees[vertex] != 0 && !AllowCyclicGraph)
                {
                    throw new NonAcyclicGraphException();
                }

                SortedVertices.Add(vertex);
                OnVertexAdded(vertex);

                // Update the count of its adjacent vertices
                UpdateAdjacentDegree(vertex);
            }

            #region Local function

            void UpdateAdjacentDegree(TVertex vertex)
            {
                foreach (TEdge edge in VisitedGraph.AdjacentEdges(vertex).Where(e => !e.IsSelfEdge()))
                {
                    --Degrees[edge.Target];

                    if (Degrees[edge.Target] < 0 && !AllowCyclicGraph)
                    {
                        throw new InvalidOperationException("Degree is negative, and cannot be.");
                    }

                    if (_heap.Contains(edge.Target))
                    {
                        _heap.Update(edge.Target);
                    }
                }
            }

            #endregion
        }