K i<(ddlZddgZejdddidd dZd Zd Zejdd  dd Zy)Nconvert_node_labels_to_integers relabel_nodesTznot copy)preserve_all_attrs mutates_input returns_graphct|r|Dcic] }||| c}n|}|r t||St||Scc}w)aRelabel 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)Gmappingcopynms V/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/relabel.pyrrsHh'/w&7"1GAJ"WA Q""1%% #s?c t|j|jzdkDrytjt |j }|jtj| tt tj|}n|Dcgc] }||vs| }}|j}|j}|D]} ||} |j| fi|j|| |k(r0|r|j#|ddD cgc]\} } } } | || k(r| n| | | f}} } } } |r;||j%|ddD cgc]\}} } } ||k(r| n|| | | fc} } } }z }t'}t)|D]{\}\}} } } | ||vs| ||| vst+| t,t.zsdn| }|||| vs| |f|vr|dz }|||| vr| |f|vr|| || f||<|j1| |f}nj|j#|dD cgc]\} } } | || k(r| n| | f}} } } |r6||j%|dD cgc]\}} } ||k(r| n|| | fc} } }z }|j3||j5||S#tj$r}tjd|d}~wwxYwcc}w#t $rYwxYwcc} } } } wcc} } } }wcc} } } wcc} } }w)Nrz[The node label sets are overlapping and no ordering can resolve the mapping. Use copy=True.T)datakeysr)lenrvaluesnxDiGraphlistitemsremove_edges_fromselfloop_edgesreversedtopological_sortNetworkXUnfeasible is_multigraph is_directedadd_nodenodesKeyErroredgesin_edgesset enumerate isinstanceintfloatadd remove_nodeadd_edges_from)r rDr&errr multigraphdirectedoldnew_targetkeyr new_edgessourceseeninew_keys rr r sg 7<<>GNN, ,-1 JJtGMMO, - B--a01 T""5"5a"89:E.qg.."J}}H'$ #,C AJJs +aggcl + #:  /0ggc4g.P*QTSF]cTBI23**StRV*2W.C 6MSvsCF 5D2;I2F 0..FFCQvY&3!F)F2C+C'1#sU{'CaG!QvYv%6667:Kt:S1 "QvYv%6667:Kt:S$*FGT#BIaLHHfg./  0*+4)@%QSF]c=I-.ZZ$Z-G)D 6MSvsDA  c #O'$P Hi$$ ''6  /      sN?'J)+ K5K"%K)K/ #K7/K? $L )K<KK K,+K,c |j}|jfd|D|jjfd|jj D|j r|jddDcgc];\}}}}j||j||||jf=}}}}}|j }t} t|D]j\} \} } } }| | | f| vr&t| ttzsd} | dz } | | | f| vr&| j!| | | f|r| j!| | | f| | | |f|| <l|j#|n*|j#fd|jdD|j$j|j$|Scc}}}}w) Nc3BK|]}j||ywN)get).0rrs r z _relabel_copy..s21W[[A&2sc3hK|])\}}j|||jf+ywrBrCr)rDrdrs rrEz _relabel_copy..s+MTQGKK1%qvvx0Ms/2T)rrrrc3K|];\}}}j||j|||jf=ywrBrG)rDn1n2rHrs rrEz _relabel_copy..sB R[[R '++b""5qvvx @ sAAr) __class__add_nodes_from_nodeupdater&rr#r(rCrr$r*r+r,r-r.r/r1graph)r rHrJrKkrHr; undirected seen_edgesr>r<r9r:rs ` rr r s A222GGNNMQWW]]_MM#$''t$'"?  RA[[R '++b""5q!&&( C  ( U .7 .B 7 *A*T63':5!#sU{3Cq63':5 NNFFC0 145"FC6IaL 7 #   wwDw1  GGNN177 H3 sAG )rrc |j|z}|dk(r/tt|jt ||}n?|dk(r:t |j}tt|t ||}n|dk(rg|j Dcgc] \}}||f } }}| jtt| Dcgc]\}}| c}}t ||}n|dk(rw|j Dcgc] \}}||f } }}| j| jtt| Dcgc]\}}| c}}t ||}ntjd|t||} |9tj| |jD cic]\} } | |  c} } || Scc}}wcc}}wcc}}wcc}}wcc} } w)aZReturns 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 defaultsortedzincreasing degreezdecreasing degreezUnknown node ordering: )number_of_nodesdictzipr&rangerWdegreesortreverser NetworkXErrorrset_node_attributesr) r first_labelorderinglabel_attributeNrnlistrrHdv_pairsrQrRvs rrrsL k)A9s1779eK&;<= X qwwy!s5% Q"789 ( ()*4v1QF44 s(3$!QA3U;5JKL ( ()*4v1QF44 s(3$!QA3U;5JKL!8 CDDa!A" qGMMO"DDAq1a4"DoV H5344 #EsG G G G"8 G( )T)rrVN)networkxr__all__ _dispatchablerr r rrrmss ,o >J?$v&v&r< ~ BT>:>: ?: rl