Neo4j faster way to find neighbors up to some depth

815 views
Skip to first unread message

Steve

unread,
Feb 14, 2017, 8:01:39 PM2/14/17
to Neo4j
I am getting started with Neo4j and wondering about finding the nodes connected to another node from at most k edges (friend of a friend of a friend ... up to k times). I will illustrate with the example from the tutorial in Neo4j itself (I put graph creation commands are at the bottom).

match (e {name:"Emil"})-[*1..2]-(p)
return DISTINCT e, p;

This query would return nodes connected to Emil and nodes connected to those nodes. My issue is that it seems this enumerates EVERY path of length 1-2 from Emil, though I don't care about enumerating all paths. This is an issue in large, dense graphs as the complexity becomes overwhelming. Based on my testing, it seems DISTINCT is a post-processing step that does not affect the runtime of the query, though it will eliminate the redundant output. Is that correct?

My main question, is there a way to form a Cypher query for this problem so that I am not traversing all the unique paths and the complexity can be reduced? Please let me know if I have a misunderstanding somewhere as well.


Best,
Steve


---- Commands to create the graph -----
 CREATE (ee:Person { name: "Emil", from: "Sweden", klout: 99 })
 MATCH (ee:Person) WHERE ee.name = "Emil"
 CREATE (js:Person { name: "Johan", from: "Sweden", learn: "surfing" }),
 (ir:Person { name: "Ian", from: "England", title: "author" }),
 (rvb:Person { name: "Rik", from: "Belgium", pet: "Orval" }),
 (ally:Person { name: "Allison", from: "California", hobby: "surfing" }),
 (ee)-[:KNOWS {since: 2001}]->(js),(ee)-[:KNOWS {rating: 5}]->(ir),
 (js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb),
 (ir)-[:KNOWS]->(js),(ir)-[:KNOWS]->(ally),
 (rvb)-[:KNOWS]->(ally)

Max De Marzi Jr.

unread,
Feb 16, 2017, 3:50:57 PM2/16/17
to Neo4j
Take  a look at https://maxdemarzi.com/2017/02/06/neo4j-is-faster-than-mysql-in-performing-recursive-query/ or try the query on Neo4j 3.2 (Alpha release) as it does a better job handling these types of queries.
Reply all
Reply to author
Forward
0 new messages