Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a single node of a networkx graph?

I wanted to know how I can change a single node name of a node of a digraph. I am new to networkx and could only find answers on how to change all node names.

In my case I am iterating over a graph A to create graph B. p and c are nodes of graph A. The edge (p,c) of graph A contains data I want to add to the node p of B. However, when I am adding the edge data from graph A to the already existing node p of graph B, I would like to update the name of p to be equal to the name of c so I am able to reference it again for the next edge of graph A because it then is the edge (c,x) and I can use the c to reference it again...

The relevant part of my code looks like this

new_stages = A.in_edge(c, data='stages')
stages = B.node[p]['stages']
stages.append(new_stages)
<<Update node p to have name of c??>> 
B.add_node(p, stages=new_stage_set)

Any help is appreciated, thanks!

like image 358
0xDr0id Avatar asked Jan 24 '23 17:01

0xDr0id


1 Answers

You have nx.relabel_nodes for this. Here's a simple use case:

G = nx.from_edgelist([('a','b'), ('f','g')])

mapping = {'b':'c'}
G = nx.relabel_nodes(G, mapping)

G.edges()
# EdgeView([('a', 'c'), ('f', 'g')])
like image 164
yatu Avatar answered Jan 29 '23 20:01

yatu