/// <summary> /// Draw a new edge. /// </summary> private void Vertex_MouseDown_DrawEdge(object sender, MouseEventArgs e) { if (selectedTool == "buttonEdge") { (sender as Vertex).SetDraggable(false); if (vStart == null) { vStart = (sender as Vertex); } else { vFinish = (sender as Vertex); if (isDirected) { vStart.SetEdge(vFinish); mapMatrix.SetDirectedEdge(vStart.GetNumberIndex(), vFinish.GetNumberIndex()); mapList.SetDirectedEdge(vStart.GetNumberIndex(), vFinish.GetNumberIndex()); } else { vStart.SetEdge(vFinish); vFinish.SetEdge(vStart); mapMatrix.SetUndirectedEdge(vStart.GetNumberIndex(), vFinish.GetNumberIndex()); mapList.SetUndirectedEdge(vStart.GetNumberIndex(), vFinish.GetNumberIndex()); } vStart.SetSelected(false); vFinish.SetSelected(false); vStart.Refresh(); vFinish.Refresh(); ResetBoard(); vStart = null; vFinish = null; } } }
// 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); } }
// 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); } }