Repository navigation
Stop the cost model calling merge sort, binary search and tree walks exponential - #3
Merged
Merged
Conversation
…exponential Any function with two self-calls written down was labelled O(2^n) and got a high-severity "Exponential recursion" finding telling the reader to memoise it. That fired on recursive binary search (two calls in separate returns, one runs), merge sort and quicksort (divide and conquer), and every binary-tree traversal: most of the recursion people paste. The estimate now counts self-calls per invocation (calls in different `return` statements are alternatives) and reads the arguments: tree children (`node.left`) are a tree walk, halves are divide and conquer, and only a parameter stepping by a constant (`n - 1`, `s[1:]`) keeps the exponential label. fib, Hanoi, grid paths and decode-ways stay O(2^n). Library costs had the same over-reach: a call anywhere in the function was multiplied by the deepest loop, so `sum(xs)` before a loop made it O(n^2). Each call is now multiplied only by the loops it actually runs inside, a for-each iterable counts as evaluated once, and a sort inside a loop is a sort per iteration, matching how linear calls were already treated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UigC7feaY8NkAy9oay3GDW
ethanzhoucool
marked this pull request as ready for review
November 3, 2026 17:12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The asymptotic-cost heuristic labelled almost every recursive function that has two self-calls as O(2^n). That label also produces a high-severity "Exponential recursion" finding that tells the reader to memoise the function. In practice it fired on the textbook recursion people paste into an explainer:
return search(...)per branch)xs[:mid],xs[mid:])partitionversionsnode.left,node.right; Python, JS, C++->)Library-call costs had a related problem. A known-cost call anywhere in the function was multiplied by the deepest loop, even when the call was not inside that loop:
total = sum(xs)thenfor x in xs: ...sumscans ... inside it"ys = sorted(xs)then two nested loopsfor (const x of xs.filter(Boolean))for g in groups: out.append(sorted(g))The last row is a deliberate change. A linear call inside a loop already scored n × n = n². A sort inside a loop only added a log factor, which disagreed with its own reason text. Now both rules agree: one sort per iteration.
Changes
All in
analysis/complexity.py:return, so self-calls sitting in different return statements are alternatives. This handles binary search and BST find in every language, because it only uses the IR'sRETURNnodes..left,->right,.children…) mean a tree walk, O(n);HALVINGpattern:mid,// 2,>> 1…) means divide and conquer, O(n log n);n - 1,i + 1,s[1:]) means exponential, as before. Requiring a parameter is what keeps in-place quicksort'sp - 1/p + 1out of this case;COST_ORDERconstant.Testing
pyteston Python 3.13, 3.12 and 3.11: 167 passed, 3 skipped.ruff check explain_my_code testsis clean.mypyreports the same 26 errors as before.New parametrised tests in
tests/test_analysis.py:test_divide_and_conquer_and_tree_recursion_are_not_exponential(9 cases across Python, JS and C++);test_constant_step_branching_recursion_stays_exponential(5 cases);test_library_costs_only_multiply_the_loops_they_run_inside(6 cases).Run against
main's source, 13 of these fail, and the 7 that pass are the ones that should already pass. The existing fib, memoised-fib and sort-in-loop tests still pass, and the README's headlinefibexample gives the same output.Notes
callee, becauseJavaAdapter._meta_callis never invoked (_COMMON_METAbinds the base-class function). So Java recursion and library costs can't be classified at all right now: Javafibreads as "part of a recursive cycle through another function". That is a separate adapter fix, coming in its own PR.dfs(r + 1, c)…) with a visited set is still called exponential, as it was before. BST insert written asnode.left = insert(...)/node.right = insert(...)reads as O(n), the worst case, because the two calls are in separate if/else arms rather than separate returns.Generated by Claude Code