Skip to content

dsu/kruskal_tree: tighten allocation to 2*n-1#242

Merged
cameroncuster merged 1 commit into
devfrom
fix/kruskal-tree-tight-alloc
Jul 12, 2026
Merged

dsu/kruskal_tree: tighten allocation to 2*n-1#242
cameroncuster merged 1 commit into
devfrom
fix/kruskal-tree-tight-alloc

Conversation

@cameroncuster

Copy link
Copy Markdown
Member

What

Tighten the kr_tree allocation from 2 * n to 2 * n - 1, the exact
upper bound on the number of nodes in a Kruskal reconstruction tree over
n leaves.

Why

The old 2 * n sizing was one element too large. The tight 2 * n - 1
matches the actual node count.

Note on n == 0

2 * n - 1 underflows to a huge size_t when n == 0, so the library
now documents requires n >= 1 at each site with this kind of
allocation. This is the same latent constraint that already existed for
the suffix-array lcp (size n - 1) and manacher (2 * n - 1) code, so
a //! requires n >= 1 note was added there as well for discoverability.

Files

  • library/dsu/kruskal_tree.hpp — code fix (2*n2*n-1) + note
  • library/strings/manacher/manacher.hpp — note
  • library/strings/suffix_array/suffix_array.hpp — note
  • library/strings/suffix_array/suffix_array_short.hpp — note

A Kruskal reconstruction tree over n leaves has at most 2*n-1 nodes, so
allocate exactly that instead of 2*n. Document that n >= 1 is required,
since 2*n-1 (and the lcp size n-1 in the suffix-array / manacher code)
underflows to a huge size_t when n == 0.
@cameroncuster cameroncuster merged commit 0a00545 into dev Jul 12, 2026
7 of 8 checks passed
@cameroncuster cameroncuster deleted the fix/kruskal-tree-tight-alloc branch July 12, 2026 17:49
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