Skip to content

Commit

Permalink
temp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelsonric committed Nov 17, 2024
1 parent f02b09c commit a96feba
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 158 deletions.
8 changes: 4 additions & 4 deletions src/Decompositions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,24 +228,24 @@ function Decompositions.StrDecomp(sgraph::AbstractSymmetricGraph, stree::Superno
seperator = seperators(stree)
foreach(sort!, seperator)

n = length(stree)
n = length(stree.tree)
graph = Graph(n)
objects = Vector{OTYPE}(undef, 2n - 1)
morphisms = Vector{MTYPE}(undef, 2n - 2)

for i in 1:n
snd = supernode(stree, i)
sep = seperator[i]
objects[i] = induced_subgraph(sgraph, order(stree, [snd; sep]))
objects[i] = induced_subgraph(sgraph, order(stree.ograph, [snd; sep]))
end

for i in 1:n - 1
sep = seperator[i]
objects[n + i] = induced_subgraph(sgraph, order(stree, sep))
objects[n + i] = induced_subgraph(sgraph, order(stree.ograph, sep))
end

for i in 1:n - 1
j = parentindex(stree, i)
j = parentindex(stree.tree, i)
add_edge!(graph, i, j)

sep_i = seperator[i]
Expand Down
103 changes: 11 additions & 92 deletions src/junction_trees/elimination_trees.jl
Original file line number Diff line number Diff line change
@@ -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} <: AbstractTree
struct EliminationTree{T <: AbstractTree}
tree::T # elimination tree
ograph::OrderedGraph # ordered graph
end
Expand Down Expand Up @@ -59,7 +59,7 @@ end
# Gilbert, Ng, and Peyton
# Figure 3: Implementation of algorithm to compute row and column counts.
function supcnt(etree::EliminationTree)
order = postorder(etree)
order = postorder(etree.tree)
index = inverse(order)
rc, cc = supcnt(EliminationTree{PostorderTree}(etree, order))
rc[index], cc[index]
Expand All @@ -70,7 +70,7 @@ end
# Gilbert, Ng, and Peyton
# Figure 3: Implementation of algorithm to compute row and column counts.
function supcnt(etree::EliminationTree{PostorderTree})
n = length(etree)
n = length(etree.tree)

#### Disjoint Set Union ####

Expand All @@ -96,22 +96,22 @@ function supcnt(etree::EliminationTree{PostorderTree})
wt = ones(Int, n)

for u in 1:n - 1
wt[parentindex(etree, u)] = 0
wt[parentindex(etree.tree, u)] = 0
end

for p in 1:n - 1
wt[parentindex(etree, p)] -= 1
wt[parentindex(etree.tree, p)] -= 1

for u in outneighbors(etree, p)
if firstdescendant(etree, p) > prev_nbr[u]
for u in outneighbors(etree.ograph, p)
if firstdescendant(etree.tree, p) > prev_nbr[u]
wt[p] += 1
pp = prev_p[u]

if iszero(pp)
rc[u] += level(etree, p) - level(etree, u)
rc[u] += level(etree.tree, p) - level(etree.tree, u)
else
q = find(pp)
rc[u] += level(etree, p) - level(etree, q)
rc[u] += level(etree.tree, p) - level(etree.tree, q)
wt[q] -= 1
end

Expand All @@ -121,13 +121,13 @@ function supcnt(etree::EliminationTree{PostorderTree})
prev_nbr[u] = p
end

union(p, parentindex(etree, p))
union(p, parentindex(etree.tree, p))
end

cc = wt

for v in 1:n - 1
cc[parentindex(etree, v)] += cc[v]
cc[parentindex(etree.tree, v)] += cc[v]
end

rc, cc
Expand All @@ -140,84 +140,3 @@ function outdegrees(etree::EliminationTree)
rc, cc = supcnt(etree)
cc .- 1
end


# Get the number of nodes in T.
function Base.length(etree::EliminationTree)
length(etree.tree)
end


# Get the level of a node i.
function level(etree::EliminationTree, i::Integer)
level(etree.tree, i)
end


# Get the first descendant of a node i.
function firstdescendant(etree::EliminationTree, i::Integer)
firstdescendant(etree.tree, i)
end


# Determine whether a vertex i is a descendant of a node j.
function isdescendant(etree::EliminationTree, i::Integer, j::Integer)
isdescendant(etree.tree, i, j)
end


# Get the vertex σ(i).
function order(etree::EliminationTree, i)
order(etree.ograph, i)
end


# Get the index σ⁻¹(v),
function inverse(etree::EliminationTree, i)
inverse(etree.ograph, i)
end


##########################
# Indexed Tree Interface #
##########################


function AbstractTrees.rootindex(etree::EliminationTree)
rootindex(etree.tree)
end


