I have a simple code to create a graph,G in networkx.
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib notebook
G = nx.DiGraph()
G.add_edge(1,2); G.add_edge(1,4)
G.add_edge(3,1); G.add_edge(3,2)
G.add_edge(3,4); G.add_edge(2,3)
G.add_edge(4,3)
I want to find "which node in G is connected to the other nodes by a shortest path of length equal to the diameter of G ".
there are two of these combinations, [1,3] and [2,4], which can be found by nx.shortest_path(G, 1) and nx.shortest_path(G, 2),respectively.
Or, for example, if I use nx.shortest_path_length(G, source=2) then I get {2: 0, 3: 1, 1: 2, 4: 2}. so the length=2 is from node 2 to node 4, which is ok.
now, I'm trying to generalise it for all of the nodes to see if I can find the target nodes.
for node in G.nodes():
target = [k for k,v in nx.shortest_path_length(G, node).items() if v == nx.diameter(G)]
print(target)
and I get this odd result:
[3]
[1, 4]
[1, 2]
[]
Can anybody explain what this result means? as I'm trying to apply this method to solve a bigger problem.