/// <inheritdoc />
        protected override void InternalCompute()
        {
            var sets = new ForestDisjointSet <TVertex>(VisitedGraph.VertexCount);

            foreach (TVertex vertex in VisitedGraph.Vertices)
            {
                sets.MakeSet(vertex);
            }

            ThrowIfCancellationRequested();

            var queue = new BinaryQueue <TEdge, double>(_edgeWeights);

            foreach (TEdge edge in VisitedGraph.Edges)
            {
                queue.Enqueue(edge);
            }

            ThrowIfCancellationRequested();

            while (queue.Count > 0)
            {
                TEdge edge = queue.Dequeue();
                OnExamineEdge(edge);

                if (!sets.AreInSameSet(edge.Source, edge.Target))
                {
                    OnTreeEdge(edge);
                    sets.Union(edge.Source, edge.Target);
                }
            }
        }
Пример #2
0
        private void InitializeInDegrees()
        {
            foreach (TVertex vertex in VisitedGraph.Vertices)
            {
                InDegrees.Add(vertex, 0);
            }

            foreach (TEdge edge in VisitedGraph.Edges)
            {
                if (edge.IsSelfEdge())
                {
                    throw new NonAcyclicGraphException();
                }

                TVertex successor = _direction == TopologicalSortDirection.Forward
                    ? edge.Target
                    : edge.Source;

                ++InDegrees[successor];
            }

            foreach (TVertex vertex in VisitedGraph.Vertices)
            {
                _heap.Enqueue(vertex);
            }
        }
        protected override void InternalCompute()
        {
            var cancelManager = this.Services.CancelManager;
            var ds = new ForestDisjointSet<TVertex>(this.VisitedGraph.VertexCount);
            foreach (var v in this.VisitedGraph.Vertices)
                ds.MakeSet(v);

            if (cancelManager.IsCancelling)
                return;

            var queue = new BinaryQueue<TEdge, double>(this.edgeWeights);
            foreach (var e in this.VisitedGraph.Edges)
                queue.Enqueue(e);

            if (cancelManager.IsCancelling)
                return;

            while (queue.Count > 0)
            {
                var e = queue.Dequeue();
                this.OnExamineEdge(e);
                if (!ds.AreInSameSet(e.Source, e.Target))
                {
                    this.OnTreeEdge(e);
                    ds.Union(e.Source, e.Target);
                }
            }
        }
 private void InitializeInDegrees()
 {
     foreach (TVertex vertex in VisitedGraph.Vertices)
     {
         Degrees.Add(vertex, VisitedGraph.AdjacentDegree(vertex));
         _heap.Enqueue(vertex);
     }
 }
        private void EnqueueEdgesFrom([NotNull] TVertex vertex)
        {
            foreach (TEdge edge in _verticesEdges[vertex])
            {
                if (_edges.Contains(edge))
                {
                    continue;
                }

                _edges.Add(edge);
                _queue.Enqueue(edge);
            }
        }
Пример #6
0
        private void InitializeInDegrees()
        {
            if (!AllowCyclicGraph && VisitedGraph.Edges.Any(edge => edge.IsSelfEdge()))
            {
                throw new NonAcyclicGraphException();
            }

            foreach (TVertex vertex in VisitedGraph.Vertices)
            {
                Degrees.Add(vertex, VisitedGraph.AdjacentDegree(vertex));
                _heap.Enqueue(vertex);
            }
        }
        private void InitializeQueue()
        {
            _edges = new HashSet <TEdge>();
            _queue = new BinaryQueue <TEdge, double>(_edgeWeights);
            TVertex lastVertex = _visitedVertices.First();

            foreach (TEdge edge in _verticesEdges[lastVertex])
            {
                if (!_edges.Contains(edge))
                {
                    _edges.Add(edge);
                    _queue.Enqueue(edge);
                }
            }
        }
Пример #8
0
        public void ToString2()
        {
            ToStringTest(1, 2, 3, 4);
            ToStringTest(
                new TestVertex("1"),
                new TestVertex("2"),
                new TestVertex("3"),
                new TestVertex("4"));
            ToStringTest(
                new EquatableTestVertex("1"),
                new EquatableTestVertex("2"),
                new EquatableTestVertex("3"),
                new EquatableTestVertex("4"));

            #region Local function

            void ToStringTest <TVertex>(
                TVertex vertex1,
                TVertex vertex2,
                TVertex vertex3,
                TVertex vertex4)
            {
                var queue = new BinaryQueue <TVertex, double>(_ => 1.0);

                // Empty heap => consistent
                StringAssert.StartsWith(Consistent, queue.ToString2());

                queue.Enqueue(vertex1);
                queue.Enqueue(vertex2);
                queue.Enqueue(vertex2);
                queue.Enqueue(vertex3);
                queue.Enqueue(vertex1);

                StringAssert.StartsWith(Consistent, queue.ToString2());

                queue.Dequeue();
                queue.Dequeue();

                StringAssert.StartsWith(Consistent, queue.ToString2());

                queue.Dequeue();
                queue.Dequeue();
                queue.Dequeue();

                StringAssert.StartsWith(Consistent, queue.ToString2());

                queue.Enqueue(vertex4);

                StringAssert.StartsWith(Consistent, queue.ToString2());
            }

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

            var sets = new ForestDisjointSet <TVertex>(VisitedGraph.VertexCount);

            foreach (TVertex vertex in VisitedGraph.Vertices)
            {
                sets.MakeSet(vertex);
            }

            if (cancelManager.IsCancelling)
            {
                return;
            }

            var queue = new BinaryQueue <TEdge, double>(_edgeWeights);

            foreach (TEdge edge in VisitedGraph.Edges)
            {
                queue.Enqueue(edge);
            }

            if (cancelManager.IsCancelling)
            {
                return;
            }

            while (queue.Count > 0)
            {
                TEdge edge = queue.Dequeue();
                OnExamineEdge(edge);

                if (!sets.AreInSameSet(edge.Source, edge.Target))
                {
                    OnTreeEdge(edge);
                    sets.Union(edge.Source, edge.Target);
                }
            }
        }
        private void InitializeInDegrees()
        {
            foreach (TVertex vertex in VisitedGraph.Vertices)
            {
                InDegrees.Add(vertex, 0);
            }

            foreach (TEdge edge in VisitedGraph.Edges)
            {
                if (edge.IsSelfEdge())
                {
                    throw new NonAcyclicGraphException();
                }

                ++InDegrees[edge.Target];
            }

            foreach (TVertex vertex in VisitedGraph.Vertices)
            {
                _heap.Enqueue(vertex);
            }
        }
        private void InitializeInDegrees()
        {
            foreach (TVertex vertex in VisitedGraph.Vertices)
            {
                InDegrees.Add(vertex, 0);
            }

            foreach (TEdge edge in VisitedGraph.Edges)
            {
                if (edge.IsSelfEdge())
                {
                    continue;
                }

                ++InDegrees[edge.Target];
            }

            foreach (TVertex vertex in VisitedGraph.Vertices)
            {
                _heap.Enqueue(vertex);
            }
        }
