Esempio n. 1
0
        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);
        }
Esempio n. 2
0
 private void incidenceMatrixSaveButton_Click(object sender, EventArgs e)
 {
     WriteToFile(GraphRepresentation.toIncidenceMatrix(graph));
 }