5.2.1. PageRank

This section describes the PageRank algorithm in the Neo4j Graph Data Science library.

This topic includes:

5.2.1.1. Introduction

The PageRank algorithm measures the importance of each node within the graph, based on the number incoming relationships and the importance of the corresponding source nodes. The underlying assumption roughly speaking is that a page is only as important as the pages that link to it.

PageRank is introduced in the original Google paper as a function that solves the following equation:

page rank formula

where,

  • we assume that a page A has pages T1 to Tn which point to it.
  • d is a damping factor which can be set between 0 and 1. It is usually set to 0.85.
  • C(A) is defined as the number of links going out of page A.

This equation is used to iteratively update a candidate solution and arrive at an approximate solution to the same equation.

For more information on this algorithm, see:

Running this algorithm requires sufficient memory availability. Before running this algorithm, we recommend that you read Section 3.1, “Memory Estimation”.

5.2.1.2. Considerations

There are some things to be aware of when using the PageRank algorithm:

  • If there are no links from within a group of pages to outside of the group, then the group is considered a spider trap.
  • Rank sink can occur when a network of pages is forming an infinite cycle.
  • Dead-ends occur when pages have no out-links. If a page contains a link to another page which has no out-links, the link would be known as a dangling link.

Changing the value of damping factor can help with these considerations. It can be interpreted as a probability of a web surfer to sometimes jump to a random page and therefore not getting stuck in sinks.

5.2.1.3. Syntax

This section covers the syntax used to execute the PageRank algorithm in each of its execution modes. We are describing the named graph variant of the syntax. To learn more about general syntax variants, see Section 5.1, “Syntax overview”.

Stream

Run PageRank in stream mode on a named graph. 

CALL gds.pageRank.stream(
  graphName: String,
  configuration: Map
)
YIELD
  nodeId: Integer,
  score: Float

Table 5.1. Parameters
Name Type Default Optional Description

graphName

String

n/a

no

The name of a graph stored in the catalog.

configuration

Map

{}

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 5.2. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

nodeLabels

String[]

['*']

yes

Filter the named graph using the given node labels.

relationshipTypes

String[]

['*']

yes

Filter the named graph using the given relationship types.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm.

Table 5.3. Algorithm specific configuration
Name Type Default Optional Description

dampingFactor

Float

0.85

yes

The damping factor of the Page Rank calculation.

maxIterations

Integer

20

yes

The maximum number of iterations of Page Rank to run.

tolerance

Float

0.0000001

yes

Minimum change in scores between iterations. If all scores change less than the tolerance value the result is considered stable and the algorithm returns.

relationshipWeightProperty

String

null

yes

The property name that contains weight. If null, treats the graph as unweighted. Must be numeric.

sourceNodes

List

[]

yes

A set of nodes to use for computing Personalized Page Rank.

Table 5.4. Results
Name Type Description

nodeId

Integer

Node ID

score

Float

PageRank score

Stats

Run PageRank in stats mode on a named graph. 

CALL gds.pageRank.stats(
  graphName: String,
  configuration: Map
)
YIELD
  ranIterations: Integer,
  didConverge: Boolean,
  createMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  centralityDistribution: Map,
  configuration: Map

Table 5.5. Parameters
Name Type Default Optional Description

graphName

String

n/a

no

The name of a graph stored in the catalog.

configuration

Map

{}

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 5.6. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

nodeLabels

String[]

['*']

yes

Filter the named graph using the given node labels.

relationshipTypes

String[]

['*']

yes

Filter the named graph using the given relationship types.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm.

Table 5.7. Algorithm specific configuration
Name Type Default Optional Description

dampingFactor

Float

0.85

yes

The damping factor of the Page Rank calculation.

maxIterations

Integer

20

yes

The maximum number of iterations of Page Rank to run.

tolerance

Float

0.0000001

yes

Minimum change in scores between iterations. If all scores change less than the tolerance value the result is considered stable and the algorithm returns.

relationshipWeightProperty

String

null

yes

The property name that contains weight. If null, treats the graph as unweighted. Must be numeric.

sourceNodes

List

[]

yes

A set of nodes to use for computing Personalized Page Rank.

Table 5.8. Results
Name Type Description

ranIterations

Integer

The number of iterations run.

didConverge

Boolean

Indicates if the algorithm converged.

