Skip to content

Instantly share code, notes, and snippets.

@captainjackrana
Last active December 2, 2017 07:19
Show Gist options
  • Save captainjackrana/18f63466063e40884628a9fad001f6c5 to your computer and use it in GitHub Desktop.
Save captainjackrana/18f63466063e40884628a9fad001f6c5 to your computer and use it in GitHub Desktop.
Weighted Page Rank Algorithm in Neo4j using Cypher
/* we have to go through all nodes */
MATCH (node:Page)
WITH
COLLECT(distinct node) AS pages
UNWIND pages as dest
/* let's find all source citations for a given node */
MATCH (source:Page)-[:NEXT]->(dest)
WITH
COLLECT(DISTINCT source) AS sources,
dest AS dest
UNWIND sources AS src
/* we have to know how many relationships the source node has */
MATCH (src)-[r:NEXT]->(dest)
WITH src, dest, sum(r.weight) AS prob
MATCH (src)-[r:NEXT]->()
WITH
/* The source citation will pass a portion of its pagerank as juice */
src.pagerank * (prob/ sum(r.weight)) AS juice,
dest AS dest
/* now we have all information to update the destination node with the new pagerank */
WITH
sum(juice) AS p,
dest AS dest
/* Using damping factor d = 0.85 */
set dest.pagerank = 0.15 + 0.85 * p;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment