Feature #3: Plot and Select Path

Implementing the "Plot and Select Path" feature for our "Uber" project.

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:

  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. ...

import scala.collection.immutable.List
import scala.collection.mutable
import scala.collection.mutable.{HashMap, HashSet}
object Main {
def getTotalCost(GMap: List[List[String]], pathCosts: Array[Double], drivers: List[String], user: String): Array[Double] = {
var city = new HashMap[String, HashMap[String, Double]]
// Step 1). build the city from the GMap
for (i <- 0 until GMap.size) {
var checkPoints = GMap(i)
var sourceNode = checkPoints(0)
var destNode = checkPoints(1)
var pathCost = pathCosts(i)
if (!city.contains(sourceNode))
city(sourceNode) = new HashMap[String, Double]()
if (!city.contains(destNode))
city(destNode) = new HashMap[String, Double]()
(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[Double](drivers.size)
for (i <- 0 until drivers.size) {
val driver = drivers(i)
if (!city.contains(driver) || !city.contains(user)) results(i) = -1.0
else {
val visited = new HashSet[String]
results(i) = backtrackEvaluate(city, driver, user, 0, visited)
}
}
results
}
def backtrackEvaluate(city: HashMap[String, HashMap[String, Double]], currNode: String, targetNode: String, accSum: Double, visited: mutable.HashSet[String]): Double = { // mark the visit
visited.add(currNode)
var ret = -1.0
val neighbors = city(currNode)
if (neighbors.contains(targetNode))
ret = accSum + neighbors(targetNode)
else {
var keys = neighbors.keys.toList
var i:Int = 0
var breakit = true
while (i<keys.size && breakit==true) {
var nextNode = keys(i)
if (visited.contains(nextNode)) {
//--- do nothing
}
ret = backtrackEvaluate(city, nextNode, targetNode, accSum + neighbors(nextNode), visited)
if (ret != -1.0)
breakit = false
i += 1
}
}
// unmark the visit, for the next backtracking
visited.remove(currNode)
ret
}
def main(args: Array[String]): Unit = { // Driver code
val GMap = List(List("a", "b"), List("b", "c"), List("a", "e"), List("d", "e"))
val pathCosts = Array(12.0, 23.0, 26.0, 18.0)
val drivers = List("c", "d", "e", "f")
val user = "a"
val allPathCosts = getTotalCost(GMap, pathCosts, drivers, user)
print("Total cost of all paths: [")
allPathCosts.foreach({
el => print(el + ", ")
})
print("]")
}
}
Plot and Select Path

Complexity measures

Time
...