How to remove nodes iteratively base on their strength in an weighted graph in python

Viewed 43

I am trying to remove node with highest strength one at a time until the graph is disconnected but after the first node is removed I am getting an error that the node doesn't exist in the graph. Is there a way i can fix this? below is my attempt:

def node_strength(G_input):
    G=G_input.copy()
    n= G.number_of_nodes()
    list_nodes=[]
    for i in range(n):
        '''Get sum of all nodes weights'''
        weights=[]
        for j in range (n):
            weights.append(sum(G[j][neigh]['capacity'] for neigh in G.neighbors(j)))
        '''Get node with highest weight'''
        node=weights.index(max(weights))
        list_node.append(node)
        '''removetarget node'''
        G.remove_node(node)
    return list_nodes
0 Answers
Related