neo4j cypher query to find root node

1,738 views
Skip to first unread message

vipul jain

unread,
Oct 13, 2013, 11:03:01 PM10/13/13
to ne...@googlegroups.com
hi Guys,

if I have following tree :

                                    A                                              A1
                                  /   \                                             /     \
                                B    C                                         B1    C1
                             /    \   /   \                                      /    \      /    \
                          D    E F    G                              D1 E1    F1  G1
                          :                                                 :
                          : n length                                    : n length
                          :                                                  :
                          /  \                                               /  \
                         Y   Z                                          Y1   Z1
in this tree I know the id of Y, Z, Y1, Z1.

I want to write the cypher query which return the root node A A1 only. Relationship between each node is like A is PARENT_OF B and so on.

Can one help me on this .

Thanks
vipul

Wes Freeman

unread,
Oct 13, 2013, 11:12:19 PM10/13/13
to ne...@googlegroups.com
something like this:

START y=node(id)
MATCH (y)<-[:PARENT_OF*]-(root)
WHERE NOT (root)<-[:PARENT_OF]-()
RETURN root

Koen Kleingeld

unread,
Oct 13, 2013, 11:41:19 PM10/13/13
to ne...@googlegroups.com
this will give the degree,indegree and outdegree  of all nodes .... indegree=0 is your root

(code thanks to wes/michael some days ago in one of my posts)

match (n)-[r]-()
with n, sum(case when startnode(r) = n then 1 else 0 end) as outdegree, sum(case when endnode(r) = n then 1 else 0 end) as indegree
return id(n),n.nodename as nodename, outdegree, indegree, outdegree+indegree AS degree
order by indegree asc

this only indegree 0

match (n)-[r]-()
with n, sum(case when endnode(r) = n then 1 else 0 end) as indegree where indegree=0
return id(n),n.nodename as nodename, indegree
order by indegree asc

reg koen



2013/10/14 Wes Freeman <freem...@gmail.com>

--
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/groups/opt_out.



--
Groeten / Best Regards,

Koen

Michael Hunger

unread,
Oct 14, 2013, 8:25:28 AM10/14/13
to ne...@googlegroups.com
Yup

Michael

vipul jain

unread,
Oct 15, 2013, 6:56:15 AM10/15/13
to ne...@googlegroups.com
Hi koen,

kindly check my this query, is there any problem because it gives me no result

START n=node:users(CURR_DUNS_NUMBER="832311906")

match (n)-[r]-()
with n, sum(case when endnode(r) = n then 1 else 0 end) as indegree where indegree=0
return id(n),n.nodename as nodename, indegree
order by indegree asc

i think in place of r i have put PARENT_OF. because this is my relationship.

kindly modify your query with PARENT_OF relationship

Thanks,
Vipul


START n=node:users(CURR_DUNS_NUMBER="832311906")
match (n)-[PARENT_OF]-()

with n, sum(case when endnode(r) = n then 1 else 0 end) as indegree where indegree=0
return id(n),n.nodename as nodename, indegree
order by indegree asc

Thanks,
Vipul

Koen Kleingeld

unread,
Oct 15, 2013, 7:56:30 AM10/15/13
to ne...@googlegroups.com
START n=node(*)

match (n)-[r]-()
with n, sum(case when endnode(r) = n then 1 else 0 end) as indegree where indegree=0
return id(n),n.nodename as nodename, indegree
order by indegree asc


2013/10/15 vipul jain <pansariv...@gmail.com>

Wes Freeman

unread,
Oct 15, 2013, 9:09:38 AM10/15/13
to ne...@googlegroups.com
Are you still trying to find the root?

Wes

Koen Kleingeld

unread,
Oct 15, 2013, 9:24:26 AM10/15/13
to ne...@googlegroups.com
lol


2013/10/15 Wes Freeman <freem...@gmail.com>
Reply all
Reply to author
Forward
0 new messages