5.2.2. Betweenness Centrality

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

This topic includes:

5.2.2.1. Introduction

Betweenness centrality is a way of detecting the amount of influence a node has over the flow of information in a graph. It is often used to find nodes that serve as a bridge from one part of a graph to another.

The algorithm calculates the shortest paths between all pairs of nodes in an unweighted graph. Each node receives a score, based on the number of shortest paths that pass through the node. Nodes that more frequently lie on shortest paths between other nodes will have higher betweenness centrality scores.

The GDS implementation is based on Brandes' approximate algorithm for unweighted graphs. The implementation requires O(n + m) space and runs in O(n * m) time, where n is the number of nodes and m the number of relationships in the graph.

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.2.2. Considerations and sampling

The Betweenness Centrality algorithm can be very resource-intensive to compute. Brandes' approximate algorithm computes single-source shortest paths (SSSP) for a set of source nodes. When all nodes are selected as source nodes, the algorithm produces an exact result. However, for large graphs this can potentially lead to very long runtimes. Thus, approximating the results by computing the SSSPs for only a subset of nodes can be useful. In GDS we refer to this technique as sampling, where the size of the source node set is the sampling size.

There are two things to consider when executing the algorithm on large graphs:

  • A higher parallelism leads to higher memory consumption as each thread executes SSSPs for a subset of source nodes sequentially.

    • In the worst case, a single SSSP requires the whole graph to be duplicated in memory.
  • A higher sampling size leads to more accurate results, but also to a potentially much longer execution time.

Changing the values of the configuration parameters concurrency and samplingSize, respectively, can help to manage these considerations.

Sampling strategies

Brandes defines several strategies for selecting source nodes. The GDS implementation is based on the random degree selection strategy, which selects nodes with a probability proportional to their degree. The idea behind this strategy is that such nodes are likely to lie on many shortest paths in the graph and thus have a higher contribution to the betweenness centrality score.

5.2.2.3. Syntax

This section covers the syntax used to execute the Betweenness Centrality 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”.

Example 5.2. Betweenness Centrality syntax per mode

Run Betweenness Centrality in stream mode on a named graph. 

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

Table 5.28. 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.29. 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.30. Algorithm specific configuration
Name Type Default Optional Description

samplingSize

Integer

node count

yes

The number of source nodes to consider for computing centrality scores.

samplingSeed

Integer

null

yes

The seed value for the random number generator that selects start nodes.

Table 5.31. Results
Name Type Description

nodeId

Integer

Node ID.

score

Float

Betweenness Centrality score.

Run Betweenness Centrality in stats mode on a named graph. 

CALL gds.betweenness.stats(
  graphName: String,
  configuration: Map
)
YIELD
  minimumScore: Float,
  maximumScore: Float,
  scoreSum: Float,
  createMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  configuration: Map

Table 5.32. 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.33. 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.34. Algorithm specific configuration
Name Type Default Optional Description

samplingSize

Integer

node count

yes

The number of source nodes to consider for computing centrality scores.

samplingSeed

Integer

null

yes

The seed value for the random number generator that selects start nodes.

Table 5.35. Results
Name Type Description

minimumScore

Float

Minimum centrality score.

maximumScore

Float

Maximum centrality score.

scoreSum

Float

Sum of all centrality scores.

createMillis

Integer

Milliseconds for creating the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the statistics.

configuration

Map

Configuration used for running the algorithm.

Run Betweenness Centrality in mutate mode on a named graph. 

CALL gds.betweenness.mutate(
  graphName: String,
  configuration: Map
)
YIELD
  minimumScore: Float,
  maximumScore: Float,
  scoreSum: Float,
  createMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  mutateMillis: Integer,
  nodePropertiesWritten: Integer,
  configuration: Map

Table 5.36. 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.37. 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 centrality is written.

Table 5.38. Algorithm specific configuration
Name Type Default Optional Description

samplingSize

Integer

node count

yes

The number of source nodes to consider for computing centrality scores.

samplingSeed

Integer

null

yes

The seed value for the random number generator that selects start nodes.

Table 5.39. Results
Name Type Description

minimumScore

Float

Minimum centrality score.

maximumScore

Float

Maximum centrality score.

scoreSum

Float

Sum of all centrality scores.

createMillis

Integer

Milliseconds for creating the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the statistics.

mutateMillis

Integer

Milliseconds for adding properties to the in-memory graph.

nodePropertiesWritten

Integer

Number of properties added to the in-memory graph.

configuration

Map

Configuration used for running the algorithm.

Run Betweenness Centrality in write mode on a named graph. 

