All problems
Harmonic Closeness Centrality
Problem
Compute the harmonic closeness centrality of every node of an undirected unweighted graph.
For each node sum 1 / d(v, t) over all other reachable nodes t, ignoring unreachable nodes because their reciprocal distance is zero. BFS from every node gives the distances. edges is a list of [u, v] pairs. Return the list of float scores in node order.
Hint
Reciprocal distances stay finite for disconnected graphs, unlike the classical closeness.
Test cases
case 1
in: [3,[[0,1],[1,2]]]
expected: [1.5,2,1.5]
case 2
in: [4,[[0,1],[0,2],[0,3]]]
expected: [3,2,2,2]
case 3
in: [3,[[0,1]]]
expected: [1,1,0]
case 4
in: [1,[]]
expected: [0]
Starter codePython
def harmonic_closeness(n, edges):
# Your code here
pass4 test cases · run your code against them in the editor