Example #1
0
        private void btn_search_sortest_Click(object sender, EventArgs e) // calculate the shortest path from dijiksar
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            LinkedList <int[]> graph       = new LinkedList <int[]>(); // data store as metrics
            List <DataResult>  dataResults = new List <DataResult>();

            try
            {
                for (int yy = 0; yy < list.Count; yy++)
                {                                                                                                                             // this for loop useing to stations row one by one read
                    int[] xdata    = new int[list.Count];                                                                                     //this array used to store one station have distance combination ex xdata={0,2,0,0,4}
                    int   stationy = list[yy].id;                                                                                             //this variable use to get current station id
                    for (int xx = 0; xx < list.Count; xx++)
                    {                                                                                                                         // this loop using to calulate one station have cobinations
                        int stationx = list[xx].id;                                                                                           //current station id for -> this side
                        var ss       = db.stationDistances.Where(a => a.station1id == stationy && a.station2id == stationx).FirstOrDefault(); // find the value this combination from tbdistance table
                        if (ss != null)
                        {
                            xdata[xx] = Convert.ToInt32(ss.distance);
                        }
                        else
                        {
                            xdata[xx] = 0; //if value is null then assign the 0
                        }
                    }
                    graph.AddFirst(xdata); //add the link list as a 1d array
                }
            }catch (Exception ex)
            {
                popError("error while preparing data for to find shortestest path" + Environment.NewLine + ex.Message);
                return;
            }
            int selectpath             = loc3.SelectedIndex; // get select station
            LinkedList <int[]> getdata = null;

            try
            {
                getdata = Dijkstra.DijkstraAlgo(graph, selectpath, list.Count);//this methed help find the shortest path from dijikstar
            }catch (Exception ex)
            {
                popError("Error occured while finding the shortest distance" + Environment.NewLine + ex.Message);
                return;
            }
            try
            {
                int   co        = 0;
                int[] arraypath = new int[list.Count]; //create array to store to shotest path from link list
                int[] ditance   = new int[list.Count]; // create an arra for store distance from link list
                ;
                foreach (int[] ld in getdata)
                {
                    ditance[co]   = ld[0];
                    arraypath[co] = ld[1]; // storing data

                    co++;
                }
                for (int j = 0; j < list.Count; j++)
                {
                    int    stationname = -1;     //add stationname index
                    string sname       = " -> "; // add shortest path
                    string ans         = "yes";  // this using confirm in using first time
                    int    index       = -1;
                    while (true)
                    {
                        if (selectpath == index)
                        {
                            ans = "no";
                            DataResult dr = new DataResult();   //create detset object
                            dr.Path     = sname + list[j].Name; // asign the shortest path
                            dr.Distance = ditance[j];           //asign the distance
                            dataResults.Add(dr);                //add the object
                            //result = result + (list[selectpath].Name + sname + list[j].Name) + " = " + ditance[j] + "\n";
                            break;
                        }
                        else
                        {
                            int sid;
                            if (ans == "yes")
                            {
                                ans   = "no";
                                sname = "";
                                sid   = list[arraypath[j]].id;
                            }
                            else
                            {
                                sid = list[stationname].id;
                                //sid = list.FindIndex(a => a.id == temp);
                            }

                            index       = list.FindIndex(a => a.id == sid);
                            stationname = arraypath[index];
                            sname       = list[index].Name + " -> " + sname;
                        }
                        // counter++;
                    }
                }
            }catch (Exception ex)
            {
                popError("Error occured while delivering the results" + Environment.NewLine + ex.Message);
            }
            stopWatch.Stop();
            TimeSpan ts          = stopWatch.Elapsed;
            string   elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);

            lbl_time.Text            = "Calculated Time " + elapsedTime;
            dataGridView1.DataSource = dataResults; //add data to data grid
        }
        private void btn_search_Click(object sender, EventArgs e)
        {
            LinkedList <int[]> graph = new LinkedList <int[]>();



            var list = db.stations.ToList();


            for (int yy = 0; yy < list.Count; yy++)
            {
                int[] xdata    = new int[list.Count];
                int   stationy = list[yy].id;
                for (int xx = 0; xx < list.Count; xx++)
                {
                    int stationx = list[xx].id;
                    var ss       = db.stationDistances.Where(a => a.station1id == stationy && a.station2id == stationx).FirstOrDefault();
                    if (ss != null)
                    {
                        xdata[xx] = Convert.ToInt32(ss.distance);
                    }
                    else
                    {
                        xdata[xx] = 0;
                    }
                }
                graph.AddFirst(xdata);
            }

            int selectpath = loc1.SelectedIndex;
            LinkedList <int []> getdata = Dijkstra.DijkstraAlgo(graph, selectpath, list.Count);
            int co = 0;

            int[]  arraypath = new int[list.Count];
            int[]  ditance   = new int[list.Count];
            string result    = "";

            foreach (int[] ld in getdata)
            {
                ditance[co]   = ld[0];
                arraypath[co] = ld[1];

                co++;
            }
            for (int j = 0; j < list.Count; j++)
            {
                int    stationname = -1;
                string sname       = " -> ";
                string ans         = "yes";
                while (true)
                {
                    if (selectpath == arraypath[j] || stationname == arraypath[j])
                    {
                        ans    = "no";
                        result = result + (list[selectpath].Name + sname + list[j].Name) + " = " + ditance[j] + "\n";
                        break;
                    }
                    else
                    {
                        if (ans == "yes")
                        {
                            ans   = "no";
                            sname = "";
                        }
                        int sid   = list[arraypath[j]].id;
                        int index = list.FindIndex(a => a.id == sid);
                        stationname = index;
                        sname       = sname + " -> " + list[index].Name + " -> ";
                    }
                }
            }
            lbl_result.Text = result;
        }