Search⌘ K
AI Features

Feature #3: Plot and Select Path

Explore how to determine the optimal driver path to a user by calculating travel costs through checkpoints and using depth-first search to find the lowest cost route. This lesson helps you understand graph building, path existence checks, and accumulative cost calculations, preparing you for related interview problems involving graph traversal and optimization.

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,

  • GMap has the values [["a","b"],["b","c"],["a","e"],["d","e"]].

  • pathCosts 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:

  1. Build the graph using the city map array GMap.

  2. Assign the cost to each edge while building the graph.

  3. Once the graph is built, evaluate each driver’s path in the drivers array by searching for a path between the driver node and the user node. ...

Javascript (babel-node)
class Solution {
getTotalCost(GMap, pathCosts, drivers, user) {
var city = {}
// Step 1). build the city from the GMap
for (var i = 0; i < GMap.length; i++) {
var checkPoints = GMap[i]
var sourceNode = checkPoints[0]
var destNode = checkPoints[1]
pathCost = pathCosts[i]
if (!city.hasOwnProperty(sourceNode))
city[sourceNode] = {}
if (!city.hasOwnProperty(destNode))
city[destNode] = {}
city[sourceNode][destNode] = pathCost
city[destNode][sourceNode] = pathCost
}
// Step 2). Evaluate each driver via bactracking (DFS)
// by verifying if there exists a path from driver to user
var results = new Array(drivers.length).fill(0)
for (var i = 0; i < drivers.length; i++) {
var driver = drivers[i]
if (!city.hasOwnProperty(driver) || !city.hasOwnProperty(user))
results[i] = -1.0
else {
var visited = new Set()
results[i] = this.backtrackEvaluate(city, driver, user, 0, visited)
}
}
return results;
}
backtrackEvaluate(city, currNode, targetNode, accSum, visited) {
// mark the visit
visited.add(currNode)
var ret = -1.0
var neighbors = city[currNode]
if (neighbors.hasOwnProperty(targetNode))
ret = accSum + neighbors[targetNode]
else {
for (pair in neighbors) {
var nextNode = pair
if (visited.hasOwnProperty(nextNode))
continue
ret = this.backtrackEvaluate(city, nextNode, targetNode,
accSum + neighbors[pair], visited)
if (ret != -1.0)
break
}
}
// unmark the visit, for the next backtracking
visited.delete(currNode)
return ret
}
}
// Driver code
var sol = new Solution()
var G_map = [["a","b"],["b","c"],["a","e"],["d","e"]]
var path_costs = [12.0,23.0,26.0,18.0]
var drivers = ["c", "d", "e", "f"]
var user = "a"
var all_path_costs = sol.getTotalCost(G_map, path_costs, drivers, user)
console.log("Total cost of all paths", all_path_costs)
Plot and Select Path

Complexity measures

Time
...
Ask