Skip to content

Instantly share code, notes, and snippets.

@chebee7i
Created March 2, 2015 21:14
Show Gist options
  • Save chebee7i/e30158ba1cdbd6d8c538 to your computer and use it in GitHub Desktop.
Save chebee7i/e30158ba1cdbd6d8c538 to your computer and use it in GitHub Desktop.
Using GraphMatcher to find a subgraph.
"""
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