Пример #12
0
        protected override void InternalCompute()
        {
            var cancelManager = this.Services.CancelManager;
            var visetedVert   = new List <TVertex>();
            var visetedEdges  = new List <TEdge>();
            var ds            = new ForestDisjointSet <TVertex>(this.VisitedGraph.VertexCount);

            foreach (var v in this.VisitedGraph.Vertices)
            {
                if (visetedVert.Count == 0)
                {
                    visetedVert.Add(v);
                }
                ds.MakeSet(v);
            }

            if (cancelManager.IsCancelling)
            {
                return;
            }

            var queue = new BinaryQueue <TEdge, double>(this.edgeWeights);

            foreach (var edge in this.VisitedGraph.Edges)
            {
                if (!visetedEdges.Contains(edge) && (visetedVert.Contains(edge.Source) || visetedVert.Contains(edge.Target)))
                {
                    queue.Enqueue(edge);
                    visetedEdges.Add(edge);
                }
            }

            if (cancelManager.IsCancelling)
            {
                return;
            }

            while (queue.Count > 0)
            {
                foreach (var edge in this.VisitedGraph.Edges)
                {
                    if (!visetedEdges.Contains(edge) && (visetedVert.Contains(edge.Source) || visetedVert.Contains(edge.Target)))
                    {
                        queue.Enqueue(edge);
                        visetedEdges.Add(edge);
                    }
                }
                var e = queue.Dequeue();
                this.OnExamineEdge(e);
                if (!ds.AreInSameSet(e.Source, e.Target))
                {
                    this.OnTreeEdge(e);
                    ds.Union(e.Source, e.Target);
                    if (visetedVert.Contains(e.Source))
                    {
                        visetedVert.Add(e.Target);
                    }
                    else
                    {
                        visetedVert.Add(e.Source);
                    }
                }
            }
        }
        protected override void InternalCompute()
        {
            var dic           = new Dictionary <TVertex, HashSet <TEdge> >();
            var cancelManager = this.Services.CancelManager;
            var visetedVert   = new HashSet <TVertex>();
            var edges         = new HashSet <TEdge>();
            var queue         = new BinaryQueue <TEdge, double>(this.edgeWeights);
            var ds            = new ForestDisjointSet <TVertex>(this.VisitedGraph.VertexCount);

            foreach (var v in this.VisitedGraph.Vertices)
            {
                if (visetedVert.Count == 0)
                {
                    visetedVert.Add(v);
                }
                ds.MakeSet(v);
                dic.Add(v, new HashSet <TEdge>());
            }
            foreach (var e in this.VisitedGraph.Edges)
            {
                dic[e.Source].Add(e);
                dic[e.Target].Add(e);
            }

            if (cancelManager.IsCancelling)
            {
                return;
            }
            var enumerator = visetedVert.GetEnumerator();

            enumerator.MoveNext();
            var lastVert = enumerator.Current;

            foreach (var edge in dic[lastVert])
            {
                if (!edges.Contains(edge))
                {
                    edges.Add(edge);
                    queue.Enqueue(edge);
                }
            }
            if (cancelManager.IsCancelling)
            {
                return;
            }

            while (edges.Count > 0 && visetedVert.Count < VisitedGraph.VertexCount)
            {
                var mined = queue.Dequeue();
                this.OnExamineEdge(mined);
                if (!ds.AreInSameSet(mined.Source, mined.Target))
                {
                    this.OnTreeEdge(mined);
                    ds.Union(mined.Source, mined.Target);
                    if (visetedVert.Contains(mined.Source))
                    {
                        lastVert = mined.Target;
                        visetedVert.Add(mined.Target);
                    }
                    else
                    {
                        lastVert = mined.Source;
                        visetedVert.Add(mined.Source);
                    }
                    foreach (var edge in dic[lastVert])
                    {
                        if (!edges.Contains(edge))
                        {
                            edges.Add(edge);
                            queue.Enqueue(edge);
                        }
                    }
                }
            }
        }
Пример #14
0
        public void ToPairsArray()
        {
            ToPairsArrayTest(1, 2, 3, 4);
            ToPairsArrayTest(
                new TestVertex("1"),
                new TestVertex("2"),
                new TestVertex("3"),
                new TestVertex("4"));
            ToPairsArrayTest(
                new EquatableTestVertex("1"),
                new EquatableTestVertex("2"),
                new EquatableTestVertex("3"),
                new EquatableTestVertex("4"));

            #region Local function

            void ToPairsArrayTest <TVertex>(
                TVertex vertex1,
                TVertex vertex2,
                TVertex vertex3,
                TVertex vertex4)
            {
                var distances = new Stack <double>(new[] { 123.0, 3.0, 2.0, 4.0, 5.0, 1.0 });
                var queue     = new BinaryQueue <TVertex, double>(_ => distances.Pop());

                // Empty heap
                CollectionAssert.IsEmpty(queue.ToPairsArray());

                queue.Enqueue(vertex1);
                queue.Enqueue(vertex2);
                queue.Enqueue(vertex2);
                queue.Enqueue(vertex3);
                queue.Enqueue(vertex1);

                // Array not sorted with distance
                CollectionAssert.AreEquivalent(
                    new[]
                {
                    new KeyValuePair <double, TVertex>(1.0, vertex1),
                    new KeyValuePair <double, TVertex>(5.0, vertex2),
                    new KeyValuePair <double, TVertex>(4.0, vertex2),
                    new KeyValuePair <double, TVertex>(2.0, vertex3),
                    new KeyValuePair <double, TVertex>(3.0, vertex1)
                },
                    queue.ToPairsArray());

                queue.Dequeue();
                queue.Dequeue();

                // Array not sorted with distance
                CollectionAssert.AreEquivalent(
                    new[]
                {
                    new KeyValuePair <double, TVertex>(3.0, vertex1),
                    new KeyValuePair <double, TVertex>(5.0, vertex2),
                    new KeyValuePair <double, TVertex>(4.0, vertex2)
                },
                    queue.ToPairsArray());

                queue.Dequeue();
                queue.Dequeue();
                queue.Dequeue();

                CollectionAssert.IsEmpty(queue.ToPairsArray());

                queue.Enqueue(vertex4);
                CollectionAssert.AreEqual(
                    new[] { new KeyValuePair <double, TVertex>(123.0, vertex4) },
                    queue.ToPairsArray());
            }

            #endregion
        }