KatInterview

Longest Dependency Chain in a Task DAG

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

Last updated

Find the longest total-duration chain in a task dependency DAG and return its length and task names, using topological-order DP with predecessor tracking.

A build pipeline is made of tasks. Each task is a dictionary with three keys:

  • "name": a unique string identifying the task
  • "dependencies": a list of names of tasks that must finish before this task can begin
  • "duration": a positive integer, the time this task takes

A task can only start once every task it depends on has finished. Tasks with no unfinished prerequisites may run in parallel. The dependency relationships contain no cycles, and every name listed in "dependencies" refers to a task in the input.

Write a function that finds the bottleneck chain of the pipeline: the sequence of tasks, where each task depends on the previous one, that has the largest total duration.

Return a two-element list [total, path]:

  1. total is the sum of durations along the longest chain. This is also the earliest time the whole project can be complete.
  2. path is the list of task names on one such chain, ordered from the first task to execute to the last. Each task in the list must directly depend on the one before it.

Details:

  • The chain may start at any task with no dependencies and may end at any task. The last task in the input is not necessarily the end of the longest chain.
  • Tasks may appear in the input in any order, so a task can list a dependency that appears later in the list.
  • If several chains tie for the largest total, returning any one of them is acceptable.
  • If the task list is empty, return [0, []].

Constraints

  • 0 <= len(tasks) <= 10^5
  • Total number of dependency entries is at most 2 * 10^5.
  • Task names are unique, non-empty strings.
  • Each duration is a positive integer, and the total fits in a 64-bit integer.
  • The dependency graph is acyclic and every dependency refers to an existing task.
  • The expected running time is linear in the number of tasks plus dependency entries.

Examples

Example 1

Input: longest_dependency_chain([{'name': 'A', 'dependencies': [], 'duration': 3}, {'name': 'B', 'dependencies': [], 'duration': 5}, {'name': 'C', 'dependencies': ['A'], 'duration': 5}])

Output: [8, ['A', 'C']]

Example 2

Input: longest_dependency_chain([{'name': 'A', 'dependencies': [], 'duration': 2}, {'name': 'B', 'dependencies': ['A'], 'duration': 4}, {'name': 'C', 'dependencies': ['B'], 'duration': 3}, {'name': 'D', 'dependencies': ['C'], 'duration': 6}])

Output: [15, ['A', 'B', 'C', 'D']]

Example 3

Input: longest_dependency_chain([{'name': 'A', 'dependencies': [], 'duration': 3}, {'name': 'B', 'dependencies': ['A'], 'duration': 2}, {'name': 'C', 'dependencies': ['A'], 'duration': 7}, {'name': 'D', 'dependencies': ['B', 'C'], 'duration': 4}, {'name': 'E', 'dependencies': ['D'], 'duration': 2}])

Output: [16, ['A', 'C', 'D', 'E']]

Example 4

Input: longest_dependency_chain([{'name': 'A', 'dependencies': [], 'duration': 3}, {'name': 'B', 'dependencies': [], 'duration': 8}, {'name': 'C', 'dependencies': ['A'], 'duration': 2}, {'name': 'D', 'dependencies': ['C'], 'duration': 5}, {'name': 'E', 'dependencies': ['B'], 'duration': 1}, {'name': 'F', 'dependencies': ['D', 'E'], 'duration': 4}])

Output: [14, ['A', 'C', 'D', 'F']]

Hints

Hint 1

Think of each task as a node with a weight. The best chain ending at a task is its own duration plus the best chain ending at one of its prerequisites.

Hint 2

Process tasks in an order where every prerequisite is handled before the tasks that depend on it (topological order, for example Kahn's algorithm with in-degree counts).

Hint 3

While computing the best total ending at each task, remember which prerequisite gave that best value. Start from the task with the overall largest total, follow these links back, then reverse the result.