In an undirected graph, I would like to order its nodes according to their degree. However, to do this I have to enter the edges of the graph first. When I do this, NetworkX in Python reads the nodes from the entries of the edges and doesn't consider the contribution of the
add_nodes_from command. Is there any way to overcome this problem? Here is a sample of code I'm using:
g = nx.Graph() weighted_edges = [(4,3,150),(1,2,15),(1,4,100),(1,3,100),(1,5,130) ,(2,3,205),(2,4,211),(2,5,200),(3,5,100),(4,5,10)] #Sorting edges according to node degree sorted_edges = sorted(g.degree(weight='weight'), key=lambda x: x, reverse=True) # extract the sorted nodes nodes =  for e in range(len(sorted_edges)): temp = sorted_edges[e] nodes.append(temp) g.add_nodes_from(nodes) g.add_weighted_edges_from(weighted_edges)
according to the above sample of code,
print(nodes) output is an empty set, since
add_weighted_edges_from is coming after. When we add the edges first,
g.nodes outputs following the order edge entries. nodes are meant to be the sorted nodes according to their degree, and g.nodes are the nodes according to the graph edges entries.
I appreciate your help!