コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
        /// <inheritdoc />
        protected override void InternalCompute()
        {
            ThrowIfCancellationRequested();

            while (_edges.Count > 0 && _visitedVertices.Count < VisitedGraph.VertexCount)
            {
                TEdge minEdge = _queue.Dequeue();
                OnExamineEdge(minEdge);

                if (!_sets.AreInSameSet(minEdge.Source, minEdge.Target))
                {
                    OnTreeEdge(minEdge);
                    _sets.Union(minEdge.Source, minEdge.Target);

                    TVertex lastVertex;
                    if (_visitedVertices.Contains(minEdge.Source))
                    {
                        lastVertex = minEdge.Target;
                        _visitedVertices.Add(minEdge.Target);
                    }
                    else
                    {
                        lastVertex = minEdge.Source;
                        _visitedVertices.Add(minEdge.Source);
                    }

                    EnqueueEdgesFrom(lastVertex);
                }
            }
        }
コード例 #3
0
        /// <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);
                }
            }
        }
コード例 #4
0
        public void Unions(int elementCount, [PexAssumeNotNull]KeyValuePair<int,int>[] unions)
        {
            PexAssume.IsTrue(0 < elementCount);
            PexSymbolicValue.Minimize(elementCount);
            PexAssume.TrueForAll(
                unions, 
                u => 0 <= u.Key && u.Key < elementCount && 
                     0 <= u.Value && u.Value < elementCount
                     );

            var target = new ForestDisjointSet<int>();
            // fill up with 0..elementCount - 1
            for (int i = 0; i < elementCount; i++)
            {
                target.MakeSet(i);
                Assert.IsTrue(target.Contains(i));
                Assert.AreEqual(i + 1, target.ElementCount);
                Assert.AreEqual(i + 1, target.SetCount);
            }

            // apply Union for pairs unions[i], unions[i+1]
            for (int i = 0; i < unions.Length; i++)
            {
                var left = unions[i].Key;
                var right= unions[i].Value;

                var setCount = target.SetCount;
                bool unioned = target.Union(left, right);
                // should be in the same set now
                Assert.IsTrue(target.AreInSameSet(left, right));
                // if unioned, the count decreased by 1
                PexAssert.ImpliesIsTrue(unioned, () => setCount - 1 == target.SetCount);
            }
        }
コード例 #5
0
        public void AreInSameSet()
        {
            AreInSameSetTest(1, 2);
            AreInSameSetTest(
                new TestVertex("1"),
                new TestVertex("2"));
            AreInSameSetTest(
                new EquatableTestVertex("1"),
                new EquatableTestVertex("2"));

            #region Local function

            void AreInSameSetTest <TValue>(TValue value1, TValue value2)
            {
                var set = new ForestDisjointSet <TValue>();

                AssertSetSizes(set, 0, 0);

                set.MakeSet(value1);
                Assert.IsTrue(set.AreInSameSet(value1, value1));

                set.MakeSet(value2);
                Assert.IsTrue(set.AreInSameSet(value1, value1));
                Assert.IsFalse(set.AreInSameSet(value1, value2));
                Assert.IsFalse(set.AreInSameSet(value2, value1));
                Assert.IsTrue(set.AreInSameSet(value2, value2));

                set.Union(value1, value2);
                Assert.IsTrue(set.AreInSameSet(value1, value1));
                Assert.IsTrue(set.AreInSameSet(value1, value2));
                Assert.IsTrue(set.AreInSameSet(value2, value1));
                Assert.IsTrue(set.AreInSameSet(value2, value2));
            }

            #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);
                }
            }
        }
コード例 #7
0
        private void Unions(int elementCount, [NotNull] KeyValuePair <int, int>[] unions)
        {
            Assert.IsTrue(0 < elementCount);
            QuikGraphAssert.TrueForAll(
                unions,
                u => 0 <= u.Key &&
                u.Key < elementCount &&
                0 <= u.Value &&
                u.Value < elementCount);

            var target = new ForestDisjointSet <int>();

            // Fill up with 0..elementCount - 1
            for (int i = 0; i < elementCount; ++i)
            {
                target.MakeSet(i);
                Assert.IsTrue(target.Contains(i));
                Assert.AreEqual(i + 1, target.ElementCount);
                Assert.AreEqual(i + 1, target.SetCount);
            }

            // Apply Union for pairs unions[i], unions[i+1]
            foreach (KeyValuePair <int, int> pair in unions)
            {
                int left  = pair.Key;
                int right = pair.Value;

                int  setCount = target.SetCount;
                bool unioned  = target.Union(left, right);

                // Should be in the same set now
                Assert.IsTrue(target.AreInSameSet(left, right));

                // If unioned, the count decreased by 1
                QuikGraphAssert.ImpliesIsTrue(unioned, () => setCount - 1 == target.SetCount);
            }
        }
