public void Test_Maze_generate() { /** * 1. Éú³ÉSquareGridͼ * * */ var graph = new SquireGridGraph(row, col, false); DisjSet ds = new DisjSet(graph.VerticesNum()); Random r = new Random(Environment.TickCount); while (ds.Find(0) != ds.Find(graph.VerticesNum() - 1)) { int v1 = r.Next(0, graph.VerticesNum()); if (graph.GetDegree(v1) >= 3) { continue; } Direction direction = getRandomDirection(); int v2 = graph.GetNeighborVertex(v1, direction); if (v2 != -1) { graph.SetEdge(v1, v2, 1); ds.UnionSets(v1, v2); } } Assert.IsTrue(graph.EdgeNum() > 1); Assert.IsTrue(ds.GetConnectedComponentNumber() > 1); }
public void Test_Graph_DisjSet() { var g = new SquireGridGraph(ROW, COL, false); DisjSet ds = new DisjSet(g.VerticesNum()); for (int i = 0; i < g.VerticesNum(); i++) { int row, col, v; g.GetRowAndColFromVertex(i, out row, out col); g.SetNeighbor(row, col, Direction.East, 1); if ((v = g.GetNeighborVertex(i, Direction.East)) != -1) { ds.UnionSets(i, v); } g.SetNeighbor(row, col, Direction.South, 1); if ((v = g.GetNeighborVertex(i, Direction.South)) != -1) { ds.UnionSets(i, v); } g.SetNeighbor(row, col, Direction.West, 1); if ((v = g.GetNeighborVertex(i, Direction.West)) != -1) { ds.UnionSets(i, v); } g.SetNeighbor(row, col, Direction.North, 1); if ((v = g.GetNeighborVertex(i, Direction.North)) != -1) { ds.UnionSets(i, v); } } Assert.AreEqual(1, ds.GetConnectedComponentNumber()); }