Sort graph nodes according to their degree

famargar picture famargar · Jan 22, 2018 · Viewed 10.6k times · Source

I have a graph G in networkx and would like to sort the nodes according to their degree. However, the following code does not work in latest networkx versions:

sorted(set(G.degree().values()))

and the following seems a bit clunky as it requires converting the networkx DegreeView to a python list of tuples

degrees = [(node,val) for (node, val) in G.degree()]
sorted(degrees, key=lambda x: x[1], reverse=True)

is there any better way?

Answer

rodgdor picture rodgdor · Jan 22, 2018

The following works:

sorted(G.degree, key=lambda x: x[1], reverse=True)