aplbrain / grandiso-networkx

Performant, pure-Python subgraph isomorphism and monomorphism search (aka "motif search")
Apache License 2.0
52 stars 10 forks source link

subgraph Isomorphism by edge and node labels #35

Closed Matt-81 closed 1 year ago

Matt-81 commented 1 year ago

Thank you for this very useful library, we will definitely consider it for our future research. In our specific case, we would need to count subgraph isomorphisms by edge and node labels. E.g., given a source directed graph with nodes n0, n1, and n2 with labels 'class', 'sublcass' and 'subclass' respectively, and with edges, n1->n0, n2->n0 both with labels 'gen', we would like to count the exact matches in a given target graph.

Is there an option to do this? Thank you!

j6k4m8 commented 1 year ago

Hi @Matt-81! If you know the attributes ahead of time, you can just assign them to your motif queries like this:

import networkx as nx
from grandiso import find_motifs

motif = nx.DiGraph()
motif.add_edge("n1", "n0", label="gen")
motif.add_edge("n2", "n0", label="gen")
motif.add_node("n0", label="class")
motif.add_node("n1", label="subclass")
motif.add_node("n2", label="subclass")

find_motifs(motif, G)

As long as those attributes exist on the host graph as well, grandiso.find_motifs will automatically restrict to these attributes!

Does that solve your problem?

Matt-81 commented 1 year ago

Hi @j6k4m8! It works great, thank you!

j6k4m8 commented 1 year ago

Super awesome! Closing for now, feel free to reopen or start a new one if you have more questions!

Matt-81 commented 1 year ago

Definitely! I'd also like to update you on how we're going to use it, if you're interested. Keep you posted :) Thanks again

j6k4m8 commented 1 year ago

@Matt-81 I'd love that!! Please do :)