Skip to content

Fix for filtered MultiGraph views from edge_subgraph (#7724). - #7729

Merged
Schefflera-Arboricola merged 2 commits into
networkx:mainfrom
nelsonaloysio:fix-#7724
Nov 21, 2024
Merged

Schefflera-Arboricola merged 2 commits into
networkx:mainfrom
nelsonaloysio:fix-#7724

Conversation

@nelsonaloysio

Copy link
Copy Markdown
Contributor

Fixes #7724: MultiGraph views return inconsistent has_edge results.


Minimum working example:

import networkx as nx

G = nx.MultiDiGraph()

G.add_edges_from([("a", "b"),
                  ("a", "c"),       # <-- to be filtered out
                  ("c", "b")])

H = nx.edge_subgraph(G, [("a", "b", 0), ("c", "b", 0)])

print(f"{H.edges()}\n\n"
      f"{H.has_edge('a', 'c')} \t H.has_edge('a', 'c')\n"
      f"{('a', 'c') in H.edges()} \t ('a', 'c') in H.edges()\n\n"
      f"{'c' in H['a']} \t 'c' in H['a']\n"
      f"{'c' in list(H['a'])} \t 'c' in list(H['a'])")

Output before the PR:

# [('a', 'b'), ('b', 'c')]
#
# True      H.has_edge('a', 'c')    # <-- inconsistent result
# False     ('a', 'c') in H.edges()
#
# True      'c' in H['a']           # <-- inconsistent result
# False     'c' in list(H['a'])

Output after the PR:

# [('a', 'b'), ('b', 'c')]

# False      H.has_edge('a', 'c')
# False      ('a', 'c') in H.edges()

# False      'c' in H['a']
# False      'c' in list(H['a'])

@dschult

dschult commented Nov 21, 2024

Copy link
Copy Markdown
Member

Great work!
Could you add a test method or two to the tests/test_subgraphviews.py test class for edge_subgraph views? Can be added at the very end of the file. The test should check the examples you came up with that fail before the fix and now pass. I think all you'd need is the code up to the first "print" in your initial code quotation. Then assert 'c' not in H['a'] and assert not H.has_edge('a', 'c'). And put a comment at the start of the test method with something like: # see gh-7724.

Thanks!!

@nelsonaloysio

Copy link
Copy Markdown
Contributor Author

Done! Added a new test method to TestEdgeSubGraph. Let me know in case anything should be changed. Thanks!

@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.

This looks good to me!
Thanks @nelsonaloysio !

@Schefflera-Arboricola
Schefflera-Arboricola merged commit f21f284 into networkx:main Nov 21, 2024
@jarrodmillman jarrodmillman added this to the 3.5 milestone Nov 21, 2024
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.

MultiGraph views return inconsistent has_edge results

4 participants