Repository navigation
Add boundary checking to optimal bst - #11771
Conversation
manuelaidos123
left a comment
There was a problem hiding this comment.
Tryt to Simplify the boundary checks for r_start and r_end, by adding additional checks to prevent IndexError, and replacing print statements with a logging mechanism, and configured logging for better debugging.
|
@manuelaidos123 Simplified boundary checks in Knuth's optimization for Optimal BST. Replaced the explicit fallback logic with direct min/max operations to ensure valid array bounds. |
priya-sundaram-dev
left a comment
There was a problem hiding this comment.
I applied the branch and the doctests still pass (5/5), so this is safe as a defensive change. My question before approving is whether it fixes an observed bug or is purely precautionary.
Knuth's optimization relies on the monotonicity invariant root[i][j-1] <= root[i][j] <= root[i+1][j], which (given the bottom-up DP fill order here) already keeps the temporal root r inside [i, j]. So under correct execution the extra max(i, min(...)) / min(j, max(...)) clamps should never actually clip anything, and the r > i / r < j guards are equivalent to the old r != i / r != j.
If you hit a concrete input where the old code went out of bounds or produced a wrong cost, could you add it as a doctest? That would (a) justify the change and (b) guard against regressions. If it is purely defensive with no failing case, a one-line comment saying so — plus dropping the now-redundant r_start/r_end if j > i else ... branches, since the loop body is already skipped when i == j — would keep the hot loop lean. Happy to approve either way once the intent is documented.
|
@priya-sundaram-dev Can you please add "solution" comments to this PR so we can get it reviewed and merged? |
|
I reviewed the change against
On the "solution comments" request: the function is already reasonably documented, but the one thing that would make this fully self-explanatory for a reviewer is a one-line comment above the new clamp block stating why it exists — e.g. |
Describe your change:
The changes address potential issues with small intervals and overlapping boundaries in the current implementation of Knuth's optimization of Optimal BST algorithm.
find_optimal_binary_search_treefunctionFixes #11724
Checklist: