Esempio n. 1
0
 public static void dijkstra(priorityQueue pq)
 {
     while (!pq.IsEmpty())//v homa 3shan 3mleen el tre2a el tanya enhom msh by7oto kol el nodes
     {
         Node minNode = pq.GetMin();
         if (minNode.color != "b")//theta (e' log v')
         {
             //element: shayl id-->.key, w TUPLE-->.value(.value.Item1, .value.Item2)
             foreach (KeyValuePair <int, Tuple <double, double> > element in minNode.neighbours)//e
             {
                 nodeList[element.Key].color = "g";
                 double time = element.Value.Item1 / element.Value.Item2;
                 //relaxation
                 if (nodeList[element.Key].weight > time + (minNode.weight))
                 {
                     nodeList[element.Key].weight = time + (minNode.weight);
                     //set neighbours parent to minNode ID
                     nodeList[element.Key].parent = minNode.id;
                     pq.heapifyUp(nodeList[element.Key].indexInQueue);//log v
                 }
             }
             minNode.color = "b";
             //speeding up Dijkstra: law el minNode hia el distination, w 5lst kol el neighbours bto3ha(black)
             // f ana msh m7taga akml ba2y el nodes
             if (minNode.id == maxID + 1)//destination
             {
                 break;
             }
         }
         pq.Dequeue();//theta of v
     }
 }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Stopwatch totalExecutionTime = Stopwatch.StartNew();

            StreamReader mapFile;

            mapFile = new StreamReader("map1.txt");
            Console.SetIn(mapFile);
            //call function readFile to read nodes and edges
            string[] mapLines          = readFile(mapFile);
            int      originalNodesSize = int.Parse(mapLines[0]);

            mapFile = new StreamReader("queries1.txt");
            Console.SetIn(mapFile);
            // call function readFile to read queries
            string[] queryLines = readFile(mapFile);
            int      queryCount = int.Parse(queryLines[0]);


            constructGraph(mapLines);


            double source_X = 0, source_Y = 0, destination_X = 0, destination_Y = 0, radius = 0;

            Stopwatch queryExecutionTime = Stopwatch.StartNew();

            for (int i = 1; i <= queryCount; i++)
            {
                string[] line = queryLines[i].Split(' ');
                source_X      = double.Parse(line[0]);
                source_Y      = double.Parse(line[1]);
                destination_X = double.Parse(line[2]);
                destination_Y = double.Parse(line[3]);
                radius        = double.Parse(line[4]);
                radius       /= 1000;

                nodesWithinRadius(originalNodesSize, source_X, source_Y, destination_X, destination_Y, radius);

                //adding destination node and source node to the graph
                Node destinationNode = new Node(maxID + 1, destination_X, destination_Y);
                Node sourceNode      = new Node(maxID + 2, source_X, source_Y);
                sourceNode.weight = 0;
                sourceNode.parent = -1;
                add_Source_Destination(sourceNode, destinationNode, originalNodesSize);

                // construct el min-heap
                priorityQueue pq = new priorityQueue();
                foreach (KeyValuePair <int, Node> element in nodeList)
                {
                    element.Value.indexInQueue = pq.siz;
                    //bmshy 3la el nodeList w ba5od id el dictionary w ab3t el value bta3toh (Node)
                    pq.Enqueue(nodeList[element.Key]);
                }

                //get min time
                dijkstra(pq);
                double totalDistance = getTotalDistance();
                //shortest path
                string shortestPathStr = "";
                for (int p = shortestPath.Count - 1; p >= 0; p--)
                {
                    if (p != 0)
                    {
                        shortestPathStr += shortestPath[p].ToString() + " ";
                    }
                    else
                    {
                        shortestPathStr += shortestPath[p];
                    }
                }


                //Console.WriteLine("shortestpath " + shortestPathStr);
                double totalTime = nodeList[maxID + 1].weight;
                totalTime *= 60;
                //Console.WriteLine("Time = " + totalTime.ToString("F2") + " mins");
                //Console.WriteLine("Distance = " + totalDistance.ToString("F2") + " km");
                //Console.WriteLine("Walking Distance = " + (walkingFromSource + walkingToDistination).ToString("F2") + " km");
                //Console.WriteLine("Vehicle Distance = " + getVehicleDistance(totalDistance).ToString("F2") + " km");
                //Console.WriteLine();
                //Console.WriteLine("Executoin Time: " + queryExecutionTime.ElapsedMilliseconds + " ms");
                //Console.WriteLine();

                //writing in file

                allLines.Add(shortestPathStr.ToString());
                allLines.Add(totalTime.ToString("F2") + " mins");
                allLines.Add(totalDistance.ToString("F2") + " km");
                allLines.Add((walkingFromSource + walkingToDistination).ToString("F2") + " km");
                allLines.Add(getVehicleDistance(totalDistance).ToString("F2") + " km");
                allLines.Add("\n");

                //msh 3ayz kda 3ayz ytal3 el total time l kol el queiries
                // allLines.Add(queryExecutionTime.ElapsedMilliseconds.ToString() + " ms");
                //if (i != queryCount)
                //    allLines.Add("\n");



                //remove source and destination from the graph
                remove_Source_Destination();
                nearestToDestination.Clear(); //O(S)
                nearestToSource.Clear();      //O(D)
                shortestPath.Clear();
            }//num of queries
            queryExecutionTime.Stop();//homa ele alo kda :'D
            allLines.Add(queryExecutionTime.ElapsedMilliseconds.ToString() + " ms");
            // allLines.Add("\n");

            totalExecutionTime.Stop();
            allLines.Add("\n");

            //Console.WriteLine(totalExecutionTime.ElapsedMilliseconds);
            allLines.Add(totalExecutionTime.ElapsedMilliseconds.ToString() + " ms");
            System.IO.File.WriteAllLines("C:\\Users\\Mai Nour Al-Deen\\Desktop\\map1output.txt", allLines);
        }