Пример #1
0
        // Graph/Tree Traversal (Group A) is implemented here.
        private void ButtonNext_Click(object sender, EventArgs e)
        {
            if (buttonNext.Text == "Close")
            {
                this.Close();
            }
            else
            {
                labelInformation.Visible = true;

                // Step 1 operation
                if (currentStep == 1)
                {
                    // Highlight current step
                    label1.ForeColor     = Color.Red;
                    labelStep1.ForeColor = Color.Red;
                    label2.ForeColor     = SystemColors.ControlText;
                    labelStep2.ForeColor = SystemColors.ControlText;
                    label3.ForeColor     = SystemColors.ControlText;
                    labelStep3.ForeColor = SystemColors.ControlText;

                    // Initialise Prim's algorithm
                    for (int i = 0; i < mapMatrix.GetSize(); i++)
                    {
                        if (mapMatrix.IsVertexExisting(i))
                        {
                            remainingVertices.Add(i);
                        }
                    }
                    labelInformation.Text = "Please pick a vertex of your choice:\nPlease click on the vertex of the graph.";
                    buttonNext.Enabled    = false;
                }

                // Step 2 operation
                else if (currentStep == 2)
                {
                    // Highlight current step
                    label1.ForeColor     = SystemColors.ControlText;
                    labelStep1.ForeColor = SystemColors.ControlText;
                    label2.ForeColor     = Color.Red;
                    labelStep2.ForeColor = Color.Red;
                    label3.ForeColor     = SystemColors.ControlText;
                    labelStep3.ForeColor = SystemColors.ControlText;

                    // Find minimum value of edge weight and candidate edges
                    double       min            = Double.MaxValue;
                    List <int[]> candidateEdges = new List <int[]>();

                    foreach (int i in visitedVertices)
                    {
                        foreach (int j in remainingVertices)
                        {
                            if (mapMatrix.ContainsEdge(i, j) && mapMatrix.GetEdge(i, j) < min) // Find minimum value of edge weight
                            {
                                min = mapMatrix.GetEdge(i, j);
                            }
                        }
                    }

                    foreach (int i in visitedVertices)
                    {
                        foreach (int j in remainingVertices)
                        {
                            if (mapMatrix.ContainsEdge(i, j) && mapMatrix.GetEdge(i, j) == min) // Find candidate edges
                            {
                                candidateEdges.Add(new int[2] {
                                    i, j
                                });
                            }
                        }
                    }

                    // More than two candidate edges - User operation required
                    if (candidateEdges.Count >= 2)
                    {
                        // Show explanation
                        labelInformation.Text = "Edges ";
                        foreach (int[] edge in candidateEdges)
                        {
                            labelInformation.Text += Convert.ToChar(edge[0] + 'A').ToString() + Convert.ToChar(edge[1] + 'A').ToString() + ", ";
                        }
                        labelInformation.Text  = labelInformation.Text.TrimEnd(", ".ToCharArray());
                        labelInformation.Text += " have the same weight (" + min.ToString() + "). Please pick one of your choice:\n"
                                                 + "Please click on the vertex or the weight.";

                        // Highlight candidate edges
                        for (int i = 0; i < candidateEdges.Count; i++)
                        {
                            int    v1        = Math.Min(candidateEdges[i][0], candidateEdges[i][1]);
                            int    v2        = Math.Max(candidateEdges[i][0], candidateEdges[i][1]);
                            string labelName = "label" + Convert.ToChar(v1 + 'A').ToString() + Convert.ToChar(v2 + 'A').ToString();
                            foreach (Label label in exampleGraph.labelWeights)
                            {
                                if (label.Name == labelName)
                                {
                                    label.ForeColor = Color.Red;
                                    break;
                                }
                            }
                            foreach (Vertex vertex in vertices)
                            {
                                if (vertex.GetNumberIndex() == candidateEdges[i][1])
                                {
                                    vertex.labelName.ForeColor = Color.Red;
                                    break;
                                }
                            }
                        }

                        currentStep        = 2;
                        buttonNext.Enabled = false;
                    }

                    // Only one candidate edge avaliable - No user operation needed
                    else
                    {
                        // Show explanation
                        int[] newEdge = new int[2] {
                            candidateEdges[0][0], candidateEdges[0][1]
                        };
                        labelInformation.Text = "Edge " + Convert.ToChar(candidateEdges[0][0] + 'A').ToString() + Convert.ToChar(candidateEdges[0][1] + 'A').ToString()
                                                + " has the minimum weight joining a vertex already included to a vertex not already included (" + min.ToString() + "),"
                                                + " therefore it has been added to the Minimum Spanning Tree.";

                        // Update Prim's algorithm
                        foreach (Vertex vertex in vertices)
                        {
                            if (vertex.GetNumberIndex() == newEdge[1])
                            {
                                weightMST             += min;
                                labelTotalWeight.Text += min.ToString() + " + ";
                                visitedVertices.Add(newEdge[1]);
                                remainingVertices.Remove(newEdge[1]);
                                EdgeFocusOn(newEdge[0], newEdge[1]);
                                exampleGraph.LabelFocusOn(newEdge[0], newEdge[1]);
                                currentStep = 3;
                                break;
                            }
                        }
                    }
                }

                // Step 3 operation
                else // if (currentStep == 3)
                {
                    // Highlight current steps
                    label1.ForeColor     = SystemColors.ControlText;
                    labelStep1.ForeColor = SystemColors.ControlText;
                    label2.ForeColor     = SystemColors.ControlText;
                    labelStep2.ForeColor = SystemColors.ControlText;
                    label3.ForeColor     = Color.Red;
                    labelStep3.ForeColor = Color.Red;

                    // Check if Prim's algorithm has finished
                    if (remainingVertices.Any())
                    {
                        labelInformation.Text = "We have not yet formed a Minimum Spanning Tree, so go back to STEP 2.";
                        currentStep           = 2;
                    }
                    else
                    {
                        labelInformation.Text    = "Now we have picked " + (mapMatrix.Count() - 1).ToString() + " edges and has formed a Minimum Spanning Tree.\nTherefore Prim's algorithm has finished.";
                        labelTotalWeight.Text    = labelTotalWeight.Text.TrimEnd(" + ".ToCharArray());
                        labelTotalWeight.Text   += " = " + weightMST.ToString();
                        labelTotalWeight.Visible = true;
                        buttonNext.Text          = "Close";
                    }
                }
            }
        }
