Ejemplo n.º 1
0
        public int dijkstra_Two_Way(Graph G, queries Q, int index)
        {
            double res            = 1e17;
            int    real_end_index = -1;

            bool[] visted = new bool[G.Number_Of_Intersection + 2];
            for (int i = 0; i < G.Number_Of_Intersection + 2; i++)
            {
                visted[i] = false;
            }
            Priority_Queue PQ     = new Priority_Queue(time);
            Priority_Queue PQ_End = new Priority_Queue(time_End);

            PQ.buildHeap();
            PQ_End.buildHeap();
            while (true)
            {
                int u = PQ.extractMin();
                int num_of_neighbours_of_u = G.Nodes_edges[u].Count;  // for speed
                for (int j = 0; j < num_of_neighbours_of_u; j++)
                {
                    // retrive index of adjecent (المجاورة) vertex of minimum node (u)
                    int Current_index = G.Nodes_edges[u][j].Item1;
                    // time from u to one of the  neighbours
                    double time_to_neighbour = G.Nodes_edges[u][j].Item2 / G.Nodes_edges[u][j].Item3;

                    if (time[u] + time_to_neighbour < time[Current_index])
                    {
                        time[Current_index] = time[u] + time_to_neighbour;

                        PQ.heap_update_key(Current_index, time[u] + time_to_neighbour);

                        distance[Current_index] = distance[u] + G.Nodes_edges[u][j].Item2;
                        parent[Current_index]   = u;
                    }
                }

                //checking if this node u may be on the shortest path between source and destination
                if (time[u] + time_End[u] < res)
                {
                    res            = time[u] + time_End[u];
                    real_end_index = u;
                }

                if (visted[u] == true)
                {
                    break;
                }
                visted[u] = true;

                int u_End = PQ_End.extractMin();

                int num_of_neighbours_of_u_End = G.Nodes_edges[u_End].Count;  // for speed
                for (int j = 0; j < num_of_neighbours_of_u_End; j++)
                {
                    // retrive index of adjecent (المجاورة) vertex of minimum node (u)
                    int Current_index = G.Nodes_edges[u_End][j].Item1;
                    // time from u to one of the  neighbours
                    double time_to_neighbour = G.Nodes_edges[u_End][j].Item2 / G.Nodes_edges[u_End][j].Item3;

                    if (time_End[u_End] + time_to_neighbour < time_End[Current_index])
                    {
                        time_End[Current_index] = time_End[u_End] + time_to_neighbour;

                        PQ_End.heap_update_key(Current_index, time_End[u_End] + time_to_neighbour);

                        distance_End[Current_index] = distance_End[u_End] + G.Nodes_edges[u_End][j].Item2;
                        parent_End[Current_index]   = u_End;
                    }
                }
                //checking if this node u_End may be on the shortest path between source and destination
                if (time[u_End] + time_End[u_End] < res)
                {
                    res            = time[u_End] + time_End[u_End];
                    real_end_index = u_End;
                }
                if (visted[u_End] == true)
                {
                    break;
                }
                visted[u_End] = true;
            }
            return(real_end_index);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            queries   Q          = new queries();
            Stopwatch input_time = Stopwatch.StartNew();

            Q.ReadQueries();
            input_time.Stop();


            Graph G = new Graph();

            G.add_Intersection();
            G.add_roads();


            shortest_path P        = new shortest_path();
            Stopwatch     with_Out = Stopwatch.StartNew();

            for (int i = 0; i < Q.Number_Of_Queries; i++)
            {
                P.Calculate_ShortestPath(G, Q, i);
            }
            with_Out.Stop();
            Console.WriteLine();

            Console.WriteLine(P.ExcuationTime_withOut_InOut + " ms");

            Console.WriteLine();

            Console.WriteLine((input_time.ElapsedMilliseconds + with_Out.ElapsedMilliseconds).ToString() + " ms");



            // Remove /*  to start by auto Correction

            /*
             * List<int> wrong = new List<int>();
             * CheckCorrectness che = new CheckCorrectness("OLOutput", Q.Number_Of_Queries);
             *
             * bool aCCEPTED = true ;
             * int counter = 0;
             * Stopwatch total = Stopwatch.StartNew();
             * for (int i = 0;i<Q.Number_Of_Queries;i++)
             * {
             *  Stopwatch sw = Stopwatch.StartNew();
             *
             *  shortest_path P = new shortest_path();
             *  P.Calculate_ShortestPath(G, Q, i);
             *
             *  sw.Stop();
             *  //Console.WriteLine("Case " + (i + 1).ToString() + "  " + (sw.ElapsedMilliseconds).ToString() + " ms");
             *
             *  string m = che.checkoutput(P.totalTime, P.totalDistance, P.totalWalkingDistance, P.totalVehD,  (int)sw.ElapsedMilliseconds, i , P.shortest_path_nodes );
             *
             * Console.WriteLine(m);
             *  if (m != "Case # " + (i + 1).ToString() + " Succeeded")
             *  {
             *      aCCEPTED = false;
             *      //break;
             *      counter++;
             *      wrong.Add(i + 1);
             *
             *  }
             *
             *
             * }
             * total.Stop();
             * Console.WriteLine("Total Execution Time = " + total.ElapsedMilliseconds);
             * if (aCCEPTED)
             *  Console.WriteLine(" Yes AAAACCCCCEPPPTEEDDDD ");
             * else
             *  Console.WriteLine(counter + " Cases are wrong");
             * for (int i = 0; i < wrong.Count; ++i)
             *  Console.WriteLine(wrong[i]); // */
        }