CALL gds.betweenness.write(
  graphName: String,
  configuration: Map
)
YIELD
  minimumScore: Float,
  maximumScore: Float,
  scoreSum: Float,
  createMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  writeMillis: Integer,
  nodePropertiesWritten: Integer,
  configuration: Map

Table 5.40. 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.41. 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 centrality is written.

Table 5.42. Algorithm specific configuration
Name Type Default Optional Description

samplingSize

Integer

node count

yes

The number of source nodes to consider for computing centrality scores.

samplingSeed

Integer

null

yes

The seed value for the random number generator that selects start nodes.

Table 5.43. Results
Name Type Description

minimumScore

Float

Minimum centrality score.

maximumScore

Float

Maximum centrality score.

scoreSum

Float

Sum of all centrality scores.

createMillis

Integer

Milliseconds for creating the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the statistics.

writeMillis

Integer

Milliseconds for writing result data back.

nodePropertiesWritten

Integer

Number of properties written to Neo4j.

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 Betweenness Centrality in write mode on an anonymous graph: 

CALL gds.betweenness.write(
  configuration: Map
)
YIELD
  minimumScore: Float,
  maximumScore: Float,
  scoreSum: Float,
  createMillis: Integer,
  computeMillis: Integer,
  writeMillis: Integer,
  nodePropertiesWritten: Integer,
  configuration: Map

Table 5.44. 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 centrality is written.

Table 5.45. Algorithm specific configuration
Name Type Default Optional Description

samplingSize

Integer

node count

yes

The number of source nodes to consider for computing centrality scores.

samplingSeed

Integer

null

yes

The seed value for the random number generator that selects start nodes.

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

5.2.2.4. Examples

In this section we will show examples of executing the Betweenness Centrality 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. We will do this on a small social network graph of a handful nodes connected in a particular pattern. The example graph looks like this:

betweenness centrality

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

CREATE
  (alice:User {name: 'Alice'}),
  (bob:User {name: 'Bob'}),
  (carol:User {name: 'Carol'}),
  (dan:User {name: 'Dan'}),
  (eve:User {name: 'Eve'}),
  (frank:User {name: 'Frank'}),
  (gale:User {name: 'Gale'}),

  (alice)-[:FOLLOWS]->(carol),
  (bob)-[:FOLLOWS]->(carol),
  (carol)-[:FOLLOWS]->(dan),
  (carol)-[:FOLLOWS]->(eve),
  (dan)-[:FOLLOWS]->(frank),
  (eve)-[:FOLLOWS]->(frank),
  (frank)-[:FOLLOWS]->(gale);

With the graph in Neo4j we can now project it into the graph catalog to prepare it for algorithm execution. We do this using a native projection targeting the User nodes and the FOLLOWS relationships.

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', 'User', 'FOLLOWS')

In the following examples we will demonstrate using the Betweenness Centrality algorithm on this graph.

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.betweenness.write.estimate('myGraph', { writeProperty: 'betweenness' })
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory

Table 5.46. Results
nodeCount relationshipCount bytesMin bytesMax requiredMemory

7

7

2912

2912

"2912 Bytes"

As is discussed in Section 5.2.2.2, “Considerations and sampling” we can configure the memory requirements using the concurrency configuration parameter.

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

CALL gds.betweenness.write.estimate('myGraph', { writeProperty: 'betweenness', concurrency: 1 })
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory

Table 5.47. Results
nodeCount relationshipCount bytesMin bytesMax requiredMemory

7

7

848

848

"848 Bytes"

Here we can note that the estimated memory requirements were lower than when running with the default concurrency setting. Similarly, using a higher value will increase the estimated memory requirements.

Stream

In the stream execution mode, the algorithm returns the centrality 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 betweenness centrality.

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.betweenness.stream('myGraph')
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score
ORDER BY name ASC

Table 5.48. Results
name score

"Alice"

0.0

"Bob"

0.0

"Carol"

8.0

"Dan"

3.0

"Eve"

3.0

"Frank"

5.0

"Gale"

0.0

We note that the 'Carol' node has the highest score, followed by the 'Frank' node. Studying the example graph we can see that these nodes are in bottleneck positions in the graph. The 'Carol' node connects the 'Alice' and 'Bob' nodes to all other nodes, which increases its score. In particular, the shortest path from 'Alice' or 'Bob' to any other reachable node passes through 'Carol'. Similarly, all shortest paths that lead to the 'Gale' node passes through the 'Frank' node. Since 'Gale' is reachable from each other node, this causes the score for 'Frank' to be high.

