ó
 ,µ[c           @   sF   d  Z  d d l Z d j d g ƒ Z d d g Z d „  Z d „  Z d S(	   s$   
Utilities for connectivity package
iÿÿÿÿNs   
s%   Jordi Torrents <jtorrents@milnou.net>t!   build_auxiliary_node_connectivityt!   build_auxiliary_edge_connectivityc   	      C   s'  |  j  ƒ  } i  } t j ƒ  } xp t |  ƒ D]b \ } } | | | <| j d | d | ƒ| j d | d | ƒ| j d | d | d d ƒq+ Wg  } xi |  j ƒ  D][ \ } } | j d | | d | | f ƒ | s¤ | j d | | d | | f ƒ q¤ q¤ W| j | d d ƒ| | j	 d <| S(	   sU  Creates a directed graph D from an undirected graph G to compute flow
    based node connectivity.

    For an undirected graph G having `n` nodes and `m` edges we derive a
    directed graph D with `2n` nodes and `2m+n` arcs by replacing each
    original node `v` with two nodes `vA`, `vB` linked by an (internal)
    arc in D. Then for each edge (`u`, `v`) in G we add two arcs (`uB`, `vA`)
    and (`vB`, `uA`) in D. Finally we set the attribute capacity = 1 for each
    arc in D [1]_.

    For a directed graph having `n` nodes and `m` arcs we derive a
    directed graph D with `2n` nodes and `m+n` arcs by replacing each
    original node `v` with two nodes `vA`, `vB` linked by an (internal)
    arc (`vA`, `vB`) in D. Then for each arc (`u`, `v`) in G we add one 
    arc (`uB`, `vA`) in D. Finally we set the attribute capacity = 1 for
    each arc in D.

    A dictionary with a mapping between nodes in the original graph and the
    auxiliary digraph is stored as a graph attribute: H.graph['mapping'].

    References
    ----------
    .. [1] Kammer, Frank and Hanjo Taubig. Graph Connectivity. in Brandes and
        Erlebach, 'Network Analysis: Methodological Foundations', Lecture
        Notes in Computer Science, Volume 3418, Springer-Verlag, 2005.
        http://www.informatik.uni-augsburg.de/thi/personen/kammer/Graph_Connectivity.pdf

    s   %dAt   ids   %dBt   capacityi   s   %sBs   %sAt   mapping(
   t   is_directedt   nxt   DiGrapht	   enumeratet   add_nodet   add_edget   edgest   appendt   add_edges_fromt   graph(	   t   Gt   directedR   t   Ht   it   nodeR   t   sourcet   target(    (    sE   lib/python2.7/site-packages/networkx/algorithms/connectivity/utils.pyR       s     
"#*c         C   s±   |  j  ƒ  rH t j ƒ  } | j |  j ƒ  ƒ | j |  j ƒ  d d ƒ| St j ƒ  } | j |  j ƒ  ƒ x? |  j ƒ  D]1 \ } } | j | | f | | f g d d ƒqt W| Sd S(   sR  Auxiliary digraph for computing flow based edge connectivity

    If the input graph is undirected, we replace each edge (`u`,`v`) with
    two reciprocal arcs (`u`, `v`) and (`v`, `u`) and then we set the attribute
    'capacity' for each arc to 1. If the input graph is directed we simply
    add the 'capacity' attribute. Part of algorithm 1 in [1]_ .

    References
    ----------
    .. [1] Abdol-Hossein Esfahanian. Connectivity Algorithms. (this is a
        chapter, look for the reference of the book).
        http://www.cse.msu.edu/~cse835/Papers/Graph_connectivity_revised.pdf
    R   i   N(   R   R   R   t   add_nodes_fromt   nodesR   R   (   R   R   R   R   (    (    sE   lib/python2.7/site-packages/networkx/algorithms/connectivity/utils.pyR   A   s    )(   t   __doc__t   networkxR   t   joint
   __author__t   __all__R    R   (    (    (    sE   lib/python2.7/site-packages/networkx/algorithms/connectivity/utils.pyt   <module>   s   		4