createMillis

Integer

Milliseconds for creating the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the centralityDistribution.

centralityDistribution

Map

Map containing min, max, mean as well as p50, p75, p90, p95, p99 and p999 percentile values of centrality values.

configuration

Map

The configuration used for running the algorithm.

Mutate

Run PageRank in mutate mode on a graph stored in the catalog. 

CALL gds.pageRank.mutate(
  graphName: String,
  configuration: Map
)
YIELD
  nodePropertiesWritten: Integer,
  ranIterations: Integer,
  didConverge: Boolean,
  createMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  mutateMillis: Integer,
  centralityDistribution: Map,
  configuration: Map

Table 5.9. Parameters
Name Type Default Optional Description

graphName

String

n/a

no

The name of a graph stored in the catalog.

configuration

Map

{}

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 5.10. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

nodeLabels

String[]

['*']

yes

Filter the named graph using the given node labels.

relationshipTypes

String[]

['*']

yes

Filter the named graph using the given relationship types.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm.

mutateProperty

String

n/a

no

The node property in the GDS graph to which the score is written.

Table 5.11. Algorithm specific configuration
Name Type Default Optional Description

dampingFactor

Float

0.85

yes

The damping factor of the Page Rank calculation.

maxIterations

Integer

20

yes

The maximum number of iterations of Page Rank to run.

tolerance

Float

0.0000001

yes

Minimum change in scores between iterations. If all scores change less than the tolerance value the result is considered stable and the algorithm returns.

relationshipWeightProperty

String

null

yes

The property name that contains weight. If null, treats the graph as unweighted. Must be numeric.

sourceNodes

List

[]

yes

A set of nodes to use for computing Personalized Page Rank.

Table 5.12. Results
Name Type Description

ranIterations

Integer

The number of iterations run.

didConverge

Boolean

Indicates if the algorithm converged.

createMillis

Integer

Milliseconds for creating the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the centralityDistribution.

mutateMillis

Integer

Milliseconds for adding properties to the in-memory graph.

nodePropertiesWritten

Integer

The number of properties that were written to Neo4j.

centralityDistribution

Map

Map containing min, max, mean as well as p50, p75, p90, p95, p99 and p999 percentile values of centrality values.

configuration

Map

The configuration used for running the algorithm.

Write

Run PageRank in write mode on a named graph. 

CALL gds.pageRank.write(
  graphName: String,
  configuration: Map
)
YIELD
  nodePropertiesWritten: Integer,
  ranIterations: Integer,
  didConverge: Boolean,
  createMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  writeMillis: Integer,
  centralityDistribution: Map,
  configuration: Map

Table 5.13. Parameters
Name Type Default Optional Description

graphName

String

n/a

no

The name of a graph stored in the catalog.

configuration

Map

{}

yes

Configuration for algorithm-specifics and/or graph filtering.

Table 5.14. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

nodeLabels

String[]

['*']

yes

Filter the named graph using the given node labels.

relationshipTypes

String[]

['*']

yes

Filter the named graph using the given relationship types.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm. Also provides the default value for 'writeConcurrency'.

writeConcurrency

Integer

value of 'concurrency'

yes

The number of concurrent threads used for writing the result to Neo4j.

writeProperty

String

n/a

no

The node property in the Neo4j database to which the score is written.

Table 5.15. Algorithm specific configuration
Name Type Default Optional Description

dampingFactor

Float

0.85

yes

The damping factor of the Page Rank calculation.

maxIterations

Integer

20

yes

The maximum number of iterations of Page Rank to run.

tolerance

Float

0.0000001

yes

Minimum change in scores between iterations. If all scores change less than the tolerance value the result is considered stable and the algorithm returns.

relationshipWeightProperty

String

null

yes

The property name that contains weight. If null, treats the graph as unweighted. Must be numeric.

sourceNodes

List

[]

yes

A set of nodes to use for computing Personalized Page Rank.

Table 5.16. Results
Name Type Description

ranIterations

Integer

The number of iterations run.

didConverge

Boolean

Indicates if the algorithm converged.

createMillis

Integer

Milliseconds for creating the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the centralityDistribution.

writeMillis

Integer

Milliseconds for writing result data back.

nodePropertiesWritten

Integer

The number of properties that were written to Neo4j.

centralityDistribution

Map

