Skip to content

Commit

Permalink
BicoloredRootedTreeIterator is canonical (#73)
Browse files Browse the repository at this point in the history
* BicoloredRootedTreeIterator is canonical; fixes #72

* bump patch version
  • Loading branch information
ranocha authored Jan 23, 2022
1 parent e1375a0 commit 16264a3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RootedTrees"
uuid = "47965b36-3f3e-11e9-0dcf-4570dfd42a8c"
authors = ["Hendrik Ranocha <[email protected]> and contributors"]
version = "2.9.1-pre"
version = "2.9.1"

[deps]
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
Expand Down
20 changes: 20 additions & 0 deletions src/RootedTrees.jl
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,26 @@ function canonical_representation!(t::RootedTree{Int, Vector{Int}})
end


"""
check_canonical(t::AbstractRootedTree)
Check whether `t` is in canonical representation.
!!! warn "Internal interface"
This function is considered to be an internal implementation detail and
will not necessarily be stable.
"""
function check_canonical(t::AbstractRootedTree)
for subtree in SubtreeIterator(t)
if !check_canonical(subtree)
return false
end
end

return issorted(SubtreeIterator(t), rev=true)
end


"""
normalize_root!(t::AbstractRootedTree, root=one(eltype(t.level_sequence)))
Expand Down
16 changes: 14 additions & 2 deletions src/colored_trees.jl
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,21 @@ end
inner_state, color_id = state

# If we can iterate more by changing the color sequence, let's do so.
if color_id < iter.number_of_colors
while color_id < iter.number_of_colors
binary_digits!(iter.t.color_sequence, color_id)
return (iter.t, (inner_state, color_id + 1))

# This simple enumeration of all possible colors can also yield colored
# trees that are not in canonical representation. For example, the trees
# rootedtree([1, 2, 2], Bool[0, 0, 1])
# rootedtree([1, 2, 2], Bool[1, 0, 1])
# are not in canonical representation.
# TODO: ColoredRootedTrees. Is there a more efficient way to get only
# canonical representations?
if check_canonical(iter.t)
return (iter.t, (inner_state, color_id + 1))
else
color_id = color_id + 1
end
end

# Now, we need to iterate to a new baseline (uncolored) tree - if possible
Expand Down
13 changes: 12 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,18 @@ end # @testset "RootedTree"
num += 1
end
# number of plain rooted trees times number of possible color sequences
@test num == number_of_rooted_trees[order] * 2^order
# <= since not all possible color sequences are in canonical representation
@test num <= number_of_rooted_trees[order] * 2^order
end
end

# https://github.com/SciML/RootedTrees.jl/issues/72
@testset "BicoloredRootedTreeIterator is canonical" begin
for o in 1:10
for t_iterator in BicoloredRootedTreeIterator(o)
t_canonical = RootedTrees.canonical_representation(t_iterator)
@test t_iterator == t_canonical
end
end
end

Expand Down

2 comments on commit 16264a3

@ranocha
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/53039

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.9.1 -m "<description of version>" 16264a340ab496e65a53c2864443522e612429fb
git push origin v2.9.1

Please sign in to comment.