-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add regular tree generator #197
base: master
Are you sure you want to change the base?
Changes from 5 commits
01896fe
dac8f9a
7abb310
9a0fb29
f441197
ec15727
b26cf91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -536,6 +536,54 @@ function double_binary_tree(k::Integer) | |||||||||||||||||||||||||||||||||
return g | ||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||
regular_tree(k::Integer, z::integer) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Create a [regular tree](https://en.wikipedia.org/wiki/Bethe_lattice), | ||||||||||||||||||||||||||||||||||
also known as Bethe Lattice or Cayley Tree, | ||||||||||||||||||||||||||||||||||
of depth `k` and degree `z`. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
# Examples | ||||||||||||||||||||||||||||||||||
```jldoctest | ||||||||||||||||||||||||||||||||||
julia> regular_tree(4, 3) | ||||||||||||||||||||||||||||||||||
{40, 39} undirected simple Int64 graph | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
Comment on lines
+549
to
+551
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove it and I would put it in the docstring of the function regular_tree(k,z).
Suggested change
|
||||||||||||||||||||||||||||||||||
julia> regular_tree(5, 2) == binary_tree(5) | ||||||||||||||||||||||||||||||||||
true | ||||||||||||||||||||||||||||||||||
Comment on lines
+554
to
+556
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above.
Suggested change
|
||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||
function regular_tree(k::T, z::T) where {T<:Integer} | ||||||||||||||||||||||||||||||||||
k <= 0 && return SimpleGraph(0) | ||||||||||||||||||||||||||||||||||
k == 1 && return SimpleGraph(1) | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In these cases we return a graph of of eltype There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be completely honest, I am not sure if it is the best idea to determine the eltype of the graphs from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can have a function signature regular_tree(T::Type{<:Integer}, k::Integer, z::Integer) and then define something like regular_tree(k::Integer, z::Integer) = regular_tree(Int64, k, z) that for be similar to the function |
||||||||||||||||||||||||||||||||||
if Graphs.isbounded(k) && BigInt(z)^k - 1 > typemax(k) | ||||||||||||||||||||||||||||||||||
throw(DomainError(k, "z^k - 1 not representable by type $T")) | ||||||||||||||||||||||||||||||||||
aurorarossi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
n = T((z^k - 1) / (z - 1)) | ||||||||||||||||||||||||||||||||||
ne = Int(n - 1) | ||||||||||||||||||||||||||||||||||
fadjlist = Vector{Vector{T}}(undef, n) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
@inbounds fadjlist[1] = convert.(T, 2:(z + 1)) | ||||||||||||||||||||||||||||||||||
@inbounds for l in 2:(k - 1) | ||||||||||||||||||||||||||||||||||
w = Int((z^(l - 1) - 1) / (z - 1)) | ||||||||||||||||||||||||||||||||||
x = w + z^(l - 1) | ||||||||||||||||||||||||||||||||||
@simd for i in 1:(z^(l - 1)) | ||||||||||||||||||||||||||||||||||
j = w + i | ||||||||||||||||||||||||||||||||||
fadjlist[j] = [ | ||||||||||||||||||||||||||||||||||
ceil(T, (j - x) / z) + w | ||||||||||||||||||||||||||||||||||
convert.(T, (x + (i - 1) * z + 1):(x + i * z)) | ||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||
l = k | ||||||||||||||||||||||||||||||||||
w = Int((z^(l - 1) - 1) / (z - 1)) | ||||||||||||||||||||||||||||||||||
x = w + z^(l - 1) | ||||||||||||||||||||||||||||||||||
@inbounds @simd for j in (w + 1):x | ||||||||||||||||||||||||||||||||||
fadjlist[j] = T[ceil(Int, (j - x) / z) + w] | ||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||
return SimpleGraph(ne, fadjlist) | ||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add the docstring also to this function.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's quite often the case, that the docstrings for multiple methods of a function are only added for a single method - I think that would also be better here, than adding docstrings for both methods of this function. |
||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||
roach_graph(k) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That wikipedia article might be a bit confusing, because it talks about infinite trees in the introduction.
Maybe we can decribe that a bit more clearly here?
If I understood correctly, what you want to create is a perfect k-ary tree such as described here? https://en.wikipedia.org/wiki/M-ary_tree#Types_of_m-ary_trees