Example #1
0
        public void CreateGraph(Graph.Graph gr)
        {
            graph = gr;
            g.Clear(Color.White);
            float  r      = Math.Min((float)picturebox.Width / 2, (float)picturebox.Height / 2) - 20 - Node.NODE_SIZE;
            double unit   = 2.0 * Math.PI / graph.Nodes.Count;
            double deg    = 0;
            PointF center = new PointF((float)picturebox.Width / 2, (float)picturebox.Height / 2);

            for (int i = 0; i < graph.Nodes.Count; i++)
            {
                float x = center.X + (r * (float)Math.Cos(deg));
                float y = center.Y + (r * (float)Math.Sin(deg));
                graph.Nodes[i].Position = new Point((int)x, (int)y);
                deg += unit;
            }
            objects.Clear();
            for (int i = 0; i < graph.Nodes.Count; i++)
            {
                objects.Insert(0, graph.Nodes[i]);
                graph.Nodes[i].Selected   += Node_Selected;
                graph.Nodes[i].Deselected += Node_Deselected;
            }
            for (int i = 0; i < graph.Edges.Count; i++)
            {
                objects.Add(graph.Edges[i]);
                graph.Edges[i].Selected   += Edge_Selected;
                graph.Edges[i].Deselected += Edge_Deselected;
            }

            Repaint();
            //Console.WriteLine(r);
        }
Example #2
0
 private void newToolStripMenuItem_Click(object sender, EventArgs e)
 {
     graph = new Graph.Graph();
     selectButton.Checked  = true;
     nodeButton.Checked    = false;
     edgeButton.Checked    = false;
     weightTextbox.Enabled = false;
     g.Clear(Color.White);
 }
Example #3
0
 private void MainForm_Load(object sender, EventArgs e)
 {
     graph = new Graph.Graph();
     selectButton.Checked  = true;
     nodeButton.Checked    = false;
     edgeButton.Checked    = false;
     weightTextbox.Enabled = false;
     g = picturebox.CreateGraphics();
 }
Example #4
0
        private void openGraph_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();

            open.Filter = "Text|*.txt";
            open.Title  = "Окрыть";
            open.ShowDialog();
            if (open.FileName != "")
            {
                StreamReader reader = new StreamReader(open.OpenFile());
                string[]     items  = reader.ReadLine().Split(' ');
                int          n      = int.Parse(items[0]);
                int          m      = int.Parse(items[1]);
                int          maxId  = int.Parse(items[2]);
                graph                 = new Graph.Graph();
                graph.MaxId           = maxId;
                selectButton.Checked  = true;
                nodeButton.Checked    = false;
                edgeButton.Checked    = false;
                weightTextbox.Enabled = false;
                g.Clear(Color.White);
                objects.Clear();

                for (int i = 0; i < n; i++)
                {
                    items = reader.ReadLine().Split(' ');
                    int  id   = int.Parse(items[0]);
                    int  x    = int.Parse(items[1]);
                    int  y    = int.Parse(items[2]);
                    Node node = graph.CreateNewNode(new Point(x, y), id);
                    node.Draw(g);
                    node.Selected   += Node_Selected;
                    node.Deselected += Node_Deselected;
                    objects.Insert(0, node);
                }

                for (int i = 0; i < m; i++)
                {
                    items = reader.ReadLine().Split(' ');
                    int  u     = int.Parse(items[0]);
                    int  v     = int.Parse(items[1]);
                    int  w     = int.Parse(items[2]);
                    Node nodeu = graph.FindNodeWithId(u);
                    Node nodev = graph.FindNodeWithId(v);
                    Edge edge  = graph.CreateNewEdge(nodeu, nodev, w);
                    edge.Selected   += Edge_Selected;
                    edge.Deselected += Edge_Deselected;
                    objects.Add(edge);
                    int count = graph.CountEdge(edge);
                    edge.Draw(g, count);
                }

                //UpdateMatrices();
                reader.Close();
            }
        }
