Skip to content

Add tree isomorphism - #4067

Merged
jarrodmillman merged 12 commits into
networkx:masterfrom
craigschmidt:tree-isomorphism
Aug 5, 2020
Merged

jarrodmillman merged 12 commits into
networkx:masterfrom
craigschmidt:tree-isomorphism

Conversation

@craigschmidt

Copy link
Copy Markdown
Contributor

Add an algorithm for finding if two undirected trees are isomorphic,
and if so returns an isomorphism between the two sets of nodes.

This algorithm uses a routine to tell if two rooted trees (trees with a
specified root node) are isomorphic, which may be independently useful.

Includes tests with 100% coverage that show correctness for all trees up to a certain size (assuming the nx.nonisomorphic_trees routine is correct)

@pep8speaks

pep8speaks commented Jul 11, 2020 •

Copy link
Copy Markdown

Hello @craigschmidt! Thanks for updating this PR.

Line 6:1: F403 'from networkx.algorithms.isomorphism.tree_isomorphism import ' used; unable to detect undefined names
Line 6:1: F401 'networkx.algorithms.isomorphism.tree_isomorphism.
' imported but unused

Please install and run psf/black.

Comment last updated at 2020-08-03 22:17:59 UTC

@dschult

dschult commented Jul 12, 2020

Copy link
Copy Markdown
Member

Thanks for this! I have only scanned through the code quickly at this point. Here are some comments. If you're up for learning a little more about how the package is set up, take a stab at fixing them. Otherwise I'll get to it.

Rename the module to tree_isomorph.py to match the other modules' naming system. (similar for test file)

Add a definition of the variable __all__ just after your imports. It should equal a list of all functions you want added to the networkx package when importing. Look at some nearby modules for examples.

Hook into the namespace: Add an entry to algorithms/isomorphism/__init_.py which imports your module into the isomorphisms module (and thus into the main networkx namespace). Mimic the other entries there.

Check for yourself that importing networkx from your repository allows access to your functions.

Hook your tests into the documentation system by making an entry in doc/reference/algorithms/isomorphism.rst that mimics the entries there already. I think a subsection for tree isomorphisms is reasonable and it should include the functions you "make public" in the __all__ variable above.

You should install and run "black" (also known as psf/black) on your two modules -- this is a recent addition to networkx and we hope to unify the style and spacing. Right now the PEP8speaks tells you what needs fixing. But black ought to fix it for you.

Thanks again!

@craigschmidt

Copy link
Copy Markdown
Contributor Author

@dschult
Hi Dan, I incorporated the changes you suggested, and ran the code through black. I wasn't entirely sure how to use the development version in other code, so I didn't verify that from networkx import * allows the two public facing functions.

@dschult

dschult commented Aug 3, 2020

Copy link
Copy Markdown
Member

This is ready to be merged. :}

