Query for nodes, degree,in degree, outdegree

1,840 views
Skip to first unread message

Koen

unread,
Jun 30, 2013, 10:30:13 AM6/30/13
to ne...@googlegroups.com
hi, sorry for asking so much questions .. but trying to get my personal "coin" dropping in my head for cypher ..

for example how can i query all nodes in a graph and create a list of all nodes and for each node the name,degree,indegree and outdegree ?

i have managed to create seperate queries doing part of this (so all nodes with total degree) .. or all nodes with in degree .. but somehow  i cannot succeed in (at the end) outputting this in one combined result

i guess the WITH has to come in play maybe ?

reesult should looke like

node    degree   indegree   outdegree
a          5      3    2
b          7      1     6
c          3       2    1
d          1       0     1

thx koen


Koen Kleingeld

unread,
Jun 30, 2013, 1:49:28 PM6/30/13
to ne...@googlegroups.com
wow . i solved my own question...

start home=node(*)
match home--()
WITH home,count(*) as degree
match home-->()
WITH home,degree,count(*) as outdegree
match home<--()
return home.name,degree,outdegree,count(*) as indegree
order by home.name

is this the most "minimal" query for my goal ?

thx Koen


2013/6/30 Koen <kdwykl...@gmail.com>



--
You received this message because you are subscribed to a topic in the Google Groups "Neo4j" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/neo4j/CQJXB-spc5E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Groeten / Best Regards,

Koen

Peter Neubauer

unread,
Jun 30, 2013, 3:39:17 PM6/30/13
to Neo4j User
Cool,
steep learning curve Koen!

/peter


Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

Kids in Malmö this summer?        - http://www.kidscraft.se
Neo4j questions? Use GraphGist. - http://gist.neo4j.org


--
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.

Michael Hunger

unread,
Jun 30, 2013, 8:20:07 PM6/30/13
to ne...@googlegroups.com
You can also aggregate the degree from both:

start home=node(*)

match home-->()
WITH home,degree,count(*) as outdegree
match home<--()
return home.name, count(*) + outdegree AS degree, outdegree, count(*) AS indegree
order by home.name

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.

Koen Kleingeld

unread,
Jul 1, 2013, 12:38:46 AM7/1/13
to ne...@googlegroups.com
hi thx, thats great... i was in the process of removing the last WITH myself but could not succeed in calculating the remaining degree.. this will help

is it actually so that for every WITH you add the whole graph is traversed again according to what has been defined in the very first "start"  command ?   so when you have a "start bla=node(*)" the whole graph is traversed again for every new WITH ?

reg koen


2013/7/1 Michael Hunger <michael...@neotechnology.com>

Michael Hunger

unread,
Jul 1, 2013, 1:17:29 AM7/1/13
to ne...@googlegroups.com
No, WITH is like a unix pipe

It specify what is forwarded to the next part but keeps the same cardinality (except if you aggregate or filter with WHERE)

The traversal of the graph comes from MATCH

Michael


Sent from mobile device

Koen Kleingeld

unread,
Jul 1, 2013, 1:56:09 AM7/1/13
to ne...@googlegroups.com
thx, actually the algorithm does not work since it drops the beginning and end nodes of the graph..  you only get results for the nodes that are not at the edges ..

any ideas to cover all nodes ?

thx














2013/7/1 Michael Hunger <michael...@neopersistence.com>

Michael Hunger

unread,
Jul 1, 2013, 4:09:54 AM7/1/13
to ne...@googlegroups.com
What do you mean?

Do you have an example?

E.g set one up in console.neo4j.org and share it with the "share" button.

Michael

Koen

unread,
Jul 1, 2013, 5:18:31 AM7/1/13
to ne...@googlegroups.com
hi, see 2 pictures

Op zondag 30 juni 2013 16:30:13 UTC+2 schreef Koen het volgende:
queryandresults.jpg
rbacsamplegraph.jpg

Koen Kleingeld

unread,
Jul 2, 2013, 1:55:43 AM7/2/13
to ne...@googlegroups.com
Hi, if you run the query against the demo setup in the neo console you can see it leaves out 3 nodes that are located at the edges (or have only incoming edges)   - (architect / 0 and trinity)

START home=node(*)
MATCH home-->()
WITH home,count(*) AS outdegree
MATCH home<--()
RETURN home.name, count(*)+ outdegree AS degree, outdegree, count(*) AS indegree
ORDER BY home.name

.. i guess we have to start matching for "degree" .. so   match home--()

but when aggregating you loose the nodes that have only one direction as connection..


reg koen

Michael Hunger

unread,
Jul 2, 2013, 4:58:23 AM7/2/13
to ne...@googlegroups.com
I don't see what's wrong with the output?

Oh you mean that some nodes don't show up because they didn't have in degree or out-degree? That's because they are filtered out as non-matching.

you can also use path-expressions as expressions to compute that.

start n=node(*)
WITH n,length(n-->()) as outdegree, length(n<--()) as indegree
return n, outdegree, indegree, outdegree+indegree AS degree

Michael

--
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.
 
 
<queryandresults.jpg><rbacsamplegraph.jpg>

Koen Kleingeld

unread,
Jul 2, 2013, 7:15:54 AM7/2/13
to ne...@googlegroups.com
hi, thx,  yes thats what i mean... i was not sure how to retain the first aggregation result ...  
but at the end all is possible it seems !!  hopefully i will reach this cypher knowledge level some time ... 

this algoritm works out fine

reg koen




2013/7/2 Michael Hunger <michael...@neotechnology.com>

--
You received this message because you are subscribed to a topic in the Google Groups "Neo4j" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/neo4j/CQJXB-spc5E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to neo4j+un...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages