ó
|£*^c           @   sƒ   d  Z  d d l Z d d l Z d d l m Z d d d d g Z e d ƒ Z d	 „  Z	 d
 „  Z
 d „  Z d „  Z 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

    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.

    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 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://dx.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(    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pyt   breadth_first_searcha   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   (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pyR   r   s    


c         S   s   i  |  ] } d  | “ q S(   N(   R   (   t   .0R
   (    (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pys
   <dictcomp>€   s   	 c         S   s   i  |  ] } d  | “ q S(   N(   R   (   R   R
   (    (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pys
   <dictcomp>�   s   	 i    i   c         S   s+   i  |  ]! \ } } | d  k	 r | | “ q S(   N(   R   (   R   t   kR
   (    (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pys
   <dictcomp>�   s   	 c         S   s+   i  |  ]! \ } } | d  k	 r | | “ q S(   N(   R   (   R   R   R
   (    (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pys
   <dictcomp>�   s   	 N(   t   bipartite_setst   collectionst   dequeR   t   itemst   dictt	   itertoolst   chain(   R   R   t   rightt   num_matched_pairsR
   (    (   R   R   R   R   R   R   R   sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pyR   7   s     *c            sî  i  ‰  x= |  D]5 } x, |  | D]  } | ˆ  k r | ˆ  | <Pq q Wq Wx¡t  réi  ‰ g  ‰ ‡ f d †  |  Dƒ ‰ x ˆ  D] } ˆ ˆ  | =qx Wt ˆ ƒ } xÅ | r`ˆ r`i  } xK | D]C } x: |  | D]. } | ˆ k rÇ | j | g  ƒ j | ƒ qÇ qÇ Wq¶ Wg  } xW | D]O } | | ˆ | <| ˆ  k rL| j ˆ  | ƒ | ˆ ˆ  | <q
ˆ j | ƒ q
Wqœ Wˆ s°i  } x< |  D]4 } x+ |  | D] } | ˆ k r…d | | <q…q…WqtWˆ  S‡  ‡ ‡ ‡ ‡ f d †  ‰ x ˆ D] } ˆ | ƒ qÒWqI Wd S(   s
  Returns the maximum cardinality matching of the bipartite graph `G`.

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

      Undirected bipartite graph

    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.

    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 Also
    --------

    hopcroft_karp_matching

    c            s   i  |  ] } ˆ  | “ q S(    (    (   R   R   (   t	   unmatched(    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/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!   (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pyR(   ú   s    
N(   R   t   listt
   setdefaultR   R   (   R   R   R
   t   layert   newLayert	   unlayered(    (   R%   R&   R'   R(   R!   sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pyR   ›   sH     
	$c            s~   d „  | j  ƒ  Dƒ ‰ t ˆ  j ƒ  ƒ ˆ ‰ t ‡  ‡ ‡ ‡ ‡ f d † ‰ ˆ | t ˆ  ƒ d t ƒp} ˆ | t ˆ  ƒ 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`.

    `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  |  ]$ \ } } | | k r | | f ’ q S(    (    (   R   R   R
   (    (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pys	   <setcomp>   s   	 c            s„   |  ˆ k r t  S| d k  r  t S| r, ˆ n ˆ } xK ˆ  |  D]? } |  | f | k sg | |  f | k r= ˆ | | d | ƒ Sq= Wt S(   sÖ  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`.

        `depth` specifies the maximum recursion depth of the depth-first
        search.

        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    i   (   R   R   (   R   t   deptht   along_matchedt   valid_edgesR
   (   R   t   _alternating_dfst   matched_edgest   targetst   unmatched_edges(    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pyR1   #  s    $R/   (   R   t   sett   edgesR   t   lenR   (   R   R
   R%   R3   (    (   R   R1   R2   R3   R4   sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pyt!   _is_connected_by_alternating_path	  s
    (c            s   ‡  ‡ ‡ 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`.

    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+   h  |  ]! } t  ˆ  | ˆ ˆ ƒ r | ’ q S(    (   R8   (   R   R
   (   R   R%   R3   (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pys	   <setcomp>b  s   	 (    (   R   R%   R3   (    (   R   R%   R3   sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pyt   _connected_by_alternating_pathsO  s    c         C   sT   t  |  ƒ \ } } t |  ƒ t | ƒ } | | @} t |  | | ƒ } | | | | @BS(   sT  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.

    Returns
    -------

    vertex_cover : :class:`set`

      The minimum vertex cover in `G`.

    Notes
    -----

    This function is implemented using the procedure guaranteed by `Konig's
    theorem
    <http://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]

    (   R   R5   R9   (   R   R%   R#   t   Rt   unmatched_verticest   Ut   Z(    (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pyR   f  s
    1
(   t   __doc__R   R   t   networkx.algorithms.bipartiteR    R   t   __all__t   floatR   R   R   R8   R9   R   R   (    (    (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/matching.pyt   <module>+   s   			d	n	F		?