...

/

Feature #3: Plot and Select Path

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 GMap as a list of different checkpoints. Another list pathCosts, at each index, will represent the cost of traveling between the corresponding checkpoints in GMap. 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,

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

  4. Return ...

package main
import (
"fmt"
)
func backtrackEvaluate(city map[string]map[string]float64, currNode string, targetNode string, accSum float64, visited map[string]bool) float64 {
// mark the visit
visited[currNode] = true
ret := -1.0
neighbors := city[currNode]
if i, ok := neighbors[targetNode]; ok {
ret = accSum + i
} else {
for nextNode, val := range neighbors {
if _, ok := visited[nextNode]; ok {
continue
}
ret = backtrackEvaluate(city, nextNode, targetNode,
accSum + val, visited)
if ret != -1.0 {
break
}
}
}
// unmark the visit, for the next backtracking
delete(visited, currNode)
return ret
}
func getTotalCost(GMap [][]string, pathCosts []float64, drivers []string, user string) []float64 {
results := make([]float64, len(drivers))
city := make(map[string]map[string]float64)
// Step 1). build the city from the GMap
for i := 0; i < len(GMap); i++ {
checkPoints := GMap[i]
sourceNode := checkPoints[0]
destNode := checkPoints[1]
pathCost := pathCosts[i]
if _, ok := city[sourceNode]; !ok {
city[sourceNode] = make(map[string]float64)
}
if _, ok := city[destNode]; !ok {
city[destNode] = make(map[string]float64)
}
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
for i := 0; i < len(drivers); i++ {
driver := drivers[i]
_, temp := city[user]
if _, ok := city[driver]; !ok || !temp {
results[i] = -1.0
} else {
visited := make(map[string]bool)
results[i] = backtrackEvaluate(city, driver, user, 0, visited)
}
}
return results
}
func main() {
// Driver code
GMap := [][]string{{"a","b"}, {"b","c"}, {"a","e"}, {"d","e"}}
pathCosts := []float64{12.0,23.0,26.0,18.0}
drivers := []string{"c", "d", "e", "f"}
user := "a"
print(getTotalCost(GMap, pathCosts, drivers, user))
}
Plot and Select Path

Complexity measures

...