← run

py-09-networkx-dep-chain

1.000
8/8 tests· lib-knowledge

Proposed solution

```python
import networkx as nx

def longest_dependency_chain(deps: dict[str, list[str]]) -> int:
    # Create a directed graph
    G = nx.DiGraph()
    
    # Add all nodes and edges
    for node, prerequisites in deps.items():
        G.add_node(node)
        for prereq in prerequisites:
            G.add_edge(prereq, node)
    
    # Handle empty graph case
    if len(G.nodes()) == 0:
        return 0
    
    # Find the longest path in the DAG
    longest_path = nx.dag_longest_path(G)
    
    # Return the number of nodes in the longest path
    return len(longest_path)
```

Test output (stdout)

........                                                                 [100%]
8 passed in 0.06s
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.