ó žÃÒYc@sLddlZddgZed„Zd„Zd„Zdddd „ZdS( iÿÿÿÿNtconvert_node_labels_to_integerst relabel_nodescsXtˆdƒs.t‡fd†|Dƒƒ}nˆ}|rGt||ƒSt||ƒSdS(s$ Relabel the nodes of the graph G. 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. 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] Notes ----- Only the nodes specified in the mapping will be relabeled. 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. See Also -------- convert_node_labels_to_integers t __getitem__c3s!|]}|ˆ|ƒfVqdS(N((t.0tn(tmapping(sf/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/relabel.pys ]sN(thasattrtdictt _relabel_copyt_relabel_inplace(tGRtcopytm((Rsf/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/relabel.pyR s P c Csát|jƒƒ}t|jƒƒ}t||@ƒdkr³tjt|jƒƒƒ}|jtj |ƒƒyt ttj |ƒƒƒ}Wq¹tj k r¯tj dƒ‚q¹Xn|}|j ƒ}|jƒ}x |D]}y||} Wntk rqØnX| |krqØny|j| |j|Wn!tk rStd|ƒ‚nX|rg|j|dtdtƒD]6\} } } } | || kr| n| | | f^qv}|r¿|g|j|dtdtƒD]6\}} } } ||krû| n|| | | f^q×7}q¿n¥g|j|dtƒD]0\} } } | || krT| n| | f^q0}|r¿|g|j|dtƒD]0\}} } ||kr¦| n|| | f^q…7}n|j|ƒ|j|ƒqØW|S(Nis[The node label sets are overlapping and no ordering can resolve the mapping. Use copy=True.sNode %s is not in the graphtdatatkeys(tsetRtvaluestlentnxtDiGraphtlisttitemstremove_edges_fromtselfloop_edgestreversedttopological_sorttNetworkXUnfeasiblet is_multigrapht is_directedtKeyErrortadd_nodetnodestedgestTruetin_edgest remove_nodetadd_edges_from(R Rt old_labelst new_labelstDRt multigraphtdirectedtoldtnewt_ttargettkeyR t new_edgestsource((sf/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/relabel.pyR fsJ      U\IM csì|jƒ}|j‡fd†|Dƒƒ|jj‡fd†|jjƒDƒƒ|jrnd|j|_n|jƒr¬|j‡fd†|j dt dt ƒDƒƒn)|j‡fd†|j dt ƒDƒƒ|j j|j ƒ|S(Nc3s!|]}ˆj||ƒVqdS(N(tget(RR(R(sf/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/relabel.pys ›sc3s3|])\}}ˆj||ƒ|jƒfVqdS(N(R1R (RRtd(R(sf/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/relabel.pys œss(%s)c3sK|]A\}}}}ˆj||ƒˆj||ƒ||jƒfVqdS(N(R1R (Rtn1tn2tkR2(R(sf/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/relabel.pys ¡sRR c3sE|];\}}}ˆj||ƒˆj||ƒ|jƒfVqdS(N(R1R (RR3R4R2(R(sf/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/relabel.pys ¤s( t fresh_copytadd_nodes_fromt_nodetupdateRRtnameRR$R R!tgraph(R RtH((Rsf/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/relabel.pyR™s )   itdefaultc Csô|jƒ|}|dkrCtt|jƒt||ƒƒƒ}nR|dkr‚t|jƒƒ}tt|t||ƒƒƒ}n|dkrýg|jƒD]\}}||f^q›} | jƒttg| D]\}}|^qÐt||ƒƒƒ}n˜|dkr‚g|jƒD]\}}||f^q} | jƒ| jƒttg| D]\}}|^qUt||ƒƒƒ}nt j d|ƒ‚t ||ƒ} d|j d| _ |d k rðt j| td„|jƒDƒƒ|ƒn| S( s’Return 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. See Also -------- relabel_nodes R=tsortedsincreasing degreesdecreasing degreesUnknown node ordering: %st(s)_with_int_labelscss!|]\}}||fVqdS(N((RR5tv((sf/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/relabel.pys ßsN(tnumber_of_nodesRtzipRtrangeR>tdegreetsorttreverseRt NetworkXErrorRR:tNonetset_node_attributesR( R t first_labeltorderingtlabel_attributetNRtnlistRR2tdv_pairsR<((sf/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/relabel.pyRªs,  ' ! + : +  : " ( tnetworkxRt__all__R!RR RRHR(((sf/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/relabel.pyts   Z 3