Esempio n. 1
0
 public static void drawGraph(PictureBox panel, Graph graph)
 {
     for (int i = 0; i < graph.data.Length; i++)
         graph.nodes[i].drawNode(panel);
     for (int i = 0; i < graph.numberOfEdges; i++)
         graph.edges[i].drawEdge(panel);
 }
Esempio n. 2
0
 private void buttonClear_Click(object sender, EventArgs e)
 {
     graph = null;
     createGraph = null;
     dgwMatrix.Rows.Clear();
     dgwMatrix.Columns.Clear();
     dgwMatrix.Visible = false;
     pictureBox1.Controls.Clear();
     pictureBox1.Refresh();
     dgwOutput.Rows.Clear();
     dgwOutput.Visible = false;
     listOutput.Items.Clear();
 }
Esempio n. 3
0
        public static void AlgFloydYorshall(PictureBox pictureBox1, Graph graph, DataGridView matrix, ListBox listOutput)
        {
            count = 0;
            listOutput.Items.Add(" Алгоритм Флойда-Уоршала. ");
            listOutput.Items.Add(" Матриця найкоротших шляхів.");
            matrix.ColumnCount = graph.nodes.Length;
            for (int i = 0; i < graph.nodes.Length; i++)
            {
                matrix.Columns[i].Name = Convert.ToString(i);
                matrix.Columns[i].Width = matrix.Width / (graph.nodes.Length + 1);
                matrix.Rows.Add();
            }

            int[][] D;
            D = graph.data;
            for (int i = 0; i < D.Length; i++)
                for (int j = 0; j < D.Length; j++)
                    if (D[i][j] == -1) D[i][j] = 1000;

            for (int k = 0; k < D.Length; k++)
                for (int i = 0; i < D.Length; i++)
                    for (int j = 0; j < D.Length; j++)
                    {
                        D[i][j] = Math.Min(D[i][j], D[i][k] + D[k][j]);
                        count++;
                    }

            for (int i = 0; i < D.Length; i++)
                D[i][i] = 0;

            for (int i = 0; i < D.Length; i++)
                for (int j = 0; j < D.Length; j++)
                    if (D[i][j] != 1000)
                        matrix.Rows[i].Cells[j].Value = D[i][j];
                    else matrix.Rows[i].Cells[j].Value = '∞';
            StreamWriter sw;
            if (File.Exists("fLoydaAlg.txt") == true)
               File.Delete("floydaAlg.txt");
            sw = File.CreateText("floydaAlg.txt");
            sw.WriteLine(Convert.ToString(graph.data.Length));
            for (int i = 0; i < D.Length; i++)
               for (int j = 0; j < D.Length; j++)
                   sw.WriteLine(D[i][j]);
            sw.WriteLine(count);
            sw.Close();
        }
Esempio n. 4
0
        private static int[] belman(Graph graph, int v)
        {
            int[]D;
            D = graph.data[v];
            for (int i = 0; i < D.Length; i++)
                if (D[i] == -1) D[i] = 1000;

            D[v] = 0;
            int n = graph.data.Length;
            int m = graph.edges.Length;
            //for (int i=0; i<n-1; ++i)
                for (int j = 0; j < m; ++j)
                {
                   if (D[Array.IndexOf(graph.nodes, graph.edges[j].startNode)] < 1000)
                        D[Array.IndexOf(graph.nodes, graph.edges[j].endNode)] =
                            Math.Min(D[Array.IndexOf(graph.nodes, graph.edges[j].endNode)], D[Array.IndexOf(graph.nodes, graph.edges[j].startNode)] + graph.edges[j].weightEdge);
                    count++;
                }
               return D;
        }
Esempio n. 5
0
        private void buttonSave_Click(object sender, EventArgs e)
        {
            graph = new Graph(createGraph.getNodes(), createGraph.getEdges());
            pictureBox1.MouseClick -= new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
            dgwMatrix.CellValueChanged -= new DataGridViewCellEventHandler(this.matrix_CellValueChanged);
            dgwMatrix.ReadOnly = true;
            graph = new Graph(createGraph.getNodes(), createGraph.getEdges());
            StreamWriter sw = File.CreateText("j");

            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.FileName = "graph";
            saveFileDialog1.DefaultExt = ".txt";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StringBuilder sb = new StringBuilder();

                sb.AppendLine(graph.data.Length.ToString());
                for (int i = 0; i < graph.data.Length; i++)
                {
                    for (int j = 0; j < graph.data.Length; j++)
                    {
                        if (graph.data[i][j] != -1)
                        sb.AppendLine("" + (i+1).ToString() + " " + (j+1).ToString() + " " + graph.data[i][j]);
                    }
                }
                sb.AppendLine();
                sb.AppendLine();

                using (StreamWriter outfile = new StreamWriter(saveFileDialog1.FileName, true))
                {
                    outfile.Write(sb.ToString());
                }
            }
        }
