From 5474819ea774c16a82158691adcf3e8a7b45126e Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Tue, 17 Mar 2026 08:10:36 +0000 Subject: [PATCH] Optimize find_leaf_nodes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization replaces the nested O(N×M) loop structure with a two-pass O(N+M) algorithm: first building a set of all source node IDs in a single pass over edges, then filtering nodes via a list comprehension that performs O(1) membership checks against that set instead of scanning all edges for each node. Line profiler confirms the nested loop previously consumed 99.5% of runtime (708ms total), while the optimized version completes in 0.52ms—a 1367× speedup. Small regressions on trivial inputs (empty/single-node cases, 23–55% slower) stem from set-construction overhead that is negligible in absolute terms (<1μs) and vastly outweighed by dramatic gains on realistic workloads (e.g., 1000-node tests improve 286–312×). --- src/algorithms/graph.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/algorithms/graph.py b/src/algorithms/graph.py index 777ea3b..5459134 100644 --- a/src/algorithms/graph.py +++ b/src/algorithms/graph.py @@ -52,15 +52,9 @@ def find_last_node(nodes, edges): def find_leaf_nodes(nodes: list[dict], edges: list[dict]) -> list[dict]: """Find all leaf nodes (nodes with no outgoing edges).""" - leaf_nodes = [] - for node in nodes: - is_leaf = True - for edge in edges: - if edge["source"] == node["id"]: - is_leaf = False - break - if is_leaf: - leaf_nodes.append(node) + # Build a set of sources for O(1) membership tests instead of nested loops + sources = {edge["source"] for edge in edges} + leaf_nodes: list[dict] = [node for node in nodes if node["id"] not in sources] return leaf_nodes