algorithm - Find all simple cycles through a given node in Neo4j -
i'm working graphs , knew neo4j.
can neo4j me find simple cycles go through given node in graph?
i can in java
/python
code implementing a modification of johnson's algorithm.
this example of graph created, cypher code can executed on neo4j database:
create (john:person { name : '@john',facebook: 'facebook.com/john'}) create (josh:person { name : '@josh',facebook: 'facebook.com/josh'}) create (dan:person { name : '@dan',facebook: 'facebook.com/dan'}) create (kenny:person { name : '@kenny',facebook: 'facebook.com/kenny'}) create (bart:person { name : '@bart',facebook: 'facebook.com/bart'}) create (mike:person { name : '@mike',facebook: 'facebook.com/mike'}) create (jenny:person { name : '@jenny',facebook: 'facebook.com/jenny'}) create (frank:person { name : '@frank',facebook: 'facebook.com/frank'}) create (erick:person { name : '@erick',facebook: 'facebook.com/erick'}) create (lynda:person { name : '@lynda',facebook: 'facebook.com/lynda'}) create (lynda)-[:knows]-> (josh) create (lynda)-[:knows]-> (frank) create (lynda)-[:knows]-> (bart) create (josh)-[:knows]-> (erick) create (josh)-[:knows]-> (jenny) create (josh)-[:knows]-> (dan) create (dan)-[:knows]-> (lynda) create (dan)-[:knows]-> (josh) create (dan)-[:knows]-> (mike) create (dan)-[:knows]-> (kenny) create (mike)-[:knows]-> (kenny) create (kenny)-[:knows]-> (bart) create (bart)-[:knows]-> (josh) create (frank)-[:knows]-> (erick) create (erick)-[:knows]-> (frank)
...and these cycles within graph:
josh->dan->lynda->josh josh->dan->lynda->bart->josh josh->dan->josh josh->dan->mike->kenny->bart->josh josh->dan->kenny->bart->josh
here list of simple test cases:
1- input: josh output (all cycles): josh->dan->lynda->josh josh->dan->lynda->bart->josh josh->dan->josh josh->dan->mike->kenny->bart->josh josh->dan->kenny->bart->josh 2- input: lynda output: josh->dan->lynda->josh josh->dan->lynda->bart->josh
you can in cypher following query :
match p=(n)-[*]->(n) return nodes(p)
a textual representation of query :
find me paths start node , end node same , complete path has outgoing direction
note expensive query on medium/large graphs, can limit depth of path, ex :
match p=(n)-[*1..15]->(n) return nodes(p)
maybe want have minimum depth 2, because node having relationship returned depth of 1 ;-)
Comments
Post a Comment