Search⌘ K
AI Features

Feature #3: Plot and Select Path

Explore how to implement path selection from Uber drivers to users in Elixir by building a graph of checkpoints and calculating travel costs. Learn to apply depth-first search to find paths and select the driver with the minimum travel cost, understanding both time and space complexities involved.

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

Elixir
defmodule Paths do
def backtrack_evaluate(city, curr_node, target_node, acc_sum, visited) do
visited = MapSet.put(visited, curr_node)
ret = -1.0
neighbors = city[curr_node]
ret =
cond do
Map.has_key?(neighbors, target_node) -> acc_sum + neighbors[target_node]
true ->
neighbors
|> Enum.map(fn {neighbor, value} ->
cond do
Map.has_key?(visited, neighbor) -> {:cont, ret}
ret != -1.0 -> {:break, ret}
true -> backtrack_evaluate(city, neighbor, target_node, acc_sum + value, visited)
end
end)
end
_visited = visited |> Map.delete(curr_node)
ret
end
def get_total_cost(g_map, path_costs, drivers, user) do
# Step 1). build the city from the G_map
city =
Enum.zip(g_map, path_costs)
|> Enum.reduce(%{}, fn {[source_node | dest_node], path_cost}, city ->
dest_node = hd(dest_node)
value_source = Map.get(city, source_node, %{})
value_dest = Map.get(city, dest_node, %{})
city = Map.put(city, source_node, Map.put(value_source, dest_node, path_cost))
Map.put(city, dest_node, Map.put(value_dest, source_node, path_cost))
end)
# Step 2). Evaluate each driver via backtracking (DFS)
# by verifying if there exists a path from driver to user
drivers
|> Enum.reduce([], fn driver, results ->
ret =
cond do
# Either node does not exist
!Map.has_key?(city, driver) or !Map.has_key?(city, user) -> -1.0
true ->
visited = MapSet.new()
backtrack_evaluate(city, driver, user, 0, visited)
end
ret = if is_list(ret), do: hd(ret), else: ret
[ret | results]
end)
|> :lists.reverse
end
end
# Driver code
IO.puts "-----------------------------"
IO.puts "PROGRAM OUTPUT:"
g_map = [["a","b"],["b","c"],["a","e"],["d","e"]]
path_costs = [12.0,23.0,26.0,18.0]
drivers = ["c", "d", "e", "f"]
user = "a"
all_path_costs = Paths.get_total_cost(g_map, path_costs, drivers, user)
IO.puts "Total cost of all paths #{Main.stringify(all_path_costs)}"

Complexity measures

Time
...