Exemplo n.º 1
0
        public void testUnionAndConnected_MultiConnection()
        {
            QuickFindUF quickFindUF = new QuickFindUF(10);

            quickFindUF.Union(0, 1);
            quickFindUF.Union(2, 1);
            Assert.IsTrue(quickFindUF.Connected(0, 1));
            Assert.IsTrue(quickFindUF.Connected(2, 1));
            Assert.IsTrue(quickFindUF.Connected(2, 0));
            Assert.AreEqual(8, quickFindUF.Count());
            Assert.AreEqual(1, quickFindUF.Find(1));
            Assert.AreEqual(1, quickFindUF.Find(0));
            Assert.AreEqual(1, quickFindUF.Find(2));

            quickFindUF.Union(2, 5);
            Assert.IsTrue(quickFindUF.Connected(0, 1));
            Assert.IsTrue(quickFindUF.Connected(2, 1));
            Assert.IsTrue(quickFindUF.Connected(2, 0));
            Assert.IsTrue(quickFindUF.Connected(5, 0));
            Assert.IsTrue(quickFindUF.Connected(5, 1));
            Assert.IsTrue(quickFindUF.Connected(5, 2));
            Assert.AreEqual(7, quickFindUF.Count());
            Assert.AreEqual(5, quickFindUF.Find(1));
            Assert.AreEqual(5, quickFindUF.Find(0));
            Assert.AreEqual(5, quickFindUF.Find(2));
            Assert.AreEqual(5, quickFindUF.Find(5));
        }
Exemplo n.º 2
0
        public void testUnionAndConnected_NewConnection()
        {
            QuickFindUF quickFindUF = new QuickFindUF(10);

            Assert.IsFalse(quickFindUF.Connected(0, 1));
            Assert.AreEqual(10, quickFindUF.Count());
            quickFindUF.Union(0, 1);
            Assert.IsTrue(quickFindUF.Connected(0, 1));
            Assert.AreEqual(9, quickFindUF.Count());
            Assert.AreEqual(1, quickFindUF.Find(1));
            Assert.AreEqual(1, quickFindUF.Find(0));
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            //Quick-find
            QuickFindUF grid = new QuickFindUF(10);

            grid.Union(2, 3);
            grid.Union(3, 6);
            grid.Union(6, 5);
            Console.WriteLine($"Does 2 and 5 connect? {grid.Connected(2, 5)}");
            Console.WriteLine($"Does 6 and 1 connect? {grid.Connected(1, 6)}");

            //Quick-union
            QuickUnionUF quuf = new QuickUnionUF(10);

            quuf.Union(2, 3);
            quuf.Union(3, 6);
            quuf.Union(6, 5);
            Console.WriteLine($"Does 2 and 5 connect? {quuf.Connected(2, 5)}");
            Console.WriteLine($"Does 6 and 1 connect? {quuf.Connected(1, 6)}");

            Console.WriteLine(GreatestCommonDenominator.GetGCD(9, 0));
        }
Exemplo n.º 4
0
    public static void Main()
    {
        QuickFindUF uf = new QuickFindUF(10);

        uf.Union(0, 5);
        Console.WriteLine("Connected(0,5) " + uf.Connected(0, 5));
        Console.WriteLine("Connected(5,6) " + uf.Connected(5, 6));
        uf.Union(5, 6);
        uf.Union(1, 2);
        uf.Union(2, 7);
        uf.Union(3, 4);
        uf.Union(4, 8);
        uf.Union(8, 9);
        Console.WriteLine("Connected(0,7) " + uf.Connected(0, 7));
        Console.WriteLine("Connected(1,2) " + uf.Connected(1, 2));
        Console.WriteLine("Connected(3,4) " + uf.Connected(3, 4));
        Console.WriteLine("Connected(5,9) " + uf.Connected(5, 9));
        Console.WriteLine("Connected(8,9) " + uf.Connected(8, 9));
    }
Exemplo n.º 5
0
    public static void Main(String[] args)
    {
        int         n  = Convert.ToInt32(Console.ReadLine());
        QuickFindUF uf = new QuickFindUF(n);
        string      line;

        while ((line = Console.ReadLine()) != null && line != "")
        {
            string[] a_temp = line.Split(' ');
            int[]    array  = Array.ConvertAll(a_temp, Int32.Parse);
            int      p      = array[0];
            int      q      = array[1];

            if (uf.Connected(p, q))
            {
                continue;
            }
            uf.Union(p, q);

            Console.WriteLine(p + " " + q);
        }
        Console.WriteLine(uf.Count() + " components");
        Console.ReadLine();
    }
Exemplo n.º 6
0
        public void testConnected_ParameterValidation_QLessThan0()
        {
            QuickFindUF quickFindUF = new QuickFindUF(10);

            quickFindUF.Connected(4, -1);
        }
Exemplo n.º 7
0
        public void testConnected_ParameterValidation_PLargerThanN()
        {
            QuickFindUF quickFindUF = new QuickFindUF(10);

            quickFindUF.Connected(11, 4);
        }
Exemplo n.º 8
0
        public void testConnected_ParameterValidation_PEqualN()
        {
            QuickFindUF quickFindUF = new QuickFindUF(10);

            quickFindUF.Connected(10, 4);
        }