Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add source for tree reroot fold #67

Merged
merged 1 commit into from
Feb 25, 2024
Merged
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
9 changes: 5 additions & 4 deletions src/RerootFold.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ g :: b -> c -> b must be commutative, in the sense that
(b `g` c1) `g` c2 = (b `g` c2) `g` c1

Sources:
* Informal discussions. I am unable to find a good published source for this technique :(
* pajenegod, "The Ultimate Reroot Template"
https://codeforces.com/blog/entry/124286

Implementation notes:
* Thanks to Haskell's laziness it is possible to write a more concise implementation with a single
dfs, but this seems to be slower in practice. Here is a possible implementation:
https://gist.github.com/meooow25/6460e45327355106cedbcf3bd5166cd6
* Reroot problems can often be solved in O(n) time, if there is a way to *take way* the contribution
of a node from an accumulated value. Avoiding this requirement, it takes O(n log n) but is far
simpler.
* Reroot problems can often be solved in O(n) time, if there is a way to *take away* the
contribution of a node from an accumulated value. Avoiding this requirement, it takes O(n log n)
but is far simpler.

foldReroot
Returns the same tree with each vertex accompanied by the fold of the tree if that vertex is made
Expand Down
Loading