KatInterview

Safe Edge Insertion Check for a Dependency Graph

Trees & GraphsAuto-graded · 24 test cases
HardAuto-gradedTrees & Graphsgraphsdepth-first-searchcycle-detection

Last updated

Tests graph reachability and cycle detection: determine whether a new directed edge can be added to an acyclic dependency graph without introducing a cycle.

Background

A dependency graph is a directed acyclic graph (DAG) where each node represents a task or module, and a directed edge from a to b means "a depends on b." The acyclic property is what makes a valid build or execution order possible.

Your job is to decide whether a proposed new dependency link can be safely added.

Task

You are given a DAG with n nodes labeled 0 through n-1. The graph is passed as a dictionary graph where graph[u] is a list of nodes that u points to.

Important — key types: The dictionary keys may be integers or strings (e.g., "0", "1"). The node arguments src and dst are always integers. Look up neighbors with graph.get(node, []) where node is whatever type was used to walk to that node — start the traversal with the integer dst, and follow the neighbor values exactly as stored in the lists. Do not convert keys or values; use them as-is. This means that if the dictionary keys happen to be strings, an integer lookup will simply find no neighbors, which is the correct and expected behavior.

Write a function safe_to_link(n, graph, src, dst) that returns True if the directed edge src → dst can be added while keeping the graph acyclic, and False otherwise.

An edge must be rejected under any of these three conditions, checked in order:

  1. Self-reference: src == dst.
  2. Already exists: dst is already in graph.get(src, []).
  3. Creates a cycle: starting from dst, a reachability traversal (DFS or BFS) using graph.get(node, []) reaches src. Because neighbor values are used as-is for subsequent lookups, the traversal naturally respects whatever key type the dictionary uses.

If none of these conditions hold, return True.

Constraints

  • 0 <= src, dst < n
  • 1 <= n <= 10,000
  • graph is a dictionary; isolated nodes may not appear as keys; keys may be integers or strings
  • Node arguments src and dst are always integers
  • The input graph is guaranteed to be a valid DAG at the time of the call (though the reachability traversal must terminate correctly regardless)
  • Do not modify graph

Examples

Example 1

Input: safe_to_link(3, {'0': [1], '1': [2], '2': []}, 0, 2)

Output: True

Example 2

Input: safe_to_link(3, {'0': [1], '1': [2], '2': []}, 2, 1)

Output: True

Example 3

Input: safe_to_link(3, {'0': [1], '1': [2], '2': []}, 0, 1)

Output: True

Example 4

Input: safe_to_link(4, {'0': [1], '1': [], '2': [3], '3': []}, 1, 2)

Output: True

Hints

Hint 1

Check the two cheapest rejection conditions first — self-loop and duplicate — before doing any graph traversal.

Hint 2

To detect whether adding src → dst would create a cycle, ask: is src already reachable from dst in the current graph? Use an iterative DFS starting from dst, following neighbor values exactly as stored in the lists.

Hint 3

Use graph.get(node, []) for every neighbor lookup, starting with the integer dst. If the dictionary uses string keys, integer lookups return nothing — that is correct behavior, not a bug to fix.