Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Q 4 Driving Directions Assume you want to build your own driving directions application benefiting from Google Roads Updates services. Your application will not ask
Q
Driving Directions
Assume you want to build your own driving directions application benefiting from Google Roads Updates services. Your application will not ask the user to select among routes and will always select the shortest route possible.
Assume we have a map of locations and roads that connect them. Roads are twoway roads but you cannot turn in the middle of the road. Every road has a distance measured in km Also, Google Roads updates service provides information related to the traffic on each road and the driving speed on that road at the time of the update. Updates from Google come periodically with a timestamp and include the new status of roads related to your area.
Your goal is to use the driving directions that will get you the shortest time possible according to the received updates.
Example:
Consider the following locations.
Assume you want to travel from location to location
Also, assume that the initial speeds are
For the sake of simplicity, assume the speed numbers represent the speed for both directions for each road.
The goal is to get from source to destination as soon as possible.
Assume that your application receives the following updates from the journey start:
Update at Minute :
road new speed is kmhour
Update at Minute :
road new speed is kmhour
what is the soonest to arrive at the destination? and which route is used?
Answer:
The goal is to get from source to destination as soon as possible.
There are several routes:
According to the initial speeds given, here is how much time each route takes if the speed does not change:
For the time will be minutes
For the time will be minutes
For the time will be minutes
For the time will be minutes
Accordingly, your application should select the first route and start.
After you start driving, upgrades start arriving from Google service:
The first update at Minute :
road new speed is kmhour
road was supposed to take minutes according to the previous speed, but now at minute the road speed changed from to
At minute the car already crossed km of distance from road What is left is km but it will use the new speed, kmhour will take minutes to complete the road. so the total number of minutes to get from to is minutes
The second update at Minute :
road new speed is kmhour
When the car arrives at location it will recalculate the shortest path according to received updates.
road new speed is kmhour means that road will take minutes. this is how to calculate time distance speed hours minutes
Since our final destination is and we are at location now, from to route is no longer the best minutes
route will be more efficient minutes
Assume no more updates during the trip,
The shortest time is minutes by going through the path
Write the implementation of DrivingDirections function which takes as input the following
The number of locations
The number of connections connecting them,
Four arrays, two integer arrays to indicate the from and to for each connection, a double array indicates the distance of each road, and a double array indicates the initial speed on each road.
The number of updates
An array of update objects, where each update contains minute, from, to and new speed
source and destination
Output:
Shortest time
Route used#include
#include
#include
#include
#include Include for DBLMAX
#include "GoogleUpdate.h
using namespace std;
void updateSpeedvector& updatedSpeed, int from, int to double newSpeed
updatedSpeedfromto newSpeed;
updatedSpeedtofrom newSpeed;
void Dijkstraint source, vector& graph, vector& initialSpeed,
vector& updatedSpeed, vector& path, vector& time
int n graph.size;
priorityqueue, vector greater pq;
vector visitedn false;
pqpush source;
timesource;
while pqempty
int u pqtopsecond;
pqpop;
if visitedu continue;
visitedu true;
for auto& neighbor : graphu
int v neighbor.first;
double roadDistance neighbor.second;
double currentSpeed updatedSpeeduv updatedSpeeduv : initialSpeedu;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started