KatInterview

Distinct Transitive Prerequisites in a Dependency DAG

Trees & GraphsAuto-graded · 21 test cases
HardAuto-gradedTrees & Graphsgraphdagtopological-sort

Last updated

For each node in a DAG whose links may name unknown items, count the distinct nodes reachable along link direction, counting shared descendants once.

You are given a list of distinct item names and a list of directed links between names. A link [a, b] means that a requires b.

For each item in names, count how many different items it requires, either directly or through a chain of requirements. If a requires b and b requires c, then a requires both b and c.

Precise definition

Call a link valid if both of its names appear in names. Ignore every other link completely.

For an item u, its answer is the number of distinct items v in names with v != u such that there is a sequence of valid links [u, x1], [x1, x2], ..., [xk, v] with k >= 0. Each link in the sequence starts at the name where the previous one ended, and the sequence follows the link direction, from the first name to the second.

The count is therefore about what u requires, never about what requires u. For [a, b], the item a gains b in its count, and b gains nothing from a.

Return a list of integers with one answer per item, in the same order as names.

Rules

  • Invalid links are dropped before anything else. A dropped link takes part in no chain. Dropping it never creates a connection between the remaining items. For example, if x requires ghost and ghost requires y, and ghost is not in names, then x does not require y unless a chain of valid links says so.
  • Duplicate links may appear and have no effect on any count.
  • Distinct counting. An item reachable along several different chains is counted once.
  • No self-counting. An item is never counted as requiring itself. Self-links [a, a] do not occur.
  • No requirements, no count. An item with no valid outgoing link has answer 0. This includes items that appear in no link at all.
  • Case sensitivity. Names are compared exactly, so "a" and "A" are different items.
  • Acyclic input. The valid links never form a cycle.
  • Empty input. If names is empty, return an empty list.

Task

Implement count_required_nodes(names, links), which returns the list of answers described above.

Constraints

  • 0 <= len(names) <= 2000. All names are unique and non-empty strings.
  • 0 <= len(links) <= 10000. Each link is a list of exactly two strings.
  • Links with an endpoint that is not in names are ignored.
  • The graph formed by the valid links is acyclic.

Examples

Example 1

Input: count_required_nodes(['A', 'B', 'C', 'D', 'E'], [['B', 'A'], ['C', 'A'], ['D', 'B'], ['D', 'C'], ['E', 'ghost'], ['ghost', 'A']])

Output: [0, 1, 1, 3, 0]

Hints

Hint 1

Treat each valid link a -> b as a directed edge and ask which nodes you can reach by following edges forward from a node. Filter out links with unknown endpoints before building any graph.

Hint 2

Adding up the counts of a node's direct requirements overcounts when two chains share a node. Track which nodes are reachable, not just how many, and merge with a union.

Hint 3

Use Kahn's algorithm on the reversed graph so a node is handled only after everything it requires. Push each node's finished reachable set, plus the node itself, to the nodes that require it. A Python int used as a bitmask makes the union a single OR.