From ddb1a65675831f5dba61613dd4fcd12a6d72eecd Mon Sep 17 00:00:00 2001 From: Pruthviraj Gojare Date: Sun, 22 Nov 2026 00:16:12 +0530 Subject: [PATCH 1/2] Simplify Prim heap index handling Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- graphs/minimum_spanning_tree_prims2.py | 27 ++++++++++---------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/graphs/minimum_spanning_tree_prims2.py b/graphs/minimum_spanning_tree_prims2.py index d22111289a48..bd75572747a5 100644 --- a/graphs/minimum_spanning_tree_prims2.py +++ b/graphs/minimum_spanning_tree_prims2.py @@ -44,7 +44,7 @@ def get_child_right_position(position: int) -> int: >>> get_child_right_position(0) 2 """ - return (2 * position) + 2 + return get_child_left_position(position) + 1 class MinPriorityQueue[T]: @@ -152,23 +152,16 @@ def _bubble_down(self, elem: T) -> None: _, weight = self.heap[curr_pos] child_left_position = get_child_left_position(curr_pos) child_right_position = get_child_right_position(curr_pos) - if child_left_position < self.elements and child_right_position < self.elements: - _, child_left_weight = self.heap[child_left_position] - _, child_right_weight = self.heap[child_right_position] - if child_right_weight < child_left_weight and child_right_weight < weight: - self._swap_nodes(child_right_position, curr_pos) - return self._bubble_down(elem) if child_left_position < self.elements: - _, child_left_weight = self.heap[child_left_position] - if child_left_weight < weight: - self._swap_nodes(child_left_position, curr_pos) - return self._bubble_down(elem) - else: - return None - if child_right_position < self.elements: - _, child_right_weight = self.heap[child_right_position] - if child_right_weight < weight: - self._swap_nodes(child_right_position, curr_pos) + child_position = child_left_position + if ( + child_right_position < self.elements + and self.heap[child_right_position][1] + < self.heap[child_left_position][1] + ): + child_position = child_right_position + if self.heap[child_position][1] < weight: + self._swap_nodes(child_position, curr_pos) return self._bubble_down(elem) return None From 0af9e31e7bdd9eb095a29175179b193b311cd316 Mon Sep 17 00:00:00 2001 From: Pruthviraj Gojare Date: Sun, 22 Nov 2026 00:16:32 +0530 Subject: [PATCH 2/2] Record Prim heap refactor evidence Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- work/R23-T048/BASELINE.md | 34 ++++++++++++++++++++++++++++++ work/R23-T048/REFACTOR.md | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 work/R23-T048/BASELINE.md create mode 100644 work/R23-T048/REFACTOR.md diff --git a/work/R23-T048/BASELINE.md b/work/R23-T048/BASELINE.md new file mode 100644 index 000000000000..eef13a886df7 --- /dev/null +++ b/work/R23-T048/BASELINE.md @@ -0,0 +1,34 @@ +# R23-T048 baseline + +Captured before changing `graphs/minimum_spanning_tree_prims2.py` on +`R23-T048` (base `574d100b7181956e9c8384552fd078165e4cc326`). + +## Tests and coverage + +Command: + +```bash +uv run pytest graphs/minimum_spanning_tree_prims2.py --cov=graphs.minimum_spanning_tree_prims2 --cov-report=term +``` + +Result: 5 pytest items passed. Coverage was 126 statements, 9 missed, 93%. + +The direct doctest run reported 24 doctest examples passed across the priority +queue, heap index helpers, and `prims_algo`. + +## Complexity + +`uvx ruff check --select C90 graphs/minimum_spanning_tree_prims2.py` passed +with Ruff's default maximum complexity of 10. For numeric measurements, the +same check was run with `lint.mccabe.max-complexity=1` to report each function: + +| Function | Baseline complexity | +| --- | ---: | +| `extract_min` | 3 | +| `update_key` | 3 | +| `_bubble_up` | 3 | +| `_bubble_down` | 7 | +| `add_node` | 2 | +| `prims_algo` | 8 | + +`prims_algo` was the worst function at complexity 8. diff --git a/work/R23-T048/REFACTOR.md b/work/R23-T048/REFACTOR.md new file mode 100644 index 000000000000..c69cf63e4a90 --- /dev/null +++ b/work/R23-T048/REFACTOR.md @@ -0,0 +1,44 @@ +# Prim heap refactor + +## What changed + +- `get_child_right_position` now derives its result from + `get_child_left_position(position) + 1`, so the shared `2 * position + 1` + calculation is written once. +- `_bubble_down` selects the smaller existing child once, then compares that + child's weight with the current node. This replaces separate right- and + left-child swap paths while retaining strict comparisons (equal child + weights still prefer the left child). +- `get_parent_position` remains shared by `update_key` and `_bubble_up`. +- No doctests were changed. + +## C90 complexity + +Ruff's default C90 check uses a maximum complexity of 10 and passed both before +and after. To show numeric values for every reported function, complexity was +measured with `lint.mccabe.max-complexity=1`. + +| Function | Before | After | +| --- | ---: | ---: | +| `extract_min` | 3 | 3 | +| `update_key` | 3 | 3 | +| `_bubble_up` | 3 | 3 | +| `_bubble_down` | 7 | 4 | +| `add_node` | 2 | 2 | +| `prims_algo` | 8 | 8 | + +`prims_algo` remains the file-wide worst function at 8. The refactored heap +method `_bubble_down` decreased from 7 to 4. + +## Test and coverage results + +| Check | Before | After | +| --- | --- | --- | +| `uv run pytest graphs/minimum_spanning_tree_prims2.py` | 5 passed | 5 passed | +| Direct doctests | 24 passed | 24 passed | +| Target file coverage | 93% (126 statements, 9 missed) | 95% (116 statements, 6 missed) | +| `uvx ruff check graphs/minimum_spanning_tree_prims2.py` | passed | passed | +| `uvx ruff check --select C90 graphs/minimum_spanning_tree_prims2.py` | passed | passed | + +After the refactor, 100 seeded priority-queue trials with 50 elements and 100 +key updates per trial also extracted elements in nondecreasing priority order.