Created
March 2, 2015 21:14
-
-
Save chebee7i/e30158ba1cdbd6d8c538 to your computer and use it in GitHub Desktop.
Using GraphMatcher to find a subgraph.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example demonstrating how to modify the GraphMatcher class so that | |
subgraph_is_isomorphic() will determine whether G2 is a subgraph (with | |
an identity isomorphism). | |
""" | |
import networkx as nx | |
G = nx.karate_club_graph() | |
# Grab a subgraph. | |
subG_good = G.subgraph(range(10)) | |
# Make an isomorphic subgraph. | |
subG_bad = nx.relabel_nodes(subG_good, dict(zip(range(10), 'abcdefghij'))) | |
class GraphMatcher(nx.algorithms.isomorphism.GraphMatcher): | |
def semantic_feasibility(self, G1_node, G2_node): | |
return G1_node == G2_node | |
gm = GraphMatcher(G, subG_good) | |
print gm.subgraph_is_isomorphic() # True | |
gm = GraphMatcher(G, subG_bad) | |
print gm.subgraph_is_isomorphic() # False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment