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:

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.

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.

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"})
MERGE (a)-[:LINK]->(b)
MERGE (b)-[:LINK]->(c)
MERGE (d)-[:LINK]->(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'})
YIELD nodes,loadMillis, computeMillis, writeMillis;
```

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)`

.

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'})
YIELD nodes,loadMillis, computeMillis, writeMillis;
```

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'}
);
```

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})
YIELD nodes, loadMillis, computeMillis, writeMillis
```

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'. |

readConcurrency |
int |
value of 'concurrency' |
yes |
The number of concurrent threads used for reading the graph. |

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

Name | Type | Description |
---|---|---|

nodes |
int |
The number of nodes considered. |

loadMillis |
int |
Milliseconds for loading data. |

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
```

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'. |

readConcurrency |
int |
value of 'concurrency' |
yes |
The number of concurrent threads used for reading the graph. |

Name | Type | Description |
---|---|---|

node |
long |
Node ID. |

centrality |
float |
Closeness centrality weight. |

The Harmonic Centrality algorithm supports the following graph types:

- ✓ undirected, unweighted
- ❏ undirected, weighted