static void Compute()
        {
            var size               = 200;
            var quickFind          = new QuickFindUF(size);
            var quickUnion         = new QuickUnionUF(size);
            var weightedQuickUnion = new WeightedQuickUnionUF(size);
            var connections        = ErdosRenyi.Generate(size);

            var quickFindResult = new int[size];
            var quickUnionResult = new int[size];
            var weightedQuickUnionResult = new int[size];
            int p, q;

            for (var i = 0; i < size; i++)
            {
                p = connections[i].P;
                q = connections[i].Q;

                quickFind.Union(p, q);
                quickUnion.Union(p, q);
                weightedQuickUnion.Union(p, q);
                quickFindResult[i]          = quickFind.ArrayVisitCount;
                quickUnionResult[i]         = quickUnion.ArrayVisitCount;
                weightedQuickUnionResult[i] = weightedQuickUnion.ArrayParentVisitCount + weightedQuickUnion.ArraySizeVisitCount;

                quickFind.ResetArrayCount();
                quickUnion.ResetArrayCount();
                weightedQuickUnion.ResetArrayCount();
            }

            Draw(quickFindResult, "Quick-Find");
            Draw(quickUnionResult, "Quick-Union");
            Draw(weightedQuickUnionResult, "Weighted Quick-Union");
        }
        static void Main(string[] args)
        {
            int N = 10;

            int[] edges = new int[5];
            for (int i = 0; i < 5; i++)
            {
                var uf = new UF(N);
                Console.WriteLine(N + "\t" + ErdosRenyi.Count(uf));
                N *= 10;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 进行若干次随机试验,输出平均 union 次数,返回平均耗时。
        /// </summary>
        /// <param name="uf">用于测试的并查集。</param>
        /// <param name="connections">用于测试的输入。</param>
        /// <returns>平均耗时。</returns>
        static long RunTest(UF uf, Connection[] connections)
        {
            var timer      = new Stopwatch();
            var repeatTime = 5;

            timer.Start();
            for (var i = 0; i < repeatTime; i++)
            {
                ErdosRenyi.Count(uf, connections);
            }
            timer.Stop();

            return(timer.ElapsedMilliseconds / repeatTime);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            for (int n = 10; n < 10000; n *= 2)
            {
                int total = 0;
                for (int i = 0; i < 100; i++)
                {
                    UF uf = new UF(n);
                    total += ErdosRenyi.Count(uf);
                }

                Console.WriteLine("实验结果:" + total / 100);
                Console.WriteLine("1/2NlnN:" + Math.Log(n) * n * 0.5);
                Console.WriteLine();
            }
        }
        /// <summary>
        /// 进行若干次随机试验,输出平均 union 次数,返回平均耗时。
        /// </summary>
        /// <param name="uf">用于测试的并查集。</param>
        /// <returns>平均耗时。</returns>
        static long RunTest(UF uf)
        {
            Stopwatch timer      = new Stopwatch();
            int       total      = 0;
            int       repeatTime = 10;

            timer.Start();
            for (int i = 0; i < repeatTime; i++)
            {
                total += ErdosRenyi.Count(uf);
            }
            timer.Stop();
            Console.WriteLine("平均次数:" + total / repeatTime);

            return(timer.ElapsedMilliseconds / repeatTime);
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            var n = 10000;

            for (var t = 0; t < 5; t++)
            {
                var input = ErdosRenyi.Generate(n);
                var weightedQuickUnionUF = new WeightedQuickUnionUF(n);
                var weightedQuickUnionPathCompressionUF = new WeightedQuickUnionPathCompressionUF(n);

                Console.WriteLine("N:" + n);

                var weightedQuickUnionTime = RunTest(weightedQuickUnionUF, input);
                var weightedQuickUnionPathCompressionTime = RunTest(weightedQuickUnionPathCompressionUF, input);

                Console.WriteLine("加权 quick-find 耗时(毫秒):" + weightedQuickUnionTime);
                Console.WriteLine("带路径压缩的加权 quick-union 耗时(毫秒):" + weightedQuickUnionPathCompressionTime);
                Console.WriteLine("比值:" + (double)weightedQuickUnionTime / weightedQuickUnionPathCompressionTime);
                Console.WriteLine();

                n *= 2;
            }
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            var n = 2000;

            for (var t = 0; t < 5; t++)
            {
                var input      = ErdosRenyi.Generate(n);
                var quickFind  = new QuickFindUF(n);
                var quickUnion = new QuickUnionUF(n);

                Console.WriteLine("N:" + n);

                var quickFindTime  = RunTest(quickFind, input);
                var quickUnionTime = RunTest(quickUnion, input);

                Console.WriteLine("quick-find 耗时(毫秒):" + quickFindTime);
                Console.WriteLine("quick-union 耗时(毫秒):" + quickUnionTime);
                Console.WriteLine("比值:" + (double)quickFindTime / quickUnionTime);
                Console.WriteLine();

                n *= 2;
            }
        }