Пример #2
0
        // Graph/Tree Traversal (Group A) is implemented here.
        private void ButtonNext_Click(object sender, EventArgs e)
        {
            if (buttonNext.Text == "Close")
            {
                this.Close();
            }
            else
            {
                // Step 1 operation
                if (currentStep == 1)
                {
                    // Highlight current step
                    label1.ForeColor     = Color.Red;
                    labelStep1.ForeColor = Color.Red;
                    label2.ForeColor     = SystemColors.ControlText;
                    labelStep2.ForeColor = SystemColors.ControlText;
                    label3.ForeColor     = SystemColors.ControlText;
                    labelStep3.ForeColor = SystemColors.ControlText;

                    // Show the headers of the list of all edges
                    labelList.Visible        = true;
                    labelEdgeLeft.Visible    = true;
                    labelWeightLeft.Visible  = true;
                    labelEdgeRight.Visible   = true;
                    labelWeightRight.Visible = true;

                    // Initialise and sort the list of all edges
                    int edgeCount = 0;
                    for (int v1 = 0; v1 < mapMatrix.GetSize() - 1; v1++)
                    {
                        for (int v2 = v1 + 1; v2 < mapMatrix.GetSize(); v2++)
                        {
                            if (mapMatrix.ContainsEdge(v1, v2))
                            {
                                edgeCount++;
                                edgeList.Add(new Edge
                                {
                                    vStart  = v1,
                                    vFinish = v2,
                                    weight  = mapMatrix.GetEdge(v1, v2)
                                });
                            }
                        }
                    }
                    edgeCount /= 2;
                    QuickSort(edgeList, 0, edgeList.Count - 1);

                    // Show the list of all edges
                    Point[] originalLocations = new Point[2];
                    originalLocations[0] = new Point(24, 332);
                    originalLocations[1] = new Point(originalLocations[0].X + 228, originalLocations[0].Y);
                    for (int i = 0; i < edgeList.Count; i++)
                    {
                        Point location = new Point(originalLocations[i / edgeCount].X, originalLocations[i / edgeCount].Y + 44 * (i % edgeCount));
                        labelEdges.Add(new Label()
                        {
                            AutoSize    = true,
                            Font        = new Font("Microsoft YaHei", 12F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134))),
                            Location    = location,
                            MinimumSize = new Size(100, 44),
                            Name        = "labelEdge" + i.ToString(),
                            Size        = new Size(100, 44),
                            Text        = Convert.ToChar(edgeList[i].vStart + 'A').ToString() + Convert.ToChar(edgeList[i].vFinish + 'A').ToString(),
                            TextAlign   = ContentAlignment.TopCenter
                        });
                        labelEdges[i].Click += new EventHandler(LabelEdge_Click);
                        labelWeights.Add(new Label()
                        {
                            AutoSize    = true,
                            Font        = new Font("Microsoft YaHei", 12F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134))),
                            Location    = new Point(location.X + 100, location.Y),
                            MinimumSize = new Size(100, 44),
                            Name        = "labelWeight" + i.ToString(),
                            Size        = new Size(100, 44),
                            Text        = edgeList[i].weight.ToString(),
                            TextAlign   = ContentAlignment.TopCenter
                        });
                        labelWeights[i].Click += new EventHandler(LabelEdge_Click);
                        labelEdgeUsed.Add(new Label()
                        {
                            AutoSize    = true,
                            Font        = new Font("Microsoft YaHei", 12F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134))),
                            Location    = new Point(location.X + 200, location.Y),
                            MinimumSize = new Size(0, 22),
                            Name        = "labelEdgeUsed" + i.ToString(),
                            Size        = new Size(22, 22),
                            Text        = "×",
                            TextAlign   = ContentAlignment.TopCenter,
                            Visible     = false
                        });
                        this.Controls.Add(labelEdges[i]);
                        this.Controls.Add(labelWeights[i]);
                        this.Controls.Add(labelEdgeUsed[i]);
                    }
                    currentEdgeIndex = 0;
                    currentWeight    = edgeList[0].weight;
                    currentStep      = 2;
                }

                // Step 2 operations
                else if (currentStep == 2)
                {
                    // Highlight current step
                    label1.ForeColor     = SystemColors.ControlText;
                    labelStep1.ForeColor = SystemColors.ControlText;
                    label2.ForeColor     = Color.Red;
                    labelStep2.ForeColor = Color.Red;
                    label3.ForeColor     = SystemColors.ControlText;
                    labelStep3.ForeColor = SystemColors.ControlText;
                    foreach (Label label in labelEdges)
                    {
                        label.ForeColor = SystemColors.ControlText;
                    }
                    foreach (Label label in labelWeights)
                    {
                        label.ForeColor = SystemColors.ControlText;
                    }
                    labelEdges[currentEdgeIndex].ForeColor   = Color.Red;
                    labelWeights[currentEdgeIndex].ForeColor = Color.Red;

                    // Initialise text explanation label
                    labelInformation.Text    = "";
                    labelInformation.Visible = true;

                    // Perform Kruskal's algorithm
                    if (Find(edgeList[currentEdgeIndex].vStart).GetLeader() == Find(edgeList[currentEdgeIndex].vFinish).GetLeader()) // Reject edges forming a cycle
                    {
                        labelInformation.Text = "Adding edge " + labelEdges[currentEdgeIndex].Text + " will form a cycle, therefore edge " + labelEdges[currentEdgeIndex].Text + " should not be chosen.";
                        labelEdgeUsed[currentEdgeIndex].Text      = "×";
                        labelEdgeUsed[currentEdgeIndex].ForeColor = Color.Red;
                        labelEdgeUsed[currentEdgeIndex].Visible   = true;
                        labelEdges[currentEdgeIndex].Font         = new Font("Microsoft YaHei", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Strikeout))), System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                        labelWeights[currentEdgeIndex].Font       = new Font("Microsoft YaHei", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Strikeout))), System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                        do
                        {
                            currentEdgeIndex++;
                        } while (labelEdgeUsed[currentEdgeIndex].Visible);
                        currentWeight = edgeList[currentEdgeIndex].weight;
                        currentStep   = 2;
                    }
                    else
                    {
                        int         i            = currentEdgeIndex + 1;
                        List <Edge> tempEdgeList = new List <Edge>
                        {
                            edgeList[currentEdgeIndex]
                        };
                        while (edgeList[i].weight == currentWeight && !labelEdgeUsed[i].Visible)
                        {
                            if (Find(edgeList[i].vStart).GetLeader() != Find(edgeList[i].vFinish).GetLeader()) // Highlight feasible candidate edges
                            {
                                labelEdges[i].ForeColor   = Color.Red;
                                labelWeights[i].ForeColor = Color.Red;
                                tempEdgeList.Add(edgeList[i]);
                            }
                            else // Reject edges forming a cycle
                            {
                                labelInformation.Text     += "Adding edge " + labelEdges[i].Text + " will form a cycle, therefore edge " + labelEdges[i].Text + " should not be chosen.\n";
                                labelEdgeUsed[i].Text      = "×";
                                labelEdgeUsed[i].ForeColor = Color.Red;
                                labelEdgeUsed[i].Visible   = true;
                                labelEdges[i].Font         = new Font("Microsoft YaHei", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Strikeout))), System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                                labelWeights[i].Font       = new Font("Microsoft YaHei", 12F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Strikeout))), System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                            }
                            i++;
                        }
                        if (tempEdgeList.Count >= 2) // More than two candidate edges - user operation required
                        {
                            labelInformation.Text += "Edges ";
                            foreach (Edge edge in tempEdgeList)
                            {
                                labelInformation.Text += Convert.ToChar(edge.vStart + 'A').ToString() + Convert.ToChar(edge.vFinish + 'A').ToString() + ", ";
                            }
                            labelInformation.Text  = labelInformation.Text.TrimEnd(", ".ToCharArray());
                            labelInformation.Text += " have the same weight. Please pick one of your choice.\n"
                                                     + "Please click on the edge in the list (NOT on the graph).";
                            currentStep        = 2;
                            buttonNext.Enabled = false;
                        }
                        else // One candidate edge only
                        {
                            labelInformation.Text += "Edge " + labelEdges[currentEdgeIndex].Text + " has been added to the Minimum Spanning Tree.";
                            labelEdgeUsed[currentEdgeIndex].Text      = "√";
                            labelEdgeUsed[currentEdgeIndex].ForeColor = Color.Green;
                            labelEdgeUsed[currentEdgeIndex].Visible   = true;
                            EdgeFocusOn(edgeList[currentEdgeIndex].vStart, edgeList[currentEdgeIndex].vFinish);
                            exampleGraph.LabelFocusOn(edgeList[currentEdgeIndex].vStart, edgeList[currentEdgeIndex].vFinish);
                            treeEdgeCount++;
                            weightMST += currentWeight;
                            Union(Find(edgeList[currentEdgeIndex].vStart).GetLeader(), Find(edgeList[currentEdgeIndex].vFinish).GetLeader());
                            currentEdgeIndex = i;
                            while (labelEdgeUsed[currentEdgeIndex].Visible)
                            {
                                currentEdgeIndex++;
                            }
                            currentWeight = edgeList[currentEdgeIndex].weight;
                            currentStep   = 3;
                        }
                    }
                }

                // Step 3 operation
                else // if (currentStep == 3)
                {
                    // Highlight current step
                    label1.ForeColor     = SystemColors.ControlText;
                    labelStep1.ForeColor = SystemColors.ControlText;
                    label2.ForeColor     = SystemColors.ControlText;
                    labelStep2.ForeColor = SystemColors.ControlText;
                    label3.ForeColor     = Color.Red;
                    labelStep3.ForeColor = Color.Red;
                    foreach (Label label in labelEdges)
                    {
                        label.ForeColor = SystemColors.ControlText;
                    }
                    foreach (Label label in labelWeights)
                    {
                        label.ForeColor = SystemColors.ControlText;
                    }

                    // Initialise text explanation label
                    labelInformation.Text    = "";
                    labelInformation.Visible = true;

                    // Check if the algorithm is finished
                    if (treeEdgeCount < mapMatrix.Count() - 1)
                    {
                        labelInformation.Text = "We have not yet formed a Minimum Spanning Tree, so go back to STEP 2.";
                        currentStep           = 2;
                    }
                    else
                    {
                        labelInformation.Text = "Now we have picked " + (mapMatrix.Count() - 1).ToString() + " edges and has formed a Minimum Spanning Tree.\nTherefore Kruskal's algorithm has finished.";
                        for (int i = 0; i < edgeList.Count; i++)
                        {
                            if (labelEdgeUsed[i].Text == "√")
                            {
                                labelTotalWeight.Text += labelWeights[i].Text + " + ";
                            }
                        }
                        labelTotalWeight.Text    = labelTotalWeight.Text.TrimEnd(" + ".ToCharArray());
                        labelTotalWeight.Text   += " = " + weightMST.ToString();
                        labelTotalWeight.Visible = true;
                        buttonNext.Text          = "Close";
                    }
                }
            }
        }
