java - Searching for strongly connected components using neo4j -


i'm trying implement scc algorithm using neo4j storing graph.

here implementation of dfs:

void dfs(graphdatabaseservice g, node node, long counter) {      transaction tx = g.begintx();     node.setproperty("explored", true);     tx.success();     tx.finish();     system.out.println("exporing node " + node.getproperty("name") + "with depth " + counter);      iterator<relationship> = node.getrelationships(direction.outgoing, reltypes.knows).iterator();     while (it.hasnext()) {         node end = it.next().getendnode();         if (!(boolean) end.getproperty("explored"))             dfs(g, end, ++counter);     }   } 

it throws stackoverflowerror. well, obvious reason depth of recursion getting big. maybe there wrong code?

there's no need write own recursive dfs neo4j provides out of box. i'd rewrite method in following fashion:

void dfs(graphdatabaseservice g, node node) {      //neo4j provided traversal api     traversaldescription traversaldescription = new traversaldescriptionimpl()             .depthfirst()             .relationships(reltypes.knows, direction.outgoing)             .uniqueness(uniqueness.node_global);      iterable<node> nodesincomponent = traversaldescription.traverse(node).nodes();      //uses graphaware save lines of code     new iterableinputbatchtransactionexecutor<>(g, 1000, nodesincomponent, new unitofwork<node>() {         @override         public void execute(graphdatabaseservice database, node input) {             system.out.println("exploring node " + input.getproperty("name"));             if (!(boolean) input.getproperty("explored", false)) {                 input.setproperty("explored", true);             }         }     }).execute(); } 

the first 4 lines use pure neo4j api , retrieve lazy iterable gets nodes need.

the remaining lines write "explored" property in batches of 1000 rather in separate transactions, performance reasons. brewity, graphaware framework used (disclaimer: i'm author), written few more lines of pure neo4j code.

i've tried 10,000 nodes (a single connected component), took 26 seconds.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -