/// <summary> /// Compares this instance with a specified adjacency matrix. /// </summary> /// <param name="matrix">The specified adjacency matrix that this instantce is to be compared with.</param> /// <returns> /// Returns NULL if the two adjaceny matrices are identical. /// If the two adjacency matrices are different, return a List<int> containing the indexes of different vertices. /// </returns> public List <int> CompareTo(AdjacencyMatrix matrix) { bool isEqual = true; List <int> differentVertices = new List <int>(); if (matrix == null) { isEqual = false; for (int v = 0; v < GetSize(); v++) { if (this.IsVertexExisting(v)) { if (!differentVertices.Contains(v)) { differentVertices.Add(v); } } } differentVertices.Sort(); return(differentVertices); } for (int v = 0; v < GetSize(); v++) { if (this.IsVertexExisting(v) != matrix.IsVertexExisting(v)) { isEqual = false; if (!differentVertices.Contains(v)) { differentVertices.Add(v); } } } for (int row = 0; row < GetSize(); row++) { for (int col = 0; col < GetSize(); col++) { if (!this.GetEdge(row, col).Equals(matrix.GetEdge(row, col))) { isEqual = false; if (!differentVertices.Contains(row)) { differentVertices.Add(row); } } } } if (isEqual) { return(null); } else { differentVertices.Sort(); return(differentVertices); } }
private void ButtonSubmit_Click(object sender, EventArgs e) { bool flag = true; mapMatrix = new AdjacencyMatrix(); mapList = new AdjacencyList(); for (int col = 1; col <= 26; col++) { for (int row = 0; row < 26; row++) { dataGridViewAdjacencyMatrix[col, row].Style = dataGridViewCellStyle_Valid; try { if (dataGridViewAdjacencyMatrix[col, row].Value != null && dataGridViewAdjacencyMatrix[col, row].Value.ToString() != "") { dataGridViewAdjacencyMatrix[col, row].Value = dataGridViewAdjacencyMatrix[col, row].Value.ToString().Trim(); if (dataGridViewAdjacencyMatrix[col, row].Value != null && Convert.ToDouble(dataGridViewAdjacencyMatrix[col, row].Value.ToString()) > 0) // Valid entry { mapMatrix.SetDirectedEdge(col - 1, row, Convert.ToDouble(dataGridViewAdjacencyMatrix[col, row].Value.ToString())); mapList.SetDirectedEdge(col - 1, row, Convert.ToDouble(dataGridViewAdjacencyMatrix[col, row].Value.ToString())); } else if (dataGridViewAdjacencyMatrix[col, row].Value != null && Convert.ToDouble(dataGridViewAdjacencyMatrix[col, row].Value.ToString()) < 0) // Negative edge exception { dataGridViewAdjacencyMatrix[col, row].Style = dataGridViewCellStyle_Invalid; string colName = Convert.ToChar('A' + col - 1).ToString(); string rowName = Convert.ToChar('A' + row).ToString(); MessageBox.Show("Negative weight at row: " + rowName + ", column: " + colName + "!"); flag = false; } } } catch // Invalid weight input { dataGridViewAdjacencyMatrix[col, row].Style = dataGridViewCellStyle_Invalid; string colName = Convert.ToChar('A' + col - 1).ToString(); string rowName = Convert.ToChar('A' + row).ToString(); MessageBox.Show("Invalid input at row: " + rowName + ", column: " + colName + "!"); flag = false; } } } submitSuccessful = flag; }
// Trees (Group A) are implemented here. // Graph/Tree Traversal (Group A) is implemented here. // Complex user-defined algorithm (Group A) is implemented here. /// <summary> /// Returns the the Minimum Spanning Tree of the graph, in the form of adjacency matrix, using Prim's algorithm. /// </summary> /// <param name="vStart">The starting vertex.</param> public AdjacencyMatrix Prim_GetTree_Matrix(int vStart) { if (!CheckUndirectedGraph()) { MessageBox.Show("Error in finding the Minimum Spanning Tree: \nThe graph is not undirected!"); return(null); } else { List <int> visitedVertices = new List <int>(); List <int> remainingVertices = new List <int>(); AdjacencyMatrix outputMST = new AdjacencyMatrix(); for (int i = 0; i < this.GetSize(); i++) { if (this.IsVertexExisting(i)) { remainingVertices.Add(i); } } visitedVertices.Add(vStart); remainingVertices.Remove(vStart); while (remainingVertices.Any()) { double min = Double.MaxValue; int newVStart = -1, newVFinish = -1; foreach (int i in visitedVertices) { foreach (int j in remainingVertices) { if (ContainsEdge(i, j) && GetEdge(i, j) < min) { min = GetEdge(i, j); newVStart = i; newVFinish = j; } } } visitedVertices.Add(newVFinish); remainingVertices.Remove(newVFinish); outputMST.SetUndirectedEdge(newVStart, newVFinish, min); } return(outputMST); } }
private void ButtonSubmit_Click(object sender, EventArgs e) { bool flag = true; mapMatrix = new AdjacencyMatrix(); mapList = new AdjacencyList(); for (int vertex = 0; vertex < 26; vertex++) { if (dataGridViewAdjacencyList[1, vertex].Value != null) { string adjacentEdges = dataGridViewAdjacencyList[1, vertex].Value.ToString(); byte[] adjacentEdgesBytes = Encoding.ASCII.GetBytes(adjacentEdges.Replace(" ", string.Empty).Replace(",", "\n")); MemoryStream adjacentEdgesStream = new MemoryStream(adjacentEdgesBytes); using (StreamReader reader = new StreamReader(adjacentEdgesStream)) { try { string newValue; string state = "vertex"; char finishingVertex = '\0'; double weight; while ((newValue = reader.ReadLine()) != null) { if (state == "vertex") { finishingVertex = Convert.ToChar(newValue); if (!(finishingVertex >= 'A' && finishingVertex <= 'Z')) // Invalid vertex name exception { dataGridViewAdjacencyList[1, vertex].Style = dataGridViewCellStyle_Invalid; string vertexName = Convert.ToChar('A' + vertex).ToString(); MessageBox.Show("Invalid input at vertex " + vertexName + "!"); flag = false; continue; } else if (finishingVertex == vertex + 'A') // Self loop exception { dataGridViewAdjacencyList[1, vertex].Style = dataGridViewCellStyle_Invalid; string vertexName = Convert.ToChar('A' + vertex).ToString(); MessageBox.Show("Self loop at vertex " + vertexName + "!"); flag = false; state = "weight"; } else // Valid input { state = "weight"; } } else // if (state == "weight") { if (newValue.Length == 1 && Convert.ToChar(newValue) >= 'A' && Convert.ToChar(newValue) <= 'Z') // Weight is omitted - default weight 1 { if (finishingVertex == vertex + 'A') // Self loop exception { dataGridViewAdjacencyList[1, vertex].Style = dataGridViewCellStyle_Invalid; string vertexName = Convert.ToChar('A' + vertex).ToString(); MessageBox.Show("Self loop at vertex " + vertexName + "!"); flag = false; } else // Valid input { mapMatrix.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), 1); mapList.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), 1); finishingVertex = Convert.ToChar(newValue); } state = "weight"; } else // Weight is specified { weight = Convert.ToDouble(newValue); if (weight > 0) // Valid input { mapMatrix.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), weight); mapList.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), weight); } else if (weight < 0) // Negative edge exception { dataGridViewAdjacencyList[1, vertex].Style = dataGridViewCellStyle_Invalid; string vStart = Convert.ToChar('A' + vertex).ToString(); string vFinish = finishingVertex.ToString(); MessageBox.Show("Negative weight at edge " + vStart + vFinish + "!"); flag = false; } finishingVertex = '\0'; state = "vertex"; } } } if (finishingVertex != '\0') { mapMatrix.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), 1); mapList.SetDirectedEdge(vertex, Convert.ToInt32(finishingVertex - 'A'), 1); } } catch { dataGridViewAdjacencyList[1, vertex].Style = dataGridViewCellStyle_Invalid; string vertexName = Convert.ToChar('A' + vertex).ToString(); MessageBox.Show("Invalid input at vertex " + vertexName + "!"); flag = false; } } } } submitSuccessful = flag; }
public DoTaskControls(int index) { this.labelTaskIndex = new System.Windows.Forms.Label(); this.labelTask = new System.Windows.Forms.Label(); this.textBoxInputAnswer = new System.Windows.Forms.TextBox(); this.buttonInputGraph = new System.Windows.Forms.Button(); this.labelCorrectWrong = new System.Windows.Forms.Label(); this.labelAnswer = new System.Windows.Forms.Label(); this.buttonShowAnswer = new System.Windows.Forms.Button(); answerValue = ""; answerMatrix = null; System.Drawing.Point originalLocation = new System.Drawing.Point(6, 33); System.Drawing.Point location = new System.Drawing.Point(originalLocation.X, originalLocation.Y + (index - 1) * 54); // // labelTaskIndex // this.labelTaskIndex.AutoSize = true; this.labelTaskIndex.Location = location; this.labelTaskIndex.Name = "labelTask" + index.ToString(); this.labelTaskIndex.Size = new System.Drawing.Size(46, 17); this.labelTaskIndex.Text = "Task " + index.ToString(); // // labelTask // this.labelTask.AutoSize = true; this.labelTask.Location = new System.Drawing.Point(location.X + 55, location.Y); this.labelTask.Name = "labelTaskDescription" + index.ToString(); this.labelTask.Size = new System.Drawing.Size(514, 17); this.labelTask.Text = ""; // // textBoxInputAnswer // this.textBoxInputAnswer.Location = new System.Drawing.Point(location.X + 58, location.Y + 21); this.textBoxInputAnswer.Name = "textBoxInputAnswer" + index.ToString(); this.textBoxInputAnswer.Size = new System.Drawing.Size(338, 23); // // buttonInputGraph // this.buttonInputGraph.Enabled = false; this.buttonInputGraph.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.buttonInputGraph.Location = new System.Drawing.Point(location.X + 58, location.Y + 21); this.buttonInputGraph.Name = "buttonInputGraph" + index.ToString(); this.buttonInputGraph.Size = new System.Drawing.Size(150, 23); this.buttonInputGraph.Text = ""; this.buttonInputGraph.UseVisualStyleBackColor = true; this.buttonInputGraph.Visible = false; // // labelCorrectWrong // this.labelCorrectWrong.AutoSize = true; this.labelCorrectWrong.Font = new System.Drawing.Font("Microsoft YaHei", 12F, System.Drawing.FontStyle.Bold); this.labelCorrectWrong.Location = new System.Drawing.Point(location.X + 398, location.Y + 21); this.labelCorrectWrong.Name = "labelCorrectWrong" + index.ToString(); this.labelCorrectWrong.Size = new System.Drawing.Size(22, 22); this.labelCorrectWrong.Text = "×"; this.labelCorrectWrong.Visible = false; // // labelAnswer // this.labelAnswer.AutoSize = true; this.labelAnswer.ForeColor = System.Drawing.Color.Red; this.labelAnswer.Location = new System.Drawing.Point(location.X + 417, location.Y + 24); this.labelAnswer.Name = "labelAnswer" + index.ToString(); this.labelAnswer.Size = new System.Drawing.Size(85, 17); this.labelAnswer.Text = "Answer: "; this.labelAnswer.Visible = false; // // buttonShowAnswer // this.buttonShowAnswer.Enabled = false; this.buttonShowAnswer.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.buttonShowAnswer.Location = new System.Drawing.Point(location.X + 510, location.Y + 21); this.buttonShowAnswer.Name = "buttonShowAnswer" + index.ToString(); this.buttonShowAnswer.Size = new System.Drawing.Size(90, 23); this.buttonShowAnswer.Text = "Answer"; this.buttonShowAnswer.UseVisualStyleBackColor = true; this.buttonShowAnswer.Visible = false; }
public void SetInputMatrix(AdjacencyMatrix newInputMatrix) { this.inputMatrix = newInputMatrix; }
public void SetAnswerMatrix(AdjacencyMatrix newAnswerMatrix) { this.answerMatrix = newAnswerMatrix; }
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 = "-"; } } } }