Skip to main content
added 8 characters in body
Source Link
Amedeo
  • 443
  • 3
  • 9

I know this is not a typical answer, but I decided to post this tweak to help others when they are facing the same problem. I have added another initiation for the graph method using the same name of the graph g=nx.graph() just before the definition of the sorted nodes, such that the graph will consider the sorted nodes as g.nodes, the code will be such that

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[1], reverse=True)
# extract the sorted nodes 
nodes = []
for e in range(len(sorted_edges)):
    temp = sorted_edges[e][0]
    nodes.append(temp)
g=nx.graph() # this line was added to restart the graph definition
g.add_nodes_from(nodes)
g.add_weighted_edges_from(weighted_edges)
print (g.nodes)
output: NodeView((2, 3, 4, 5, 1)) # g.nodes are sorted according to the node degree

I know this is not a typical answer, but I decided to post this tweak to help others when they are facing the same problem. I have added another initiation for the graph method using the same name of the graph g=nx.graph() just before the definition of the sorted nodes, such that the graph will consider the sorted nodes as g.nodes, the code will be such that

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[1], reverse=True)
# extract the sorted nodes 
nodes = []
for e in range(len(sorted_edges)):
temp = sorted_edges[e][0]
nodes.append(temp)
g=nx.graph() # this line was added to restart the graph definition
g.add_nodes_from(nodes)
g.add_weighted_edges_from(weighted_edges)
print (g.nodes)
output: NodeView((2, 3, 4, 5, 1)) # g.nodes are sorted according to the node degree

I know this is not a typical answer, but I decided to post this tweak to help others when they are facing the same problem. I have added another initiation for the graph method using the same name of the graph g=nx.graph() just before the definition of the sorted nodes, such that the graph will consider the sorted nodes as g.nodes, the code will be such that

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[1], reverse=True)
# extract the sorted nodes 
nodes = []
for e in range(len(sorted_edges)):
    temp = sorted_edges[e][0]
    nodes.append(temp)
g=nx.graph() # this line was added to restart the graph definition
g.add_nodes_from(nodes)
g.add_weighted_edges_from(weighted_edges)
print (g.nodes)
output: NodeView((2, 3, 4, 5, 1)) # g.nodes are sorted according to the node degree
Source Link
Amedeo
  • 443
  • 3
  • 9

I know this is not a typical answer, but I decided to post this tweak to help others when they are facing the same problem. I have added another initiation for the graph method using the same name of the graph g=nx.graph() just before the definition of the sorted nodes, such that the graph will consider the sorted nodes as g.nodes, the code will be such that

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[1], reverse=True)
# extract the sorted nodes 
nodes = []
for e in range(len(sorted_edges)):
temp = sorted_edges[e][0]
nodes.append(temp)
g=nx.graph() # this line was added to restart the graph definition
g.add_nodes_from(nodes)
g.add_weighted_edges_from(weighted_edges)
print (g.nodes)
output: NodeView((2, 3, 4, 5, 1)) # g.nodes are sorted according to the node degree