Conversely, there are no shortest paths that pass through either of the nodes 'Alice', 'Bob' or 'Gale' which causes their betweenness centrality score to be zero.

Stats

In the stats execution mode, the algorithm returns a single row containing a summary of the algorithm result. In particular, Betweenness Centrality returns the minimum, maximum and sum of all centrality scores. 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 in stats mode: 

CALL gds.betweenness.stats('myGraph')
YIELD minimumScore, maximumScore, scoreSum

Table 5.49. Results
minimumScore maximumScore scoreSum

0.0

8.0

19.0

Comparing this to the results we saw in the stream example, we can find our minimum and maximum values from the table. It is worth noting that unless the graph has a particular shape involving a directed cycle, the minimumScore value will almost always be zero. The scoreSum value can be used to compute an average centrality value by dividing over the number of nodes in the graph.

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 centrality 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.betweenness.mutate('myGraph', { mutateProperty: 'betweenness' })
YIELD minimumScore, maximumScore, scoreSum, nodePropertiesWritten

Table 5.50. Results
minimumScore maximumScore scoreSum nodePropertiesWritten

0.0

8.0

19.0

7

The returned result is the same as in the stats example. Additionally, the graph 'myGraph' now has a node property betweenness which stores the betweenness centrality score for each node. To find out how to inspect the new schema of the in-memory graph, see Section 4.1.2, “Listing graphs in the catalog”.

Write

The write execution mode extends the stats mode with an important side effect: writing the centrality 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.betweenness.write('myGraph', { writeProperty: 'betweenness' })
YIELD minimumScore, maximumScore, scoreSum, nodePropertiesWritten

Table 5.51. Results
minimumScore maximumScore scoreSum nodePropertiesWritten

0.0

8.0

19.0

7

The returned result is the same as in the stats example. Additionally, each of the seven nodes now has a new property betweenness in the Neo4j database, containing the betweenness centrality score for that node.

Sampling

Betweenness Centrality can be very resource-intensive to compute. To help with this, it is possible to approximate the results using a sampling technique. The configuration parameters samplingSize and samplingSeed are used to control the sampling. We illustrate this on our example graph by approximating Betweenness Centrality with a sampling size of two. The seed value is an arbitrary integer, where using the same value will yield the same results between different runs of the procedure.

The following will run the algorithm in stream mode with a sampling size of two: 

CALL gds.betweenness.stream('myGraph', {samplingSize: 2, samplingSeed: 0})
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score
ORDER BY name ASC

Table 5.52. Results
name score

"Alice"

0.0

"Bob"

0.0

"Carol"

4.0

"Dan"

2.0

"Eve"

2.0

"Frank"

2.0

"Gale"

0.0

Here we can see that the 'Carol' node has the highest score, followed by a three-way tie between the 'Dan', 'Eve', and 'Frank' nodes. We are only sampling from two nodes, where the probability of a node being picked for the sampling is proportional to its outgoing degree. The 'Carol' node has the maximum degree and is the most likely to be picked. The 'Gale' node has an outgoing degree of zero and is very unlikely to be picked. The other nodes all have the same probability to be picked.

With our selected sampling seed of 0, we seem to have selected either of the 'Alice' and 'Bob' nodes, as well as the 'Carol' node. We can see that because either of 'Alice' and 'Bob' would add four to the score of the 'Carol' node, and each of 'Alice', 'Bob', and 'Carol' adds one to all of 'Dan', 'Eve', and 'Frank'.

To increase the accuracy of our approximation, the sampling size could be increased. In fact, setting the samplingSize to the node count of the graph (seven, in our case) will produce exact results.

Undirected

Betweenness Centrality can also be run on undirected graphs. To illustrate this, we will project our example graph using the UNDIRECTED orientation.

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

CALL gds.graph.create('myUndirectedGraph', 'User', {FOLLOWS: {orientation: 'UNDIRECTED'}})

Now we can run Betweenness Centrality on our undirected graph. The algorithm automatically figures out that the graph is undirected.

Running the algorithm on an undirected graph is about twice as computationally intensive compared to a directed graph.

The following will run the algorithm in stream mode on the undirected graph: 

CALL gds.betweenness.stream('myUndirectedGraph')
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score
ORDER BY name ASC

Table 5.53. Results
name score

"Alice"

0.0

"Bob"

0.0

"Carol"

9.5

"Dan"

3.0

"Eve"

3.0

"Frank"

5.5

"Gale"

0.0

The central nodes now have slightly higher scores, due to the fact that there are more shortest paths in the graph, and these are more likely to pass through the central nodes. The 'Dan' and 'Eve' nodes retain the same centrality scores as in the directed case.