Deleting nodes with tie=1 in a large NetworkX graph

狂风中的少年 提交于 2021-02-18 19:09:44

问题


I have made large graph with NetworkX with about 20,000 nodes. I would like to delete nodes with only one tie (or zero ties) to try to reduce the clutter. Since it is a very large graph I do not know the nodes by name or ID that have tie=1 or 0.

Does anyone know how to delete these nodes without specifying the node ID or name?

enter image description here


回答1:


Iterating on a Graph g yields all of g's nodes, one at a time -- I believe you can't alter g during the iteration itself, but you can selectively make a list of nodes to be deleted, then remove them all:

to_del = [n for n in g if g.degree(n) <= 1]
g.remove_nodes_from(to_del)



回答2:


I think you're after this one-liner:

G= nx.k_core(G,k=2)

You should be aware that if you delete some nodes, you'll have new nodes whose degree is just 1 or 0. If you want to repeat this process until no such nodes exist, you're generating the "k-core" with k=2. That is you're generating the largest network for which all nodes have degree at least 2. This is a built-in function:

import networkx as nx
G = nx.fast_gnp_random_graph(10000,0.0004) #erdos renyi graph, average degree = 4
G = nx.k_core(G,k=2)

Finally, you could do:

for node in G.nodes():
    if G.degree(node)<2:
        G.remove_node(node)

but this would yield a different result from the 2-core I described above, and a different result from A Martelli's as well since some of the later nodes in the list may originally have degree 2 but be reduced to 1 before you reach them. And it wouldn't be as clean because it creates the list G.nodes() rather than using a nicer iterator (if you're in networkx v1.x and you aren't altering a graph in the loop, it's usually better to loop through the nodes with G.nodes_iter() rather than G.nodes())



来源:https://stackoverflow.com/questions/29268491/deleting-nodes-with-tie-1-in-a-large-networkx-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!