function AbstractTrees.parentindex(etree::EliminationTree, i::Integer)
parentindex(etree.tree, i)
end


function AbstractTrees.childindices(etree::EliminationTree, i::Integer)
childindices(etree.tree, i)
end


function AbstractTrees.NodeType(::Type{IndexNode{EliminationTree, Int}})
HasNodeType()
end


function AbstractTrees.nodetype(::Type{IndexNode{EliminationTree{T}, Int}}) where T
IndexNode{EliminationTree{T}, Int}
end


############################
# Abstract Graph Interface #
############################


function BasicGraphs.inneighbors(etree::EliminationTree, i::Integer)
inneighbors(etree.ograph, i)
end


function BasicGraphs.outneighbors(etree::EliminationTree, i::Integer)
outneighbors(etree.ograph, i)
end
66 changes: 6 additions & 60 deletions src/junction_trees/supernode_trees.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# An ordered graph (G, σ) equipped with a supernodal elimination tree T.
struct SupernodeTree <: AbstractTree
struct SupernodeTree
tree::PostorderTree # supernodal elimination tree
ograph::OrderedGraph # ordered graph
representative::Vector{Int} # representative vertex
Expand Down Expand Up @@ -57,7 +57,7 @@ end
# Vanderberghe and Andersen
# Algorithm 4.1: Maximal supernodes and supernodal elimination tree.
function stree(etree::EliminationTree, degree::AbstractVector, stype::SupernodeType)
n = length(etree)
n = length(etree.tree)
index = zeros(Int, n)
snd = Vector{Int}[]
q = Int[]
Expand All @@ -78,7 +78,7 @@ function stree(etree::EliminationTree, degree::AbstractVector, stype::SupernodeT
push!(snd[i], v)
end

for w in childindices(etree, v)
for w in childindices(etree.tree, v)
if w !== ww
j = index[w]
q[j] = i
Expand All @@ -94,7 +94,7 @@ end
# Construct an elimination graph.
function eliminationgraph(stree::SupernodeTree)
ograph = deepcopy(stree.ograph)
n = length(stree)
n = length(stree.tree)

for i in 1:n - 1
for u in supernode(stree, i)[1:end - 1]
Expand All @@ -108,7 +108,7 @@ function eliminationgraph(stree::SupernodeTree)
end

u = last(supernode(stree, i))
v = first(supernode(stree, parentindex(stree, i)))
v = first(supernode(stree, parentindex(stree.tree, i)))

for w in outneighbors(ograph, u)
if v < w
Expand Down Expand Up @@ -137,7 +137,7 @@ end

# Compute the (unsorted) seperators of every node in T.
function seperators(stree::SupernodeTree)
n = length(stree)
n = length(stree.tree)
seperator = Vector{Vector{Int}}(undef, n)
ograph = eliminationgraph(stree)

Expand All @@ -149,57 +149,3 @@ function seperators(stree::SupernodeTree)

seperator
end


# Get the number of nodes in T.
function Base.length(stree::SupernodeTree)
length(stree.tree)
end


# Get the level of node i.
function level(stree::SupernodeTree, i)
level(stree.tree, i)
end


# Get the vertex σ(i).
function order(stree::SupernodeTree, i)
order(stree.ograph, i)
end


# Get the index σ⁻¹(v),
function inverse(stree::SupernodeTree, i)
inverse(stree.ograph, i)
end


##########################
# Indexed Tree Interface #
##########################


function AbstractTrees.rootindex(stree::SupernodeTree)
rootindex(stree.tree)
end


function AbstractTrees.parentindex(stree::SupernodeTree, i)
parentindex(stree.tree, i)
end


function AbstractTrees.childindices(stree::SupernodeTree, i)
childindices(stree.tree, i)
end


function AbstractTrees.NodeType(::Type{IndexNode{SupernodeTree, Int}})
HasNodeType()
end


function AbstractTrees.nodetype(::Type{IndexNode{SupernodeTree, Int}})
IndexNode{SupernodeTree, Int}
end
4 changes: 2 additions & 2 deletions src/junction_trees/supernode_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function findchild(etree::EliminationTree, degree::AbstractVector, stype::Node,
# v ∈ snd(w).
# If no such child exists, return nothing.
function findchild(etree::EliminationTree, degree::AbstractVector, stype::Maximal, v::Integer)
for w in childindices(etree, v)
for w in childindices(etree.tree, v)
if degree[w] == degree[v] + 1
return w
end
Expand All @@ -55,7 +55,7 @@ end
# v ∈ snd(w).
# If no such child exists, return nothing.
function findchild(etree::EliminationTree, degree::AbstractVector, stype::Fundamental, v::Integer)
ws = childindices(etree, v)
ws = childindices(etree.tree, v)

if length(ws) == 1
w = only(ws)
Expand Down

0 comments on commit a96feba

Please sign in to comment.