Exemplo n.º 1
0
        public static void Run()
        {
            DisjointSets <int> ds = new DisjointSets <int>();
            WeightedDisjointSetsWithPathCompression <int> wdspc = new WeightedDisjointSetsWithPathCompression <int>();

            var sw = Stopwatch.StartNew();

            for (int j = 10001; j < 20000; j++)
            {
                for (int i = 1; i < 10000; i++)
                {
                    if (!ds.Find(i, j))
                    {
                        ds.Union(i, j);
                    }
                }
            }

            for (int j = 10001; j < 20000; j++)
            {
                for (int i = 1; i < 10000; i++)
                {
                    if (!ds.Find(i, j))
                    {
                        ds.Union(i, j);
                    }
                }
            }
            sw.Stop();
            Console.WriteLine("RunTime without path compression" + sw.Elapsed);

            sw = Stopwatch.StartNew();
            for (int j = 10001; j < 20000; j++)
            {
                for (int i = 1; i < 10000; i++)
                {
                    if (!wdspc.Find(i, j))
                    {
                        wdspc.Union(i, j);
                    }
                }
            }

            for (int j = 10001; j < 20000; j++)
            {
                for (int i = 1; i < 10000; i++)
                {
                    if (!wdspc.Find(i, j))
                    {
                        wdspc.Union(i, j);
                    }
                }
            }
            sw.Stop();
            Console.WriteLine("RunTime path compression" + sw.Elapsed);
        }
Exemplo n.º 2
0
        public static void Run()
        {
            DisjointSets <char> ds = new DisjointSets <char>();

            ds.UnionOfRoots('a', 'b');
            ds.UnionOfRoots('c', 'd');
            ds.UnionOfRoots('e', 'd');
            Console.WriteLine(ds.GetRoot('e'));
            Console.WriteLine(ds.GetRoot('a'));
            ds.UnionOfRoots(ds.GetRoot('a'), ds.GetRoot('d'));
            Console.WriteLine(ds.GetRoot('a'));
        }