Betweenness Centrality
This section describes the Betweenness Centrality algorithm in the Neo4j Graph Data Science library.
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 unweighted shortest paths between all pairs of nodes in a 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 Memory Estimation. 
2. Considerations and sampling
The Betweenness Centrality algorithm can be very resourceintensive to compute. Brandes' approximate algorithm computes singlesource 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.
2.1. 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.
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 Syntax overview.
CALL gds.betweenness.stream(
graphName: String,
configuration: Map
)
YIELD
nodeId: Integer,
score: Float
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

String[] 

yes 
Filter the named graph using the given node labels. 

String[] 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 
Name  Type  Default  Optional  Description 

samplingSize 
Integer 

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

yes 
The seed value for the random number generator that selects start nodes. 
Name  Type  Description 

nodeId 
Integer 
Node ID. 
score 
Float 
Betweenness Centrality score. 
CALL gds.betweenness.stats(
graphName: String,
configuration: Map
)
YIELD
centralityDistribution: Map,
createMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

String[] 

yes 
Filter the named graph using the given node labels. 

String[] 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 
Name  Type  Default  Optional  Description 

samplingSize 
Integer 

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

yes 
The seed value for the random number generator that selects start nodes. 
Name  Type  Description 

centralityDistribution 
Map 
Map containing min, max, mean as well as p50, p75, p90, p95, p99 and p999 percentile values of centrality values. 
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. 
CALL gds.betweenness.mutate(
graphName: String,
configuration: Map
)
YIELD
centralityDistribution: Map,
createMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
mutateMillis: Integer,
nodePropertiesWritten: Integer,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

String[] 

yes 
Filter the named graph using the given node labels. 

String[] 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

mutateProperty 
String 

no 
The node property in the GDS graph to which the centrality is written. 
Name  Type  Default  Optional  Description 

samplingSize 
Integer 

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

yes 
The seed value for the random number generator that selects start nodes. 
Name  Type  Description 

centralityDistribution 
Map 
Map containing min, max, mean as well as p50, p75, p90, p95, p99 and p999 percentile values of centrality values. 
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 inmemory graph. 
nodePropertiesWritten 
Integer 
Number of properties added to the inmemory graph. 
configuration 
Map 
Configuration used for running the algorithm. 
CALL gds.betweenness.write(
graphName: String,
configuration: Map
)
YIELD
centralityDistribution: Map,
createMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
writeMillis: Integer,
nodePropertiesWritten: Integer,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

String[] 

yes 
Filter the named graph using the given node labels. 

String[] 

yes 
Filter the named graph using the given relationship types. 

Integer 

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

Integer 

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

String 

no 
The node property in the Neo4j database to which the centrality is written. 
Name  Type  Default  Optional  Description 

samplingSize 
Integer 

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

yes 
The seed value for the random number generator that selects start nodes. 
Name  Type  Description 

centralityDistribution 
Map 
Map containing min, max, mean as well as p50, p75, p90, p95, p99 and p999 percentile values of centrality values. 
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. 
3.1. 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 modespecific configuration for the write
mode for brevity.
For more information on syntax variants, see Syntax overview.
CALL gds.betweenness.write(
configuration: Map
)
YIELD
centralityDistribution: Map,
createMillis: Integer,
computeMillis: Integer,
writeMillis: Integer,
nodePropertiesWritten: Integer,
configuration: Map
Name  Type  Default  Optional  Description 

nodeProjection 
String, String[] or Map 

yes 
The node projection used for anonymous graph creation via a Native projection. 
relationshipProjection 
String, String[] or Map 

yes 
The relationship projection used for anonymous graph creation a Native projection. 
nodeQuery 
String 

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

yes 
The Cypher query used to select the relationships for anonymous graph creation via a Cypher projection. 
nodeProperties 
String, String[] or Map 

yes 
The node properties to project during anonymous graph creation. 
relationshipProperties 
String, String[] or Map 

yes 
The relationship properties to project during anonymous graph creation. 
Integer 

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

readConcurrency 
Integer 

yes 
The number of concurrent threads used for creating the graph. 
Integer 

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

String 

no 
The node property in the Neo4j database to which the centrality is written. 
Name  Type  Default  Optional  Description 

samplingSize 
Integer 

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

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.
4. Examples
In this section we will show examples of running 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:
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. 
CALL gds.graph.create('myGraph', 'User', 'FOLLOWS')
In the following examples we will demonstrate using the Betweenness Centrality algorithm on this graph.
4.1. 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 Automatic estimation and execution blocking.
For more details on estimate
in general, see Memory Estimation.
CALL gds.betweenness.write.estimate('myGraph', { writeProperty: 'betweenness' })
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
nodeCount  relationshipCount  bytesMin  bytesMax  requiredMemory 

7 
7 
2912 
2912 
"2912 Bytes" 
As is discussed in Considerations and sampling we can configure the memory requirements using the concurrency
configuration parameter.
CALL gds.betweenness.write.estimate('myGraph', { writeProperty: 'betweenness', concurrency: 1 })
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
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.
4.2. Stream
In the stream
execution mode, the algorithm returns the centrality for each node.
This allows us to inspect the results directly or postprocess 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 Stream.
stream
mode:CALL gds.betweenness.stream('myGraph')
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS name, score
ORDER BY name ASC
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.
4.3. 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 Stats.
stats
mode:CALL gds.betweenness.stats('myGraph')
YIELD centralityDistribution
RETURN centralityDistribution.min AS minimumScore, centralityDistribution.mean AS meanScore
minimumScore  meanScore 

0.0 
2.714292253766741 
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 minimum score will almost always be zero.
4.4. 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 Mutate.
mutate
mode:CALL gds.betweenness.mutate('myGraph', { mutateProperty: 'betweenness' })
YIELD centralityDistribution, nodePropertiesWritten
RETURN centralityDistribution.min AS minimumScore, centralityDistribution.mean AS meanScore, nodePropertiesWritten
minimumScore  meanScore  nodePropertiesWritten 

0.0 
2.714292253766741 
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 inmemory graph, see :/managementops/graphcatalogops/index.adoc#cataloggraphlist.
4.5. 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 Write.
write
mode:CALL gds.betweenness.write('myGraph', { writeProperty: 'betweenness' })
YIELD centralityDistribution, nodePropertiesWritten
RETURN centralityDistribution.min AS minimumScore, centralityDistribution.mean AS meanScore, nodePropertiesWritten
minimumScore  meanScore  nodePropertiesWritten 

0.0 
2.714292253766741 
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.
4.6. Sampling
Betweenness Centrality can be very resourceintensive 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.
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
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 threeway 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.
4.7. Undirected
Betweenness Centrality can also be run on undirected graphs.
To illustrate this, we will project our example graph using the UNDIRECTED
orientation.
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. 
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
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.
Was this page helpful?