private void Draw_Click(object sender, EventArgs e) { if (Map.Text == "") { MessageBox.Show("Select Map First !", "", MessageBoxButtons.OK, MessageBoxIcon.Hand); } else if (Qery_text.text == "" || Convert.ToInt32(Qery_text.text) > queriesNo) { MessageBox.Show("Invalid Query Number !", "", MessageBoxButtons.OK, MessageBoxIcon.Hand); } else { Short_Path.Enabled = true; //Graph.Map_file = Map.SelectedItem.ToString(); Stopwatch ConstructionTime = Stopwatch.StartNew(); graph = new Graph(Map.SelectedItem.ToString()); ConstructionTime.Stop(); GraphConstructionTime = (int)ConstructionTime.ElapsedMilliseconds; // G.add_Intersection(); //G.add_roads(); scl = 0.079F; roadwide = 3; intersectionwide = 1; if (Map.SelectedIndex == 5) { queries = new queries("OLQueries"); } else if (Map.SelectedIndex == 6) { queries = new queries("SFQueries"); } else { queries = new queries("queries" + (Map.SelectedIndex + 1).ToString()); roadwide = 20; intersectionwide = 10; scl = 400; if (Map.SelectedIndex > 1) { scl = 20; } } panel2.Visible = false; Graphics g = draw_panal.CreateGraphics(); g.Clear(draw_panal.BackColor); g.ScaleTransform(zoom, zoom); TestCaseNo = Convert.ToInt32(Qery_text.text) - 1; drawMap(graph.Nodes_edgesFordrawing, graph.intersections, draw_panal, scl); drawSourceDestination(queries, TestCaseNo, draw_panal, scl); } }
void drawSourceDestination(queries Q, int index, Panel c, float scl) { Graphics g = c.CreateGraphics(); float xS = (float)Q.Start_X_Coordinate[index] * scl; float yS = (float)Q.Start_Y_Coordinate[index] * scl; g.DrawRectangle(new Pen(Color.Red), xS, yS, 15, 15); Font font = new Font("Courier New", 12f, FontStyle.Bold); g.DrawString("Source", font, Brushes.White, xS, yS); float xD = (float)Q.Destination_X_Coordinate[index] * scl; float yD = (float)Q.Destination_Y_Coordinate[index] * scl; g.DrawRectangle(new Pen(Color.Red), xD, yD, 15, 15); g.DrawString("Destination", font, Brushes.White, xD, yD); }
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); }
public void Calculate_ShortestPath_AllQueries(Graph G, queries Q) //calc and construct the shortest path of each query { for (int i = 0; i < Q.Number_Of_Queries; ++i) { Stopwatch Sw = 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; } } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // dijkstra(G, Q.Start_X_Coordinate[i], Q.Start_Y_Coordinate[i], Q.Destination_X_Coordinate[i], Q.Destination_Y_Coordinate[i], Q.R[i]); int end_point = dijkstra_Two_Way(G, Q, i); construct_two_way_dijkstra_path(G, end_point); totalTime = (double)Math.Round((decimal)((time[end_point] + time_End[end_point]) * 60), 2); totalDistance = (double)Math.Round((decimal)(distance[end_point] + distance_End[end_point]), 2); totalWalkingDistance = (double)Math.Round((decimal)(distance[shortest_path_nodes[1]] + distance_End[shortest_path_nodes[shortest_path_nodes.Count - 2]]), 2); totalVehD = (double)Math.Round((decimal)(totalDistance - totalWalkingDistance), 2); Sw.Stop(); Console.WriteLine("Time = " + (totalTime)); // totalDistance = Math.Round(totalDistance, 2); Console.WriteLine("Distance = " + totalDistance); Console.WriteLine("Walking Distance = " + totalWalkingDistance + " km"); Console.WriteLine("Vehicle Distance = " + totalVehD + " km"); // Console.WriteLine("Shortest path Contains Intersection "); /*for (int w = 1; w <shortest_path_nodes.Count-1; w++) * { * Console.Write(shortest_path_nodes[w] + " " ); * } * Console.WriteLine();*/ Console.WriteLine("Excution Time = " + Sw.ElapsedMilliseconds); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// } }
void check(string mapfile) { if (mapfile == "OLMap") { queries = new queries("OLQueries"); ch = new CheckCorrectness("OLOutput", queries.Number_Of_Queries); } else if (mapfile == "SFMap") { queries = new queries("SFQueries"); ch = new CheckCorrectness("SFOutput", queries.Number_Of_Queries); } else { if (mapfile == "map1") { queries = new queries("queries1"); ch = new CheckCorrectness("output1", queries.Number_Of_Queries); } else if (mapfile == "map2") { queries = new queries("queries2"); ch = new CheckCorrectness("output2", queries.Number_Of_Queries); } else if (mapfile == "map3") { queries = new queries("queries3"); ch = new CheckCorrectness("output3", queries.Number_Of_Queries); } else if (mapfile == "map4") { queries = new queries("queries4"); ch = new CheckCorrectness("output4", queries.Number_Of_Queries); } else if (mapfile == "map5") { queries = new queries("queries5"); ch = new CheckCorrectness("output5", queries.Number_Of_Queries); } } graph = new Graph(mapfile); shortest = new ShortestPathAlgorithm(); string m; bool accepted = true; for (int i = 0; i < queries.Number_Of_Queries; i++) { Stopwatch s = Stopwatch.StartNew(); shortest.Calculate_ShortestPath(graph, queries, i); s.Stop(); richTextBox1.AppendText("Total Time = " + shortest.totalTime.ToString() + " mins\n"); richTextBox1.AppendText("Total Distance = " + shortest.totalDistance.ToString() + " Km\n"); richTextBox1.AppendText("Total Walking Distance = " + shortest.totalWalkingDistance.ToString() + " Km\n"); richTextBox1.AppendText("Total Vehcile Distance = " + shortest.totalVehD.ToString() + " Km\n"); richTextBox1.AppendText("Execution Time = " + (int)s.ElapsedMilliseconds + " ms\n"); m = ch.checkoutput(shortest.totalTime, shortest.totalDistance, shortest.totalWalkingDistance, shortest.totalVehD, (int)s.ElapsedMilliseconds, i); richTextBox1.AppendText(m + "\n"); richTextBox1.AppendText("\n"); if (m != "Case # " + (i + 1).ToString() + " Succeeded") { accepted = false; break; } richTextBox1.ScrollToCaret(); } if (accepted) { richTextBox1.AppendText("All Cases Succeeded\n"); } }