Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetworkX: Subgraph Isomorphism by edge and node attributes

Suppose I have 2 graphs A and B and I want to know if A is a subgraph of B. The nodes contain attributes, say, 'size' and 'material'.

When I run:

GM = networkx.algorithms.isomorphism.GraphMatcher(B,A)
print networkx.algorithms.isomorphism.subgraph_is_isomorphic()

This only matches graph by edges only and not by edges and attribute.

Any clue on how check attributes?

Also, suppose B contains 2 connected graphs of A.

When I run:

GM.mapping

This will output only 1 of the subgraphs of A. Any idea on how to output every subgraph?

like image 541
drum Avatar asked Mar 27 '13 23:03

drum


Video Answer


2 Answers

I've solved this by using:

print GM = iso.GraphMatcher(B,A,node_match=iso.categorical_node_match(['material', 'size'],['metal',1]))

What I didn't know before is that ['metal',1] is just a default and not a hard match.

like image 71
drum Avatar answered Sep 28 '22 00:09

drum


You can iterate over all possible subgraphs in the following way

GM = networkx.algorithms.isomorphism.GraphMatcher(B,A)
for subgraph in GM.subgraph_isomorphisms_iter():
    print subgraph

subgraph in this example is a dictionary that maps nodes of B to nodes of A.

For the question of attribute matching, drum's suggestion has worked for me. Additional attribute matching actually speeds up things significantly for large graphs.

like image 23
user48668 Avatar answered Sep 28 '22 00:09

user48668