
    h<(                         S SK rSS/r\R                  " SSS0SS9SS j5       rS	 rS
 r\R                  " SSS9 SS j5       rg)    Nconvert_node_labels_to_integersrelabel_nodesTznot copy   )preserve_all_attrsmutates_inputreturns_graphc                     [        U5      (       a  U  Vs0 s H  o3U" U5      _M     snOUnU(       a  [        X5      $ [        X5      $ s  snf )a3  Relabel the nodes of the graph G according to a given mapping.

The original node ordering may not be preserved if `copy` is `False` and the
mapping includes overlap between old and new labels.

Parameters
----------
G : graph
   A NetworkX graph

mapping : dictionary
   A dictionary with the old labels as keys and new labels as values.
   A partial mapping is allowed. Mapping 2 nodes to a single node is allowed.
   Any non-node keys in the mapping are ignored.

copy : bool (optional, default=True)
   If True return a copy, or if False relabel the nodes in place.

Examples
--------
To create a new graph with nodes relabeled according to a given
dictionary:

>>> G = nx.path_graph(3)
>>> sorted(G)
[0, 1, 2]
>>> mapping = {0: "a", 1: "b", 2: "c"}
>>> H = nx.relabel_nodes(G, mapping)
>>> sorted(H)
['a', 'b', 'c']

Nodes can be relabeled with any hashable object, including numbers
and strings:

>>> import string
>>> G = nx.path_graph(26)  # nodes are integers 0 through 25
>>> sorted(G)[:3]
[0, 1, 2]
>>> mapping = dict(zip(G, string.ascii_lowercase))
>>> G = nx.relabel_nodes(G, mapping)  # nodes are characters a through z
>>> sorted(G)[:3]
['a', 'b', 'c']
>>> mapping = dict(zip(G, range(1, 27)))
>>> G = nx.relabel_nodes(G, mapping)  # nodes are integers 1 through 26
>>> sorted(G)[:3]
[1, 2, 3]

To perform a partial in-place relabeling, provide a dictionary
mapping only a subset of the nodes, and set the `copy` keyword
argument to False:

>>> G = nx.path_graph(3)  # nodes 0-1-2
>>> mapping = {0: "a", 1: "b"}  # 0->'a' and 1->'b'
>>> G = nx.relabel_nodes(G, mapping, copy=False)
>>> sorted(G, key=str)
[2, 'a', 'b']

A mapping can also be given as a function:

>>> G = nx.path_graph(3)
>>> H = nx.relabel_nodes(G, lambda x: x**2)
>>> list(H)
[0, 1, 4]

In a multigraph, relabeling two or more nodes to the same new node
will retain all edges, but may change the edge keys in the process:

>>> G = nx.MultiGraph()
>>> G.add_edge(0, 1, value="a")  # returns the key for this edge
0
>>> G.add_edge(0, 2, value="b")
0
>>> G.add_edge(0, 3, value="c")
0
>>> mapping = {1: 4, 2: 4, 3: 4}
>>> H = nx.relabel_nodes(G, mapping, copy=True)
>>> print(H[0])
{4: {0: {'value': 'a'}, 1: {'value': 'b'}, 2: {'value': 'c'}}}

This works for in-place relabeling too:

>>> G = nx.relabel_nodes(G, mapping, copy=False)
>>> print(G[0])
{4: {0: {'value': 'a'}, 1: {'value': 'b'}, 2: {'value': 'c'}}}

Notes
-----
Only the nodes specified in the mapping will be relabeled.
Any non-node keys in the mapping are ignored.

The keyword setting copy=False modifies the graph in place.
Relabel_nodes avoids naming collisions by building a
directed graph from ``mapping`` which specifies the order of
relabelings. Naming collisions, such as a->b, b->c, are ordered
such that "b" gets renamed to "c" before "a" gets renamed "b".
In cases of circular mappings (e.g. a->b, b->a), modifying the
graph is not possible in-place and an exception is raised.
In that case, use copy=True.

If a relabel operation on a multigraph would cause two or more
edges to have the same source, target and key, the second edge must
be assigned a new key to retain all edges. The new key is set
to the lowest non-negative integer not already used as a key
for edges between these two nodes. Note that this means non-numeric
keys may be replaced by numeric keys.

See Also
--------
convert_node_labels_to_integers
)callable_relabel_copy_relabel_inplace)Gmappingcopynms        B/var/www/html/env/lib/python3.13/site-packages/networkx/relabel.pyr   r      sI    h '/w&7&7"1GAJ"WAQ""%% 	#s   A	c                 d   [        UR                  5       UR                  5       -  5      S:  a|  [        R                  " [        UR                  5       5      5      nUR                  [        R                  " U5      5         [        [        [        R                  " U5      5      5      nOU  Vs/ s H  oUU;   d  M
  UPM     nnU R                  5       nU R                  5       nU GH  n X   n	U R                  " U	40 U R                  U   D6  X:X  a  M0  U(       Ga  U R#                  USSS9 V
VVVs/ s H  u  ppXU:X  a  U	OUX4PM     nnnn
nU(       a8  UU R%                  USSS9 VV
VVs/ s H  u  ppX:X  a  U	OUXU4PM     snnn
n-  n['        5       n[)        U5       H  u  nu  ppXU   ;   d  M  XU   U   ;   d  M   [+        U[,        [.        -  5      (       d  SOUnUX   U   ;   d  UU4U;   a  US-  nUX   U   ;   a  M  UU4U;   a  M  XUU4UU'   UR1                  UU45        M     OlU R#                  USS9 V
VVs/ s H  u  pnXU:X  a  U	OUU4PM     nnn
nU(       a4  UU R%                  USS9 VV
Vs/ s H  u  pnX:X  a  U	OUX4PM     snn
n-  nU R3                  U5        U R5                  U5        GM     U $ ! [        R                   a  n[        R                  " S5      UeS nAff = fs  snf ! [          a     GM*  f = fs  snnnn
f s  snnn
nf s  snnn
f s  snn
nf )Nr   z[The node label sets are overlapping and no ordering can resolve the mapping. Use copy=True.T)datakeys   r   )lenr   valuesnxDiGraphlistitemsremove_edges_fromselfloop_edgesreversedtopological_sortNetworkXUnfeasibleis_multigraphis_directedadd_nodenodesKeyErroredgesin_edgesset	enumerate
isinstanceintfloataddremove_nodeadd_edges_from)r   r   Dr&   errr   
multigraphdirectedoldnew_targetkeyr   	new_edgessourceseeninew_keys                      r   r   r      s2   
7<<>GNN,,-1 JJtGMMO,-	B--a01	T""5"5a"89:E .AqgA."J}}H	,CJJs+aggcl+ : /0ggc4g.P.P*Q F]cB.P   23**StRV*2W2W.C  MSvsF2W 	
 5D2;I2F..FCvY&3F)F2C+C'1#sU{'C'CaG!QYv%6667:Kt:S1 "QYv%6667:Kt:S$*GT#BIaLHHfg./ 3G *+4)@)@%Q F]c=)@   -.ZZ$Z-G-G)D  MSvsA-G 	 	
c	#O P Hi $$ 	''6 	 /  		


sN   (K
 /	K=<K=+$L4L0L	L$
L+
K:K55K:
LLc                   ^ U R                  5       nUR                  U4S jU  5       5        UR                  R                  U4S jU R                  R                  5        5       5        U R                  5       (       Ga	  U R                  SSS9 VVVVs/ s H7  u  p4pVTR                  X35      TR                  XD5      XVR                  5       4PM9     nnnnnU R                  5       (       + n[        5       n	[        U5       Hq  u  n
u  ppXU4U	;   a-  [        U[        [        -  5      (       d  SnUS-  nXU4U	;   a  M-  U	R!                  XU45        U(       a  U	R!                  XU45        XX4Xz'   Ms     UR#                  U5        O(UR#                  U4S jU R                  SS9 5       5        UR$                  R                  U R$                  5        U$ s  snnnnf )	Nc              3   F   >#    U  H  nTR                  X5      v   M     g 7fN)get).0r   r   s     r   	<genexpr> _relabel_copy.<locals>.<genexpr>   s     21W[[&&s   !c              3   j   >#    U  H(  u  pTR                  X5      UR                  5       4v   M*     g 7frB   rC   r   )rD   r   dr   s      r   rE   rF      s'     M_TQGKK%qvvx0_s   03T)r   r   r   r   c              3      >#    U  H9  u  pnTR                  X5      TR                  X"5      UR                  5       4v   M;     g 7frB   rH   )rD   n1n2rI   r   s       r   rE   rF      s;      
1 [[ '++b"5qvvx@1s   AAr   )	__class__add_nodes_from_nodeupdater&   r   r#   r(   rC   r   r$   r*   r+   r,   r-   r.   r/   r1   graph)r   r   HrK   rL   krI   r;   
undirected
seen_edgesr>   r<   r9   r:   r   s    `             r   r   r      s   	A222GGNNMQWW]]_MM #$''t$'"?
"? [[ '++b"5q&&(C"? 	 
 (
U
.7	.B*A*3':5!#sU{33Cq 3':5 NNFC0145"C6IL /C 	
#	 
 wwDw1
 	
 GGNN177H3
s   >G
)r   r   c           
         U R                  5       U-   nUS:X  a.  [        [        U R                  5       [	        X5      5      5      nGOFUS:X  a9  [        U R                  5       5      n[        [        U[	        X5      5      5      nGOUS:X  ai  U R                  5        VVs/ s H  u  pxX4PM
     n	nnU	R                  5         [        [        U	 VVs/ s H  u  pUPM	     snn[	        X5      5      5      nOUS:X  ay  U R                  5        VVs/ s H  u  pxX4PM
     n	nnU	R                  5         U	R                  5         [        [        U	 VVs/ s H  u  pUPM	     snn[	        X5      5      5      nO[        R                  " SU 35      e[        X5      n
Ub:  [        R                  " XR                  5        VVs0 s H  u  pX_M	     snnU5        U
$ s  snnf s  snnf s  snnf s  snnf s  snnf )a  Returns a copy of the graph G with the nodes relabeled using
consecutive integers.

Parameters
----------
G : graph
   A NetworkX graph

first_label : int, optional (default=0)
   An integer specifying the starting offset in numbering nodes.
   The new integer labels are numbered first_label, ..., n-1+first_label.

ordering : string
   "default" : inherit node ordering from G.nodes()
   "sorted"  : inherit node ordering from sorted(G.nodes())
   "increasing degree" : nodes are sorted by increasing degree
   "decreasing degree" : nodes are sorted by decreasing degree

label_attribute : string, optional (default=None)
   Name of node attribute to store old label.  If None no attribute
   is created.

Notes
-----
Node and edge attribute data are copied to the new (relabeled) graph.

There is no guarantee that the relabeling of nodes to integers will
give the same two integers for two (even identical graphs).
Use the `ordering` argument to try to preserve the order.

See Also
--------
relabel_nodes
defaultsortedzincreasing degreezdecreasing degreezUnknown node ordering: )number_of_nodesdictzipr&   rangerX   degreesortreverser   NetworkXErrorr   set_node_attributesr   )r   first_labelorderinglabel_attributeNr   nlistr   rI   dv_pairsrR   rS   vs                r   r   r      s   L 	
k)A9s1779eK&;<=	X	qwwy!s5%"789	(	()*4vQF4s(3($!A(3U;5JKL	(	()*4vQF4s(3($!A(3U;5JKL!8
CDDa!A"
qMMO"DODA14O"DoVH 534 4 #Es    GGG$G*?G0)T)r   rW   N)networkxr   __all___dispatchabler   r   r   r        r   <module>rn      so    ,o
> J?$v&v&r<~B T>:>: ?:rm   