Skip to content

fix: heap Max() and PopLast() return wrong element#20

Open
suykerbuyk wants to merge 1 commit into
coder:mainfrom
suykerbuyk:fix/heap-max
Open

fix: heap Max() and PopLast() return wrong element#20
suykerbuyk wants to merge 1 commit into
coder:mainfrom
suykerbuyk:fix/heap-max

Conversation

@suykerbuyk
Copy link
Copy Markdown
Contributor

Summary

  • Max() returned data[len-1], which is not the maximum in a binary min-heap
  • PopLast() called Remove(Len()-1), removing an arbitrary element instead of the worst
  • This caused incorrect evictions during neighbor selection, silently degrading graph quality

Fix: scan leaf nodes (indices n/2..n-1) to find the true maximum.

Test plan

  • Added TestHeap_MaxAndPopLast — pushes known values, asserts Max/PopLast return the largest
  • Updated TestGraph_AddSearch — expected results now reflect correct eviction behavior
  • go test ./... and go vet ./... pass

In a binary min-heap, the last array element is not necessarily the
maximum. Max() must scan the leaf nodes (indices n/2..n-1) to find
the true maximum. PopLast() used Remove(Len()-1) which removed an
arbitrary element instead of the worst.

This caused incorrect evictions during neighbor selection and search
result trimming, degrading graph quality.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant