} //END OF ReadGraph METHOD /// <summary> /// Event handler for the Open Button, generates a solution to the traveling /// salesmen proble, /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxOpenButton_Click(object sender, EventArgs e) { if(uxOpenDialog.ShowDialog() == DialogResult.OK) { try { UndirectedGraph graph = ReadGraph(uxOpenDialog.FileName); MSTNode parent = graph.GetMinSpanningTree(); uxTour.Text = parent.Walk(); new TreeForm(parent, 100).Show(); }catch(Exception ex) { MessageBox.Show("This error has occurred " + ex.ToString()); } //END OF TRY-CATCH BLOCK } //END OF IF STATEMENT } //END OF EVENT HANDLER
/// <summary> /// This method calculates the minimum cost spanning /// tree of the graph by using Prim’s algorithm. /// </summary> /// <returns></returns> public MSTNode GetMinSpanningTree() { MSTNode[] tree = new MSTNode[_size]; bool[] added = new bool[_size]; for (int i = 1; i < tree.Length; i++) { tree[i] = new MSTNode(Int32.MaxValue, 0, _nodes[i]); } tree[0] = new MSTNode(0, -1, _nodes[0]); for (int j = 0; j < _size; j++) { int minIndex = 0; int minData = Int32.MaxValue; for (int i = 0; i < _size; i++) { if (tree[i].Data < minData && !added[i]) { minData = tree[i].Data; minIndex = i; } } added[minIndex] = true; if (j != 0) { int parent = tree[minIndex].Parent; tree[parent].AddChild(tree[minIndex]); } for (int i = 0; i < _size; i++) { if (!added[i] && _adjMatrix[minIndex, i] != 0 && _adjMatrix[minIndex, i] < tree[i].Data) { tree[i].Data = _adjMatrix[minIndex, i]; tree[i].Parent = minIndex; } } } return(tree[0]); }
/// <summary> /// Calculates the minimum cost spanning tree of the graph by using "Prim's Algorithm" /// </summary> /// <returns>MTSNode</returns> public MSTNode GetMinSpanningTree() { MSTNode[] tree = new MSTNode[_size]; bool[] nodesAdded = new bool[_size]; for (int i = 1; i < _size; i++) { tree[i] = new MSTNode(Int32.MaxValue, 0, _nodes[i]); } tree[0] = new MSTNode(0, -1, _nodes[0]); int minIndex = -1; for (int i = 0; i < _size; i++) { int min = Int32.MaxValue; for (int j = 0; j < _size; j++) { if (!nodesAdded[j] && tree[j].Data < min) { min = tree[j].Data; minIndex = j; } } nodesAdded[minIndex] = true; if (i != 0) { tree[tree[minIndex].Parent].AddChild(tree[minIndex]); } for (int k = 0; k < _size; k++) { if (_adjMatrix[minIndex, k] != 0 && !nodesAdded[k] && _adjMatrix[minIndex, k] < tree[k].Data) { tree[k].Parent = minIndex; tree[k].Data = _adjMatrix[minIndex, k]; } } } return(tree[0]); }
} //END OF AddEdge METHOD /// <summary> /// Calculates the minimum cost spanning tree of the graph by using Prim's algorithm /// </summary> /// <returns>The root of the tree</returns> public MSTNode GetMinSpanningTree() { MSTNode[] spanningTree = new MSTNode[_size]; bool[] nodesAdded = new bool[_size]; for (int i = 1; i < _size; i++) { spanningTree[i] = new MSTNode(Int32.MaxValue, 0, _nodes[i]); } //END OF FOR LOOP spanningTree[0] = new MSTNode(0, -1, _nodes[0]); int minIndex = -1; for (int i = 0; i < _size; i++) { int min = Int32.MaxValue; for(int j = 0; j < _size; j++) { if(!nodesAdded[j] && spanningTree[j].Data < min) { min = spanningTree[j].Data; minIndex = j; } //END OF IF STATEMENT } //END OF FOR LOOP nodesAdded[minIndex] = true; if(i != 0) { spanningTree[spanningTree[minIndex].Parent].AddChild(spanningTree[minIndex]); } //END OF IF STATEMENT for (int k = 0; k < _size; k++) { if (_adjMatrix[minIndex, k] > 0 && !nodesAdded[k] && _adjMatrix[minIndex, k] < spanningTree[k].Data) { spanningTree[k].Data = _adjMatrix[minIndex, k]; spanningTree[k].Parent = minIndex; } //END OF IF STATEMENT } //END OF FOR LOOP } //END OF FOR LOOP return spanningTree[0]; } //END OF GetMinSpanning METHOD
/// <summary> /// This method adds the given child to the _children list /// </summary> /// <param name="child"></param> public void AddChild(MSTNode child) { _children.Add(child); }