1

I have a multigraph and I am interested in counting the number of edges connecting each pair of nodes; moreover, I would need to get, for each pair of nodes, the maximum value of a certain attribute.

Unfortunately it seems to me we can use .group().by(...) with one attribute only, while I would need to group by inV and outV. In Cypher I would write something like

MATCH (e0: Employee)-[fb: R]-> (e1: Employee)
WITH e0, e1, count(*) AS frequency, min(fb.value) AS min_val
RETURN e0, e1, frequency, min_val

Can anybody help?

Cheers!

Alberto
  • 597
  • 3
  • 17

1 Answers1

5

Answered on the Gremlin users mailing list, but to close the circle, here's the traversal again:

gremlin> g.V().as("e0").outE("e").as("e").inV().as("e1").select("e0","e","e1").
gremlin>       group().by(select("e0","e1").by("name")).
gremlin>               by(fold().match(__.as("x").count(local).as("freq"),
gremlin>                               __.as("x").unfold().select("e").by("value").max().as("max")).select("freq","max")
gremlin>               ).next()
==>{e0=c, e1=d}={freq=1, max=9}
==>{e0=b, e1=d}={freq=1, max=9}
==>{e0=f, e1=h}={freq=1, max=9}
==>{e0=e, e1=h}={freq=1, max=9}
==>{e0=a, e1=b}={freq=2, max=10000}
==>{e0=b, e1=c}={freq=4, max=4}
==>{e0=e, e1=f}={freq=1, max=9}
==>{e0=f, e1=g}={freq=1, max=9}
==>{e0=a, e1=c}={freq=1, max=9}
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34
  • I've just realised that is the script for the [connected components query](http://stackoverflow.com/questions/33895587/tinkerpop-3-compute-connected-components-with-gremlin-traversal/33996539#33996539), could you copy-paste the other solution you posted in the thread? – Alberto Nov 30 '15 at 10:38
  • Hi @Daniel Kuppitz, I've finally found some time to inspect the traversal: in my case the output is different than yours: i got `==>{e0=c, e1=d}={{freq=1, max=9}=1} ==>{e0=b, e1=d}={{freq=1, max=9}=1} ==>{e0=f, e1=h}={{freq=1, max=9}=1} ==>{e0=e, e1=h}={{freq=1, max=9}=1} ==>{e0=a, e1=b}={{freq=1, max=10}=1, {freq=1, max=10000}=1} ==>{e0=b, e1=c}={{freq=1, max=0}=1, {freq=1, max=2}=1, {freq=1, max=3}=1, {freq=1, max=4}=1} ==>{e0=e, e1=f}={{freq=1, max=9}=1} ==>{e0=f, e1=g}={{freq=1, max=9}=1} ==>{e0=a, e1=c}={{freq=1, max=9}=1}` Looks like some aggregation is missing... – Alberto Dec 01 '15 at 13:02
  • I'm checking what's wrong. Have you, by chance, copy-pasted a previous version of your traversal? – Alberto Dec 01 '15 at 13:02
  • Looks like I've used a 3.1.x version and you've used 3.0.x. You can either bump your TP version or add a 3rd `by()`. Something like `.by(limit(1))` or `.by {it.head()}` should work. – Daniel Kuppitz Dec 01 '15 at 13:48
  • I've changed my gremlin version. Now it works :) Thanks! – Alberto Dec 01 '15 at 13:57