@jarrodmillman
jarrodmillman merged commit 123fc2b into networkx:master Aug 5, 2020
jarrodmillman added a commit to mbruhns/networkx that referenced this pull request Aug 11, 2020
* upstream/master:
  Update apt-get on circleci image (networkx#4147)
  MAINT: fixups to parse_edgelist. (networkx#4128)
  Use dict instead of OrderedDict since dict is ordered by default from Python 3.6. (networkx#4145)
  MAINT: Switch to abc-based isinstance checks in to_networkx_graph (networkx#4136)
  Add tree isomorphism (networkx#4067)
  Updated draw_networkx to accept numpy array for edgelist (networkx#4132)
  CI: temporary fix for CI latex installation issues (networkx#4131)
  Fix parse_edgelist behavior with multiple attributes (networkx#4125)
jarrodmillman pushed a commit that referenced this pull request Dec 15, 2020
* Simple maxcut heuristics (#2)

* Simple randomized maxcut heuristic

* Remove approximation guarantee test

Test is not expected to pass all the time due to randomness.

* Preserve random state for cut heuristic

* Add one exchange max cut heuristic

* Fixes && Refactoring && Default deterministic output

* maxcut/simple.py: Add docstring and make initial_cut optional.

* maxcut/simple.py: Add shuffle again. Make initial set a set.

Co-authored-by: Jonas Charfreitag <[email protected]>

* Fix parse_edgelist behavior with multiple attributes (#4125)

* Fix parse_edgelist behavior with multiple attributes

* fixed test case

Co-authored-by: chris <chris@orchid>

* CI: temporary fix for CI latex installation issues (#4131)

* Updated draw_networkx to accept numpy array for edgelist (#4132)

* updated draw_networkx + added test

* added newline

* skip test if numpy is not installed

* change skip if numpy is not available

* switch elif to if

* Add tree isomorphism (#4067)

* add code for tree isomorphism, with tests

* fix typo in comment

* one more typo in comments

* fix some PEP8 formatting, that flake8 didn't care about

* rename files as tree_isomorphism

* run code through black formatter

* incorporate feedback from dschult in PR4067

* fix missing import for not_implemented_for decorator

* swap edge order randomly in testing routine positive_single_tree

* run black on test_tree_isomorphism.py

* spacing tweak to allow CI test of docs

Co-authored-by: Dan Schult <[email protected]>

* maxcut/simple.py: Updated documentation and some names.

* maxcut/simple.py (Tests): Add _is_valid_cut function and make use of it.

* Move maxcut code to approximation dir.

* maxcut.py: Add not implemented for decorator to maxcut functions.

* test_maxcut.py: Testcase for global optimality.

* maxcut: Fix line width to 88.

* Maxcut: Add __all__ to maxcut.py.

* Maxcut: Use networkx style seed for functions.

* Maxcut: Move to fully soft dependence on numpy.

* Maxcut: Bugfix. Replace choice with sample in test.

* Remove unnecessary set constructor

* Add Max Cut reference in docs

* Fix seed in Max Cut tests

* Add negative weight test for Max Cut

* Improve docstrings

* Fix formatting

* Fix formatting for test file

* Fix formatting for test file

* Docstring formatting touchups.

* Call graph generator from top-level namespace.

* Update networkx/algorithms/approximation/maxcut.py

Docstring type error.

Co-authored-by: Ross Barnowski <[email protected]>

* Update networkx/algorithms/approximation/maxcut.py

Use compact set construction in randomized_partitioning.

Co-authored-by: Ross Barnowski <[email protected]>

* Update networkx/algorithms/approximation/maxcut.py

Use compact set construction in _swap_node_partition.

Co-authored-by: Ross Barnowski <[email protected]>

* Update networkx/algorithms/approximation/tests/test_maxcut.py

Remove comments with no additional information.

Co-authored-by: Ross Barnowski <[email protected]>

* Change cut_value to scalar.

* test_maxcut: Better naming for import.

* maxcut.py: Remove one whitespace.

* Update networkx/algorithms/approximation/tests/test_maxcut.py

Co-authored-by: Ross Barnowski <[email protected]>

* Add comment to why shuffling nodes in one exchange has some benefit.

Co-authored-by: Mohammed Ghannam <[email protected]>
Co-authored-by: Christoph Martin <[email protected]>
Co-authored-by: chris <chris@orchid>
Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Tanguy Fardet <[email protected]>
Co-authored-by: Craig Schmidt <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Mohammed Ghannam <[email protected]>
MridulS pushed a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
* add code for tree isomorphism, with tests

* fix typo in comment

* one more typo in comments

* fix some PEP8 formatting, that flake8 didn't care about

* rename files as tree_isomorphism

* run code through black formatter

* incorporate feedback from dschult in PR4067

* fix missing import for not_implemented_for decorator

* swap edge order randomly in testing routine positive_single_tree

* run black on test_tree_isomorphism.py

* spacing tweak to allow CI test of docs

Co-authored-by: Dan Schult <[email protected]>
MridulS pushed a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
* Simple maxcut heuristics (#2)

* Simple randomized maxcut heuristic

* Remove approximation guarantee test

Test is not expected to pass all the time due to randomness.

* Preserve random state for cut heuristic

* Add one exchange max cut heuristic

* Fixes && Refactoring && Default deterministic output

* maxcut/simple.py: Add docstring and make initial_cut optional.

* maxcut/simple.py: Add shuffle again. Make initial set a set.

Co-authored-by: Jonas Charfreitag <[email protected]>

* Fix parse_edgelist behavior with multiple attributes (networkx#4125)

* Fix parse_edgelist behavior with multiple attributes

* fixed test case

Co-authored-by: chris <chris@orchid>

* CI: temporary fix for CI latex installation issues (networkx#4131)

* Updated draw_networkx to accept numpy array for edgelist (networkx#4132)

* updated draw_networkx + added test

* added newline

* skip test if numpy is not installed

* change skip if numpy is not available

* switch elif to if

* Add tree isomorphism (networkx#4067)

* add code for tree isomorphism, with tests

* fix typo in comment

* one more typo in comments

* fix some PEP8 formatting, that flake8 didn't care about

* rename files as tree_isomorphism

* run code through black formatter

* incorporate feedback from dschult in PR4067

* fix missing import for not_implemented_for decorator

* swap edge order randomly in testing routine positive_single_tree

* run black on test_tree_isomorphism.py

* spacing tweak to allow CI test of docs

Co-authored-by: Dan Schult <[email protected]>

* maxcut/simple.py: Updated documentation and some names.

* maxcut/simple.py (Tests): Add _is_valid_cut function and make use of it.

* Move maxcut code to approximation dir.

* maxcut.py: Add not implemented for decorator to maxcut functions.

* test_maxcut.py: Testcase for global optimality.

* maxcut: Fix line width to 88.

* Maxcut: Add __all__ to maxcut.py.

* Maxcut: Use networkx style seed for functions.

* Maxcut: Move to fully soft dependence on numpy.

* Maxcut: Bugfix. Replace choice with sample in test.

* Remove unnecessary set constructor

* Add Max Cut reference in docs

* Fix seed in Max Cut tests

* Add negative weight test for Max Cut

* Improve docstrings

* Fix formatting

* Fix formatting for test file

* Fix formatting for test file

* Docstring formatting touchups.

* Call graph generator from top-level namespace.

* Update networkx/algorithms/approximation/maxcut.py

Docstring type error.

Co-authored-by: Ross Barnowski <[email protected]>

* Update networkx/algorithms/approximation/maxcut.py

Use compact set construction in randomized_partitioning.

Co-authored-by: Ross Barnowski <[email protected]>

* Update networkx/algorithms/approximation/maxcut.py

Use compact set construction in _swap_node_partition.

Co-authored-by: Ross Barnowski <[email protected]>

* Update networkx/algorithms/approximation/tests/test_maxcut.py

Remove comments with no additional information.

Co-authored-by: Ross Barnowski <[email protected]>

* Change cut_value to scalar.

* test_maxcut: Better naming for import.

* maxcut.py: Remove one whitespace.

* Update networkx/algorithms/approximation/tests/test_maxcut.py

Co-authored-by: Ross Barnowski <[email protected]>

* Add comment to why shuffling nodes in one exchange has some benefit.

Co-authored-by: Mohammed Ghannam <[email protected]>
Co-authored-by: Christoph Martin <[email protected]>
Co-authored-by: chris <chris@orchid>
Co-authored-by: Ross Barnowski <[email protected]>
Co-authored-by: Tanguy Fardet <[email protected]>
Co-authored-by: Craig Schmidt <[email protected]>
Co-authored-by: Dan Schult <[email protected]>
Co-authored-by: Mohammed Ghannam <[email protected]>
@rossbar rossbar mentioned this pull request Mar 16, 2026
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.

5 participants