KatInterview

Best Net Reward Route to an Underscore Node in a DAG

Trees & GraphsAuto-graded · 20 test cases
MediumAuto-gradedTrees & Graphsgraphdagtopological-sort

Last updated

Find the highest net score of a route in a weighted DAG that ends at a node whose name starts with an underscore, using topological order and DP.

You are given a directed acyclic graph of locations. Each location has a name and a reward for visiting it. Each directed route between two locations has a travel cost.

Starting at a given source location, you walk along routes and stop at a target location. A location is a target if its name begins with the character _. Find the best achievable net score.

Scoring a walk

For a walk that starts at source and follows routes source = v0 -> v1 -> ... -> vk:

  • You collect the reward of each location you arrive at, namely v1 ... vk.
  • The source's reward is treated as 0, whatever value is listed for it.
  • You pay the cost of every route you use.
net score = (rewards of v1..vk) - (costs of the k routes used)

Rules

  • A walk with zero routes is allowed. It ends at the source and scores 0. It is valid only if the source's name begins with _.
  • A walk may pass through target locations and keep going. Every target reachable from the source is a candidate end point, not only targets with no outgoing routes.
  • A location that appears in routes but not in node_rewards has reward 0.
  • Rewards and costs may be negative or zero. The best score can be negative, and you must still return it.
  • Several routes may connect the same pair of locations. Any of them may be used.
  • Only locations reachable from source matter.

Task

Implement the function. It takes:

  • node_rewards: a dictionary mapping location name to its integer reward.
  • routes: a list of [from_name, to_name, cost] entries, one per directed route.
  • source: the name of the starting location.

Return the maximum net score over all walks from source that end at a target location. If no target location is reachable from source, return None.

The graph is guaranteed to have no directed cycles.

Constraints

  • The graph is a DAG, so it contains no directed cycles.
  • Names are non-empty strings.
  • Rewards and costs are integers and may be negative.
  • The number of locations and routes is small enough that an O(V + E) solution is comfortably fast.
  • source may or may not appear as a key in node_rewards. Its listed reward, if any, is ignored.

Examples

Example 1

Input: best_route_value({'start': 0, 'a': 5, '_end': 10}, [['start', 'a', 2], ['a', '_end', 3]], 'start')

Output: 10

Example 2

Input: best_route_value({'start': 0, 'a': 4, 'b': 12, '_slow': 1, '_fast': 7}, [['start', 'a', 1], ['a', '_slow', 1], ['start', 'b', 5], ['b', '_fast', 2]], 'start')

Output: 12

Example 3

Input: best_route_value({'start': 0, '_done': -3}, [['start', '_done', 5]], 'start')

Output: -8

Hints

Hint 1

Because the graph has no cycles, you can order the locations so that every route goes from an earlier location to a later one. Think about what that lets you compute in a single pass.

Hint 2

Keep, for each location, the best net score of any walk from the source to it, using negative infinity for locations you cannot reach. When you take a route u -> v with cost c, the candidate for v is best[u] - c + reward(v).

Hint 3

Do not seed unreachable locations with 0, since that invents walks that never started at the source. At the end, take the maximum only over reachable locations whose names start with an underscore, and return None if there are none.