I have build a graph based on networkx in which edges represent distance between them.
GraphLoc.G.add_edge(fnode_id, snode_id, score=score)
score is the edge weight.
I am not able to find API which can provide neighboring nodes which has edge and results are in sorted order of weight.
Obviously i can also sort on my own , but i don't want run-time computation. So does networkx provide any solution for this
Selecting a single node will return its neighbours. You can then sort the edge list yourself fairly easily. First, we set up the graph.
If we wanted
a
's neighbors, we just access that node directly:To sort those results, we use tools from the standard Python toolbox.
.items()
to convert thedict
to atuple
and thesorted
builtin to sort the results:If you need to make the relationship between the results and your original node explicit, it's easy to include this in the result with a list comprehension: