private void AddEdge(int distance, Vertex fromVertex, Vertex toVertex) { // Control is a vertex same bool isOk = true; foreach (Edge _edge in listEdge) { if ((_edge.FromVertex.Name == fromVertex.Name && _edge.ToVertex.Name == toVertex.Name) || (_edge.FromVertex.Name == toVertex.Name && _edge.ToVertex.Name == fromVertex.Name)) { isOk = false; break; } } if (isOk) { // distance of vertex // Add edge to the list Edge edge = new Edge(); edge.FromVertex = fromVertex; edge.ToVertex = toVertex; edge.Distance = distance; listEdge.Add(edge); // Draw edge DrawPanelInput(); UpdateListBoxEdges(); } else { MessageBox.Show("! There is an edge has same properties"); } }
private void AddVertex(string vertexName) { bool isOk = true; foreach (Vertex _vertex in listVertex) { if (_vertex.Name == vertexName) { isOk = false; break; } } if (isOk) { int countVertex = listVertex.Count; Vertex vertex = new Vertex(); vertex.Name = vertexName; foreach (LocationVertex location in listLocationVertex) { if (location.IsEmpty) { vertex.LocationVertex = location; location.IsEmpty = false; break; } } listVertex.Add(vertex); DrawPanelInput(); UpdateListBoxVertex(); if (listVertex.Count == StaticDatas.MaxVertexNumber) { btnAddVertex.Enabled = false; } } else { MessageBox.Show("! There is a vertex has same name"); } }
private void loadFromFileToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Filter = "Text|*.txt|All|*.*"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { lbEdges.Items.Clear(); lbVertex.Items.Clear(); listVertex.Clear(); listEdge.Clear(); foreach (LocationVertex lv in listLocationVertex) { lv.IsEmpty = true; } // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName); string line; int counter = 1; while ((line = file.ReadLine()) != null) { if (line[0] != '-') { Vertex vertex = new Vertex(); AddVertex(line); } else if (line[0] == '-') { IList<string> splittedString = line.Split('-').ToList<string>(); Vertex fromVertex = GetVertexForName(splittedString[1]); Vertex toVertex = GetVertexForName(splittedString[2]); if (fromVertex == null || toVertex == null) { MessageBox.Show("There is an error in text files in line " + counter); } else { int distance = -1; try { distance = Convert.ToInt16(splittedString[3]); } catch (Exception) { MessageBox.Show("There is an error in distance in text files in line " + counter); throw; } finally { AddEdge(distance, fromVertex, toVertex); } } } counter++; } file.Close(); } }
private void Calculate() { listInTreeVertex = new List<Vertex>(); // Copy list vertex copiedListVertex = listVertex.ToList(); copiedListEdge = listEdge.ToList(); foreach (Vertex vertex in listVertex) { // set key to infinite vertex.Key = 999999; // set parent to null vertex.Parent = null; } // Add root vertex to intree vertex list Vertex rootVertex = new Vertex(); if (cbRootVertex.SelectedIndex != -1) { rootVertex = listVertex[cbRootVertex.SelectedIndex]; } // Set root vertex rootVertex.Key = 0; rootVertex.Parent = rootVertex; // while (listInTreeVertex.Count < listVertex.Count) { // get the smallest vertex from copiedlistvertex Vertex currentVertex = GetTheSmallestVertex(); // If key is infinite, it means this vertex is not connected // so we add the other remaining vertex and finishes the algorithm if (currentVertex.Key == 999999) { copiedListVertex.Remove(currentVertex); listInTreeVertex.Add(currentVertex); continue; } //// Copy edge olustur List<Edge> temp2edge = new List<Edge>(); foreach (Edge edge in copiedListEdge.ToList()) { if (edge.FromVertex.Name == currentVertex.Name) { edge.IsFrom = true; temp2edge.Add(edge); copiedListEdge.Remove(edge); } else if (edge.ToVertex.Name == currentVertex.Name) { edge.IsFrom = false; temp2edge.Add(edge); copiedListEdge.Remove(edge); } } // find the smallest distance near this vertex temp2edge = SelectionSort(temp2edge); foreach (Edge edge2 in temp2edge) { Vertex tempvertex = new Vertex(); if (edge2.IsFrom) { tempvertex = edge2.ToVertex; if (tempvertex.Key > edge2.Distance) { tempvertex.Key = edge2.Distance; tempvertex.Parent = edge2.FromVertex; } } else { tempvertex = edge2.FromVertex; if (tempvertex.Key > edge2.Distance) { tempvertex.Key = edge2.Distance; tempvertex.Parent = edge2.ToVertex; } } } copiedListVertex.Remove(currentVertex); listInTreeVertex.Add(currentVertex); } //// draw result panelResult.Invalidate(); }