Graphs.jl icon indicating copy to clipboard operation
Graphs.jl copied to clipboard

Add regular tree generator

Open stecrotti opened this issue 3 years ago • 12 comments

Generator for a regular tree, also known as Bethe Lattice or Cayley Tree, which generalizes a binary tree to arbitrary degree.

I tried to keep the code as close as possible to the one for binary_tree.

stecrotti avatar Nov 23 '22 15:11 stecrotti

Codecov Report

Merging #197 (b26cf91) into master (ea6bcfe) will decrease coverage by 0.13%. The diff coverage is 92.85%.

@@            Coverage Diff             @@
##           master     #197      +/-   ##
==========================================
- Coverage   97.40%   97.26%   -0.14%     
==========================================
  Files         109      109              
  Lines        6468     6506      +38     
==========================================
+ Hits         6300     6328      +28     
- Misses        168      178      +10     

codecov[bot] avatar Nov 24 '22 07:11 codecov[bot]

By the way I called it 'regular tree' because of the analogy with regular graphs but that might not be too accurate because:

  1. A regular tree is not a regular graph (because of the leaves)
  2. z-regular graphs have nodes of degree z while z-ary trees have nodes of degree z+1, potentially confusing

That said, mary_tree(k, m), zary_tree(k, z) don't look great to me.

stecrotti avatar Nov 24 '22 23:11 stecrotti

Changes:

  • New method with type regular_tree(T::Type{<:Integer}, k::Integer, z::Integer)
  • Improved docs with correct wiki page
  • InexactError instead of DomainError when number of vertices is not representable
  • Assert z > 0, fallback to a path graph in case z = 1
  • Appropriate conversions to the provided type T, checked by test

stecrotti avatar Nov 30 '22 09:11 stecrotti

By the way I called it 'regular tree' because of the analogy with regular graphs but that might not be too accurate because:

1. A regular tree is not a regular graph (because of the leaves)

2. z-regular graphs have nodes of degree z while z-ary trees have nodes of degree z+1, potentially confusing

That said, mary_tree(k, m), zary_tree(k, z) don't look great to me.

I think regular tree is perfectly fine. For example here, the same term is used: https://www.wolframalpha.com/examples/mathematics/discrete-mathematics/graph-theory/regular-trees/

simonschoelly avatar Nov 30 '22 11:11 simonschoelly

I just realized that a $k$-ary tree is not the same as a Cayley tree of connectivity $k$ because in a $k$-ary tree, the root has connectivity $k$, and all the nodes excepting those in the leaves have connectivity $k+1$. Look, for instance, at the attached plot, nodes 2,3 and 4 have connectivity four and not three as it would be the case for a Cayley tree

Bildschirm­foto 2022-12-14 um 09 31 30

dapias avatar Dec 14 '22 08:12 dapias

Agree! Indeed it was corrected and should be ok in the current version of this PR

stecrotti avatar Dec 14 '22 12:12 stecrotti

Is there a function to create an actual Cayley tree?

dapias avatar Dec 15 '22 07:12 dapias

Not that i know of

stecrotti avatar Dec 15 '22 14:12 stecrotti

I am not an expert, but it seems trivial to be implemented, given the actual code for the tree. After setting up the central node (root), one only needs to demand that each child has $(z-1)$ children instead of $z$

dapias avatar Dec 16 '22 09:12 dapias

Changed the signature to match the rest of the file as suggested by @aurorarossi, but kept only one docstring.

Fixed potential overflow as suggested by @simonschoelly.

stecrotti avatar Dec 19 '22 10:12 stecrotti

There is this one comment from me, about k >= 0 - not sure if you have seen it, but feel free to ignore that it you think we should not change anything there.

Otherwise looks good for me - I did not check the math, but choose to believe the tests.

simonschoelly avatar Dec 20 '22 00:12 simonschoelly

Sure, i replied under your review comment:

I stuck to what was used in the rest of the file which is that for k<=0 it returns a SimpleGraph(zero(T)). I guess there was a good reason for that? If not it makes total sense to error if k<0.

stecrotti avatar Dec 20 '22 09:12 stecrotti