Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort graph nodes according to their degree

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?

like image 414
famargar Avatar asked Jan 22 '18 13:01

famargar


People also ask

Can there be more than one topological sorting for a graph?

There can be more than one topological sorting for a graph. For example, another topological sorting of the following graph is “4 5 2 3 1 0”. The first vertex in topological sorting is always a vertex with in-degree as 0 (a vertex with no incoming edges).

How do you find the cycle of an undirected graph?

We print the nodes which have not been visited (nodes that have degree >= 2) as the nodes forming the cycle in the graph. If all vertices have been visited, there is no cycle in the graph. An undirected graph has only 1 vertex whose degree is 2.

What is topological sorting for a DAG?

Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.

How do you sort vertices in topological sorting?

The first vertex in topological sorting is always a vertex with in-degree as 0 (a vertex with no incoming edges). In DFS, we print a vertex and then recursively call DFS for its adjacent vertices.


1 Answers

The following works:

sorted(G.degree, key=lambda x: x[1], reverse=True)
like image 57
rodgdor Avatar answered Oct 10 '22 14:10

rodgdor