private void buttonKruskal_Click(object sender, EventArgs e) { Stopwatch t = new Stopwatch(); t.Start(); var p = KruskalAlgorithm.Kruskal(graph); t.Stop(); Graph dist = new Graph(); foreach (var x in graph.Nodes) { dist.AddNode(x); } double cost = 0; for (int i = 0; i < p.Length / 2; i++) { foreach (var x in graph.Edges) { if (x.From.Id == p[i, 0] && x.To.Id == p[i, 1] || x.From.Id == p[i, 1] && x.To.Id == p[i, 0]) { x.IsMinE = true; cost += x.Weight; dist.AddEdge(x); } } } cost = 0; var distRez = DijkstraAlgorithm.Dijkstra(GraphRepresentation.toWeightMatrix(dist), 0, dist.Nodes.Count); textBoxResult.Text = "Расстояние в метрах от главного коммутатора до: \n\r"; for (int i = 1; i < dist.Nodes.Count; i++) { textBoxResult.Text += "Коммутатора " + i + " = " + distRez[i] + " м." + Environment.NewLine; cost += distRez[i]; } labelCost.Text = "Итоговая стоимость (р) " + cost * Convert.ToDouble(textBoxCost.Text); labelTime.Text = "Время в тиках " + t.ElapsedTicks; Repaint(); }
private void viewGraphButton_Click(object sender, EventArgs e) { int selectedIndex = viewGraphComboBox.SelectedIndex; DataGridView viewMatrix = new DataGridView(); string name = ""; switch (selectedIndex) { case 0: // Adjacency Matrix int[,] matrix = GraphRepresentation.toAdjacencyMatrix(graph); viewMatrix.Columns.Clear(); viewMatrix.ColumnCount = graph.Nodes.Count; for (int i = 0; i < graph.Nodes.Count; i++) { viewMatrix.Columns[i].Name = graph.Nodes[i].Name; } for (int i = 0; i < matrix.GetLength(0); i++) { int index = viewMatrix.Rows.Add(); viewMatrix.Rows[index].HeaderCell.Value = graph.Nodes[i].Name; for (int j = 0; j < matrix.GetLength(1); j++) { viewMatrix.Rows[index].Cells[j].Value = matrix[i, j]; } } name = "Матрица смежности"; break; case 1: matrix = GraphRepresentation.toIncidenceMatrix(graph); viewMatrix.Columns.Clear(); if (graph.Edges.Count > 0) { viewMatrix.ColumnCount = graph.Edges.Count; for (int i = 0; i < graph.Edges.Count; i++) { viewMatrix.Columns[i].Name = graph.Edges[i].From.Name + "/" + graph.Edges[i].To.Name; } for (int i = 0; i < matrix.GetLength(0); i++) { int index = viewMatrix.Rows.Add(); viewMatrix.Rows[index].HeaderCell.Value = graph.Nodes[i].Name; for (int j = 0; j < matrix.GetLength(1); j++) { viewMatrix.Rows[index].Cells[j].Value = matrix[i, j]; } } } name = "Матрица инцидентности"; break; case 2: matrix = GraphRepresentation.toWeightMatrix(graph); viewMatrix.Columns.Clear(); viewMatrix.ColumnCount = graph.Nodes.Count; for (int i = 0; i < graph.Nodes.Count; i++) { viewMatrix.Columns[i].Name = graph.Nodes[i].Name; } for (int i = 0; i < matrix.GetLength(0); i++) { int index = viewMatrix.Rows.Add(); viewMatrix.Rows[index].HeaderCell.Value = graph.Nodes[i].Name; for (int j = 0; j < matrix.GetLength(1); j++) { if (matrix[i, j] == 1000000) { viewMatrix.Rows[index].Cells[j].Value = "oo"; } else { viewMatrix.Rows[index].Cells[j].Value = matrix[i, j]; } } } name = "Матрица весов"; break; case 3: matrix = GraphRepresentation.toEdgeList(graph); viewMatrix.Columns.Clear(); if (graph.Edges.Count > 0) { viewMatrix.ColumnCount = graph.Edges.Count; for (int i = 0; i < matrix.GetLength(0); i++) { int index = viewMatrix.Rows.Add(); if (i == 0) { viewMatrix.Rows[index].HeaderCell.Value = "r"; } else { viewMatrix.Rows[index].HeaderCell.Value = "t"; } for (int j = 0; j < matrix.GetLength(1); j++) { viewMatrix.Rows[index].Cells[j].Value = matrix[i, j]; } } } name = "Список ребер"; break; case 4: viewMatrix.Columns.Clear(); viewMatrix.ColumnCount = 1; viewMatrix.Columns[0].Name = "Adj[x]"; for (int i = 0; i < graph.Nodes.Count; i++) { string s = ""; foreach (Edge edge in graph.Edges) { if (edge.From == graph.Nodes[i]) { s += edge.To.Name + ", "; } } int index = viewMatrix.Rows.Add(); viewMatrix.Rows[index].HeaderCell.Value = graph.Nodes[i].Name; viewMatrix.Rows[index].Cells[0].Value = s; } name = "Структура смежности"; break; default: return; } using (ViewForm viewForm = new ViewForm(viewMatrix, name)) { viewForm.ShowDialog(this); } Console.WriteLine(selectedIndex); }
private void weightMatrixSaveButton_Click(object sender, EventArgs e) { WriteToFile(GraphRepresentation.toWeightMatrix(graph)); }