Esempio n. 6
0
        public static void algDeijkstra(PictureBox pictureBox1, Graph graph, DataGridView matrix, ListBox listOutput)
        {
            count = 0;
            listOutput.Items.Add(" Алгоритм Дейкстри. ");
            listOutput.Items.Add(" Матриця найкоротших шляхів.");
            matrix.ColumnCount = graph.nodes.Length;
            for (int i = 0; i < graph.nodes.Length; i++)
            {
                matrix.Columns[i].Name = Convert.ToString(i);
                matrix.Columns[i].Width = matrix.Width / (graph.nodes.Length + 1) ;
                matrix.Rows.Add();
            }
            int[][] ans = new int[graph.nodes.Length][];

            for (int i = 0; i < graph.data.Length; i++)
            {
                Edge[] edge = graph.edges;
                Node[] node = graph.nodes;
                node[i].highLight(pictureBox1, Color.Brown);
                ans[i] = deijkstra(pictureBox1, i, ref edge, ref node, graph.data);
                for (int j = 0; j < ans.Length; j++)
                    matrix.Rows[i].Cells[j].Value = Convert.ToString(ans[i][j]);
                Thread.Sleep(1000);
                SystemFunction.drawGraph(pictureBox1, graph);
            }

            StreamWriter sw;
            if (File.Exists("deijkstraAlg.txt") == true)
                File.Delete("deijkstraAlg.txt");
            sw = File.CreateText("deijkstraAlg.txt");
            sw.WriteLine(Convert.ToString(graph.data.Length));
            for (int i = 0; i < ans.Length; i++)
                for (int j = 0; j < ans.Length; j++)
                    sw.WriteLine(ans[i][j]);
            sw.WriteLine(count);
            sw.Close();
        }
Esempio n. 7
0
        public static void Algoritm_Forda(PictureBox pictureBox1, Graph graph, DataGridView matrix, ListBox listOutput)
        {
            listOutput.Items.Clear();
            listOutput.Items.Add("Алгоритм Белмана-Форда");
            listOutput.Items.Add("Матриця найкоротших війдстаней");
            count = 0;

            matrix.ColumnCount = graph.nodes.Length;
            for (int i = 0; i < graph.nodes.Length; i++)
            {
                matrix.Columns[i].Name = Convert.ToString(i);
                matrix.Columns[i].Width = matrix.Width / (graph.nodes.Length + 1);
                matrix.Rows.Add();
            }
            int[][] D = new int[graph.data.Length][];

            for (int i = 0; i < graph.data.Length; i++)
                D[i] = belman(graph, i);

            for (int i = 0; i < D.Length; i++)
                for (int j = 0; j < D.Length; j++)
                    if (D[i][j] != 1000)
                        matrix.Rows[i].Cells[j].Value = D[i][j];
                    else matrix.Rows[i].Cells[j].Value = '∞';

            StreamWriter sw;
            if (File.Exists("fordaAlg.txt") == true)
                File.Delete("fordaAlgg.txt");
            sw = File.CreateText("fordaAlg.txt");
            sw.WriteLine(Convert.ToString(graph.data.Length));
            for (int i = 0; i < D.Length; i++)
                for (int j = 0; j < D.Length; j++)
                    sw.WriteLine(D[i][j]);
            sw.WriteLine(count);
            sw.Close();
        }
Esempio n. 8
0
        private void downloadFromFile_Click(object sender, EventArgs e)
        {
            AboutProgram.Hide();
            aboutAuthorPanel.Hide();
            buttonClear.Show();
            paintPanel.Show();

            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            int[][] data = SystemFunction.readFile(openFileDialog1.FileName);
                            dgwMatrix.Visible = true;
                            graph = new Graph(this.dgwMatrix, data);
                            dgwMatrix.CellValueChanged -= new DataGridViewCellEventHandler(this.matrix_CellValueChanged);
                            SystemFunction.drawGraph(pictureBox1, graph);
                            dgwMatrix.Visible = true;
                            pictureBox1.MouseClick -= new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
                            pictureBox1.MouseDoubleClick -= new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDoubleClick);
                          }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("   Щось пішло не так. Спробуйте ще раз ");
                }
            }
        }