Skip to content

Commit 6c9a1f4

Browse files
committed
add checks for buchheim assumptions
1 parent 0d170f8 commit 6c9a1f4

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/buchheim.jl

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function layout(para::Buchheim, adj_matrix::AbstractMatrix)
7070
end
7171

7272
function layout(para::Buchheim{Ptype,T}, adj_list::AbstractVector) where {Ptype,T}
73-
# TODO: check if adj_list represents directed tree? julia crashes for dir graph!
73+
assert_rooted_tree(adj_list)
7474
nodesize = ones(T, length(adj_list))
7575
for i in 1:min(length(adj_list), length(para.nodesize))
7676
nodesize[i] = para.nodesize[i]
@@ -85,8 +85,7 @@ end
8585
function parent(v, t::Tree)
8686
tree = t.nodes
8787
for i in 1:length(tree)
88-
y = findall(x -> (x == v), tree[i])
89-
if length(y) != 0
88+
if v tree[i]
9089
return i
9190
end
9291
end
@@ -264,3 +263,32 @@ function next_right(v, t::Tree)
264263
return thread[v]
265264
end
266265
end
266+
267+
"""
268+
assert_rooted_tree(adj_list::AbstractVector{<:AbstractVector})
269+
270+
Check that
271+
- every node has only one parent
272+
- node 1 is head node (has no parent)
273+
- all nodes are part of the tree
274+
275+
Which are the 3 requirements for a "rooted tree" in the Buchheim paper.
276+
"""
277+
function assert_rooted_tree(adj_list::AbstractVector{<:AbstractVector})
278+
visited = [false for _ in 1:length(adj_list)]
279+
for childs in adj_list
280+
for child in childs
281+
if visited[child] == false
282+
visited[child] = true
283+
else # node was visited before
284+
throw(ArgumentError("Pathes not unique ($child has multiple parent nodes)!"))
285+
end
286+
end
287+
end
288+
if visited[1] !== false
289+
throw(ArgumentError("Node 1 needs to be the root!"))
290+
end
291+
if !all(view(visited, 2:lastindex(visited)))
292+
throw(ArgumentError("Some nodes are not part of the tree."))
293+
end
294+
end

test/runtests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,25 @@ jagmesh_adj = jagmesh()
243243
tree = [[5, 6], [6], [7, 8], [8], [], [9], [9], [], []]
244244
locs = @time Buchheim()(tree)
245245
@test typeof(locs) == Vector{Point{2,Float64}}
246+
@testset "test requirements" begin
247+
# more than one parent
248+
g = SimpleDiGraph(3)
249+
add_edge!(g, 1, 2)
250+
add_edge!(g, 1, 3)
251+
add_edge!(g, 2, 3)
252+
@test_throws ArgumentError Buchheim()(g)
253+
254+
# node 1 not parent
255+
g = SimpleDiGraph(3)
256+
add_edge!(g, 2, 1)
257+
add_edge!(g, 2, 3)
258+
@test_throws ArgumentError Buchheim()(g)
259+
260+
# not all nodes reached
261+
g = SimpleDiGraph(4)
262+
add_edge!(g, 1, 2)
263+
add_edge!(g, 2, 3)
264+
@test_throws ArgumentError Buchheim()(g)
246265
end
247266
end
248267

0 commit comments

Comments
 (0)