public void connect_elements()
        {
            quickunion_weighted qu = new quickunion_weighted(10);
            int[] groups = qu.GetObjects();
            quickunion.Union(groups, 2, 6);

            Assert.IsTrue(qu.Connected(2, 6), "Union has not connected elements");
        }
 public void init_elements_should_belong_to_diff_groups()
 {
     quickunion_weighted qu = new quickunion_weighted(10);
     int[] groups = qu.GetObjects();
     for (int i = 0; i < groups.Length; i++) {
         // each group can only be present once
         Assert.AreEqual(1, groups.Count(x => x == groups[i]), "There is more than one occurence of the group " + groups[i]);
     }
 }
        public void are_connected()
        {
            quickunion_weighted qu = new quickunion_weighted(10);
            int[] groups = qu.GetObjects();
            groups[5] = 1;
            groups[3] = 1;

            Assert.IsTrue(qu.Connected(5, 3), "Elements should be connected");
        }
Exemplo n.º 4
0
        /*(seed = 62846)
        Give the id[] array that results from the following sequence of 9 union
        operations on a set of 10 items using the weighted quick-union algorithm from lecture.

        7-8 5-1 2-9 8-9 5-0 1-6 4-3 0-8 1-3

        Recall: when joining two trees of equal size, our weighted quick union convention is to
        make the root of the second tree point to the root of the first tree. Also, our weighted
        quick union algorithm uses union by size (number of nodes), not union by height.
         */
        public static void q2(string unionOperations)
        {
            Console.WriteLine("Question 2 - quickunion (w)");
            quickunion_weighted qu = new quickunion_weighted(10);
            int[] data = qu.GetObjects();
            Console.WriteLine("\t" + helpers.ArrayPrintout<int>(data));
            foreach (var op in unionOperations.Split(' ')) {
                int p = int.Parse(op.Split('-')[0]);
                int q = int.Parse(op.Split('-')[1]);
                Console.Write(p + "-" + q + ":\t");
                qu.Union(p, q);
                Console.WriteLine(helpers.ArrayPrintout<int>(data));
            }
            Console.WriteLine("----------------------------");
        }
 public void should_have_enough_elements()
 {
     quickunion_weighted qu = new quickunion_weighted(10);
     int[] groups = qu.GetObjects();
     Assert.AreEqual(10, groups.Length);
 }
 public void disconnected_elements()
 {
     quickunion_weighted qu = new quickunion_weighted(10);
 }