Repository navigation
Refactor tree_isomorphism to improve code reuse and readability - #7929
Conversation
| # Group nodes by their distance from the root | ||
| L = defaultdict(list) | ||
| for n, dist in nx.shortest_path_length(dT, source=0).items(): | ||
| L[dist].append(n) |
There was a problem hiding this comment.
No change needed: one potential optimization here could be using a list. You can ensure all values from [0, max_dist] are present (bc if there is a node at distance D (D>1), there has to be a node at distance D-1).
Could be something like this:
L = []
for n, dist in nx.shortest_path_length(dT, source=0).items():
while dist + 1 > len(L):
L.append([])
L[dist].append(n)
There was a problem hiding this comment.
@amcandio check out defaultdict - a very convenient data structure that is better suited for this task!
There was a problem hiding this comment.
Yes, I know defaultdict and it is nicer here for sure!
Was just suggesting a potential optimization because the list will consume less memory and will probably be faster https://www.geeksforgeeks.org/python-memory-consumption-dictionary-vs-list-of-tuples/.
I lack the context on how much significant this is in the overall performance so it might be just a micro-optimization
There was a problem hiding this comment.
Another option is to simply do L = list(nx.bfs_layers(dT, 0)). I don't know if it's faster, but it's a one-liner, and L is a list instead of a defaultdict, which makes more sense to me from a naming perspective. No other changes should be needed; L is simply indexed into by integer index, so it operates the same whether a dict or list.
There was a problem hiding this comment.
Erik's suggestion makes sense to me as well - if anything, if there's a performance difference, we should look into adapting nx.bfs_layers.
dschult
left a comment
There was a problem hiding this comment.
These look like good improvements to me.
There is a utility function called groups in networkx/utils/misc.py which groups a dict's keys by their values. But it produces a defaultdict(set). So the values of the resulting dict are sets instead of lists. I'm not sure that it would work here. And I think the current code may be more readable than making users go look up a utility function. See what you think: L = nx.utils.groups(nx.shortest_path_length(dT, source=0))
I approve this PR either way. Thanks @rossbar
eriknw
left a comment
There was a problem hiding this comment.
LGTM! I left a couple nits; take or leave 'em.
| # Group nodes by their distance from the root | ||
| L = defaultdict(list) | ||
| for n, dist in nx.shortest_path_length(dT, source=0).items(): | ||
| L[dist].append(n) |
There was a problem hiding this comment.
Another option is to simply do L = list(nx.bfs_layers(dT, 0)). I don't know if it's faster, but it's a one-liner, and L is a list instead of a defaultdict, which makes more sense to me from a naming perspective. No other changes should be needed; L is simply indexed into by integer index, so it operates the same whether a dict or list.
|
|
||
| # collect nodes into a dict by level | ||
| L = group_by_levels(levels) | ||
| h = max(L) |
There was a problem hiding this comment.
By construction, could we also do h = len(L) - 1?
A minor refactor to improve code-reuse.
- Replace assign_levels with shortest_path_length - Replace group_by_levels with defaultdict(list) - Combine two separate functions into one processing step.
cc389f9 to
26cad2c
Compare
Peiffap
left a comment
There was a problem hiding this comment.
I like how much simpler the refactored code looks!
This also has me thinking about whether it makes sense to add an is_tree argument to functions like bfs_layers to optimize for the case where the user knows the graph is a tree. Probably not, because I don't think the current data structures make that easy in the general case, but I figured it's worth putting it out there.
| # Group nodes by their distance from the root | ||
| L = defaultdict(list) | ||
| for n, dist in nx.shortest_path_length(dT, source=0).items(): | ||
| L[dist].append(n) |
There was a problem hiding this comment.
Erik's suggestion makes sense to me as well - if anything, if there's a performance difference, we should look into adapting nx.bfs_layers.
A few more minor improvements while working my way through the tree_isomorphism module (fun!)
There are two main bits in this PR where I've attempted to replace custom code with built-in functions:
tree_isomorphismessentially implements thefaster_could_be_isomorphiccheck before it actually begins searching for tree isomorphisms. I've replaced it with an explicit call tofaster_could_be_isomorphic.rooted_tree_isomorphismrelied on two helper functionsassign_levelsandgroup_by_levelsto set up the algorithm. Upon closer inspection, I think these two functions are equivalent to grouping nodes by their shortest path length to the root node - so I've replaced them with the 3-liner to do this grouping (usingdefaultdict(list)). Technically these functions are "public", but they are so specialized here that I doubt they see heavy use in user-code. In other words, I'm hoping we can get away with simply removing them without a deprecation cycle... however if we want to be safe I'm happy to do so!