Search code examples
pythonnetworkx

How does average_neighbor_degree method works in networkX for directed graphs?


Could someone explain me how does method average_neighbor_degree from networkX works? For normal graphs (not directed) results match with my intuition and my hand calculation, but I have problem with directed graphs - I don't understand how it's calculated and my hand calculation give different results then this method. My graph:

[Example directed graph]

1

I've run average_neighbor_degree with every possible combination with params source and target which implies basic attributes for directed graph. Here's my code and results:

G = nx.DiGraph([(0, 3), (1, 3), (2, 4), (3, 5), (3, 6), (4, 6), (6, 5), (4, 3)])

print(f'in-in: {nx.average_neighbor_degree(G, source="in", target= "in")}')
print(f'in-out: {nx.average_neighbor_degree(G, source="in", target= "out")}')
print(f'out-in: {nx.average_neighbor_degree(G, source="out", target= "in")}')
print(f'out-out: {nx.average_neighbor_degree(G, source="out", target= "out")}')

in-in: {0: 3.0, 3: 1.3333333333333333, 1: 3.0, 2: 1.0, 4: 5.0, 5: 0.0, 6: 1.0}
in-out: {0: 2.0, 3: 0.3333333333333333, 1: 2.0, 2: 2.0, 4: 3.0, 5: 0.0, 6: 0.0}
out-in: {0: 3.0, 3: 2.0, 1: 3.0, 2: 1.0, 4: 2.5, 5: 0.0, 6: 2.0}
out-out: {0: 2.0, 3: 0.5, 1: 2.0, 2: 2.0, 4: 1.5, 5: 0.0, 6: 0.0}

Looking at this graph I don't understand how this is calculated and from where those results come from. Could someone explain it please?

Environment: Python 3.8.8 NetworkX 2.5


Solution

  • I think you ran into this issue. The old code contained some weird normalisation (setting to 1 if respective degree is 0). This was changed in 2.6. The latest version, see code here should work as intended.