public void testUnionIllegalArgumentException1() { DisjointSets <string> disjSets = new DisjointSets <string>( "a"); disjSets.union("b", "a"); }
public void testUnion() { DisjointSets <string> disjSets = new DisjointSets <string>("a", "b", "c", "d"); Assert.AreEqual(4, disjSets.numberDisjointSets()); disjSets.union("a", "b"); Assert.AreEqual(3, disjSets.numberDisjointSets()); assertArrayEquals(disjSets.find("a"), disjSets.find("b")); disjSets.union("c", "d"); Assert.AreEqual(2, disjSets.numberDisjointSets()); assertArrayEquals(disjSets.find("c"), disjSets.find("d")); disjSets.union("b", "c"); Assert.AreEqual(1, disjSets.numberDisjointSets()); }
public void testWorkedExample() { // Should be the following when finished: // {a, b, c, d}, {e, f, g}, {h, i}, and {j} // 1. initial sets DisjointSets <string> disjSets = new DisjointSets <string>("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"); Assert.AreEqual(10, disjSets.numberDisjointSets()); Assert.AreEqual(1, disjSets.find("a").Size()); Assert.AreEqual(1, disjSets.find("b").Size()); Assert.AreEqual(1, disjSets.find("c").Size()); Assert.AreEqual(1, disjSets.find("d").Size()); Assert.AreEqual(1, disjSets.find("e").Size()); Assert.AreEqual(1, disjSets.find("f").Size()); Assert.AreEqual(1, disjSets.find("g").Size()); Assert.AreEqual(1, disjSets.find("h").Size()); Assert.AreEqual(1, disjSets.find("i").Size()); Assert.AreEqual(1, disjSets.find("j").Size()); // 2. (b, d) disjSets.union("b", "d"); Assert.AreEqual(9, disjSets.numberDisjointSets()); assertArrayEquals(disjSets.find("b"), disjSets.find("d")); // 3. (e, g) disjSets.union("e", "g"); Assert.AreEqual(8, disjSets.numberDisjointSets()); assertArrayEquals(disjSets.find("e"), disjSets.find("g")); // 4. (a, c) disjSets.union("a", "c"); Assert.AreEqual(7, disjSets.numberDisjointSets()); assertArrayEquals(disjSets.find("a"), disjSets.find("c")); // 5. (h, i) disjSets.union("h", "i"); Assert.AreEqual(6, disjSets.numberDisjointSets()); assertArrayEquals(disjSets.find("h"), disjSets.find("i")); // 6. (a, b) disjSets.union("a", "b"); Assert.AreEqual(5, disjSets.numberDisjointSets()); assertArrayEquals(disjSets.find("a"), disjSets.find("b")); assertArrayEquals(disjSets.find("b"), disjSets.find("c")); assertArrayEquals(disjSets.find("c"), disjSets.find("d")); // 7. (e, f) disjSets.union("e", "f"); Assert.AreEqual(4, disjSets.numberDisjointSets()); assertArrayEquals(disjSets.find("e"), disjSets.find("f")); assertArrayEquals(disjSets.find("f"), disjSets.find("g")); // 8. (b, c) disjSets.union("b", "c"); Assert.AreEqual(4, disjSets.numberDisjointSets()); assertArrayEquals(disjSets.find("a"), disjSets.find("b")); assertArrayEquals(disjSets.find("b"), disjSets.find("c")); assertArrayEquals(disjSets.find("c"), disjSets.find("d")); }
public Maze(int horizontalSize, int verticalSize) { int i, j; horiz = horizontalSize; vert = verticalSize; if ((horiz < 1) || (vert < 1) || ((horiz == 1) && (vert == 1))) { return; // There are no interior walls } // Create all of the horizontal interior walls. Initially, every // horizontal wall exists; they will be removed later by the maze // generation algorithm. if (vert > 1) { hWalls = new bool[horiz, vert - 1]; for (j = 0; j < vert - 1; j++) { for (i = 0; i < horiz; i++) { hWalls[i, j] = true; } } } // Create all of the vertical interior walls. if (horiz > 1) { vWalls = new bool[horiz - 1, vert]; for (i = 0; i < horiz - 1; i++) { for (j = 0; j < vert; j++) { vWalls[i, j] = true; } } } int walls = (vWalls.Length * (horizontalSize)) + (hWalls.Length * (verticalSize - 1)); int[,] array = new int[walls, 3]; int k = 0; if (vert > 1) { for (j = 0; j < vert - 1; j++) { for (i = 0; i < horiz; i++) { array[k, 0] = i; array[k, 1] = j; array[k, 2] = 0; //0 is horizontal k++; } } } if (horiz > 1) { for (i = 0; i < horiz - 1; i++) { for (j = 0; j < vert; j++) { array[k, 0] = i; array[k, 1] = j; array[k, 2] = 1; //1 is vertical k++; } } } for (int a = walls; a > 0; a--) { int randInt = Random.Range(0, a); int[] holder = new int[3]; holder[0] = array[randInt, 0]; holder[1] = array[randInt, 1]; holder[2] = array[randInt, 2]; array[randInt, 0] = array[a - 1, 0]; array[randInt, 1] = array[a - 1, 1]; array[randInt, 2] = array[a - 1, 2]; array[a - 1, 0] = holder[0]; array[a - 1, 1] = holder[1]; array[a - 1, 2] = holder[2]; } DisjointSets set = new DisjointSets(horiz * vert); for (int a = 0; a < walls; a++) { int x = array[a, 0]; int y = array[a, 1]; if (array[a, 2] == 1) //0 is horizontal { int l_map = x + (y * horiz); int r_map = l_map + 1; int left = set.find(l_map); int right = set.find(r_map); if (left != right) { set.union(left, right); vWalls[x, y] = false; } } else if (array[a, 2] == 0) //1 is vertical { int t_map = x + (y * horiz); int top = set.find(t_map); int b_map = t_map + horiz; int bot = set.find(b_map); if (top != bot) { set.union(top, bot); hWalls[x, y] = false; } } } }