Feature #3: Plot and Select Path
Implementing the "Plot and Select Path" feature for our "Uber" project.
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 being in a ride that has ended but not reached its location. In some cases, the driver can also get booked by another user and become unavailable. The driver that has the path to the user’s location with the minimum accumulated cost will be selected.
We’ll be given a city map G_map
as a list of different checkpoints. Another list path_costs
, at each index, will represent the cost of traveling between the corresponding checkpoints in G_map
. We are also given some drivers
, where each drivers[i]
represents a single driver node. We need to determine whether a path from the driver node drivers[i]
to a user
node exists or not. If the path exists, return the accumulated sum of the checkpoints between the two nodes. Otherwise, return -1
.
In the above example,
-
G_map
has the values[["a","b"],["b","c"],["a","e"],["d","e"]]
. -
path_costs
has the values[12,23,26,18]
. -
drivers
has the values["c", "d", "e", "f"]
. -
user
has 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
G_map
. -
Assign the cost to each edge while building the graph.
-
Once the graph is built, evaluate each driver’s path in the
drivers
list by searching for a path between the driver node and the user node. ...
import collectionsfrom collections import defaultdictdef get_total_cost(G_map, path_costs, drivers, user):city = defaultdict(defaultdict)def backtrack_evaluate(curr_node, target_node, acc_sum, visited):visited.add(curr_node)ret = -1.0neighbors = city[curr_node]if target_node in neighbors:ret = acc_sum + neighbors[target_node]else:for neighbor, value in neighbors.items():if neighbor in visited:continueret = backtrack_evaluate(neighbor, target_node, acc_sum + value, visited)if ret != -1.0:breakvisited.remove(curr_node)return ret# Step 1). build the city from the G_mapfor (source_node, dest_node), path_cost in zip(G_map, path_costs):# add nodes and two edges into the citycity[source_node][dest_node] = path_costcity[dest_node][source_node] = path_cost# Step 2). Evaluate each driver via backtracking (DFS)# by verifying if there exists a path from driver to userresults = []for driver in drivers:if driver not in city or user not in city:# Either node does not existret = -1.0else:visited = set()ret = backtrack_evaluate(driver, user, 0, visited)results.append(ret)return results# Driver codeG_map = [["a","b"],["b","c"],["a","e"],["d","e"]]path_costs = [12.0,23.0,26.0,18.0]drivers = ["c", "d", "e", "f"]user = "a"all_path_costs = get_total_cost(G_map, path_costs, drivers, user)print("Total cost of all paths", all_path_costs)
Complexity measures
Time |
---|