### 9.2.6. The Harmonic Centrality algorithm

This section describes the Harmonic Centrality algorithm in the Neo4j Labs Graph Algorithms library.

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.

 The Harmonic Centrality algorithm was developed by the Neo4j Labs team and is not officially supported.

This section includes:

#### 9.2.6.1. 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.

#### 9.2.6.2. 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.

#### 9.2.6.3. Harmonic Centrality algorithm sample

The following will create a sample graph:

``````MERGE (a:Node{id:"A"})
MERGE (b:Node{id:"B"})
MERGE (c:Node{id:"C"})
MERGE (d:Node{id:"D"})
MERGE (e:Node{id:"E"})

The following will run the algorithm and stream results:

``````CALL algo.closeness.harmonic.stream('Node', 'LINK') YIELD nodeId, centrality
RETURN nodeId,centrality
ORDER BY centrality DESC
LIMIT 20;``````

The following will run the algorithm and write back results:

``````CALL algo.closeness.harmonic('Node', 'LINK', {writeProperty:'centrality'})

Calculation:

`k = N-1 = 4`

```     A     B     C     D     E
---|-----------------------------
A  | 0     1     2     -     -    // distance between each pair of nodes
B  | 1     0     1     -     -    // or infinite if no path exists
C  | 2     1     0     -     -
D  | -     -     -     0     1
E  | -     -     -     1     0
---|------------------------------
A  | 0     1    1/2    0     0    // inverse
B  | 1     0     1     0     0
C  |1/2    1     0     0     0
D  | 0     0     0     0     1
E  | 0     0     0     1     0
---|------------------------------
sum |1.5    2    1.5    1     1
---|------------------------------
*k |0.37  0.5  0.37  0.25  0.25```

Instead of calculating the farness, we sum the inverse of each cell and multiply by `1/(n-1)`.

#### 9.2.6.4. Huge graph projection

The default label and relationship-type projection has a limitation of 2 billion nodes and 2 billion relationships. Therefore, if our projected graph contains more than 2 billion nodes or relationships, we will need to use huge graph projection.

Set `graph:'huge'` in the config:

``````CALL algo.closeness.harmonic('Node', 'LINK', {graph:'huge'})

#### 9.2.6.5. Cypher projection

If node label and relationship type are not selective enough to describe your subgraph to run the algorithm on, you can use Cypher statements to load or project subsets of your graph. This can also be used to run algorithms on a virtual graph. You can learn more in the Section 2.2, “Cypher projection” section of the manual.

Set `graph:'cypher'` in the config:

``````CALL algo.closeness.harmonic(
'MATCH (p:Node) RETURN id(p) as id',
'MATCH (p1:Node)-[:LINK]-(p2:Node) RETURN id(p1) as source, id(p2) as target',
{graph:'cypher', writeProperty: 'centrality'}
);``````

#### 9.2.6.6. Syntax

The following will run the algorithm and write back results:

``````CALL algo.closeness.harmonic(label:String, relationship:String,
{write:true, writeProperty:'centrality', graph:'huge', concurrency:4})

Table 9.34. Parameters
Name Type Default Optional Description

label

string

null

yes

The label to load from the graph. If null, load all nodes.

relationship

string

null

yes

The relationship type to load from the graph. If null, load all relationships.

write

boolean

true

yes

Specifies if the result should be written back as a node property.

concurrency

int

available CPUs

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.

graph

string

'huge'

yes

Use 'huge' when describing the subset of the graph with label and relationship-type parameter. Use 'cypher' for describing the subset with cypher node statement and relationship statement.

Table 9.35. Results
Name Type Description

nodes

int

The number of nodes considered.

int

evalMillis

int

Milliseconds for running the algorithm.

writeMillis

int

Milliseconds for writing result data back.

The following will run the algorithm and stream results:

``````CALL algo.closeness.harmonic.stream(label:String, relationship:String, {concurrency:4})
YIELD nodeId, centrality``````

Table 9.36. Parameters
Name Type Default Optional Description

label

string

null

yes

The label to load from the graph. If null, load all nodes.

relationship

string

null

yes

The relationship type to load from the graph. If null, load all relationships.

concurrency

int

available CPUs

yes

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

int

value of 'concurrency'

yes

Table 9.37. Results
Name Type Description

node

long

Node ID.

centrality

float

Closeness centrality weight.

#### 9.2.6.7. Graph type support

The Harmonic Centrality algorithm supports the following graph types:

• ✓ undirected, unweighted
• ❏ undirected, weighted