Ejemplo n.º 1
0
        public void Test_SGG_Creation()
        {
            var g = new SquireGridGraph(ROW, COL, false);


            for (int i = 0; i < g.VerticesNum(); i++)
            {
                int row, col;
                g.GetRowAndColFromVertex(i, out row, out col);

                g.SetNeighbor(row, col, Direction.East, 1);
                g.SetNeighbor(row, col, Direction.South, 1);
                g.SetNeighbor(row, col, Direction.West, 1);
                g.SetNeighbor(row, col, Direction.North, 1);
            }

            ITravel dfs = new Bfs(g, preVisit);

            dfs.Travel(16);
            Assert.AreEqual(6280, g.EdgeNum());
        }
Ejemplo n.º 2
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_result.Count == 0)
            {
                timer1.Stop();
                Toggle();
                return;
            }
            int      row, col;
            var      bm    = (Bitmap)pictureBox1.Image;
            Graphics gg    = Graphics.FromImage(bm);
            Brush    brush = new SolidBrush(Color.Blue);
            int      v     = _result[_result.Count - 1];

            _result.RemoveAt(_result.Count - 1);
            _graph.GetRowAndColFromVertex(v, out row, out col);
            gg.FillRectangle(brush, _margin + col * Radius + 3, _margin + row * Radius + 3, 3, 3);
            pictureBox1.Image = bm;
            pictureBox1.Refresh();

            brush.Dispose();
            gg.Dispose();
        }
Ejemplo n.º 3
0
        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());
        }
Ejemplo n.º 4
0
        private void genMazeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _graph = new SquireGridGraph(Row, Col, false);
            var ds = new DisjSet(_graph.VerticesNum());
            var 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);
                }
            }

            var      pen    = new Pen(Color.Brown, 1);
            Brush    brush  = new SolidBrush(Color.Blue);
            var      bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics gg     = Graphics.FromImage(bitmap);

            for (int i = 1; i < _graph.VerticesNum() - 1; i++)
            {
                int row, col;
                _graph.GetRowAndColFromVertex(i, out row, out col);
                int v2 = _graph.GetNeighborVertex(i, Direction.West);
                if (v2 == -1 || !_graph.IsConnected(i, v2))
                {
                    // Draw Line
                    gg.DrawLine(pen, _margin + col * Radius, _margin + row * Radius,
                                _margin + col * Radius, _margin + row * Radius + Radius);
                }

                v2 = _graph.GetNeighborVertex(i, Direction.North);
                if (v2 == -1 || !_graph.IsConnected(i, v2))
                {
                    // Draw Line
                    gg.DrawLine(pen, _margin + col * Radius, _margin + row * Radius,
                                _margin + col * Radius + Radius, _margin + row * Radius);
                }

                v2 = _graph.GetNeighborVertex(i, Direction.East);
                if (v2 == -1 || !_graph.IsConnected(i, v2))
                {
                    // Draw Line
                    gg.DrawLine(pen, _margin + col * Radius + Radius, _margin + row * Radius,
                                _margin + col * Radius + Radius, _margin + row * Radius + Radius);
                }

                v2 = _graph.GetNeighborVertex(i, Direction.South);
                if (v2 == -1 || !_graph.IsConnected(i, v2))
                {
                    // Draw Line
                    gg.DrawLine(pen, _margin + col * Radius, _margin + row * Radius + Radius,
                                _margin + col * Radius + Radius, _margin + row * Radius + Radius);
                }
            }
            pictureBox1.Image = bitmap;
            pictureBox1.Refresh();
            pen.Dispose();
            brush.Dispose();
            gg.Dispose();
        }