Map containing min, max, mean as well as p50, p75, p90, p95, p99 and p999 percentile values of centrality values.

configuration

Map

The configuration used for running the algorithm.

Anonymous graphs

It is also possible to execute the algorithm on a graph that is projected in conjunction with the algorithm execution. In this case, the graph does not have a name, and we call it anonymous. When executing over an anonymous graph the configuration map contains a graph projection configuration as well as an algorithm configuration. All execution modes support execution on anonymous graphs, although we only show syntax and mode-specific configuration for the write mode for brevity.

For more information on syntax variants, see Section 5.1, “Syntax overview”.

Run PageRank in write mode on an anonymous graph: 

CALL gds.pageRank.write(
  configuration: Map
)
YIELD
  nodePropertiesWritten: Integer,
  ranIterations: Integer,
  didConverge: Boolean,
  createMillis: Integer,
  computeMillis: Integer,
  writeMillis: Integer,
  centralityDistribution: Map,
  configuration: Map

Table 5.17. General configuration for algorithm execution on an anonymous graph.
Name Type Default Optional Description

nodeProjection

String, String[] or Map

null

yes

The node projection used for anonymous graph creation via a Native projection.

relationshipProjection

String, String[] or Map

null

yes

The relationship projection used for anonymous graph creation a Native projection.

nodeQuery

String

null

yes

The Cypher query used to select the nodes for anonymous graph creation via a Cypher projection.

relationshipQuery

String

null

yes

The Cypher query used to select the relationships for anonymous graph creation via a Cypher projection.

nodeProperties

String, String[] or Map

null

yes

The node properties to project during anonymous graph creation.

relationshipProperties

String, String[] or Map

null

yes

The relationship properties to project during anonymous graph creation.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm. Also provides the default value for 'readConcurrency' and 'writeConcurrency'.

readConcurrency

Integer

value of 'concurrency'

yes

The number of concurrent threads used for creating the graph.

writeConcurrency

Integer

value of 'concurrency'

yes

The number of concurrent threads used for writing the result to Neo4j.

writeProperty

String

n/a

no

The node property in the Neo4j database to which the score is written.

Table 5.18. Algorithm specific configuration
Name Type Default Optional Description

dampingFactor

Float

0.85

yes

The damping factor of the Page Rank calculation.

maxIterations

Integer

20

yes

The maximum number of iterations of Page Rank to run.

tolerance

Float

0.0000001

yes

Minimum change in scores between iterations. If all scores change less than the tolerance value the result is considered stable and the algorithm returns.

relationshipWeightProperty

String

null

yes

The property name that contains weight. If null, treats the graph as unweighted. Must be numeric.

sourceNodes

List

[]

yes

A set of nodes to use for computing Personalized Page Rank.

The results are the same as for running write mode with a named graph, specified above.

5.2.1.4. Examples

In this section we will show examples of executing the PageRank algorithm on a concrete graph. The intention is to illustrate what the results look like and to provide a guide in how to make use of the algorithm in a real setting. The example graph looks like this:

page rank graph

The following Cypher statement will create the example graph in the Neo4j database: 

CREATE
  (home:Page {name:'Home'}),
  (about:Page {name:'About'}),
  (product:Page {name:'Product'}),
  (links:Page {name:'Links'}),
  (a:Page {name:'Site A'}),
  (b:Page {name:'Site B'}),
  (c:Page {name:'Site C'}),
  (d:Page {name:'Site D'}),

  (home)-[:LINKS {weight: 0.2}]->(about),
  (home)-[:LINKS {weight: 0.2}]->(links),
  (home)-[:LINKS {weight: 0.6}]->(product),
  (about)-[:LINKS {weight: 1.0}]->(home),
  (product)-[:LINKS {weight: 1.0}]->(home),
  (a)-[:LINKS {weight: 1.0}]->(home),
  (b)-[:LINKS {weight: 1.0}]->(home),
  (c)-[:LINKS {weight: 1.0}]->(home),
  (d)-[:LINKS {weight: 1.0}]->(home),
  (links)-[:LINKS {weight: 0.8}]->(home),
  (links)-[:LINKS {weight: 0.05}]->(a),
  (links)-[:LINKS {weight: 0.05}]->(b),
  (links)-[:LINKS {weight: 0.05}]->(c),
  (links)-[:LINKS {weight: 0.05}]->(d);

