Search⌘ K
AI Features

Feature #3: Plot and Select Path

Explore how to build and analyze city maps to find the least costly paths from drivers to users in ride-sharing scenarios. Learn to implement Depth-First Search to verify path existence, calculate cumulative travel costs, and select the optimal driver based on availability and cost efficiency.

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

C++ 17
double backtrackEvaluate(std::unordered_map<std::string, std::unordered_map<std::string, double>>& city, std::string currNode, std::string targetNode, double accSum, std::set<std::string> visited) {
// mark the visit
visited.insert(currNode);
double ret = -1.0;
std::unordered_map<std::string, double> neighbors = city[currNode];
if (neighbors.find(targetNode) != neighbors.end())
ret = accSum + neighbors[targetNode];
else {
for (auto const& [nextNode, val] : neighbors)
{
if (visited.find(nextNode) != visited.end())
continue;
ret = backtrackEvaluate(city, nextNode, targetNode,
accSum + val, visited);
if (ret != -1.0)
break;
}
}
// unmark the visit, for the next backtracking
visited.erase(currNode);
return ret;
}
void getTotalCost(std::vector<std::vector<std::string>> GMap, std::vector<double> pathCosts, std::vector<std::string> drivers, std::string user, std::vector<double>& results) {
std::unordered_map<std::string, std::unordered_map<std::string, double>> city;
// Step 1). build the city from the GMap
for (int i = 0; i < GMap.size(); i++) {
std::vector<std::string> checkPoints = GMap[i];
std::string sourceNode = checkPoints[0];
std::string destNode = checkPoints[1];
double pathCost = pathCosts[i];
if (city.find(sourceNode) == city.end())
city[sourceNode] = std::unordered_map<std::string, double>();
if (city.find(destNode) == city.end())
city[destNode] = std::unordered_map<std::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
for (int i = 0; i < drivers.size(); i++) {
std::string driver = drivers[i];
if (city.find(driver) == city.end() || city.find(user) == city.end())
results[i] = -1.0;
else {
std::set<std::string> visited;
results[i] = backtrackEvaluate(city, driver, user, 0, visited);
}
}
}
int main() {
// Driver code
std::vector<std::vector<std::string>> GMap = {{"a","b"}, {"b","c"}, {"a","e"}, {"d","e"}};
std::vector<double> pathCosts = {12.0,23.0,26.0,18.0};
std::vector<std::string> drivers = {"c", "d", "e", "f"};
std::string user = "a";
std::vector<double> allPathCosts(drivers.size());
getTotalCost(GMap, pathCosts, drivers, user, allPathCosts);
print(allPathCosts);
return 0;
}
Plot and select path

Complexity measures

...