1
$\begingroup$

I have a large list of graphs where each node has a dictionary. I'd like to query that list like a database, i.e. I have a graph and want to find examples that contain it as a subgraph (and where the node's data somehow matches as well).

For example: find graphs in the list with a triangle subgraph with all node's values > 1?

toyDatabase = {Graph[{Property[1, "some_field" -> 1.4], 
      Property[2, "some_field" -> .1], 
      Property[3, "some_field" -> .2]}, {1 \[UndirectedEdge] 2, 
      2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 1, 
      2 \[UndirectedEdge] 4}], 
    Graph[{Property[1, "some_field" -> 1.1],  (* should find this one *)
      Property[2, "some_field" -> 1.2], 
      Property[3, "some_field" -> 1.3]}, {1 \[UndirectedEdge] 2, 
      2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 1, 
      2 \[UndirectedEdge] 4}]} ~ Join ~ RandomGraph[{6, 8}, 10];

queryPattern = (g_ /; SubgraphQ[g, h:Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 1}] && PropertyValue[{h, 1}, "some_field"] > 1 && PropertyValue[{h,2}, "some_field"] > 1 && PropertyValue[{h,3}, "some_field"] > 1])

Cases[toyDatabase,  queryPattern] (* doesnt work *) 
$\endgroup$
  • 2
    $\begingroup$ You might want to look at the SPARQL functionality new in version 12. $\endgroup$ – Carl Lange Feb 26 at 22:25
  • $\begingroup$ I’m not looking for just triangles, I want to do full sub-graph matching... does that change your suggestion? $\endgroup$ – M.R. Feb 27 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.