Repository navigation
Fix MASTSize() under-counting (root row/column of MAST table never filled) - #219
Merged
Merged
Conversation
cpp_mast() filled M[i, j] only for non-root nodes, then evaluated the root cell, which reads M[root1, child of root2] and M[child of root1, root2]. Those entries were left at zero, so a MAST confined to one child subtree of either root was missed. This affects rooted = TRUE directly and rooted = FALSE via its max over rootings of tree2. Corrects three per-rooting expectations in test-mast.R that encoded the bug (checked against brute force) and adds brute-force regression tests.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #219 +/- ##
=======================================
Coverage 97.57% 97.57%
=======================================
Files 60 60
Lines 6065 6069 +4
=======================================
+ Hits 5918 5922 +4
Misses 147 147 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Performance benchmark results
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
cpp_mast()(src/mast.cpp) fillsM[node1, node2]for every pair of non-root nodes, then evaluates the root cellM[root1, root2]. The root recurrence readsM[root1, l2],M[root1, r2],M[l1, root2]andM[r1, root2]("all of one tree vs one child subtree of the other"), but those entries were never filled, so they stayed 0. A MAST confined to one side of either root was therefore missed.This is a fault in the rooted algorithm, not in the unrooted reduction: rooting
tree1at one fixed edge and maximizing over every rooting oftree2is correct given a correct rooted MAST.rooted = FALSEunder-counted only when no rooting oftree2hid the defect.Rooted example:
MASTSize(((a,b),(c,(d,e))), (e,((a,b),(c,d))))returned 3; the MAST has 4 leaves (drope).Fix
Before the root cell, fill
M[root1, j]for every non-rootjof tree 2 (postorder) andM[i, root2]for every non-rootiof tree 1. That is two extra O(n) loops; there are no R changes.Three expectations in the existing "MAST size calculated correctly on small trees" test (
ME(2L, 1),ME(2L, 4),ME(2L, 5)) recorded the buggy values. A brute-force rooted MAST gives 3 for each, so they are nowME(3L, …).Tests
New test "MASTSize() matches brute force" covers the reported reprex (
rooted = FALSE, expect 7), the rooted example above in both argument orders (expect 4), and 12 seeded random pairs (n = 5–7) checked against brute force underrooted = TRUEandFALSE. NEWS.md has a new bullet.Caveat: the compiled package was not built or tested. The sandbox could not run
make(msysNtCreateDirectoryObjectaccess denied) and also refuses todyn.loadDLLs from writable paths. CI will run the first real compiled test.What I did run instead: a line-for-line R port of
cpp_mast(), original and patched, used viaR/tree_distance_mast.R:MASTSize()on 300/300 cases.tests/testthat/test-mast.Rrun against the emulation: patched gives 0 failures in 50 expectations; original gives 10 failures (3 old expectations + 7 brute-force checks).