@@ -70,7 +70,7 @@ function layout(para::Buchheim, adj_matrix::AbstractMatrix)
70
70
end
71
71
72
72
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)
74
74
nodesize = ones (T, length (adj_list))
75
75
for i in 1 : min (length (adj_list), length (para. nodesize))
76
76
nodesize[i] = para. nodesize[i]
85
85
function parent (v, t:: Tree )
86
86
tree = t. nodes
87
87
for i in 1 : length (tree)
88
- y = findall (x -> (x == v), tree[i])
89
- if length (y) != 0
88
+ if v ∈ tree[i]
90
89
return i
91
90
end
92
91
end
@@ -264,3 +263,32 @@ function next_right(v, t::Tree)
264
263
return thread[v]
265
264
end
266
265
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 (" Buchheim assumption broken, this is not a rooted tree: Node $child has multiple parent nodes!" ))
285
+ end
286
+ end
287
+ end
288
+ if visited[1 ] != = false
289
+ throw (ArgumentError (" Buchheim assumption broken, this is not a rooted tree: Node 1 needs to be the root!" ))
290
+ end
291
+ if ! all (view (visited, 2 : lastindex (visited)))
292
+ throw (ArgumentError (" Buchheim assumption broken, this is not a rooted tree: Some nodes are not part of the tree." ))
293
+ end
294
+ end
0 commit comments