This graph represents eight pages, linking to one another. Each relationship has a property called weight, which describes the importance of the relationship.

In the examples below we will use named graphs and native projections as the norm. However, anonymous graphs and/or Cypher projections can also be used.

The following statement will create a graph using a native projection and store it in the graph catalog under the name 'myGraph'. 

CALL gds.graph.create(
  'myGraph',
  'Page',
  'LINKS',
  {
    relationshipProperties: 'weight'
  }
)

Memory Estimation

First off, we will estimate the cost of running the algorithm using the estimate procedure. This can be done with any execution mode. We will use the write mode in this example. Estimating the algorithm is useful to understand the memory impact that running the algorithm on your graph will have. When you later actually run the algorithm in one of the execution modes the system will perform an estimation. If the estimation shows that there is a very high probability of the execution going over its memory limitations, the execution is prohibited. To read more about this, see Section 3.1.3, “Automatic estimation and execution blocking”.

For more details on estimate in general, see Section 3.1, “Memory Estimation”.

The following will estimate the memory requirements for running the algorithm: 

CALL gds.pageRank.write.estimate('myGraph', {
  writeProperty: 'pageRank',
  maxIterations: 20,
  dampingFactor: 0.85
})
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory

Table 5.19. Results
nodeCount relationshipCount bytesMin bytesMax requiredMemory

8

14

1560

1560

"1560 Bytes"

Stream

In the stream execution mode, the algorithm returns the score for each node. This allows us to inspect the results directly or post-process them in Cypher without any side effects. For example, we can order the results to find the nodes with the highest PageRank score.

For more details on the stream mode in general, see Section 3.3.1, “Stream”.

The following will run the algorithm in stream mode: 

CALL gds.pageRank.stream('myGraph')
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score
ORDER BY score DESC, name ASC

Table 5.20. Results
name score

"Home"

3.2362017153762284

"About"

1.0611098567023873

"Links"

1.0611098567023873

"Product"

1.0611098567023873

"Site A"

0.3292259009438567

"Site B"

0.3292259009438567

"Site C"

0.3292259009438567

"Site D"

0.3292259009438567

The above query is running the algorithm in stream mode as unweighted. Below is a weighted example

We are using stream mode to illustrate running the algorithm as weighted or unweighted, all the algorithm modes support this configuration parameter.

Stats

In the stats execution mode, the algorithm returns a single row containing a summary of the algorithm result. For example PageRank stats returns centrality histogram which can be used to monitor the distribution of PageRank score values across all computed nodes. This execution mode does not have any side effects. It can be useful for evaluating algorithm performance by inspecting the computeMillis return item. In the examples below we will omit returning the timings. The full signature of the procedure can be found in the syntax section.

For more details on the stats mode in general, see Section 3.3.2, “Stats”.

The following will run the algorithm and returns the result in form of statistical and measurement values. 

CALL gds.pageRank.stats('myGraph', {
  maxIterations: 20,
  dampingFactor: 0.85
})
YIELD centralityDistribution
RETURN centralityDistribution.max AS max

Table 5.21. Results
max

3.236204147338867

The centrality histogram can be useful for inspecting the computed scores or perform normalizations.

Mutate

The mutate execution mode extends the stats mode with an important side effect: updating the named graph with a new node property containing the score for that node. The name of the new property is specified using the mandatory configuration parameter mutateProperty. The result is a single summary row, similar to stats, but with some additional metrics. The mutate mode is especially useful when multiple algorithms are used in conjunction.

For more details on the mutate mode in general, see Section 3.3.3, “Mutate”.

The following will run the algorithm in mutate mode: 

CALL gds.pageRank.mutate('myGraph', {
  maxIterations: 20,
  dampingFactor: 0.85,
  mutateProperty: 'pagerank'
})
YIELD nodePropertiesWritten, ranIterations

Table 5.22. Results
nodePropertiesWritten ranIterations

8

20

Write

The write execution mode extends the stats mode with an important side effect: writing the score for each node as a property to the Neo4j database. The name of the new property is specified using the mandatory configuration parameter writeProperty. The result is a single summary row, similar to stats, but with some additional metrics. The write mode enables directly persisting the results to the database.

For more details on the write mode in general, see Section 3.3.4, “Write”.

The following will run the algorithm in write mode: 