Пример #3
0
        public FormPrimOnMatrix(int accountID, string username, string accountName, string accountType, int example)
        {
            InitializeComponent();

            // Show account name on the account menu.
            this.accountMenu.accountID             = accountID;
            this.accountMenu.username              = username;
            this.accountMenu.labelAccountName.Text = accountName;
            this.accountMenu.accountType           = accountType;
            this.example = example;

            // Select the correct example graph to perform the demonstration.
            if (example == 1)
            {
                exampleGraph = new MinimumSpanningTreeExample1(this.panelGraph);
            }
            else
            {
                exampleGraph = new MinimumSpanningTreeExample2(this.panelGraph);
            }

            // Initialise the example graph.
            vertices  = exampleGraph.GetVertices();
            mapMatrix = exampleGraph.GetMatrix();

            // Initialise the table for the example graph.
            for (int i = 0; i <= mapMatrix.Count(); i++)
            {
                DataGridViewColumn newColumn = new DataGridViewColumn
                {
                    CellTemplate = new DataGridViewTextBoxCell(),
                    SortMode     = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable,
                    Width        = 60
                };
                if (i == 0)
                {
                    newColumn.Width = 41;
                }
                dataGridViewGraph.Columns.Add(newColumn);
            }

            int count = 1;

            for (int i = 0; i < mapMatrix.GetSize(); i++)
            {
                if (mapMatrix.IsVertexExisting(i))
                {
                    dataGridViewGraph.Columns[count].HeaderText = Convert.ToChar('A' + i).ToString();
                    dataGridViewGraph.Columns[count].Name       = "Column" + dataGridViewGraph.Columns[count].HeaderText;
                    count++;
                }
            }

            count = 0;
            this.dataGridViewGraph.RowCount = mapMatrix.Count();
            for (int i = 0; i < mapMatrix.GetSize(); i++)
            {
                if (mapMatrix.IsVertexExisting(i))
                {
                    this.dataGridViewGraph[0, count++].Value = (Convert.ToChar('A' + i)).ToString();
                }
            }

            for (int col = 1; col <= mapMatrix.Count(); col++)
            {
                for (int row = 0; row < mapMatrix.Count(); row++)
                {
                    int vStartIndex  = mapMatrix.GetVertexIndex(dataGridViewGraph.Columns[col].HeaderText);
                    int vFinishIndex = mapMatrix.GetVertexIndex(this.dataGridViewGraph[0, row].Value.ToString());
                    if (mapMatrix.ContainsEdge(vStartIndex, vFinishIndex))
                    {
                        this.dataGridViewGraph[col, row].Value = mapMatrix.GetEdge(vStartIndex, vFinishIndex);
                    }
                    else
                    {
                        this.dataGridViewGraph[col, row].Value = "-";
                    }
                }
            }
        }