Repository navigation
Implement the algorithm to find the centroid(s) of a tree - #8089
Conversation
|
Does it make sense to have this method discoverable from the tree module too? |
Quite possibly. |
|
This looks like the tree module should house this code. We can/should add "see also" links to it from the center-related code docstrings. But let's give it a new section heading in The failed tests are not related to your PR. This afternoon they started failing due to a change in the drawing test software. So look to see if any of your stuff is causing an error -- but right now they are failures in the drawing tests. Just in time for your PR. :) Thanks! |
|
Ok, I've moved the algorithm and the tests over to I might want a bit more guidance on what any cross-reference from the center-related docs ought to look like, though. |
|
I think Dan just means you should go through the docstrings for the functions related to the center, and add to the "See Also" section where appropriate. At least, if my link is correct... The same thing we do in a few other places where functions in different parts of the code base are related; for example: networkx/networkx/algorithms/centrality/eigenvector.py Lines 96 to 100 in d1c41f8 |
|
Just as an aside, why doesn't edit: Never mind, I see that the centrality functions mostly return dicts of measures instead of just a bare list of node labels. |
|
All right, I've updated the |
No. The center described there is maximizing the eccentricity. A tree centroid is the point or points that have minimum "weight" (not to be confused with edge weights -- this weight is just the number of nodes accessible down each branch). Let me see if I can cook up an example where they legit have different results... |
In that case then I think we need to expand the test suite - the current tests give the same results for every case if you replace |
|
A path graph with a star at one end: The long branch doesn't contribute enough weight to throw off the centroid from the middle of the star. But the graph center wants to be close to the middle of the long path. |
|
Test added. |
|
Centrality creates measures of how central each node is. Distance measures are typically trying to find single number measures of networks -- like diameter or center. Centrality tries to find the key/important nodes. Center is trying to find the geographic middle (of a non-positioned object :) |
|
All right, I think I got all of the comments. I even made a stab at explaining the difference between centroid and center, but my writing always feels a bit clumsy. |
Peiffap
left a comment
There was a problem hiding this comment.
I might be getting lost in the sauce a little here, but some more comments.
- Perhaps most importantly: isn't the tree centroid the same thing as the tree barycenter? If I understand correctly, the centroid is (roughly)
$\arg\min_{u \in T} \max_{C \in T \setminus \{u\}} |C|$ , i.e. the node that minimizes the size of the largest component upon being removed from$T$ . The barycenter is$\arg \min_{u \in T} \sum_{v \in T} d_T(u, v)$ , the node that minimizes the sum of its distances to all nodes. I don't have time to look for a proof right now, but empirically replacing the body ofnx.tree_centroidbynx.barycenter(T)seems to work (except for the exception tests, which are really only for internal consistency. If so, maybe something similar to what was done in #7946 can be done here? - Can this easily be extended to work with weighted graphs? If not, we should probably mention that somewhere.
I strongly suspect that this is not the case, though it isn't as easy for me to think up a counter-example as it was for center vs centroid.
I don't think so given the definitions I've read, though I admit I'm not conversant with the mathematical literature on the subject. |
|
Hm. Time to break out the Knuth. |
|
A bit of ambiguous argument: I've been trying systematically generated small trees (I think I got up to 9 nodes before it was taking too long for my taste) and a variety of larger randomly generated trees. I have yet to find any that have a divergent result between these two functions. But! This is mathematics -- I'm not comfortable just saying that they are equivalent without a formal proof. I looked up the source material mentioned in the docs as the source of the barycenter definition (D. B. West, “Introduction to Graph Theory”, Prentice Hall, 2nd ed., 2001). The tree centroid is defined (although not given a name) in 2.1.56, immediately after the section defining the barycenter (2.1.55). The centroid section does not make use of the barycenter definition, and I can find no further mention elsewhere in the book. |
|
What the heck is going on with test/dispatch (3.11)? It's throwing a bunch of |
rossbar
left a comment
There was a problem hiding this comment.
Generally LGTM - the only real decisions left in my book are big-picture questions (which don't necessarily have to be addressed in this PR).
Namely - how do we expose the functionality to users. My inclination, based on the patterns we discussed for center in #7946 is that we confine the export of this functionality to the tree namespace and rename it to barycenter to match the existing distance measure.
The main motivation for doing so in my view is that we can then remove the is_tree check from tree_centroid instead relying on the fact that it is part of the tree namespace as an indication to users that it's only relevant for trees. This pattern is used in other namespaces in the library (e.g. bipartite) and has additional advantages:
- It prevents
is_treefrom being called twice (once in barycenter, once implicitly) when users callnx.barycenteron a tree - It unifies the naming scheme for distance measures (i.e. those that apply to all graphs and those that are implementations specific to trees)
Anyways - that's the API I gravitate towards but it's not a blocker for me. If others strongly prefer the API proposed herein that's okay with me. It also doesn't necessarily have to be hammered out in this PR, we can always tweak the API in follow-ups (we're several months away from the next release).
In any case, with the addition of the asymetrical test case and the confirmation that centroid == barycenter semantically this LGTM. I approve the addition and am happy to push API munging to future PRs!
amcandio
left a comment
There was a problem hiding this comment.
Sorry, just back to this one! No further comments from me
dschult
left a comment
There was a problem hiding this comment.
I'm going to merge this centroid of a tree PR.
Thanks @jbradberry for your patience.
We've got 4 approvals and another PR working on comments and ideas stemming from this one. We can work out details of the API -- hopefully before the next release. :)
f067666 to
737a41a
Compare
Peiffap
left a comment
There was a problem hiding this comment.
Happy to leave these points to a follow-up PR (either myself, or leave it to @jbradberry), but this makes for a simple place to organize my comments. Let's make sure this gets hammered out pre-3.6 though :).
| from networkx.algorithms.tree.branchings import ArborescenceIterator | ||
| from networkx.algorithms.tree.coding import * | ||
| from networkx.algorithms.tree.decomposition import * | ||
| from networkx.algorithms.tree.distance_measures import * |
There was a problem hiding this comment.
Do we additionally want to expose this in the main namespace? I'd vote no, especially if we rename from nx.tree.tree_centroid to just nx.tree.centroid or nx.tree.barycenter. Note that #8174 already exposed specialized distance measure algorithms to the nx.tree namespace.
| :func:`~networkx.algorithms.tree.distance_measures.tree_centroid` | ||
| """ | ||
| if weight is None and attr is None and sp is None: | ||
| if nx.is_tree(G) and not G.is_directed(): |
There was a problem hiding this comment.
Do we need to check if G is undirected here? Does it change the result? If directed graphs that "correspond" to trees can also use the fast path I say we should do so.
| """ | ||
| if weight is None and attr is None and sp is None: | ||
| if nx.is_tree(G) and not G.is_directed(): | ||
| return nx.tree_centroid(G) |
There was a problem hiding this comment.
In case it doesn't get exposed in the main namespace.
| return nx.tree_centroid(G) | |
| return nx.tree.tree_centroid(G) |
| if not nx.is_tree(G): | ||
| raise nx.NotATree("provided graph is not a tree") |
There was a problem hiding this comment.
This check can be removed if we don't expose this outside the nx.tree namespace.
| def test_tree_centroid_not_a_tree(self): | ||
| G = nx.cycle_graph(3) | ||
| with pytest.raises(nx.NotATree, match=r"not a tree"): | ||
| nx.tree_centroid(G) |
There was a problem hiding this comment.
And this test will then no longer be accurate :).
|
Oh, uh oh, I see that there are some conflicts. I'll hit this up after work today and get it sorted out. |
|
No need to merge the conflicts. I'm on it. (and I'm handling the recent comments too) |
down to the Examples section
but only if barycenter isn't being invoked with optional parameters, and only if the graph is an undirected tree.
737a41a to
eb7007c
Compare
) * Implement the algorithm to find the centroid(s) of a tree * Move the new tree_centroid to networkx/algorithms/tree/ * Add a cross-reference to tree_centroid() from center() * Add a tree_centroid test that diverges from the results of center() * Addressing the review comments for tree_centroid() * Move the center vs tree_centroid example description down to the Examples section * Rename the graph parameter for tree_centroid * Addressing further review comments on tree_centroid() * Expand on the meaning of the algorithm in _subtree_sizes * Call out to tree_centroid from barycenter() but only if barycenter isn't being invoked with optional parameters, and only if the graph is an undirected tree. * fix see also links --------- Co-authored-by: Dan Schult <[email protected]>
This is a measure of centrality specific to trees that is based on balancing the number of nodes around the chosen node or nodes. I believe I have successfully implemented the algorithm with O(N) time complexity.
https://en.wikipedia.org/wiki/Tree_(graph_theory)#Properties
I waffled a bit whether to put it in the
networkx/algorithms/tree/directory, but there isn't much that it really fits with over there, and it feels like it belongs near the graphcenteralgorithm.I see that there had previously been a ticket #3525 opened, that was subsequently closed without a PR.