コード例 #1
0
        public void DisjointSetForestUnionTest()
        {
            DisjointSetForest <Int32>  disjointSetInt    = new DisjointSetForest <Int32>();
            DisjointSetForest <String> disjointSetString = new DisjointSetForest <String>();

            foreach (Int32 value in _values)
            {
                disjointSetInt.MakeSet(value);
                disjointSetString.MakeSet(value.ToString());
            }

            for (Int32 i = 0; i < _values.Length; i = i + 2)
            {
                disjointSetInt.Union(_values[i], _values[i + 1]);
                disjointSetString.Union(_values[i].ToString(), _values[i + 1].ToString());
            }

            // testing representatives

            for (Int32 i = 0; i < _values.Length; i = i + 2)
            {
                Assert.AreEqual(disjointSetInt.Find(_values[i]), disjointSetInt.Find(_values[i + 1]));
                Assert.AreEqual(disjointSetString.Find(_values[i].ToString()), disjointSetString.Find(_values[i + 1].ToString()));
            }

            // testing setCounts

            Assert.AreEqual(disjointSetInt.SetCount, _values.Length / 2);
            Assert.AreEqual(disjointSetString.SetCount, _values.Length / 2);

            Assert.AreEqual(disjointSetInt.Count, _values.Length);
            Assert.AreEqual(disjointSetString.Count, _values.Length);

            // exceptions

            Assert.Throws <ArgumentException>(() => disjointSetInt.Union(_values[0], 100));
            Assert.Throws <ArgumentException>(() => disjointSetInt.Union(100, _values[0]));
            Assert.Throws <ArgumentException>(() => disjointSetInt.Union(100, 101));

            Assert.Throws <ArgumentException>(() => disjointSetString.Union(_values[0].ToString(), 100.ToString()));
            Assert.Throws <ArgumentException>(() => disjointSetString.Union(100.ToString(), _values[0].ToString()));
            Assert.Throws <ArgumentException>(() => disjointSetString.Union(100.ToString(), 101.ToString()));

            Assert.Throws <ArgumentNullException>(() => disjointSetString.Union(_values[0].ToString(), null));
            Assert.Throws <ArgumentNullException>(() => disjointSetString.Union(null, _values[0].ToString()));
            Assert.Throws <ArgumentNullException>(() => disjointSetString.Union(null, null));
        }
コード例 #2
0
 /// <summary>
 /// Computes the result of the operation.
 /// </summary>
 protected override void ComputeResult()
 {
     _forest = new DisjointSetForest <IGraphVertex>(Source.Vertices);
     foreach (IGraphEdge edge in Source.Edges.OrderBy(x => _weightMetric(x)))
     {
         if (_forest.Find(edge.Source) != _forest.Find(edge.Target))
         {
             _spanningEdges.Add(edge);
             _forest.Union(edge.Source, edge.Target);
         }
     }
 }
コード例 #3
0
        /// <summary>
        /// Computes the result of the operation.
        /// </summary>
        protected override void ComputeResult()
        {
            _edgeSet = new HashSet <IGraphEdge>();
            _forest  = new DisjointSetForest <IGraphVertex>(Source.Vertices);

            while (_forest.SetCount > 1)
            {
                IGraphVertex currentRep = null;

                foreach (IGraphVertex vertex in _forest.OrderedEnumerator)
                {
                    if (currentRep != _forest.Find(vertex))
                    {
                        //new component
                        currentRep = _forest.Find(vertex);
                        if (_edgeSet.Count > 0)
                        {
                            _spanningEdges.Add(_edgeSet.MinBy(x => _weightMetric(x)).First());
                        }
                        _edgeSet.Clear();
                    }

                    if (Source.OutEdges(vertex).Any(x => _forest.Find(x.Target) != currentRep))
                    {
                        _edgeSet.Add(
                            Source.OutEdges(vertex)
                            .Where(x => _forest.Find(x.Target) != currentRep)
                            .MinBy(y => _weightMetric(y))
                            .First());
                    }
                }

                if (_edgeSet.Count > 0)
                {
                    //on the last element there is no component change
                    _spanningEdges.Add(_edgeSet.MinBy(x => _weightMetric(x)).First());
                }

                _edgeSet.Clear();

                foreach (IGraphEdge spanningEdge in _spanningEdges)
                {
                    _forest.Union(spanningEdge.Source, spanningEdge.Target);
                }
            }
        }