G.add_edges_from also adds to the node list

68 views
Skip to first unread message

Emma Van Hoogmoed

unread,
Jun 18, 2022, 5:52:57 PM6/18/22
to networkx-discuss
Hello, 

I was hoping someone could help me figure out what I'm doing wrong. Here is my code:
def gnp_random_connected_graph(env,n, p):
    graph_seed = np.random.default_rng(2021)

    network = nx.Graph()
    node_dict = {}
    for node in range(n):
      new_node = Node(env,node, network)
      node_dict[node] = new_node

    network.add_nodes_from(node_dict.items())
     print("Network nodes 1:",network.nodes)
    edges = combinations(list(range(1,(n+1),1)), 2) 
    link_dict = {}
    for id, node_edges in groupby(edges, key=lambda x: x[0]): 
        rate = random.randint(8000000,40000000) #bps
        distance = random.randint(10,185) #meters
        node_edges = list(node_edges) 
        random_edge = tuple(graph_seed.choice(node_edges))
        link_dict[random_edge] = Link(env,random_edge,rate,distance,network)
        for e in node_edges:  
            if graph_seed.random() < p: 
                link_dict[e] = Link(env,e,rate,distance,network)

    
    network.add_edges_from(link_dict.items())
    print("Network nodes 2:",network.nodes)
 
    return network

The first print "Network nodes 1" will give me the following: [(0, <__main__.Node object at 0x7f2a2e43af10>), (1, <__main__.Node object at 0x7f2a2e43afd0>), (2, <__main__.Node object at 0x7f2a2e4200d0>), (3, <__main__.Node object at 0x7f2a2e420190>), (4, <__main__.Node object at 0x7f2a2e420250>)] 

Which is exactly what I want. But after creating the edges and adding then to the network the second print statement("Network nodes 2") gives me this: [(0, <__main__.Node object at 0x7f2a2e2db350>), (1, <__main__.Node object at 0x7f2a2e2db410>), (2, <__main__.Node object at 0x7f2a2e2db4d0>), (3, <__main__.Node object at 0x7f2a2e2db590>), (4, <__main__.Node object at 0x7f2a2e2db650>), (1, 5), <__main__.Link object at 0x7f2a2e36d990>, (2, 5), <__main__.Link object at 0x7f2a2e334ad0>, (2, 3), <__main__.Link object at 0x7f2a2e2db910>, (3, 4), <__main__.Link object at 0x7f2a2e2db7d0>, (4, 5), <__main__.Link object at 0x7f2a2e2db790>]

Clearly, the add_edges_from command is also adding them to the node list. Is this a bug in the library or is there another command I should be using besides G.nodes to access just the nodes and not the link and nodes


Dan Schult

unread,
Jun 18, 2022, 9:47:25 PM6/18/22
to networkx...@googlegroups.com
I think you are using `items()` when you add nodes, so the nodes become 2-tuples with the key-value pair generated by node_dict.items() in 
network.add_nodes_from(node_dict.items())
Just take off the .items() and your nodes will be integers instead of 2-tuples.

Nicolas Cadieux

unread,
Jun 19, 2022, 12:58:00 AM6/19/22
to networkx...@googlegroups.com
Hi,

Clearly, others will have a better knowledge than me but my experience is that if you add and edge, then the first and last nodes will be automatically added to the node list. A node can exist without and edge but not the other way around.

At least, that’s what I figured…


Le 18 juin 2022 à 17:52, 'Emma Van Hoogmoed' via networkx-discuss <networkx...@googlegroups.com> a écrit :

Hello, 
--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/networkx-discuss/eeb34ebc-7bf4-4a8b-a74d-e7f3471195dbn%40googlegroups.com.

Emma Van Hoogmoed

unread,
Jun 19, 2022, 7:55:06 PM6/19/22
to networkx-discuss

That was it! I wasn't attaching them to the nodes correctly. Thank you.
Reply all
Reply to author
Forward
0 new messages