0

I'm looking for a Gremlin version of a customizable PageRank algorithm. There are a few old versions out there, one (from: http://www.infoq.com/articles/graph-nosql-neo4j) is pasted below. I'm having trouble fitting the flow into the current GremlinGroovyPipeline-based structure. What is the modernized equivalent of this or something like it?

$_g := tg:open()
g:load('data/graph-example-2.xml')
$m := g:map()
$_ := g:key('type', 'song')[g:rand-nat()]

repeat 2500
  $_ := ./outE[@label='followed_by'][g:rand-nat()]/inV
  if count($_) > 0
    g:op-value('+',$m,$_[1]/@name, 1.0)
  end

  if g:rand-real() > 0.85 or count($_) = 0
    $_ := g:key('type', 'song')[g:rand-nat()]
  end
end

g:sort($m,'value',true())

Another version is available on slide 55 of http://www.slideshare.net/slidarko/gremlin-a-graphbased-programming-language-3876581. The ability to use the if statements and change the traversal based on them is valuable for customization.

many thanks

Ziggy Eunicien
  • 2,858
  • 1
  • 23
  • 28

1 Answers1

1

I guess I'll answer it myself in case somebody else needs it. Be warned that this is not a very efficient PageRank calculation. It should only be viewed as a learning example.

g = new TinkerGraph()
g.loadGraphML('graph-example-2.xml')
m = [:]
g.V('type','song').sideEffect{m[it.name] = 0}

// pick a random song node that has 'followed_by' edge
def randnode(g) {
  return(g.V('type','song').filter{it.outE('followed_by').hasNext()}.shuffle[0].next())
}

v = randnode(g)
for(i in 0..2500) {
  v = v.outE('followed_by').shuffle[0].inV
  v = v.hasNext()?v.next():null
  if (v != null) {
    m[v.name] += 1
  }

  if ((Math.random() > 0.85) || (v == null)) {
    v = randnode(g)
  }
}

msum = m.values().sum()
m.each{k,v -> m[k] = v / msum} 
println "top 10 songs: (normalized PageRank)"
m.sort {-it.value }[0..10]

Here's a good reference for a simplified one-liner: https://groups.google.com/forum/m/#!msg/gremlin-users/CRIlDpmBT7g/-tRgszCTOKwJ (as well as the Gremlin wiki: https://github.com/tinkerpop/gremlin/wiki)

Ziggy Eunicien
  • 2,858
  • 1
  • 23
  • 28