diff --git a/src/JunctionTrees.jl b/src/JunctionTrees.jl index 1c756b4..e99a8a2 100644 --- a/src/JunctionTrees.jl +++ b/src/JunctionTrees.jl @@ -30,7 +30,6 @@ export SupernodeTree, width, height, seperators, supernode, order include("junction_trees/orders.jl") include("junction_trees/elimination_algorithms.jl") include("junction_trees/ordered_graphs.jl") -include("junction_trees/abstract_trees.jl") include("junction_trees/trees.jl") include("junction_trees/postorder_trees.jl") include("junction_trees/elimination_trees.jl") diff --git a/src/junction_trees/abstract_trees.jl b/src/junction_trees/abstract_trees.jl deleted file mode 100644 index 618e23d..0000000 --- a/src/junction_trees/abstract_trees.jl +++ /dev/null @@ -1,25 +0,0 @@ -# A rooted tree. -abstract type AbstractTree end - - -# Compute a postordering of tree's vertices. -function postorder(tree::AbstractTree) - n = length(tree) - order = Vector{Int}(undef, n) - index = Vector{Int}(undef, n) - - for node in PreOrderDFS(IndexNode(tree)) - order[n] = node.index - index[node.index] = n - n -= 1 - end - - Order(order, index) -end - - -# Get the height of a tree. -function height(tree::AbstractTree) - n = length(tree) - maximum(map(i -> level(tree, i), 1:n)) -end diff --git a/src/junction_trees/elimination_trees.jl b/src/junction_trees/elimination_trees.jl index ee76266..f6cbe18 100644 --- a/src/junction_trees/elimination_trees.jl +++ b/src/junction_trees/elimination_trees.jl @@ -1,6 +1,6 @@ # An ordered graph (G, σ) equipped with the elimination tree T of its elimination graph. # Nodes i in T correspond to vertices σ(i) in G. -struct EliminationTree{T <: AbstractTree} +struct EliminationTree{T <: Union{Tree, PostorderTree}} tree::T # elimination tree ograph::OrderedGraph # ordered graph end diff --git a/src/junction_trees/postorder_trees.jl b/src/junction_trees/postorder_trees.jl index 450282c..319454b 100644 --- a/src/junction_trees/postorder_trees.jl +++ b/src/junction_trees/postorder_trees.jl @@ -1,5 +1,5 @@ # A postordered rooted tree. -struct PostorderTree <: AbstractTree +struct PostorderTree parent::Vector{Int} # parent children::Vector{Vector{Int}} # children level::Vector{Int} # level @@ -36,7 +36,7 @@ end # Postorder a tree. -function PostorderTree(tree::AbstractTree, order::Order) +function PostorderTree(tree::Tree, order::Order) n = length(tree) parent = collect(1:n) @@ -72,6 +72,12 @@ function isdescendant(tree::PostorderTree, i::Integer, j::Integer) end +# Get the height of a tree. +function height(tree::PostorderTree) + maximum(tree.level) +end + + ########################## # Indexed Tree Interface # ########################## diff --git a/src/junction_trees/trees.jl b/src/junction_trees/trees.jl index 0b26371..fcc2069 100644 --- a/src/junction_trees/trees.jl +++ b/src/junction_trees/trees.jl @@ -1,5 +1,5 @@ # A rooted tree. -struct Tree <: AbstractTree +struct Tree root::Int # root parent::Vector{Int} # parent children::Vector{Vector{Int}} # children @@ -35,6 +35,22 @@ function Base.length(tree::Tree) end +# Compute a postordering of tree's vertices. +function postorder(tree::Tree) + n = length(tree) + order = Vector{Int}(undef, n) + index = Vector{Int}(undef, n) + + for node in PreOrderDFS(IndexNode(tree)) + order[n] = node.index + index[node.index] = n + n -= 1 + end + + Order(order, index) +end + + ########################## # Indexed Tree Interface # ##########################