best way to return a subgraph in cypher

769 views
Skip to first unread message

John Fry

unread,
Jun 1, 2014, 4:52:57 PM6/1/14
to ne...@googlegroups.com
Hello All,

I am looking to build the following pseudo code in Cypher:

1. find all the shortest paths between two nodes (sucessfully done - see snippet below)
START a=node(448091), b=node(6573222)
MATCH p=allshortestpaths((a)-[*..10]-(b))
return p;
2. return a complete subgraph (inc rel properties) for the results from 1 (+ remove any repeated nodes)
3. take each node and expand them all in parallel until n nodes in total have been reached
4. return a complete subgraph (inc rel properties) for the results from 3 (+ remove any repeated nodes)

Each step has to be discrete as I will do some algorithmic updates inbetween based on the relationship properties.
I am guessing that this may be a fairly common set of operations?

Many thanks in advance for any help.

Rgds, John.

John Fry

unread,
Jun 1, 2014, 5:26:56 PM6/1/14
to ne...@googlegroups.com
I think ideally it would help if I could get the subgraph results in the following format (i.e. basically a list of relation properties with src and dst nodes):

+---------------------------------------------------------------------------------------------+
| from                                  | to                                |  rel_prop                  |
+---------------------------------------------------------------------------------------------+
| Node[x]{}                          | Node[a]{}                     | :link[q]{val}              |
| Node[x]{}                          | Node[b]{}                     | :link[r]{val}              |
| Node[y]{}                          | Node[z]{}                     | :link[s]{val}              |

If the subgraph was delivered like this then I could easily load the subgraph into my java application for processing with something like this example:
//https://github.com/neo4j/neo4j/blob/2.1.1/community/cypher/docs/cypher-docs/src/test/java/org/neo4j/cypher/example/JavaQuery.java

Michael Hunger

unread,
Jun 1, 2014, 9:07:15 PM6/1/14
to ne...@googlegroups.com
How about something like this:

START a=node(448091), b=node(6573222) 
MATCH p=allshortestpaths((a)-[*..10]-(b)) 
// all nodes
UNWIND nodes(p) as n
// remove duplicates
WITH distinct n
// expand
MATCH p2=(n)-[*..2]-(m)
UNWIND nodes(p) as n
WITH distinct n
LIMIT 1000
MATCH (n)-[r]-(m)
RETURN n,r,m



--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Fry

unread,
Jun 1, 2014, 11:01:38 PM6/1/14
to ne...@googlegroups.com
Thanks - I will take a look as this approach. Just quickly UNWIND isnt avail in 2.0.2. Is there an east equivalent?

As soon as I get some good results I will post back...

John Fry

unread,
Jun 2, 2014, 12:17:38 AM6/2/14
to ne...@googlegroups.com
This is not a bad way to get a subgraph for step 1.


neo4j-sh (?)$ START a=node(448091), b=node(6573222)                                  
MATCH p=allshortestpaths((a)-[*..10]-(b))                              
return extract(n IN nodes(p)| n.title), extract(r IN rels(p)| r.weight);


yields a fairly clean subgraph in the form below. Not quit what I want but for step 2 I could fitter for the disctinct nodes in my java app.
I am still wondering if i could get NEO4J to do the last bit of filtering (am not ready to move to 2.1 yet to try unwind).

+-----------------------------------------------------------------------------------------------------------------------------------------+
| extract(n IN nodes(p)| n.title)                                                                       | extract(r IN rels(p)| r.weight) |
+-----------------------------------------------------------------------------------------------------------------------------------------+
| ["Fieldale","Cannon Mills","Davidson College","Edinburgh Festival Fringe","Yasser (play)"]            | [<null>,<null>,0.5,<null>]      |
| ["Fieldale","Cannon Mills","Davidson College","The Merchant of Venice","Yasser (play)"]               | [<null>,<null>,0.5,<null>]      |
| ["Fieldale","Bassett High School","English","Haarlem","Yasser (play)"]                                | [<null>,<null>,0.5,<null>]      |
| ["Fieldale","Cannon Mills","New York City","Haarlem","Yasser (play)"]                                 | [<null>,<null>,0.5,<null>]      |
| ["Fieldale","Fieldale, Virginia","Hispanic","Moroccan","Yasser (play)"]                               | [<null>,0.5,0.5,<null>]         |
| ["Fieldale","Fieldale, Virginia","Hispanic","Arab","Yasser (play)"]                                   | [<null>,0.5,0.5,<null>]         |
| ["Fieldale","Henry County, Virginia","Hispanic","Moroccan","Yasser (play)"]                           | [0.5,0.5,0.5,<null>]            |

Michael Hunger

unread,
Jun 2, 2014, 1:05:40 AM6/2/14
to ne...@googlegroups.com
No it's in 2.1

Not really an equivalent

There are some things you can do with reduce for uniqueness and path patterns but it won't be the same

Sent from mobile device

Am 02.06.2014 um 05:01 schrieb John Fry <frydo...@gmail.com>:

Thanks - I will take a look as this approach. Just quickly UNWIND isnt avail in 2.0.2. Is there an east equivalent?

As soon as I get some good results I will post back...

--
Reply all
Reply to author
Forward
0 new messages