ó
 ,µ[c           @   sL   d  d l  Z d d g Z e d „ Z d „  Z d „  Z d d d d	 „ Z d S(
   iÿÿÿÿNt   convert_node_labels_to_integerst   relabel_nodesc            sR   t  ˆ  d ƒ s( ‡  f d †  |  Dƒ } n ˆ  } | rA t |  | ƒ St |  | ƒ Sd S(   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__c            s   i  |  ] } ˆ  | ƒ | “ q S(    (    (   t   .0t   n(   t   mapping(    s/   lib/python2.7/site-packages/networkx/relabel.pys
   <dictcomp>]   s   	 N(   t   hasattrt   _relabel_copyt   _relabel_inplace(   t   GR   t   copyt   m(    (   R   s/   lib/python2.7/site-packages/networkx/relabel.pyR      s    Pc      	   C   sá  t  | j ƒ  ƒ } t  | j ƒ  ƒ } t | | @ƒ d k r³ t j t | j ƒ  ƒ ƒ } | j t j	 | ƒ ƒ y t
 t t j | ƒ ƒ ƒ } Wq¹ t j k
 r¯ t j d ƒ ‚ q¹ Xn | } |  j ƒ  } |  j ƒ  } x	| D]} y | | }	 Wn t k
 rqØ n X|	 | k rqØ n  y |  j |	 |  j |  Wn! t k
 rSt d | ƒ ‚ n X| rg  |  j | d t d t ƒD]6 \ }
 } } } |	 | | k r|	 n | | | f ^ qv} | r¿| g  |  j | d t d t ƒD]6 \ } }
 } } | | k rû|	 n | |	 | | f ^ q×7} q¿n¥ g  |  j | d t ƒD]0 \ }
 } } |	 | | k rT|	 n | | f ^ q0} | r¿| g  |  j | d t ƒD]0 \ } }
 } | | k r¦|	 n | |	 | f ^ q…7} n  |  j | ƒ |  j | ƒ qØ W|  S(   Ni    s[   The node label sets are overlapping and no ordering can resolve the mapping. Use copy=True.s   Node %s is not in the grapht   datat   keys(   t   setR   t   valuest   lent   nxt   DiGrapht   listt   itemst   remove_edges_fromt   selfloop_edgest   reversedt   topological_sortt   NetworkXUnfeasiblet   is_multigrapht   is_directedt   KeyErrort   add_nodet   nodest   edgest   Truet   in_edgest   remove_nodet   add_edges_from(   R	   R   t
   old_labelst
   new_labelst   DR   t
   multigrapht   directedt   oldt   newt   _t   targett   keyR   t	   new_edgest   source(    (    s/   lib/python2.7/site-packages/networkx/relabel.pyR   f   sJ    U\IMc            sÐ   |  j  ƒ  } | j ‡  f d †  |  Dƒ ƒ | j j ‡  f d †  |  j j ƒ  Dƒ ƒ |  j ƒ  r | j ‡  f d †  |  j d t	 d t	 ƒ Dƒ ƒ n) | j ‡  f d †  |  j d t	 ƒ Dƒ ƒ | j
 j |  j
 ƒ | S(   Nc         3   s!   |  ] } ˆ  j  | | ƒ Vq d  S(   N(   t   get(   R   R   (   R   (    s/   lib/python2.7/site-packages/networkx/relabel.pys	   <genexpr>›   s    c         3   s3   |  ]) \ } } ˆ  j  | | ƒ | j ƒ  f Vq d  S(   N(   R0   R
   (   R   R   t   d(   R   (    s/   lib/python2.7/site-packages/networkx/relabel.pys	   <genexpr>œ   s    c         3   sK   |  ]A \ } } } } ˆ  j  | | ƒ ˆ  j  | | ƒ | | j ƒ  f Vq d  S(   N(   R0   R
   (   R   t   n1t   n2t   kR1   (   R   (    s/   lib/python2.7/site-packages/networkx/relabel.pys	   <genexpr>ž   s   R   R   c         3   sE   |  ]; \ } } } ˆ  j  | | ƒ ˆ  j  | | ƒ | j ƒ  f Vq d  S(   N(   R0   R
   (   R   R2   R3   R1   (   R   (    s/   lib/python2.7/site-packages/networkx/relabel.pys	   <genexpr>¡   s   (   t	   __class__t   add_nodes_fromt   _nodet   updateR   R   R   R#   R   R    t   graph(   R	   R   t   H(    (   R   s/   lib/python2.7/site-packages/networkx/relabel.pyR   ™   s    ) i    t   defaultc         C   sÚ  |  j  ƒ  | } | d k rC t t |  j ƒ  t | | ƒ ƒ ƒ } nR| d k r‚ t |  j ƒ  ƒ } t t | t | | ƒ ƒ ƒ } n| d k rý g  |  j ƒ  D] \ } } | | f ^ q› }	 |	 j ƒ  t t g  |	 D] \ } } | ^ qÐ t | | ƒ ƒ ƒ } n˜ | d k r‚g  |  j ƒ  D] \ } } | | f ^ q}	 |	 j ƒ  |	 j ƒ  t t g  |	 D] \ } } | ^ qUt | | ƒ ƒ ƒ } n t	 j
 d | ƒ ‚ t |  | ƒ }
 | d k	 rÖt	 j |
 d „  | j ƒ  Dƒ | ƒ n  |
 S(   sY  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
    R;   t   sorteds   increasing degrees   decreasing degrees   Unknown node ordering: %sc         S   s   i  |  ] \ } } | | “ q S(    (    (   R   R4   t   v(    (    s/   lib/python2.7/site-packages/networkx/relabel.pys
   <dictcomp>ß   s   	 N(   t   number_of_nodest   dictt   zipR   t   rangeR<   t   degreet   sortt   reverseR   t   NetworkXErrorR   t   Nonet   set_node_attributesR   (   R	   t   first_labelt   orderingt   label_attributet   NR   t   nlistR   R1   t   dv_pairsR:   (    (    s/   lib/python2.7/site-packages/networkx/relabel.pyR    §   s*    $'!+
:+

:
(	   t   networkxR   t   __all__R    R   R   R   RF   R    (    (    (    s/   lib/python2.7/site-packages/networkx/relabel.pyt   <module>   s   Z	3	