[SOLVED] How to Change a Node, Python – Networkx

Issue

This Content is from Stack Overflow. Question asked by user2429676

I am planning a graph. I would like to build a simplified graph. Then expand it. This expansion will include changing some of the nodes in to other graphs. Since a dictionary key is immutable, are there ways to change the node from one object type to another?



Solution

I think the best approach is to make a new graph whose edges have the data type that you’re after. For example, consider the following.

G = nx.path_graph(5) # graph with edges [(0, 1), (1, 2), (2, 3), (3, 4)]
G_new = nx.Graph()
for a,b in G.edges:
    G_new.add_edge(float(a),float(b))

The graph G_new has edges [(0.0, 1.0), (1.0, 2.0), (2.0, 3.0), (3.0, 4.0)].


This Question was asked in StackOverflow by user2429676 and Answered by Ben Grossmann It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?