I was trying to calculate closeness of a graph using igraph
package in R. Sample data that I used for calculation is as follows,
1 2
1 5
2 3
5 4
4 6
2 5
3 4
When I do it manually, the result doesn’t match. This is the definition given by igraph
for closeness
:
Closeness is the inverse of the average length of the shortest paths to/from all the other vertices in the graph
Here is how I manually calculate the closeness
for Node 1:
First I calculate the shortest path from node 1, to each other node:
Node 1 can reach node 2 in 1 step, which is also the shortest path.
Node 1 can reach node 5 in 1 step, which is also the shortest path.
Node 1 can reach node 6 in 3 steps, and so on.
Then, I take the average of these shortest paths:
(1 (1→2) + 1 (1→5) + 2 (1→3) + 2 (1→4) + 3 (1→6))/5 = (1+1+2+2+3)/5 = 9/5
Finally I inverse it, giving a closeness value of 0.555
When I run the closeness
method in R from the igraph
package, the result I obtained for Node 1 is 0.1111.
Can someone help me to find what I missed in the calculation?
?closeness
gives the definition in words as you provided, but the formula on the help page is1/sum( d(v,i), i != v)
which equates to the 0.1111 value, but is the inverse sum rather than the inverse average. Maybe an email to the maintainer? – Gavin Kelly