예제 #1
0
        public void TestUnion()
        {
            _sut.Union(4, 3);
            Assert.AreEqual(9, _sut.Count());
            Assert.IsTrue(_sut.IsConnected(4, 3));
            Assert.AreEqual(4, _sut.Find(3));
            Assert.AreEqual(4, _sut.Find(4));

            _sut.Union(3, 8);
            Assert.AreEqual(8, _sut.Count());
            Assert.IsTrue(_sut.IsConnected(3, 8));
            Assert.AreEqual(4, _sut.Find(3));
            Assert.AreEqual(4, _sut.Find(8));
        }
        static double UnionFindTest(IUnionFind uf, int n)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            Random rand = new Random();

            // 进行n次操作, 每次随机选择两个元素进行合并
            for (int i = 0; i < n; i++)
            {
                int a = rand.Next(n);
                int b = rand.Next(n);
                uf.UnitElement(a, b);
            }

            // 进行n次操作, 每次随机判断两个元素是否相连接
            for (int i = 0; i < n; i++)
            {
                int a = rand.Next(n);
                int b = rand.Next(n);
                uf.IsConnected(a, b);
            }

            sw.Stop();
            return(sw.Elapsed.TotalSeconds);
        }
예제 #3
0
        private bool IsPercolated(IUnionFind uf, out int selected_top_cell, out int selected_bottom_cell)
        {
            selected_top_cell    = -1;
            selected_bottom_cell = -1;

            foreach (var top_cell in top)
            {
                foreach (var bottom_cell in bottom)
                {
                    if (!uf.IsConnected(top_cell, bottom_cell))
                    {
                        continue;
                    }
                    selected_top_cell    = top_cell;
                    selected_bottom_cell = bottom_cell;
                    return(true);
                }
            }
            return(false);
        }
 public bool IsConnected(int source, int destination)
 {
     return _unionFind.IsConnected(source, destination);
 }