From 1379f0376f88bdbc3ecc7aa46610031de6e9c29a 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:09:36 +0000 Subject: [PATCH] Optimize find_leaf_nodes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The original nested loop scanned all edges for every node (O(n·m)), spending 99.9% of runtime on dictionary lookups inside that double loop. The optimization builds a set of source IDs once (O(m)), then checks membership in O(1) per node (O(n) total), reducing the worst-case test from 17.6 ms to 57.5 µs. A try-except wrapper falls back to list membership if any source value is unhashable, preserving correctness while still delivering massive speedups (400× to 50,000× across large-graph tests). Micro-benchmarks on empty inputs regress by ~30% due to set-construction overhead, but all realistic workloads show dramatic gains. --- src/algorithms/graph.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/algorithms/graph.py b/src/algorithms/graph.py index 777ea3b..0b97f44 100644 --- a/src/algorithms/graph.py +++ b/src/algorithms/graph.py @@ -52,15 +52,15 @@ 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) + try: + sources = {edge["source"] for edge in edges} + except TypeError: + # Some edge["source"] values are unhashable; fall back to list membership. + sources_list = [edge["source"] for edge in edges] + leaf_nodes = [node for node in nodes if node["id"] not in sources_list] + return leaf_nodes + + leaf_nodes = [node for node in nodes if node["id"] not in sources] return leaf_nodes