コード例 #1
0
 public static BinaryPriorityQueue ReadOnly(BinaryPriorityQueue P)
 {
     return new BinaryPriorityQueue(ArrayList.ReadOnly(P.InnerList), P.Comparer, false);
 }
コード例 #2
0
 public static BinaryPriorityQueue Syncronized(BinaryPriorityQueue P)
 {
     return new BinaryPriorityQueue(ArrayList.Synchronized(P.InnerList), P.Comparer, false);
 }
コード例 #3
0
        private void btnRun_Click(object sender, EventArgs e)
        {
            /* Create the array containing the adjacency matrix */
            double[,] G = new double[Vertices.Count, Vertices.Count];

            /* Set the connections and weights based on each edge in the collection */
            foreach (Edge edge in Edges)
            {
                G[edge.from, edge.to] = edge.weight;
            }
            if (this.label4.Text == "A* (A Star) Algorithm")
            {
                A_star astar = new A_star(G, 0);
                dist = astar.g_score;
                path = astar.came_from;

                count = 0;
                count_of_nodes = 0;
                // nodes = new int[dist.Length];
                timer1.Enabled = true;
                // timer1.Start();
                this.btnRun.Enabled = false;

              //  richTextBox1.AppendText("\n\n" + a[Vertices.Count - 1]);
                if (dist[Vertices.Count - 1] != Double.PositiveInfinity)
                {
                    BinaryPriorityQueue pa = new BinaryPriorityQueue();

                    //int count2 = path.Length-1;
                    // shortest_path = new int[count2];
                    //shortest_path[count2 - 1] = Vertices.Count - 1;
                    // int i = path.Length - 1;
                    // pa.Push(Vertices.Count - 1);
                    // count2 -= 2;
                    for (int i = Vertices.Count - 1; i > -1; )
                    {
                        if (i == Vertices.Count - 1)
                        {
                            // MessageBox.Show(i.ToString());
                            pa.Push(i);
                            i = path[i];
                            // continue;
                        }
                        //MessageBox.Show(i.ToString());
                        pa.Push(i);
                        // shortest_path[i] = path[i];
                        i = path[i];
                        //count2 -= 1;
                        //if (path[i] == 0) break;
                        //if (i == 0) break;
                        // count2 -= count2;
                    }

                    // MessageBox.Show("Ang laog:" + pa.Count.ToString());

                    shortest_path = new int[pa.Count];
                    int c = 0;
                    for (int i = Vertices.Count - 1; i >= 0; )
                    {
                        if (i == Vertices.Count - 1)
                        {
                            // MessageBox.Show(i.ToString());
                            shortest_path[c] = i;
                            i = path[i];
                            c += 1;
                            // continue;
                        }
                        //MessageBox.Show(i.ToString());
                        shortest_path[c] = i;
                        // shortest_path[i] = path[i];
                        i = path[i];
                        c += 1;
                    }

                }
            }
            if (this.label4.Text == "Dijkstra Algorithm")
            {
                /* Runs dijkstra */
                try
                {
                    Dijkstra dijk = new Dijkstra(G, 0);
                    dist = dijk.dist;
                    path = dijk.path;
                    count = 0;
                    count_of_nodes = 0;
                    // nodes = new int[dist.Length];
                    timer1.Enabled = true;
                    // timer1.Start();
                    this.btnRun.Enabled = false;
                    if (dist[Vertices.Count - 1] != Double.PositiveInfinity)
                    {
                        BinaryPriorityQueue pa = new BinaryPriorityQueue();

                        //int count2 = path.Length-1;
                       // shortest_path = new int[count2];
                        //shortest_path[count2 - 1] = Vertices.Count - 1;
                        // int i = path.Length - 1;
                       // pa.Push(Vertices.Count - 1);
                       // count2 -= 2;
                        for (int i = Vertices.Count - 1; i > -1; )
                        {
                            if (i == Vertices.Count - 1)
                            {
                               // MessageBox.Show(i.ToString());
                                pa.Push(i);
                                i = path[i];
                               // continue;
                            }
                            //MessageBox.Show(i.ToString());
                             pa.Push(i);
                           // shortest_path[i] = path[i];
                            i = path[i];
                            //count2 -= 1;
                            //if (path[i] == 0) break;
                            //if (i == 0) break;
                            // count2 -= count2;
                        }

                       // MessageBox.Show("Ang laog:" + pa.Count.ToString());

                       shortest_path = new int[pa.Count];
                        int c = 0;
                         for (int i = Vertices.Count - 1; i >= 0; )
                        {
                             if (i == Vertices.Count - 1)
                            {
                               // MessageBox.Show(i.ToString());
                               shortest_path[c] = i;
                                i = path[i];
                                c += 1;
                               // continue;
                            }
                            //MessageBox.Show(i.ToString());
                             shortest_path[c] = i;
                           // shortest_path[i] = path[i];
                            i = path[i];
                            c += 1;
                         }

                    }
                    //  timer1.Start();
                    // HighlightText(token[1], Color.Red);

                    // for (int o = 0; o < 100000000; o++)
                    //timer1.Stop();
                    //timer1.Enabled = false;
                    // for (int i = 1; i < dist.Length ; i++)
                    // {

                    //    Graphics g = panMap.CreateGraphics();

                    //     nodes[i] = i;

                    //  Vertex p = Vertices[i];
                    // StringFormat distFormat = new StringFormat();
                    // distFormat.Alignment = StringAlignment.Center;
                    //g.DrawString(dist[d].ToString(), new Font("Verdana", 11), new SolidBrush(Color.White), p.p.X, p.p.Y - 7, distFormat);
                    //    g.FillEllipse(new SolidBrush(Color.Red), p.p.X - 25, p.p.Y - 25, 50, 50);
                    //    for (int q = 0; q < 100000000; q++);
                    //HighlightText(this.richTextBox2, token[1], Color.Red);
                    //HighlightText(this.richTextBox2, token[0], Color.Black);
                    // }

                    // count_of_nodes = 1;
                   // print_Report(dist, path);
                   // btnstepf.Enabled = true;

                    }
                    catch (ArgumentException err)
                    {
                      MessageBox.Show(err.Message);
                    }
                }
                if (this.label4.Text == "Bellman-Ford Algorithm")
                {
                    /* Runs bellman-ford */
                    try
                    {
                        BellmanFord bell = new BellmanFord(G, Edges, 0);
                        dist = bell.dist;
                        path = bell.path;
                        count = 0;
                        count_of_nodes = 0;

                        timer1.Enabled = true;

                        this.btnRun.Enabled = false;
                        if (dist[Vertices.Count - 1] != Double.PositiveInfinity)
                        {
                            BinaryPriorityQueue pa = new BinaryPriorityQueue();

                            //int count2 = path.Length-1;
                            // shortest_path = new int[count2];
                            //shortest_path[count2 - 1] = Vertices.Count - 1;
                            // int i = path.Length - 1;
                            // pa.Push(Vertices.Count - 1);
                            // count2 -= 2;
                            for (int i = Vertices.Count - 1; i > -1; )
                            {
                                if (i == Vertices.Count - 1)
                                {
                                    // MessageBox.Show(i.ToString());
                                    pa.Push(i);
                                    i = path[i];
                                    // continue;
                                }
                                //MessageBox.Show(i.ToString());
                                pa.Push(i);
                                // shortest_path[i] = path[i];
                                i = path[i];
                                //count2 -= 1;
                                //if (path[i] == 0) break;
                                //if (i == 0) break;
                                // count2 -= count2;
                            }

                            // MessageBox.Show("Ang laog:" + pa.Count.ToString());

                            shortest_path = new int[pa.Count];
                            int c = 0;
                            for (int i = Vertices.Count - 1; i >= 0; )
                            {
                                if (i == Vertices.Count - 1)
                                {
                                    // MessageBox.Show(i.ToString());
                                    shortest_path[c] = i;
                                    i = path[i];
                                    c += 1;
                                    // continue;
                                }
                                //MessageBox.Show(i.ToString());
                                shortest_path[c] = i;
                                // shortest_path[i] = path[i];
                                i = path[i];
                                c += 1;
                            }

                        }
                        /* Prints the shortest distances on the nodes */
                      /*  for (int i = 1; i < dist.Length; i++)
                        {
                            Graphics g = panMap.CreateGraphics();
                            Vertex p = Vertices[i];
                            // StringFormat distFormat = new StringFormat();
                            // distFormat.Alignment = StringAlignment.Center;
                            // g.DrawString(dist[i].ToString(), new Font("Verdana", 11), new SolidBrush(Color.White), p.p.X, p.p.Y - 7, distFormat);
                            g.FillEllipse(new SolidBrush(Color.Red), p.p.X - 25, p.p.Y - 25, 50, 50);
                            for (int q = 0; q < 100000000; q++) ;
                        } */
                       // print_Report(dist, path);
                       // count_of_nodes = 1;
                      //  btnstepf.Enabled = true;
                        //  for (int a = 0; a < dist.Length; a++)
                        //{
                        //  MessageBox.Show( dist[a].ToString());
                        //}
                    }
                    catch (ArgumentException err)
                    {
                        MessageBox.Show(err.Message);
                    }
                }
                if (this.label4.Text == "Floyd-Warshall Algorithm")
                {
                    /* Runs floyd-warshall */
                    try
                    {
                        FloydWarshall floyd = new FloydWarshall(Edges, Vertices);
                        fdist = floyd.w;
                        fpath = floyd.p;

                        count = 0;
                        count_of_nodes = 0;

                        timer1.Enabled = true;

                        this.btnRun.Enabled = false;
                        if (fdist[0,Vertices.Count - 1] != Double.PositiveInfinity)
                        {
                            BinaryPriorityQueue pa = new BinaryPriorityQueue();

                            //int count2 = path.Length-1;
                            // shortest_path = new int[count2];
                            //shortest_path[count2 - 1] = Vertices.Count - 1;
                            // int i = path.Length - 1;
                            // pa.Push(Vertices.Count - 1);
                            // count2 -= 2;
                            for (int i = Vertices.Count - 1; i > -1; )
                            {
                                if (i == Vertices.Count - 1)
                                {
                                    // MessageBox.Show(i.ToString());
                                    pa.Push(i);
                                    i = fpath[0,i];
                                    // continue;
                                }
                                //MessageBox.Show(i.ToString());
                                pa.Push(i);
                                // shortest_path[i] = path[i];
                                i = fpath[0,i];
                                //count2 -= 1;
                                //if (path[i] == 0) break;
                                //if (i == 0) break;
                                // count2 -= count2;
                            }

                            // MessageBox.Show("Ang laog:" + pa.Count.ToString());

                            shortest_path = new int[pa.Count];
                            int c = 0;
                            for (int i = Vertices.Count - 1; i >= 0; )
                            {
                                if (i == Vertices.Count - 1)
                                {
                                    // MessageBox.Show(i.ToString());
                                    shortest_path[c] = i;
                                    i = fpath[0,i];
                                    c += 1;
                                    // continue;
                                }
                                //MessageBox.Show(i.ToString());
                                shortest_path[c] = i;
                                // shortest_path[i] = path[i];
                                i = fpath[0,i];
                                c += 1;
                            }

                        }
                        //MessageBox.Show(floyd.path(0,3));
                        // this.richTextBox1.AppendText(floyd.path(0,(int)Convert.ToInt32(cmbDest.SelectedItem)));
                        /* Prints the shortest distances on the nodes */
                        // for (int i = 0; i < G.Length; i++)
                        // {
                        //Graphics g = panMap.CreateGraphics();
                        // = floyd.path();
                        // Vertex p = Vertices[i];
                        // StringFormat distFormat = new StringFormat();
                        //distFormat.Alignment = StringAlignment.Center;
                        // g.DrawString(dist[i].ToString(), new Font("Verdana", 11), new SolidBrush(Color.White), p.p.X, p.p.Y - 7, distFormat);

                        // }
                        // print_Report(dist, path, G);
                        //  for (int a = 0; a < dist.Length; a++)
                        //{
                        //  MessageBox.Show( dist[a].ToString());
                        //}
                    }
                    catch (ArgumentException err)
                    {
                        MessageBox.Show(err.Message);
                    }
                }
        }