Ejemplo n.º 1
0
        public void QU_Basic()
        {
            string path    = Path.Combine(_baseAddress, "BasicAPI_Test_input.txt");
            var    connect = new int[5, 2] {
                { 0, 1 },
                { 1, 3 },
                { 5, 4 },
                { 5, 4 },                       //to test repeated union call
                { 3, 5 }
            };

            int N  = 6;
            var QU = new QuickUnion(N);

            for (int i = 0; i <= connect.GetUpperBound(0); i++)
            {
                int p = connect[i, 0];
                int q = connect[i, 1];
                QU.Union(p, q);
            }

            //0 and 4 are expected to be connected to the same components
            Assert.AreEqual(QU.Find(0), QU.Find(4));

            Assert.AreEqual(QU.ComponentsCount, 2);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var quickFinder = new QuickUnion(10);

            quickFinder.Union(1, 3);
            /*0 3 2 3 4 5 6 7 8 9 */
            quickFinder.Union(3, 8);
            /*0 3 2 8 4 5 6 7 8 9 */
            quickFinder.Union(7, 4);
            /*0 3 2 8 4 5 6 4 8 9 */
            quickFinder.Union(5, 8);
            /*0 3 2 8 4 8 6 4 8 9 */
            quickFinder.Union(0, 5);
            /*8 3 2 8 4 8 6 4 8 9 */

            Console.WriteLine(quickFinder.Find(1, 5));

            quickFinder.Print();

            // -----------------------------
            var quickFinderWeighted = new QuickUnionWeighted(10);

            quickFinderWeighted.Union(1, 3);
            quickFinderWeighted.Union(3, 8);
            quickFinderWeighted.Union(7, 4);
            quickFinderWeighted.Union(5, 8);
            quickFinderWeighted.Union(0, 5);
            /*3 3 2 3 4 3 6 4 3 9 */

            Console.WriteLine(quickFinderWeighted.Find(1, 5));

            quickFinderWeighted.Print();
        }
Ejemplo n.º 3
0
        public void QuickUnionFindTest()
        {
            QuickUnion uf = new QuickUnion(13);

            uf.Union(0, 1);
            uf.Union(1, 2);
            uf.Union(4, 5);
            uf.Union(0, 3);
            uf.Union(7, 8);
            uf.Union(8, 9);
            uf.Union(6, 11);
            uf.Union(11, 10);
            uf.Union(9, 12);
            uf.Union(1, 12);

            Assert.Equal(12, uf.Find(0));
            Assert.Equal(5, uf.Find(4));
        }
Ejemplo n.º 4
0
        public int SolveMSt()
        {
            int mstCost = 0;

            _MstEdges = new List <Edge>();
            QuickUnion Qu = new QuickUnion(_nodes + 1);

            Array.Sort(_EdgeCost, _EdgeList);
            for (int i = 0; i < _EdgeCost.Length; i++)
            {
                if (!Qu.Find(_EdgeList[i].a, _EdgeList[i].b))
                {
                    mstCost += _EdgeCost[i];
                    _MstEdges.Add(_EdgeList[i]);
                    Qu.Union(_EdgeList[i].a, _EdgeList[i].b);
                }
            }

            return(mstCost);
        }