Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract subgraph in neo4j

Tags:

subgraph

neo4j

I have a large network stored in Neo4j. Based on a particular root node, I want to extract a subgraph around that node and store it somewhere else. So, what I need is the set of nodes and edges that match my filter criteria.

Afaik there is no out-of-the-box solution available. There is a graph matching component available, but it works only for perfect matches. The Neo4j API itself defines only graph traversal which I can use to define which nodes/edges should be visited:

Traverser exp = Traversal
    .description()
    .breadthFirst()
    .evaluator(Evaluators.toDepth(2))
    .traverse(root);

Now, I can add all nodes/edges to sets for all paths, but this is very inefficient. How would you do it? Thanks!

EDIT Would it make sense to add the last node and the last relationship of each traversal to the subgraph?

like image 739
msteiger Avatar asked Apr 19 '13 09:04

msteiger


1 Answers

As for graph matching, that has been superseded by http://docs.neo4j.org/chunked/snapshot/cypher-query-lang.html which would fit nicely, and supports fuzzy matchin with optional relationships.

For subgraph representation, I would use the Cypher output to maybe construct new Cypher statements for recreating the graph, much like a SQL export, something like

start n=node:node_auto_index(name='Neo') 
match n-[r:KNOWS*]-m 
return "create ({name:'"+m.name+"'});"

http://console.neo4j.org/r/pqf1rp for an example

like image 114
Peter Neubauer Avatar answered Oct 05 '22 04:10

Peter Neubauer