Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions graphs/minimum_spanning_tree_prims2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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

Expand Down
34 changes: 34 additions & 0 deletions work/R23-T048/BASELINE.md
Original file line number Diff line number Diff line change
@@ -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.
44 changes: 44 additions & 0 deletions work/R23-T048/REFACTOR.md
Original file line number Diff line number Diff line change
@@ -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.