Ejemplo n.º 3
0
        public void Calculate_ShortestPath(Graph G, queries Q, int i)//calc and construct the shortest path of each query
        {
            Stopwatch timer_without = Stopwatch.StartNew();
            //initializing the time  , distance , and parent arrays for this query
            int number_of_intersection_plus_2 = G.Number_Of_Intersection + 2;     // for speed

            time                 = new double[number_of_intersection_plus_2];
            time_End             = new double[number_of_intersection_plus_2];
            distance             = new double[number_of_intersection_plus_2];
            distance_End         = new double[number_of_intersection_plus_2];
            parent               = new int[number_of_intersection_plus_2];
            parent_End           = new int[number_of_intersection_plus_2];
            shortest_path_nodes1 = new List <int>();
            shortest_path_nodes2 = new List <int>();
            shortest_path_nodes  = new List <int>();
            //initializing the Available_Starting_Intersections and vailable_Ending_Intersections  lists for this query
            Available_Starting_Intersections = new List <int>();
            Available_Ending_Intersections   = new List <int>();
            // initial times
            for (int j = 0; j < number_of_intersection_plus_2; ++j)
            {
                time[j] = 1e15; time_End[j] = 1e15;
            }

            int number_of_intersection = G.Number_Of_Intersection;     // for speed ;

            for (int j = 0; j < number_of_intersection; ++j)
            {
                //Console.WriteLine(Q.Number_Of_Queries);

                double distance_toSource = Math.Sqrt(Math.Pow(Q.Start_X_Coordinate[i] - G.intersections[j].Item1, 2) + Math.Pow(Q.Start_Y_Coordinate[i] - G.intersections[j].Item2, 2));

                //checking for every intersection on the map if it can be included in (Available_Starting_Intersections or Available_Ending_Intersections) of this query
                if (distance_toSource * 1000 <= Q.R[i])
                {
                    Available_Starting_Intersections.Add(j);
                    if (time[j] > distance_toSource / 5)
                    {
                        // time to arrive from (X,Y) cordinates to source node
                        time[j] = distance_toSource / 5;

                        //distance from (X,Y) cordinates to source
                        distance[j] = distance_toSource;
                        // parent of source equal to (X,Y) cordinates
                        parent[j] = G.Number_Of_Intersection;
                    }
                    //find the shortest path for this starting node and changing the values of time array,distance array and parent array;
                    //dijkstra(G,j,Q.Start_X_Coordinate[i],Q.Start_Y_Coordinate[i],Q.Destination_X_Coordinate[i],Q.Destination_Y_Coordinate[i] , Q.R[i]);
                }

                double distance_from_distination_To_j = (Math.Sqrt(Math.Pow(G.intersections[j].Item1 - Q.Destination_X_Coordinate[i], 2) + Math.Pow(G.intersections[j].Item2 - Q.Destination_Y_Coordinate[i], 2)));
                //Console.WriteLine(u+" "+distance_from_u_to_distination);
                if (distance_from_distination_To_j * 1000 <= Q.R[i])
                {
                    Available_Ending_Intersections.Add(j);
                    // checke if time of distination more than new time by using new u node >> update
                    if (time_End[j] > (distance_from_distination_To_j / 5))
                    {
                        //Console.WriteLine(1);
                        // time to arrive from (X,Y) cordinates to source node
                        time_End[j] = (distance_from_distination_To_j / 5);
                        //distance from (X,Y) cordinates to source
                        distance_End[j] = distance_from_distination_To_j;
                        // parent of source equal to (X,Y) cordinates
                        parent_End[j] = G.Number_Of_Intersection + 1;
                    }
                }
            }

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            int end_point = dijkstra_Two_Way(G, Q, i);

            construct_two_way_dijkstra_path(G, end_point);

            totalTime            = (time[end_point] + time_End[end_point]) * 60;
            totalDistance        = distance[end_point] + distance_End[end_point];
            totalWalkingDistance = distance[shortest_path_nodes[0]] + distance_End[shortest_path_nodes[shortest_path_nodes.Count - 1]];
            totalVehD            = totalDistance - totalWalkingDistance;


            totalTime     = (double)Math.Round((decimal)(totalTime), 2);
            totalDistance = (double)Math.Round((decimal)totalDistance, 2);

            totalWalkingDistance = (double)Math.Round((decimal)totalWalkingDistance, 2);


            //    totalWalkingDistance = Math.Round(totalWalkingDistance, 2);

            //    totalVehD = (double)Math.Round((decimal)totalVehD, 2);
            totalVehD = Math.Round(totalVehD, 2);
            timer_without.Stop();

            ExcuationTime_withOut_InOut += timer_without.ElapsedMilliseconds;

            // output Same of file output
            for (int w = 1; w < shortest_path_nodes.Count - 1; w++)
            {
                Console.Write(shortest_path_nodes[w] + " ");
            }
            Console.WriteLine();
            // write Total time
            Console.WriteLine(totalTime + "mins");

            Console.WriteLine(totalDistance + " km");
            // write walkingDistance
            Console.WriteLine(totalWalkingDistance + " km");

            Console.WriteLine(totalVehD + " km");
            Console.WriteLine();


            //  Console.WriteLine("Excution Time = " + Sw.ElapsedMilliseconds); Note delete stopwatch

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        }