Search⌘ K
AI Features

Feature #3: Plot and Select Path

Explore how to build and analyze paths from drivers to users within a city map by calculating travel costs. Learn to implement graph building and depth-first search to find the cheapest available driver route. This lesson helps you understand path selection, cost accumulation, and handle availability in real-world Uber ride scenarios.

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,

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

  1. Build the graph using the city map list G_map.

  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 list by searching for a path between the driver node and the user node. ...

Kotlin
internal object Solution {
fun getTotalCost(
GMap: List<List<String>>,
pathCosts: DoubleArray,
drivers: List<String>,
user: String
): DoubleArray {
val city = HashMap<String, HashMap<String, Double>>()
// Step 1). build the city from the GMap
for (i in GMap.indices) {
val checkPoints = GMap[i]
val sourceNode = checkPoints[0]
val destNode = checkPoints[1]
val pathCost = pathCosts[i]
if (!city.containsKey(sourceNode)) city[sourceNode] = HashMap()
if (!city.containsKey(destNode)) city[destNode] = HashMap()
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
val results = DoubleArray(drivers.size)
for (i in drivers.indices) {
val driver = drivers[i]
if (!city.containsKey(driver) || !city.containsKey(user)) results[i] = -1.0 else {
val visited = HashSet<String>()
results[i] = Solution.backtrackEvaluate(city, driver, user, 0.0, visited)
}
}
return results
}
private fun backtrackEvaluate(
city: HashMap<String, HashMap<String, Double>>,
currNode: String,
targetNode: String,
accSum: Double,
visited: MutableSet<String>
): Double {
// mark the visit
visited.add(currNode)
var ret = -1.0
val neighbors: Map<String, Double> = city[currNode]!!
if (neighbors.containsKey(targetNode)) ret = accSum + neighbors[targetNode]!! else {
for ((nextNode, value) in neighbors) {
if (visited.contains(nextNode)) continue
ret = Solution.backtrackEvaluate(
city, nextNode, targetNode,
accSum + value, visited
)
if (ret != -1.0) break
}
}
// unmark the visit, for the next backtracking
visited.remove(currNode)
return ret
}
}
fun main() {
// Driver code
val gMap = listOf(
listOf("a", "b"),
listOf("b", "c"),
listOf("a", "e"),
listOf("d", "e")
)
val pathCosts = doubleArrayOf(12.0, 23.0, 26.0, 18.0)
val drivers = listOf("c", "d", "e", "f")
val user = "a"
val allPathCosts: DoubleArray = Solution.getTotalCost(gMap, pathCosts, drivers, user)
print("Total cost of all paths " + allPathCosts.contentToString())
}
Plot and Select Path

Complexity measures

Time
...