Skip to content

Refactor tree_isomorphism to improve code reuse and readability - #7929

Merged
dschult merged 2 commits into
networkx:mainfrom
rossbar:tree-iso-code-reuse
May 18, 2025
Merged

dschult merged 2 commits into
networkx:mainfrom
rossbar:tree-iso-code-reuse

Conversation

@rossbar

@rossbar rossbar commented Mar 24, 2025

Copy link
Copy Markdown
Contributor

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:

  1. tree_isomorphism essentially implements the faster_could_be_isomorphic check before it actually begins searching for tree isomorphisms. I've replaced it with an explicit call to faster_could_be_isomorphic.
  2. rooted_tree_isomorphism relied on two helper functions assign_levels and group_by_levels to 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 (using defaultdict(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!

# 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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amcandio check out defaultdict - a very convenient data structure that is better suited for this task!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 eriknw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By construction, could we also do h = len(L) - 1?

rossbar added 2 commits April 22, 2025 20:54
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.
@rossbar
rossbar force-pushed the tree-iso-code-reuse branch from cc389f9 to 26cad2c Compare April 23, 2025 03:58

@Peiffap Peiffap left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@amcandio amcandio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dschult
dschult merged commit ffaa9ef into networkx:main May 18, 2025
@jarrodmillman jarrodmillman added this to the 3.5 milestone May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

6 participants