// 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 list, using Kruskal's algorithm. /// </summary> public AdjacencyList Kruskal_GetTree_List() { if (!CheckUndirectedGraph()) { MessageBox.Show("Error in finding the Minimum Spanning Tree: \nThe graph is not undirected!"); return(null); } else { AdjacencyList outputMST = new AdjacencyList(); int count = 0; InitialiseEdges(); InitialiseUnionFind(); QuickSort(edges, 0, edges.Count - 1); while (count < Count() - 1) { if (Find(edges[0].vStart).GetLeader() != Find(edges[0].vFinish).GetLeader()) { count++; outputMST.SetUndirectedEdge(edges[0].vStart, edges[0].vFinish, edges[0].weight); Union(Find(edges[0].vStart).GetLeader(), Find(edges[0].vFinish).GetLeader()); } edges.RemoveAt(0); } return(outputMST); } }
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 list, using Prim's algorithm. /// </summary> /// <param name="vStart">The starting vertex.</param> public AdjacencyList Prim_GetTree_List(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>(); AdjacencyList outputMST = new AdjacencyList(); 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; }
/// <summary> /// Compares this instance with a specified adjacency list. /// </summary> /// <param name="matrix">The specified adjacency list that this instantce is to be compared with.</param> /// <returns> /// Returns NULL if the two adjaceny lists are identical. /// If the two adjacency lists are different, return a List<int> containing the indexes of different vertices. /// </returns> public List <int> CompareTo(AdjacencyList list) { bool isEqual = true; List <int> differentVertices = new List <int>(); if (list == 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) != list.IsVertexExisting(v)) { isEqual = false; if (!differentVertices.Contains(v)) { differentVertices.Add(v); } } } for (int v = 0; v < GetSize(); v++) { if (this.list[v].Count() != list.list[v].Count()) { isEqual = false; if (!differentVertices.Contains(v)) { differentVertices.Add(v); } } foreach (AdjacentEdge edge in this.list[v]) { if (!list.list[v].Contains(edge)) { isEqual = false; if (!differentVertices.Contains(v)) { differentVertices.Add(v); } } } } if (isEqual) { return(null); } else { differentVertices.Sort(); return(differentVertices); } }