Example #5
0
        private void createButton_Click(object sender, EventArgs e)
        {
            int  selectedIndex = matrixTypeCombobox.SelectedIndex;
            int  n             = (int)nodeNumberUpdown.Value;
            int  m             = (int)edgeNumberUpdown.Value;
            bool weightCheck   = weightCheckbox.Checked;

            int[,] input;
            Graph.Graph graph;
            try
            {
                switch (selectedIndex)
                {
                case 0:
                    // Check matrix format
                    if (matrix.Rows.Count != n)
                    {
                        throw new Exception("Количества вершин в поле и в матрице не совпадают.");
                    }
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            int index;
                            if (int.TryParse(matrix.Rows[i].Cells[j].Value.ToString(), out index) == false)
                            {
                                throw new Exception("Некорректные значения ячейк матрицы. Проверьте, пожалуйста.");
                            }
                            if (index != 0 && index != 1)
                            {
                                throw new Exception("Значение ячейки принимает только 0 или 1.");
                            }
                            if (index == 1 && i == j)
                            {
                                throw new Exception("Петлей не разрешено");
                            }
                        }
                    }

                    // Create graph
                    input = new int[n, n];
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            input[i, j] = int.Parse(matrix.Rows[i].Cells[j].Value.ToString());
                        }
                    }
                    graph = Graph.GraphRepresentation.fromAdjacencyMatrix(input);
                    break;

                case 1:
                    // Check matrix format
                    if (matrix.Rows.Count != n)
                    {
                        throw new Exception("Количества вершин в поле и в матрице не совпадают.");
                    }
                    if (matrix.Columns.Count != m)
                    {
                        throw new Exception("Количества ребр в поле и в матрице не совпадают.");
                    }
                    for (int j = 0; j < m; j++)
                    {
                        int count0 = 0;
                        int sum    = 0;
                        for (int i = 0; i < n; i++)
                        {
                            int index;
                            if (int.TryParse(matrix.Rows[i].Cells[j].Value.ToString(), out index) == false)
                            {
                                throw new Exception("Некорректные значения ячейк матрицы. Проверьте, пожалуйста.");
                            }
                            if (index != 0 && index != 1 && index != -1)
                            {
                                throw new Exception("Значение ячейки принимает только -1, 0 или 1.");
                            }
                            if (index == 0)
                            {
                                count0++;
                            }
                            sum += index;
                        }
                        if (count0 != n - 2 || sum != 0)
                        {
                            throw new Exception("Некорректный формат");
                        }
                    }
                    // Create graph
                    input = new int[n, m];
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < m; j++)
                        {
                            input[i, j] = int.Parse(matrix.Rows[i].Cells[j].Value.ToString());
                        }
                    }
                    graph = Graph.GraphRepresentation.fromIncidenceMatrix(input);
                    break;

                case 2:
                    // Check matrix format
                    if (matrix.Rows.Count != n)
                    {
                        throw new Exception("Количества вершин в поле и в матрице не совпадают.");
                    }
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            int index;
                            if (int.TryParse(matrix.Rows[i].Cells[j].Value.ToString(), out index) == false)
                            {
                                throw new Exception("Некорректные значения ячейк матрицы. Проверьте, пожалуйста.");
                            }
                            if (index < 0)
                            {
                                throw new Exception("Значение ячейки принимает только положительные числа.");
                            }
                            if (i == j && index != 0)
                            {
                                throw new Exception("Петлей не разрешено");
                            }
                        }
                    }
                    // Create graph
                    input = new int[n, n];
                    for (int i = 0; i < n; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            input[i, j] = int.Parse(matrix.Rows[i].Cells[j].Value.ToString());
                        }
                    }
                    graph = Graph.GraphRepresentation.fromWeightMatrix(input);
                    break;

                case 3:
                    // Check matrix format
                    if (matrix.Columns.Count != m)
                    {
                        throw new Exception("Количества ребр в поле и в матрице не совпадают.");
                    }
                    for (int j = 0; j < m; j++)
                    {
                        for (int i = 0; i < (weightCheck ? 3 : 2); i++)
                        {
                            int index;
                            if (int.TryParse(matrix.Rows[i].Cells[j].Value.ToString(), out index) == false)
                            {
                                throw new Exception("Некорректные значения ячейк матрицы. Проверьте, пожалуйста.");
                            }
                            if (index <= 0)
                            {
                                throw new Exception("Значение ячейки принимает только положительные числа.");
                            }
                            if (i < 2 && index > n)
                            {
                                throw new Exception("Некорректный формат");
                            }
                        }
                        if (matrix.Rows[0].Cells[j].Value.ToString() == matrix.Rows[1].Cells[j].Value.ToString())
                        {
                            throw new Exception("Петлей не разрешено");
                        }
                    }
                    // Create graph
                    input = new int[matrix.Rows.Count, m];
                    for (int i = 0; i < matrix.Rows.Count; i++)
                    {
                        for (int j = 0; j < m; j++)
                        {
                            input[i, j] = int.Parse(matrix.Rows[i].Cells[j].Value.ToString());
                        }
                    }
                    graph = Graph.GraphRepresentation.fromEdgeList(input);
                    while (graph.Nodes.Count < n)
                    {
                        graph.AddNode(new Graph.Node(graph.Nodes.Count));
                    }
                    break;

                case 4:
                    // Check matrix format
                    if (matrix.Rows.Count != n)
                    {
                        throw new Exception("Количества вершин в поле и в матрице не совпадают.");
                    }

                    for (int i = 0; i < n; i++)
                    {
                        if (matrix.Rows[i].Cells[0].Value == null)
                        {
                            continue;
                        }
                        string   s      = matrix.Rows[i].Cells[0].Value.ToString();
                        string[] tokens = s.Split(new[] { ',' });
                        for (int j = 0; j < tokens.GetLength(0); j++)
                        {
                            int index;
                            if (int.TryParse(tokens[j], out index) == false)
                            {
                                throw new Exception("Некорректные значения.");
                            }
                            if (index <= 0 || index > n)
                            {
                                throw new Exception("Некорректный формат");
                            }
                            if (index == i + 1)
                            {
                                throw new Exception("Петлей не разрешено");
                            }
                        }
                    }
                    // Create graph
                    graph = new Graph.Graph();
                    for (int i = 0; i < n; i++)
                    {
                        graph.AddNode(new Graph.Node(i));
                    }
                    for (int i = 0; i < n; i++)
                    {
                        if (matrix.Rows[i].Cells[0].Value == null)
                        {
                            continue;
                        }
                        string   s      = matrix.Rows[i].Cells[0].Value.ToString();
                        string[] tokens = s.Split(new[] { ',' });
                        for (int j = 0; j < tokens.GetLength(0); j++)
                        {
                            int index = int.Parse(tokens[j]);
                            graph.AddEdge(new Graph.Edge(graph.Nodes[i], graph.Nodes[index - 1]));
                        }
                    }
                    break;

                default:
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            graph.MaxId = graph.Nodes.Count;
            MainForm parent = (MainForm)Owner;

            parent.CreateGraph(graph);
            Close();
        }