Need help for resolving error in implementing incremental_closeness centrality

I have a data file in .txt format: The data is shown as

a_8358:b_5207, 0.3333333333333333
a_8357:b_5207, 0.3333333333333333
a_8358:b_5209, 0.5
a_8357:b_5209, 0.5
a_8358:b_5208, 0.0
a_8357:b_5208, 0.0
b_5207:c_1954, 0.1111111111111111
b_5207:c_1961, 0.2
b_5207:c_1957, 0.14285714285714285
b_5207:c_14167, 0.125
b_5207:c_37159, 0.14285714285714285
b_5209:c_1954, 0.125
b_5209:c_1961, 0.25
b_5209:c_1957, 0.16666666666666666
b_5209:c_14167, 0.14285714285714285
b_5209:c_37159, 0.16666666666666666
b_5208:c_1954, 0.0
b_5208:c_1961, 0.0
b_5208:c_1957, 0.0
b_5208:c_14167, 0.0
b_5208:c_37159, 0.0
d_3389:b_5207, 0.14285714285714285
d_22862:b_5207, 0.2
d_3389:b_5209, 0.16666666666666666
d_22862:b_5209, 0.25
d_3389:b_5208, 0.0
d_22862:b_5208, 0.0
e_1961:b_5207, 0.2
e_1961:b_5209, 0.25
e_1961:b_5208, 0.0
a_8358:c_1954, 0.1
a_8358:c_1961, 0.16666666666666666
a_8358:c_1957, 0.1111111111111111
a_8358:c_14167, 0.1
a_8358:c_37159, 0.1
a_8357:c_1954, 0.14285714285714285
a_8357:c_1961, 0.16666666666666666
a_8357:c_1957, 0.2
a_8357:c_14167, 0.16666666666666666
a_8357:c_37159, 0.25
d_3389:a_8358, 0.1
d_22862:a_8358, 0.125
d_3389:a_8357, 0.25
d_22862:a_8357, 0.5
e_1961:a_8358, 0.16666666666666666
e_1961:a_8357, 0.16666666666666666
d_3389:c_1954, 0.14285714285714285
d_22862:c_1954, 0.14285714285714285
d_3389:c_1961, 0.16666666666666666
d_22862:c_1961, 0.25
d_3389:c_1957, 0.2
d_22862:c_1957, 0.2
d_3389:c_14167, 0.16666666666666666
d_22862:c_14167, 0.16666666666666666
d_3389:c_37159, 0.125
d_22862:c_37159, 0.25
e_1961:c_1954, 0.16666666666666666
e_1961:c_1961, 0.5
e_1961:c_1957, 0.2
e_1961:c_14167, 0.16666666666666666
e_1961:c_37159, 0.16666666666666666
d_3389:e_1961, 0.16666666666666666
d_22862:e_1961, 0.25

I tried to implement incremental_closeness centrality as

import networkx as nx
# Calculate initial closeness centrality for each node
initial_closeness = nx.closeness_centrality(G)

# Print initial closeness centrality values
print("Initial closeness centrality:")
for node, value in initial_closeness.items():
    print(f"{node}: {value}")

# Make changes to the graph (e.g. add/remove edges or nodes)

# Calculate incremental closeness centrality for each node
incremental_closeness = {}
for node in G.nodes:
    # Create a copy of the graph with the current node removed
    H = G.copy()
    H.remove_node(node)
    # Calculate the closeness centrality of the remaining nodes in the new graph
    remaining_closeness = nx.closeness_centrality(H)
    # Calculate the difference in closeness centrality for the current node
    delta = initial_closeness[node] - remaining_closeness[node]
    incremental_closeness[node] = delta

# Print incremental closeness centrality values
print("Incremental closeness centrality:")
for node, value in incremental_closeness.items():
    print(f"{node}: {value}")

However, I am getting the following error:

KeyError                                  Traceback (most recent call last)
<ipython-input-28-5ac9eb6debea> in <module>
     20     remaining_closeness = nx.closeness_centrality(H)
     21     # Calculate the difference in closeness centrality for the current node
---> 22     delta = initial_closeness[node] - remaining_closeness[node]
     23     incremental_closeness[node] = delta
     24 

KeyError: 'a_8358'

Please help to resolve the error

You’re deleting the node here:

And then you try to access the (deleted) node a few lines later:

Remove H.remove_node(node) and it should work.

This is the output that I am getting after removing H.remove_node(node):

Incremental closeness centrality:
a_8358: 0.0
b_5207: 0.0
a_8357: 0.0
b_5209: 0.0
b_5208: 0.0
c_1954: 0.0
c_1961: 0.0
c_1957: 0.0
c_14167: 0.0
c_37159: 0.0
d_3389: 0.0
d_22862: 0.0
e_1961: 0.0

Please help

I’m not familiar with graph theory. What output do you expect?

According to incremental closeness centrality, there should be non-negative, non-zero numbers for each vertex in graph

OK. Again, not familiar with graph theory so you will have to provide the mathematical context for what you want to do. What is “incremental closeness” and how is it calculated?