Beispiel #1
0
        private void Generate_Click(object sender, EventArgs e)
        {
            generate        = true;
            button2.Enabled = true;

            int numVers = (int)numericUpDown1.Value;

            E = new List <Edge>();
            V = new List <Vertex>();

            for (int i = 0; i < numVers; i++)
            {
                Vertex v = new Vertex(i);

                v.Edges = new List <Edge>();
                V.Add(v);
            }

            for (int i = 0; i < numVers; i++)
            {
                //int numEdges = random.Next(numVers - 1);
                int numEdges = RandomService.GetNoOfEdges(numVers - 1);

                while (numEdges < 2)
                {
                    numEdges = random.Next(numVers - 1);
                }

                Vertex v = V[i];

                for (int j = 0; j < numEdges; j++)
                {
                    int  id = random.Next(numVers);
                    int  wt = random.Next(100);
                    Edge edge;

                    while (wt < 10)
                    {
                        wt = random.Next(100);
                    }

                    while (id == v.Id)
                    {
                        id = random.Next(numVers);
                    }

                    edge = new Edge(v, V[id], wt);

                    if (!edge.InList(E))
                    {
                        E.Add(edge);
                    }

                    edge = new Edge(V[id], v, wt);

                    if (!edge.InList(E))
                    {
                        E.Add(edge);
                    }
                }
            }

            for (int i = 0; i < E.Count; i++)
            {
                Vertex u = E[i].Lt, v = E[i].Rt;

                u.Edges.Add(new Edge(u, v, E[i].Wt));
                v.Edges.Add(new Edge(v, u, E[i].Wt));
            }

            for (int i = 0; i < V.Count; i++)
            {
                if (V[i].Edges.Count == 0)
                {
                    MessageBox.Show("Generate a new graph", "Warning Message",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            textBox1.Text  = string.Empty;
            textBox1.Text  = "Edges(u, v)\r\n";
            textBox1.Text += " u  v wt\r\n";

            for (int i = 0; i < E.Count; i++)
            {
                textBox1.Text += E[i].Lt.Id.ToString().PadLeft(2) + " ";
                textBox1.Text += E[i].Rt.Id.ToString().PadLeft(2) + " ";
                textBox1.Text += E[i].Wt.ToString().PadLeft(2) + "\r\n";
            }

            panel1.Invalidate();
        }