CALL gds.pageRank.write('myGraph', {
  maxIterations: 20,
  dampingFactor: 0.85,
  writeProperty: 'pagerank'
})
YIELD nodePropertiesWritten, ranIterations

Table 5.23. Results
nodePropertiesWritten ranIterations

8

20

Weighted

By default, the algorithm is considering the relationships of the graph to be unweighted, to change this behaviour we can use configuration parameter called relationshipWeightProperty. Below is an example of running the algorithm using this property.

The following will run the algorithm in stream mode using relationship weights: 

CALL gds.pageRank.stream('myGraph', {
  maxIterations: 20,
  dampingFactor: 0.85,
  relationshipWeightProperty: 'weight'
})
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score
ORDER BY score DESC, name ASC

Table 5.24. Results
name score

"Home"

3.5528567278757683

"Product"

1.9541301048360766

"About"

0.7513767024036497

"Links"

0.7513767024036497

"Site A"

0.18167360233856014

"Site B"

0.18167360233856014

"Site C"

0.18167360233856014

"Site D"

0.18167360233856014

We are using stream mode to illustrate running the algorithm as weighted or unweighted, all the algorithm modes support this configuration parameter.

Tolerance

The tolerance configuration parameter denotes the minimum change in scores between iterations. If all scores change less than the configured tolerance value the result stabilises, and the algorithm returns.

The following will run the algorithm in stream mode using bigger tolerance value: 

CALL gds.pageRank.stream('myGraph', {
  maxIterations: 20,
  dampingFactor: 0.85,
  tolerance: 0.1
})
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score
ORDER BY score DESC, name ASC

Table 5.25. Results
name score

"Home"

2.8504480060189965

"About"

0.9325180530548096

"Links"

0.9325180530548096

"Product"

0.9325180530548096

"Site A"

0.3035158541286364

"Site B"

0.3035158541286364

"Site C"

0.3035158541286364

"Site D"

0.3035158541286364

In this example we are using tolerance: 0.1, so the results are a bit different compared to the ones from stream example which is using the default value of tolerance. Note that the nodes 'About', 'Link' and 'Product' now have the same score, while with the default value of tolerance the node 'Product' has higher score than the other two.

Damping Factor

The damping factor configuration parameter accepts values between 0 and 1. If its value is too high then problems of sinks and spider traps may occur, and the values may oscillate so that the algorithm does not converge. If it’s too low then all scores are pushed towards 1, and the result will not sufficiently reflect the structure of the graph.

The following will run the algorithm in stream mode using smaller dampingFactor value: 

CALL gds.pageRank.stream('myGraph', {
  maxIterations: 20,
  dampingFactor: 0.05
})
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score
ORDER BY score DESC, name ASC

Table 5.26. Results
name score

"Home"

1.248730954737198

"About"

0.9708121816856135

"Links"

0.9708121816856135

"Product"

0.9708121816856135

"Site A"

0.9597081215046129

"Site B"

0.9597081215046129

"Site C"

0.9597081215046129

"Site D"

0.9597081215046129

Compared to the results from the stream example which is using the default value of dampingFactor the score values are closer to each other when using dampingFactor: 0.05. Also, note that the nodes 'About', 'Link' and 'Product' now have the same score, while with the default value of dampingFactor the node 'Product' has higher score than the other two.

Personalised PageRank

Personalized PageRank is a variation of PageRank which is biased towards a set of sourceNodes. This variant of PageRank is often used as part of recommender systems.

The following examples show how to run PageRank centered around 'Site A'.

The following will run the algorithm and stream results: 

MATCH (siteA:Page {name: 'Site A'})
CALL gds.pageRank.stream('myGraph', {
  maxIterations: 20,
  dampingFactor: 0.85,
  sourceNodes: [siteA]
})
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score
ORDER BY score DESC, name ASC

Table 5.27. Results
name score

"Home"

0.4015879109501838

"Site A"

0.1690742586266424

"About"

0.11305649263085797

"Links"

0.11305649263085797

"Product"

0.11305649263085797

"Site B"

0.01907425862664241

"Site C"

0.01907425862664241

"Site D"

0.01907425862664241

Comparing these results to the ones from the stream example (which is not using sourceNodes configuration parameter) shows that the 'Site A' node that we used in the sourceNodes list now scores second instead of fourth.