0

Can anyone help with this. I am getting a network and drawing it as a graph as bellow:

g = nx.DiGraph()
//creating the graph......
print(nx.info(g))

nx.info(g) is giving me that number of nodes is 18772.

I got the graph plot for it as follows:

First Graph

however after deleting 30% of the most important nodes as bellow:

thirty_percent = int(g.number_of_nodes() * 0.30)
temp_g = g.copy()
print('initial number of nodes:', g.number_of_nodes())
print('nodes deleted (30%):', thirty_percent)
mostImportantNodes = sorted(nx.degree_centrality(g).items(), key=lambda x: x[1], reverse=True)
for n in range(0, thirty_percent):
    node = mostImportantNodes[n][0]
    temp_g.remove_node(node)
print(nx.info(temp_g))
# drawing the network graph
print('Graph after removing 30% of the most important nodes:')
print(datetime.now())
plt.clf()
pos = nx.spring_layout(temp_g)
nx.draw_networkx_nodes(temp_g, pos, node_size=10)
nx.draw_networkx_edges(temp_g, pos, edge_color='black', alpha=0.1, arrows=True)
plt.show()
print(datetime.now())

It is giving the right info about the new graph, but the graph plot is very bad, it seems like the nodes were added not deleted!

initial number of nodes: 18772
nodes deleted (30%): 5631
Name: 
Type: DiGraph
Number of nodes: 13141
Number of edges: 44911
Average in degree:   3.4176
Average out degree:   3.4176
highest 5 degrees:  [40, 40, 38, 36, 36]

Second Graph

  • Sorry, but I can't reproduce your problem. Can you post smaller code sample which demonstrate your issue? – Laurent LAPORTE Apr 06 '21 at 20:25
  • @LaurentLAPORTE thanks for your attention. i tried to minimize the first part of the demonstration, but i left the second part so that if there are problems in it they will be clear for anyone to help. the problem is that actually deleting 30% of the initial graph is shown to be working fine when i get the nx.info(temp_g), the right number of nodes are deleted, but plotting the new graph temp_g shows an increase in the nodes, i can't relate why is this happening. – Batoul Diab Apr 06 '21 at 20:47
  • Try changing `sorted(nx.degree_centrality(g).items()` to `sorted(nx.degree_centrality(temp_g).items()`. – swatchai Apr 07 '21 at 05:28
  • @swatchai this had no change :/ the plot still appears the same – Batoul Diab Apr 07 '21 at 07:52
  • 1
    I think what you are seeing is the expected behaviour of `spring_layout`. All nodes repel each other (but connected nodes also attract each other). Unconnected nodes will hence continuously move away from the center of all nodes, forming a ring. By removing the highest degree nodes you increased the number of unconnected nodes, and hence made the ring wider. Many layout algorithms assume a single component and don't handle unconnected nodes or components well. I have an answer [here](https://stackoverflow.com/a/53156709/2912349) with some approaches to deal with this issue. – Paul Brodersen Apr 07 '21 at 10:25
  • @PaulBrodersen ok that's now clear! thank you so much for the explanation – Batoul Diab Apr 07 '21 at 18:24

0 Answers0