# Harmonic Centrality

## Glossary

Directed

Directed trait. The algorithm is well-defined on a directed graph.

Directed

Directed trait. The algorithm ignores the direction of the graph.

Directed

Directed trait. The algorithm does not run on a directed graph.

Undirected

Undirected trait. The algorithm is well-defined on an undirected graph.

Undirected

Undirected trait. The algorithm ignores the undirectedness of the graph.

Heterogeneous nodes

Heterogeneous nodes fully supported. The algorithm has the ability to distinguish between nodes of different types.

Heterogeneous nodes

Heterogeneous nodes allowed. The algorithm treats all selected nodes similarly regardless of their label.

Heterogeneous relationships

Heterogeneous relationships fully supported. The algorithm has the ability to distinguish between relationships of different types.

Heterogeneous relationships

Heterogeneous relationships allowed. The algorithm treats all selected relationships similarly regardless of their type.

Weighted relationships

Weighted trait. The algorithm supports a relationship property to be used as weight, specified via the relationshipWeightProperty configuration parameter.

Weighted relationships

Weighted trait. The algorithm treats each relationship as equally important, discarding the value of any relationship weight.

Harmonic centrality (also known as valued centrality) is a variant of closeness centrality, that was invented to solve the problem the original formula had when dealing with unconnected graphs. As with many of the centrality algorithms, it originates from the field of social network analysis.

This feature is in the alpha tier. For more information on feature tiers, see API Tiers.

## History and explanation

Harmonic centrality was proposed by Marchiori and Latora in Harmony in the Small World while trying to come up with a sensible notion of "average shortest path".

They suggested a different way of calculating the average distance to that used in the Closeness Centrality algorithm. Rather than summing the distances of a node to all other nodes, the harmonic centrality algorithm sums the inverse of those distances. This enables it deal with infinite values.

The raw harmonic centrality for a node is calculated using the following formula:

`raw harmonic centrality(node) = sum(1 / distance from node to every other node excluding itself)`

As with closeness centrality, we can also calculate a normalized harmonic centrality with the following formula:

`normalized harmonic centrality(node) = sum(1 / distance from node to every other node excluding itself) / (number of nodes - 1)`

In this formula, ∞ values are handled cleanly.

## Use-cases - when to use the Harmonic Centrality algorithm

Harmonic centrality was proposed as an alternative to closeness centrality, and therefore has similar use cases.

For example, we might use it if we’re trying to identify where in the city to place a new public service so that it’s easily accessible for residents. If we’re trying to spread a message on social media we could use the algorithm to find the key influencers that can help us achieve our goal.

## Syntax

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

Harmonic Centrality syntax per mode
The following will run the algorithm and write back results:
``````CALL gds.closeness.harmonic.write(
graphName: String,
configuration: Map
)
YIELD
centralityDistribution: Map,
preProcessingMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
writeMillis: Integer,
nodePropertiesWritten: Integer,
configuration: Map``````
Table 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 2. Configuration
Name Type Default Optional Description

concurrency

int

4

yes

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

int

value of 'concurrency'

yes

writeConcurrency

int

value of 'concurrency'

yes

The number of concurrent threads used for writing the result.

writeProperty

string

N/A

no

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

Table 3. Results
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.

preProcessingMillis

Integer

Milliseconds for preprocessing 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.

The following will run the algorithm and stream results:
``````CALL gds.closeness.harmonic.stream(
graphName: String,
configuration: Map
)
YIELD
nodeId: Integer,
score: Float``````
Table 4. 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. Configuration
Name Type Default Optional Description

nodeLabels

List of String

['*']

yes

Filter the named graph using the given node labels. Nodes with any of the given labels will be included.

relationshipTypes

List of String

['*']

yes

Filter the named graph using the given relationship types. Relationships with any of the given types will be included.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm.

jobId

String

Generated internally

yes

An ID that can be provided to more easily track the algorithm’s progress.

logProgress

Boolean

true

yes

If disabled the progress percentage will not be logged.

Table 6. Results
Name Type Description

nodeId

Integer

Node ID.

score

Float

Harmonic centrality score.

## Alpha syntax

The following will run the algorithm and write back results:
``````CALL gds.alpha.closeness.harmonic.write(configuration: Map)
YIELD nodes, preProcessingMillis, computeMillis, writeMillis, centralityDistribution``````
Table 7. Parameters
Name Type Default Optional Description

concurrency

int

4

yes

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

int

value of 'concurrency'

yes

writeConcurrency

int

value of 'concurrency'

yes

The number of concurrent threads used for writing the result.

writeProperty

string

'centrality'

yes

The property name written back to.

Table 8. Results
Name Type Description

nodes

int

The number of nodes considered.

preProcessingMillis

int

Milliseconds for preprocessing the data.

computeMillis

int

Milliseconds for running the algorithm.

writeMillis

int

Milliseconds for writing result data back.

writeProperty

string

The property name written back to.

centralityDistribution

Map

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

The following will run the algorithm and stream results:
``````CALL gds.alpha.closeness.harmonic.stream(configuration: Map)
YIELD nodeId, centrality``````
Table 9. Parameters
Name Type Default Optional Description

concurrency

int

4

yes

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

int

value of 'concurrency'

yes

Table 10. Results
Name Type Description

node

long

Node ID

centrality

float

Harmonic centrality score

## Harmonic Centrality algorithm example

The following will create a sample graph:
``````CREATE (a:User {name: "Alice"}),
(b:User {name: "Bob"}),
(c:User {name: "Charles"}),
(d:User {name: "Doug"}),
(e:User {name: "Ethan"}),
The following will project and store a named graph:
``````CALL gds.graph.project(
'graph',
'User',
)``````
The following will run the algorithm and stream results:
``````CALL gds.closeness.harmonic.stream('graph', {})
YIELD nodeId, score
RETURN gds.util.asNode(nodeId).name AS user, score
ORDER BY score DESC``````
Table 11. Results
user score

"Bob"

0.5

"Alice"

0.375

"Charles"

0.375

"Doug"

0.25

"Ethan"

0.25

The following will run the algorithm and write back results:
``````CALL gds.closeness.harmonic.write('graph', {writeProperty: 'score'})
YIELD nodePropertiesWritten``````
Table 12. Results
nodePropertiesWritten

5