ó
 ,µ[c           @   s˜   d  Z  d d l Z d d l Z d d l m Z d d l Z d d d d g Z e	 d ƒ Z
 d d	 „ Z d d
 „ Z d „  Z d „  Z d d „ Z e Z d S(   s  Provides functions for computing a maximum cardinality matching in a
bipartite graph.

If you don't care about the particular implementation of the maximum matching
algorithm, simply use the :func:`maximum_matching`. If you do care, you can
import one of the named maximum matching algorithms directly.

For example, to find a maximum matching in the complete bipartite graph with
two vertices on the left and three vertices on the right:

>>> import networkx as nx
>>> G = nx.complete_bipartite_graph(2, 3)
>>> left, right = nx.bipartite.sets(G)
>>> list(left)
[0, 1]
>>> list(right)
[2, 3, 4]
>>> nx.bipartite.maximum_matching(G)
{0: 2, 1: 3, 2: 0, 3: 1}

The dictionary returned by :func:`maximum_matching` includes a mapping for
vertices in both the left and right vertex sets.

iÿÿÿÿN(   t   setst   maximum_matchingt   hopcroft_karp_matchingt   eppstein_matchingt   to_vertex_covert   infc            s!  ‡  ‡ ‡ ‡ ‡ ‡ f d †  } ‡  ‡ ‡ ‡ ‡ f d †  ‰ t  ˆ  | ƒ \ ‰ } d „  ˆ Dƒ ‰ d „  | Dƒ ‰ i  ‰ t j ƒ  ‰ d } xJ | ƒ  rÒ x: ˆ D]2 } ˆ | d	 k r™ ˆ | ƒ rË | d 7} qË q™ q™ Wq‰ Wd „  ˆ j ƒ  Dƒ ‰ d „  ˆ j ƒ  Dƒ ‰ t t j ˆ j ƒ  ˆ j ƒ  ƒ ƒ S(
   s"  Returns the maximum cardinality matching of the bipartite graph `G`.

    Parameters
    ----------
    G : NetworkX graph

      Undirected bipartite graph

    top_nodes : container

      Container with all nodes in one bipartite node set. If not supplied
      it will be computed. But if more than one solution exists an exception
      will be raised.

    Returns
    -------
    matches : dictionary

      The matching is returned as a dictionary, `matches`, such that
      ``matches[v] == w`` if node `v` is matched to node `w`. Unmatched
      nodes do not occur as a key in mate.

    Raises
    ------
    AmbiguousSolution : Exception

      Raised if the input bipartite graph is disconnected and no container
      with all nodes in one bipartite set is provided. When determining
      the nodes in each bipartite set more than one valid solution is
      possible if the input graph is disconnected.

    Notes
    -----

    This function is implemented with the `Hopcroft--Karp matching algorithm
    <https://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm>`_ for
    bipartite graphs.

    See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
    for further details on how bipartite graphs are handled in NetworkX.

    See Also
    --------

    eppstein_matching

    References
    ----------
    .. [1] John E. Hopcroft and Richard M. Karp. "An n^{5 / 2} Algorithm for
       Maximum Matchings in Bipartite Graphs" In: **SIAM Journal of Computing**
       2.4 (1973), pp. 225--231. <https://doi.org/10.1137/0202019>.

    c             sà   xB ˆ D]: }  ˆ |  d  k r7 d ˆ |  <ˆ j |  ƒ q t ˆ |  <q Wt ˆ d  <x€ ˆ rÑ ˆ j ƒ  }  ˆ |  ˆ d  k  rR xS ˆ  |  D]D } ˆ ˆ | t k rƒ ˆ |  d ˆ ˆ | <ˆ j ˆ | ƒ qƒ qƒ WqR qR Wˆ d  t k	 S(   Ni    i   (   t   Nonet   appendt   INFINITYt   popleft(   t   vt   u(   t   Gt	   distancest   leftt   leftmatchest   queuet   rightmatches(    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pyt   breadth_first_searcht   s    

	c            sz   |  d  k	 rv xY ˆ  |  D]M } ˆ ˆ | ˆ |  d k r ˆ ˆ | ƒ rd |  ˆ | <| ˆ |  <t Sq q Wt ˆ |  <t St S(   Ni   (   R   t   TrueR   t   False(   R
   R   (   R   t   depth_first_searchR   R   R   (    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pyR   …   s    


c         S   s   i  |  ] } d  | “ q S(   N(   R   (   t   .0R
   (    (    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pys
   <dictcomp>“   s   	 c         S   s   i  |  ] } d  | “ q S(   N(   R   (   R   R
   (    (    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pys
   <dictcomp>”   s   	 i    i   c         S   s+   i  |  ]! \ } } | d  k	 r | | “ q S(   N(   R   (   R   t   kR
   (    (    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pys
   <dictcomp>¢   s   	 c         S   s+   i  |  ]! \ } } | d  k	 r | | “ q S(   N(   R   (   R   R   R
   (    (    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pys
   <dictcomp>£   s   	 N(   t   bipartite_setst   collectionst   dequeR   t   itemst   dictt	   itertoolst   chain(   R   t	   top_nodesR   t   rightt   num_matched_pairsR
   (    (   R   R   R   R   R   R   R   sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pyR   8   s     <c   
         s@  t  |  | ƒ \ } } t j |  j | ƒ ƒ }  i  ‰  x= |  D]5 } x, |  | D]  } | ˆ  k rK | ˆ  | <PqK qK Wq: WxÆt r;i  ‰ g  ‰ ‡ f d †  |  Dƒ ‰ x ˆ  D] } ˆ ˆ  | =q¥ Wt ˆ ƒ } xÅ | rˆ ri  } xK | D]C } x: |  | D]. } | ˆ k rô | j | g  ƒ j | ƒ qô qô Wqã Wg  } xW | D]O } | | ˆ | <| ˆ  k ry| j ˆ  | ƒ | ˆ ˆ  | <q7ˆ j | ƒ q7WqÉ Wˆ si  } x< |  D]4 } x+ |  | D] } | ˆ k r²d | | <q²q²Wq¡Wx" ˆ  j	 ƒ  D] }	 |	 ˆ  ˆ  |	 <qæWˆ  S‡  ‡ ‡ ‡ ‡ f d †  ‰ x ˆ D] } ˆ | ƒ q$Wqv Wd S(   s¨  Returns the maximum cardinality matching of the bipartite graph `G`.

    Parameters
    ----------
    G : NetworkX graph

      Undirected bipartite graph

    top_nodes : container

      Container with all nodes in one bipartite node set. If not supplied
      it will be computed. But if more than one solution exists an exception
      will be raised.

    Returns
    -------
    matches : dictionary

      The matching is returned as a dictionary, `matching`, such that
      ``matching[v] == w`` if node `v` is matched to node `w`. Unmatched
      nodes do not occur as a key in mate.

    Raises
    ------
    AmbiguousSolution : Exception

      Raised if the input bipartite graph is disconnected and no container
      with all nodes in one bipartite set is provided. When determining
      the nodes in each bipartite set more than one valid solution is
      possible if the input graph is disconnected.

    Notes
    -----

    This function is implemented with David Eppstein's version of the algorithm
    Hopcroft--Karp algorithm (see :func:`hopcroft_karp_matching`), which
    originally appeared in the `Python Algorithms and Data Structures library
    (PADS) <http://www.ics.uci.edu/~eppstein/PADS/ABOUT-PADS.txt>`_.

    See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
    for further details on how bipartite graphs are handled in NetworkX.

    See Also
    --------

    hopcroft_karp_matching

    c            s   i  |  ] } ˆ  | “ q S(    (    (   R   R   (   t	   unmatched(    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pys
   <dictcomp>ó   s   	 c            sw   |  ˆ k rs ˆ j  |  ƒ } xU | D]J } | ˆ k r" ˆ j  | ƒ } | ˆ k s[ ˆ | ƒ rl | ˆ  |  <t Sq" q" Wn  t S(   N(   t   popR   R   (   R
   t   LR   t   pu(   t   matchingt   predt   predst   recurseR"   (    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pyR)   &  s    
N(
   R   t   nxt   DiGrapht   edgesR   t   listt
   setdefaultR   R   t   copy(
   R   R   R   R    R   R
   t   layert   newLayert	   unlayeredt   key(    (   R&   R'   R(   R)   R"   sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pyR   ®   sP    3
	$c            s=   t  ‡  ‡ ‡ ‡ f d † } | | d t  ƒp< | | d t ƒS(   sç  Returns True if and only if the vertex `v` is connected to one of
    the target vertices by an alternating path in `G`.

    An *alternating path* is a path in which every other edge is in the
    specified maximum matching (and the remaining edges in the path are not in
    the matching). An alternating path may have matched edges in the even
    positions or in the odd positions, as long as the edges alternate between
    'matched' and 'unmatched'.

    `G` is an undirected bipartite NetworkX graph.

    `v` is a vertex in `G`.

    `matched_edges` is a set of edges present in a maximum matching in `G`.

    `unmatched_edges` is a set of edges not present in a maximum
    matching in `G`.

    `targets` is a set of vertices.

    c   	         s)  | r t  j ˆ ˆ g ƒ } n t  j ˆ ˆ g ƒ } t ƒ  } |  t ˆ  |  ƒ t | ƒ f g } xÄ | r$| d \ } } } y‰ t | ƒ } | | k r| | f | k s¹ | | f | k r| ˆ k rÉ t S| j | ƒ | j | t ˆ  | ƒ t | ƒ f ƒ qn  Wqa t k
 r | j	 ƒ  qa Xqa Wt
 S(   st  Returns True if and only if `u` is connected to one of the
        targets by an alternating path.

        `u` is a vertex in the graph `G`.

        If `along_matched` is True, this step of the depth-first search
        will continue only through edges in the given matching. Otherwise, it
        will continue only through edges *not* in the given matching.

        iÿÿÿÿ(   R   t   cyclet   sett   itert   nextR   t   addR   t   StopIterationR#   R   (	   R   t   along_matchedR,   t   visitedt   stackt   parentt   childrent   valid_edgest   child(   R   t   matched_edgest   targetst   unmatched_edges(    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pyt   _alternating_dfsL  s&    	"	0R:   (   R   R   (   R   R
   RA   RC   RB   RD   (    (   R   RA   RB   RC   sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pyt!   _is_connected_by_alternating_path5  s    #c            s_   d „  | j  ƒ  Dƒ ‰ d „  ˆ Dƒ ‰ ‡ f d †  ˆ  j ƒ  Dƒ ‰ ‡  ‡ ‡ ‡ f d †  ˆ  Dƒ S(   s»  Returns the set of vertices that are connected to one of the target
    vertices by an alternating path in `G` or are themselves a target.

    An *alternating path* is a path in which every other edge is in the
    specified maximum matching (and the remaining edges in the path are not in
    the matching). An alternating path may have matched edges in the even
    positions or in the odd positions, as long as the edges alternate between
    'matched' and 'unmatched'.

    `G` is an undirected bipartite NetworkX graph.

    `matching` is a dictionary representing a maximum matching in `G`, as
    returned by, for example, :func:`maximum_matching`.

    `targets` is a set of vertices.

    c         S   s(   h  |  ] \ } } t  | | f ƒ ’ q S(    (   t	   frozenset(   R   R   R
   (    (    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pys	   <setcomp>‰  s   	 c         S   s   h  |  ] } t  | ƒ ’ q S(    (   t   tuple(   R   t   edge(    (    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pys	   <setcomp>Š  s   	 c            s:   h  |  ]0 \ } } t  | | f ƒ ˆ  k r | | f ’ q S(    (   RF   (   R   R   R
   (   t	   edge_sets(    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pys	   <setcomp>‹  s   	 	c            s:   h  |  ]0 } | ˆ k s0 t  ˆ  | ˆ ˆ ˆ ƒ r | ’ q S(    (   RE   (   R   R
   (   R   RA   RB   RC   (    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pys	   <setcomp>Ž  s   	 (   R   R,   (   R   R&   RB   (    (   R   RI   RA   RB   RC   sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pyt   _connected_by_alternating_pathss  s    c         C   sW   t  |  | ƒ \ } } t |  ƒ t | ƒ } | | @} t |  | | ƒ } | | | | @BS(   sõ  Returns the minimum vertex cover corresponding to the given maximum
    matching of the bipartite graph `G`.

    Parameters
    ----------

    G : NetworkX graph

      Undirected bipartite graph

    matching : dictionary

      A dictionary whose keys are vertices in `G` and whose values are the
      distinct neighbors comprising the maximum matching for `G`, as returned
      by, for example, :func:`maximum_matching`. The dictionary *must*
      represent the maximum matching.

    top_nodes : container

      Container with all nodes in one bipartite node set. If not supplied
      it will be computed. But if more than one solution exists an exception
      will be raised.

    Returns
    -------

    vertex_cover : :class:`set`

      The minimum vertex cover in `G`.

    Raises
    ------
    AmbiguousSolution : Exception

      Raised if the input bipartite graph is disconnected and no container
      with all nodes in one bipartite set is provided. When determining
      the nodes in each bipartite set more than one valid solution is
      possible if the input graph is disconnected.

    Notes
    -----

    This function is implemented using the procedure guaranteed by `Konig's
    theorem
    <https://en.wikipedia.org/wiki/K%C3%B6nig%27s_theorem_%28graph_theory%29>`_,
    which proves an equivalence between a maximum matching and a minimum vertex
    cover in bipartite graphs.

    Since a minimum vertex cover is the complement of a maximum independent set
    for any graph, one can compute the maximum independent set of a bipartite
    graph this way:

    >>> import networkx as nx
    >>> G = nx.complete_bipartite_graph(2, 3)
    >>> matching = nx.bipartite.maximum_matching(G)
    >>> vertex_cover = nx.bipartite.to_vertex_cover(G, matching)
    >>> independent_set = set(G) - vertex_cover
    >>> print(list(independent_set))
    [2, 3, 4]

    See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
    for further details on how bipartite graphs are handled in NetworkX.

    (   R   R5   RJ   (   R   R&   R   R$   t   Rt   unmatched_verticest   Ut   Z(    (    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pyR   “  s
    C
(   t   __doc__R   R   t   networkx.algorithms.bipartiteR    R   t   networkxR*   t   __all__t   floatR   R   R   R   RE   RJ   R   R   (    (    (    sE   lib/python2.7/site-packages/networkx/algorithms/bipartite/matching.pyt   <module>+   s   		v‡	>	 R