Пример #1
0
        private void LoadButton_Click_1(object sender, EventArgs e)
        {
            {
                if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }

                string filename = openFileDialog1.FileName;

                Graph = new Graph(filename);
                Nodes = Graph.GetNodes();

                if (Graph.IsDirected)
                {
                    Begin = "digraph g{" + Environment.NewLine + "rankdir=LR;" + Environment.NewLine + "size = \"760,490\"" + Environment.NewLine;
                    End   = Environment.NewLine + "}";
                }
                else
                {
                    Begin = "graph g{" + Environment.NewLine + "rankdir=LR;" + Environment.NewLine + "size = \"760,490\"" + Environment.NewLine;
                    End   = Environment.NewLine + "}";
                }

                var temp = Graph.GetJoints();

                Joints = new List <Pair <Pair <int, Pair <object, object> >, bool> >();

                CurrentImage = "";

                foreach (var joint in temp)
                {
                    Joints.Add(new Pair <Pair <int, Pair <object, object> >, bool>(joint, false));
                }

                SetImage();

                pictureBox1.Image = FileDotEngine.Run(Begin + CurrentImage + End);
                Label.Text        = "Минимальный путь: 0";

                NextStepButton.Enabled = true;
            }
        }
Пример #2
0
        public async Task <int> DoDijkstraAsync(object StartingNode)
        {
            richTextBox1.Text = "Начало алгоритма" + Environment.NewLine;
            int sum = 0;
            await Task.Delay(2000);

            var temp = Graph.GetNodeNames();
            SortedList <string, int>  pathLength  = new SortedList <string, int>();
            SortedList <string, bool> nodeVisited = new SortedList <string, bool>();
            int n = Graph.Graf.Count;

            for (int i = 0; i < n; i++)
            {
                pathLength.Add(temp[i].ToString(), int.MaxValue);
                nodeVisited.Add(temp[i].ToString(), false);
            }
            pathLength[StartingNode.ToString()] = 0;
            string v = StartingNode.ToString();


            for (int i = 0; i < n; i++)
            {
                int m = int.MaxValue;

                foreach (var node in Graph.Graf.Keys.ToArray())
                {
                    if ((pathLength[node.ToString()] < m) && (!nodeVisited[node.ToString()]))
                    {
                        m = pathLength[node.ToString()];
                        v = node.ToString();
                    }
                }

                nodeVisited[v] = true;

                Pair <Pair <int, Pair <object, object> >, bool> tem1;
                //  Pair<Pair<int, Pair<object, object>>, bool> tem2;

                foreach (object node in Graph.Graf.Keys.ToArray())
                {
                    int weight;

                    try
                    {
                        weight = Graph.Graf[v][node];
                        if (!nodeVisited[node.ToString()] && ((pathLength[v] + weight) < pathLength[node.ToString()]))
                        {
                            pathLength[node.ToString()] = pathLength[v] + weight;
                            sum += pathLength[node.ToString()];
                        }
                        tem1       = Joints.First(x => Equals(x.Key.Value.Key, node) && Equals(x.Key.Value.Value, node.ToString()));
                        tem1.Value = true;

                        richTextBox1.Text += "Находим минимальное ребро через \"" + tem1.Key.Value.Key + " - " + tem1.Key.Value.Value + "\": " + pathLength + Environment.NewLine;

                        SetImage();
                        pictureBox1.Image = FileDotEngine.Run(Begin + CurrentImage + End);
                        await Task.Delay(1500);
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
            SetImage();
            pictureBox1.Image = FileDotEngine.Run(Begin + CurrentImage + End);

            Label.Text = "Минимальный путь: " + sum;


            richTextBox1.Text += "Больше путей нет. Алгоритм завершен.";

            return(sum);
        }