ó žÃÒYc@s˜dZddlZddlZddlmZddlZddddgZe dƒZ dd „Z dd „Z d „Zd „Zdd „Ze ZdS(sProvides 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(tsetstmaximum_matchingthopcroft_karp_matchingteppstein_matchingtto_vertex_covertinfcs!‡‡‡‡‡‡fd†}‡‡‡‡‡fd†‰tˆ|ƒ\‰}d„ˆDƒ‰d„|Dƒ‰i‰tjƒ‰d}xJ|ƒrÒx:ˆD]2}ˆ|d kr™ˆ|ƒrË|d7}qËq™q™Wq‰Wd„ˆjƒDƒ‰d„ˆjƒDƒ‰ttjˆ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 `_ for bipartite graphs. See :mod:`bipartite documentation ` 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. . csàxBˆD]:}ˆ|dkr7dˆ|<ˆj|ƒqtˆ|“s cSsi|]}d|“qS(N(R(RR ((s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/matching.pys ”s iicSs+i|]!\}}|dk r||“qS(N(R(RtkR ((s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/matching.pys ¢s cSs+i|]!\}}|dk r||“qS(N(R(RRR ((s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/matching.pys £s N(tbipartite_setst collectionstdequeRtitemstdictt itertoolstchain(R t top_nodesRtrighttnum_matched_pairsR ((R RR RRRRs|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/matching.pyR8s <     c s@t||ƒ\}}tj|j|ƒƒ}i‰x=|D]5}x,||D] }|ˆkrK|ˆ|`_. See :mod:`bipartite documentation ` for further details on how bipartite graphs are handled in NetworkX. See Also -------- hopcroft_karp_matching csi|]}ˆ|“qS(((RR (t unmatched(s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/matching.pys ós csw|ˆkrsˆj|ƒ}xU|D]J}|ˆkr"ˆj|ƒ}|ˆks[ˆ|ƒrl|ˆ|‰s cSsh|]}t|ƒ’qS((ttuple(Rtedge((s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/matching.pys Šs cs:h|]0\}}t||fƒˆkr||f’qS((RF(RR R (t edge_sets(s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/matching.pys ‹s cs:h|]0}|ˆks0tˆ|ˆˆˆƒr|’qS((RE(RR (R RARBRC(s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/matching.pys Žs  (RR,(R R&RB((R RIRARBRCs|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/matching.pyt_connected_by_alternating_pathssscCsWt||ƒ\}}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 `_, 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 ` for further details on how bipartite graphs are handled in NetworkX. (RR5RJ(R R&RR$tRtunmatched_verticestUtZ((s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/matching.pyR“s C (t__doc__RRtnetworkx.algorithms.bipartiteRRtnetworkxR*t__all__tfloatRRRRRERJRR(((s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/matching.pyt+s       v ‡ > R