Пример #1
0
        public void DemoTest()
        {
            IUnionFind uf = new QuickFind(10);

            uf.Union(4, 3);
            int[] ids = uf.GetIds();
            Assert.AreEqual(3, ids[3]);
            Assert.AreEqual(3, ids[4]);

            uf.Union(3, 8);
            ids = uf.GetIds();
            Assert.AreEqual(8, ids[3]);
            Assert.AreEqual(8, ids[4]);

            uf.Union(6, 5);
            ids = uf.GetIds();
            Assert.AreEqual(5, ids[5]);
            Assert.AreEqual(5, ids[6]);

            uf.Union(9, 4);
            ids = uf.GetIds();
            Assert.AreEqual(8, ids[4]);
            Assert.AreEqual(8, ids[9]);

            uf.Union(2, 1);
            ids = uf.GetIds();
            Assert.AreEqual(1, ids[1]);
            Assert.AreEqual(1, ids[2]);

            Assert.IsTrue(uf.Connected(8, 9));
            Assert.IsFalse(uf.Connected(5, 0));

            uf.Union(5, 0);
            ids = uf.GetIds();
            Assert.AreEqual(0, ids[0]);
            Assert.AreEqual(0, ids[5]);
            Assert.AreEqual(0, ids[6]);

            uf.Union(7, 2);
            ids = uf.GetIds();
            Assert.AreEqual(1, ids[1]);
            Assert.AreEqual(1, ids[2]);
            Assert.AreEqual(1, ids[7]);

            uf.Union(6, 1);
            ids = uf.GetIds();
            Assert.AreEqual(1, ids[0]);
            Assert.AreEqual(1, ids[1]);
            Assert.AreEqual(1, ids[2]);
            Assert.AreEqual(8, ids[3]);
            Assert.AreEqual(8, ids[4]);
            Assert.AreEqual(1, ids[5]);
            Assert.AreEqual(1, ids[6]);
            Assert.AreEqual(1, ids[7]);
            Assert.AreEqual(8, ids[8]);
            Assert.AreEqual(8, ids[9]);
        }
Пример #2
0
        public KruskalMst(EdgeWeightedGraph graph)
        {
            _mst = new Queue <Edge>();

            var edges = graph.Edges();

            Array.Sort(edges, (edge, edge1) => edge.CompareTo(edge1));

            var vertices = graph.Vertices();
            var uf       = new QuickFind(vertices.Length);

            var count = 0;

            while (count < edges.Length && _mst.Count < vertices.Length - 1)
            {
                var edge = edges[count];

                var v = edge.Either();
                var w = edge.Other(v);

                if (!uf.Connected(v, w))
                {
                    uf.Union(v, w);
                    _mst.Enqueue(edge);
                }

                count++;
            }
        }
Пример #3
0
    public void TestQuickFind()
    {
        var input = new List <int[]>()
        {
            new int[] { 1, 3 },
            new int[] { 1, 4 },
            new int[] { 8, 9 }
        };

        var N = 10;


        var uf = new QuickFind(N);

        foreach (var item in input)
        {
            uf.Union(item[0], item[1]);
        }

        Assert.Equal(uf.Connected(3, 4), true);
        Assert.Equal(uf.Connected(9, 0), false);
    }
Пример #4
0
        public void QuickFind_TinyFile_True()
        {
            var dots      = new Dots(@"DataStore\tinyUF.txt");
            var quickFind = new QuickFind(dots.Count);

            foreach (Tuple <int, int> item in dots.Items)
            {
                if (quickFind.Connected(item.Item1, item.Item2))
                {
                    continue;
                }
                quickFind.Union(item.Item1, item.Item2);
            }
            Assert.Equal(2, quickFind.Count);
        }