1

Lets asume that John is selling goods to Met , Met is selling goods To both Bob and Alen , and Alen sells goods to John again . What I need is a Cypher query that returns all the closed circles like in this example John..Met..Alen because Alen sells goods to John again making it a closed circle displaying also the lowest amount of relationship property (amount) .How do I do this from entire database , get me all the closed circles and min amounths .Thanks!

4

2 回答 2

4

Starting with Stefans answer, for the minimum you would want to take the lengths of the paths into account.

start n=node(*) 
match p=n-[:SELLS_TO*1..5]->n 
return p, lenght(p)

To just the the shortest path length per node

start n=node(*) 
match p=n-[:SELLS_TO*1..5]->n 
return n, min(lenght(p))

if you want to get the shortest path:

start n=node(*) 
match p=n-[:SELLS_TO*1..5]->n 
with n, collect(nodes(p)) as nodes, min(length(nodes(p))) as l
return n, head(filter(p in nodes : length(p) = l)) as shortest_circle,l

See the Neo4j console for an example: http://console.neo4j.org/r/wrm522

Something you'll note there is that if you scan the whole graph you will get the same circle multiple times for each node of the circle.

This uses the nodes, length, collect, head and filter functions and the min aggregate. see: http://docs.neo4j.org/chunked/milestone/query-function.html

As Stefan already said, scanning over all nodes is very probably quite expensive.

于 2013-03-31T01:26:00.697 回答
1

You could do a query like:

start n=node(*) 
match p=n-[:SELLS_TO*1..5]->n 
return p

where 5 ist the maximum depth for a loop.

See an example in Neo4j console. However using "node(*)" triggers a global query which scales linearly wiht the size of your graph.

于 2013-03-30T19:38:53.170 回答