Feature #3: Plot and Select Path
Explore how to select the optimal path for Uber drivers to reach users by analyzing travel costs between checkpoints. Understand graph building, Depth-First Search for pathfinding, and calculating minimum cost routes while considering driver availability. This lesson helps you master real-world routing problems in coding interviews using Rust.
We'll cover the following...
Description
After obtaining the closest drivers and calculating the cost of traveling on different roads, we need to build a functionality to select a path from the driver’s location to the user’s location. All the drivers have to pass through multiple checkpoints to reach the user’s location. Each road between checkpoints will have a cost, which we learned how to calculate in the previous lesson. It is possible that some of the k chosen drivers might not have a path to the user due to unavailability. Unavailability can occur due to a driver already ...
In the above example,
-
gmaphas the values[["a","b"],["b","c"],["a","e"],["d","e"]]. -
path_costshas the values[12,23,26,18]. -
drivershas the values["c", "d", "e", "f"]. -
userhas a value"a".
After calculating the total cost of each driver’s route to the user, we’ll select that driver that has a path to the user with the lowest cost. Here, the driver f has no path to the user due to unavailability.
Solution
The main problem comes down to finding a path between two nodes, if it exists. If the path exists, return the cumulative sums along the path as the result. Given the problem, it seems that we need to track the nodes where we come from. DFS (Depth-First Search), also known as the backtracking algorithm, will be applicable in this case.
Here is how the implementation will take place:
-
Build the graph using the city map list
gmap. -
Assign the cost to each edge while building the graph.
-
Once the graph is built, evaluate each driver’s path in the
driverslist by searching for a path between the driver node and the user node. -
Return ...