コード例 #8
0
        public void Union()
        {
            UnionTest(1, 2, 3, 4, 5);
            UnionTest(
                new TestVertex("1"),
                new TestVertex("2"),
                new TestVertex("3"),
                new TestVertex("4"),
                new TestVertex("5"));
            UnionTest(
                new EquatableTestVertex("1"),
                new EquatableTestVertex("2"),
                new EquatableTestVertex("3"),
                new EquatableTestVertex("4"),
                new EquatableTestVertex("5"));

            #region Local function

            void UnionTest <TValue>(
                TValue value1,
                TValue value2,
                TValue value3,
                TValue value4,
                TValue value5)
            {
                ForestDisjointSet <TValue> set = MakeAndFillSet();

                set.Union(value1, value2);
                AssertSetSizes(set, 4, 5);
                Assert.IsTrue(set.AreInSameSet(value1, value2));

                set.Union(value4, value3);
                AssertSetSizes(set, 3, 5);
                Assert.IsTrue(set.AreInSameSet(value3, value4));

                set.Union(value1, value4);
                AssertSetSizes(set, 2, 5);
                Assert.IsTrue(set.AreInSameSet(value1, value2));
                Assert.IsTrue(set.AreInSameSet(value1, value3));
                Assert.IsTrue(set.AreInSameSet(value1, value4));

                set.Union(value1, value5);
                AssertSetSizes(set, 1, 5);
                Assert.IsTrue(set.AreInSameSet(value1, value2));
                Assert.IsTrue(set.AreInSameSet(value1, value3));
                Assert.IsTrue(set.AreInSameSet(value1, value4));
                Assert.IsTrue(set.AreInSameSet(value1, value5));

                // Already merged
                set.Union(value1, value1);
                AssertSetSizes(set, 1, 5);
                Assert.IsTrue(set.AreInSameSet(value1, value2));
                Assert.IsTrue(set.AreInSameSet(value1, value3));
                Assert.IsTrue(set.AreInSameSet(value1, value4));
                Assert.IsTrue(set.AreInSameSet(value1, value5));

                set.Union(value1, value4);
                AssertSetSizes(set, 1, 5);
                Assert.IsTrue(set.AreInSameSet(value1, value2));
                Assert.IsTrue(set.AreInSameSet(value1, value3));
                Assert.IsTrue(set.AreInSameSet(value1, value4));
                Assert.IsTrue(set.AreInSameSet(value1, value5));

                set.Union(value4, value1);
                AssertSetSizes(set, 1, 5);
                Assert.IsTrue(set.AreInSameSet(value1, value2));
                Assert.IsTrue(set.AreInSameSet(value1, value3));
                Assert.IsTrue(set.AreInSameSet(value1, value4));
                Assert.IsTrue(set.AreInSameSet(value1, value5));


                set = MakeAndFillSet();
                set.Union(value1, value2);
                AssertSetSizes(set, 4, 5);
                Assert.IsTrue(set.AreInSameSet(value1, value2));

                set.Union(value2, value4);
                AssertSetSizes(set, 3, 5);
                Assert.IsTrue(set.AreInSameSet(value1, value2));
                Assert.IsTrue(set.AreInSameSet(value1, value4));

                set.Union(value5, value2);
                AssertSetSizes(set, 2, 5);
                Assert.IsTrue(set.AreInSameSet(value1, value2));
                Assert.IsTrue(set.AreInSameSet(value1, value4));
                Assert.IsTrue(set.AreInSameSet(value1, value5));

                #region Local function

                ForestDisjointSet <TValue> MakeAndFillSet()
                {
                    var s = new ForestDisjointSet <TValue>();

                    s.MakeSet(value1);
                    s.MakeSet(value2);
                    s.MakeSet(value3);
                    s.MakeSet(value4);
                    s.MakeSet(value5);
                    AssertSetSizes(s, 5, 5);

                    return(s);
                }

                #endregion
            }

            #endregion
        }
コード例 #9
0
        public void AreInSameSet_Throws()
        {
            var vertex  = new TestVertex("1");
            var vertex2 = new TestVertex("2");
            var set     = new ForestDisjointSet <TestVertex>();

            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            // ReSharper disable AssignNullToNotNullAttribute
            Assert.Throws <ArgumentNullException>(() => set.AreInSameSet(vertex, null));
            Assert.Throws <ArgumentNullException>(() => set.AreInSameSet(null, vertex));
            Assert.Throws <ArgumentNullException>(() => set.AreInSameSet(null, null));
            // ReSharper restore AssignNullToNotNullAttribute

            Assert.Throws <KeyNotFoundException>(() => set.AreInSameSet(vertex, vertex));
            Assert.Throws <KeyNotFoundException>(() => set.AreInSameSet(vertex, vertex2));
            Assert.Throws <KeyNotFoundException>(() => set.AreInSameSet(vertex2, vertex));
            Assert.Throws <KeyNotFoundException>(() => set.AreInSameSet(vertex2, vertex2));

            set.MakeSet(vertex);
            Assert.DoesNotThrow(() => set.AreInSameSet(vertex, vertex));
            Assert.Throws <KeyNotFoundException>(() => set.AreInSameSet(vertex, vertex2));
            Assert.Throws <KeyNotFoundException>(() => set.AreInSameSet(vertex2, vertex));
            Assert.Throws <KeyNotFoundException>(() => set.AreInSameSet(vertex2, vertex2));
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }
コード例 #10
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);
                    }
                }
            }
        }
コード例 #11
0
        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);
                        }
                    }
                }
            }
        }