DFS query optimization

49 views
Skip to first unread message

Gergely Svigruha

unread,
Oct 6, 2012, 1:53:18 AM10/6/12
to ne...@googlegroups.com
Hi,

I would like to get all the nodes at most N distance from a specific node. I use DFS with a bound for the depth. I don't want to visit a node multiple times so I use a Set<Long> for storing the already visited nodes' ids. My question is when I have the edges (Relationship objects) of a node is it possible to get the corresponding nodes' id without referencing the Node object (I think this is when Neo4j loads the Node from the physical drive when it's not cached, isn't it)? I assume the Relationship object stores the corresponding 2 Nodes' ids? Can it make any difference or not because the already visited nodes are probably cached (1M total nodes, neostore.nodestore.db.mapped_memory=1000M)? I use Neo4j community 1.8 on Linux.
        
        GraphDatabaseService graphDb = new EmbeddedGraphDatabase(dbPath, config);
        Set<String> cities = new HashSet<String>();
Set<Long> visitedIds = new HashSet<Long>();
        Transaction tx = graphDb.beginTx();
        try
        {
            Node startNode = graphDb.getNodeById(startNodeId);
            getDepth(startNode, 1, 6, cities, visitedIds);
            tx.success();
        }
        finally
        {
            tx.finish();
        }

        private static void getDepth(Node node, int depth, int maxDepth, Set<String> cities, Set<Long> visitedIds) {
if(!visitedIds.contains(node.getId())) {
visitedIds.add(node.getId());
cities.add(String.valueOf(node.getProperty("City")));
if(depth<=maxDepth) {
for(Relationship rel : node.getRelationships()) {

                                         //instead of this something like visitedIds.contains(rel.getEndNodeId())
Node target = rel.getEndNode();

if(target.getId() != node.getId()) {
getDepth(target, depth+1, maxDepth, cities, visitedIds);
}
                }
}
}
}

Thanks,
Greg

Michael Hunger

unread,
Oct 6, 2012, 2:49:03 AM10/6/12
to ne...@googlegroups.com
Neo4j traversal api already comes with uniqueness constraints

Use evaluators.toDepth(x)

See mark needhams example in yesterdays post in the google group

No the node is just a thin wrapper around the id if you didn't access props or other rels


Sent from mobile device
--
 
 
Reply all
Reply to author
Forward
0 new messages