B
    ,[                 @   s8   d dl ZddgZdddZdd Zdd	 ZdddZdS )    Nconvert_node_labels_to_integersrelabel_nodesTc                s>   t  ds fdd| D }n }|r0t| |S t| |S dS )a$	  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
    __getitem__c                s   i | ]} ||qS  r   ).0n)mappingr   /lib/python3.7/site-packages/networkx/relabel.py
<dictcomp>]   s    z!relabel_nodes.<locals>.<dictcomp>N)hasattr_relabel_copy_relabel_inplace)Gr   copymr   )r   r	   r      s    P

c       	   	      s  t | }t | }t||@ dkrtt| }|t	| yt
tt|}W q tjk
r   tdY qX n|}|  }|  }x$|D ]y|  W n tk
r   wY nX  krqy| j f| j  W n" tk
r   td Y nX |rb fdd| jdddD }|r| fdd| jdddD 7 }nF fd	d| jdd
D }|r| fdd| jdd
D 7 }|  | | qW | S )Nr   z[The node label sets are overlapping and no ordering can resolve the mapping. Use copy=True.zNode %s is not in the graphc                s,   g | ]$\}}}} |kr n|||fqS r   r   )r   _targetkeydata)newoldr   r	   
<listcomp>   s   z$_relabel_inplace.<locals>.<listcomp>T)r   keysc                s,   g | ]$\}}}}|kr n| ||fqS r   r   )r   sourcer   r   r   )r   r   r   r	   r      s   c                s(   g | ] \}}} |kr n||fqS r   r   )r   r   r   r   )r   r   r   r	   r      s   )r   c                s(   g | ] \}}}|kr n| |fqS r   r   )r   r   r   r   )r   r   r   r	   r      s   )setr   valueslennxZDiGraphlistitemsZremove_edges_fromZselfloop_edgesreversedZtopological_sortZNetworkXUnfeasibleis_multigraphZis_directedKeyErrorZadd_nodenodesedgesZin_edgesZremove_nodeadd_edges_from)	r   r   Z
old_labelsZ
new_labelsDr#   Z
multigraphZdirectedZ	new_edgesr   )r   r   r	   r   f   sJ    
r   c                s   |   }| fdd| D  |j fdd| j D  |  rl| fdd| jdddD  n | fdd| jddD  |j	| j	 |S )	Nc             3   s   | ]}  ||V  qd S )N)get)r   r   )r   r   r	   	<genexpr>   s    z _relabel_copy.<locals>.<genexpr>c             3   s&   | ]\}}  ||| fV  qd S )N)r'   r   )r   r   d)r   r   r	   r(      s    c             3   s6   | ].\}}}}  ||  |||| fV  qd S )N)r'   r   )r   n1n2kr)   )r   r   r	   r(      s   T)r   r   c             3   s2   | ]*\}}}  ||  ||| fV  qd S )N)r'   r   )r   r*   r+   r)   )r   r   r	   r(      s   )r   )
	__class__Zadd_nodes_fromZ_nodeupdater#   r   r!   r%   r$   Zgraph)r   r   Hr   )r   r	   r      s     r   defaultc       	      C   s&  |   | }|dkr.tt|  t||}n|dkrXt|  }tt|t||}n|dkrdd |  D }|  ttdd |D t||}nX|dkrdd |  D }|  |  ttd	d |D t||}nt	
d
| t| |}|dk	r"t	|dd | D | |S )aY  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.

    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
    r0   sortedzincreasing degreec             S   s   g | ]\}}||fqS r   r   )r   r   r)   r   r   r	   r      s    z3convert_node_labels_to_integers.<locals>.<listcomp>c             S   s   g | ]\}}|qS r   r   )r   r)   r   r   r   r	   r      s    zdecreasing degreec             S   s   g | ]\}}||fqS r   r   )r   r   r)   r   r   r	   r      s    c             S   s   g | ]\}}|qS r   r   )r   r)   r   r   r   r	   r      s    zUnknown node ordering: %sNc             S   s   i | ]\}}||qS r   r   )r   r,   vr   r   r	   r
      s    z3convert_node_labels_to_integers.<locals>.<dictcomp>)Znumber_of_nodesdictzipr#   ranger1   Zdegreesortreverser   ZNetworkXErrorr   Zset_node_attributesr   )	r   Zfirst_labelZorderingZlabel_attributeNr   ZnlistZdv_pairsr/   r   r   r	   r      s*    $  

)T)r   r0   N)Znetworkxr   __all__r   r   r   r   r   r   r   r	   <module>   s   
Z3 