matching.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. # This module uses material from the Wikipedia article Hopcroft--Karp algorithm
  2. # <https://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm>, accessed on
  3. # January 3, 2015, which is released under the Creative Commons
  4. # Attribution-Share-Alike License 3.0
  5. # <http://creativecommons.org/licenses/by-sa/3.0/>. That article includes
  6. # pseudocode, which has been translated into the corresponding Python code.
  7. #
  8. # Portions of this module use code from David Eppstein's Python Algorithms and
  9. # Data Structures (PADS) library, which is dedicated to the public domain (for
  10. # proof, see <http://www.ics.uci.edu/~eppstein/PADS/ABOUT-PADS.txt>).
  11. """Provides functions for computing maximum cardinality matchings and minimum
  12. weight full matchings in a bipartite graph.
  13. If you don't care about the particular implementation of the maximum matching
  14. algorithm, simply use the :func:`maximum_matching`. If you do care, you can
  15. import one of the named maximum matching algorithms directly.
  16. For example, to find a maximum matching in the complete bipartite graph with
  17. two vertices on the left and three vertices on the right:
  18. >>> G = nx.complete_bipartite_graph(2, 3)
  19. >>> left, right = nx.bipartite.sets(G)
  20. >>> list(left)
  21. [0, 1]
  22. >>> list(right)
  23. [2, 3, 4]
  24. >>> nx.bipartite.maximum_matching(G)
  25. {0: 2, 1: 3, 2: 0, 3: 1}
  26. The dictionary returned by :func:`maximum_matching` includes a mapping for
  27. vertices in both the left and right vertex sets.
  28. Similarly, :func:`minimum_weight_full_matching` produces, for a complete
  29. weighted bipartite graph, a matching whose cardinality is the cardinality of
  30. the smaller of the two partitions, and for which the sum of the weights of the
  31. edges included in the matching is minimal.
  32. """
  33. import collections
  34. import itertools
  35. import networkx as nx
  36. from networkx.algorithms.bipartite import sets as bipartite_sets
  37. from networkx.algorithms.bipartite.matrix import biadjacency_matrix
  38. __all__ = [
  39. "maximum_matching",
  40. "hopcroft_karp_matching",
  41. "eppstein_matching",
  42. "to_vertex_cover",
  43. "minimum_weight_full_matching",
  44. ]
  45. INFINITY = float("inf")
  46. @nx._dispatchable
  47. def hopcroft_karp_matching(G, top_nodes=None):
  48. """Returns the maximum cardinality matching of the bipartite graph `G`.
  49. A matching is a set of edges that do not share any nodes. A maximum
  50. cardinality matching is a matching with the most edges possible. It
  51. is not always unique. Finding a matching in a bipartite graph can be
  52. treated as a networkx flow problem.
  53. The functions ``hopcroft_karp_matching`` and ``maximum_matching``
  54. are aliases of the same function.
  55. Parameters
  56. ----------
  57. G : NetworkX graph
  58. Undirected bipartite graph
  59. top_nodes : container of nodes
  60. Container with all nodes in one bipartite node set. If not supplied
  61. it will be computed. But if more than one solution exists an exception
  62. will be raised.
  63. Returns
  64. -------
  65. matches : dictionary
  66. The matching is returned as a dictionary, `matches`, such that
  67. ``matches[v] == w`` if node `v` is matched to node `w`. Unmatched
  68. nodes do not occur as a key in `matches`.
  69. Raises
  70. ------
  71. AmbiguousSolution
  72. Raised if the input bipartite graph is disconnected and no container
  73. with all nodes in one bipartite set is provided. When determining
  74. the nodes in each bipartite set more than one valid solution is
  75. possible if the input graph is disconnected.
  76. Notes
  77. -----
  78. This function is implemented with the `Hopcroft--Karp matching algorithm
  79. <https://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm>`_ for
  80. bipartite graphs.
  81. See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
  82. for further details on how bipartite graphs are handled in NetworkX.
  83. See Also
  84. --------
  85. maximum_matching
  86. hopcroft_karp_matching
  87. eppstein_matching
  88. References
  89. ----------
  90. .. [1] John E. Hopcroft and Richard M. Karp. "An n^{5 / 2} Algorithm for
  91. Maximum Matchings in Bipartite Graphs" In: **SIAM Journal of Computing**
  92. 2.4 (1973), pp. 225--231. <https://doi.org/10.1137/0202019>.
  93. """
  94. # First we define some auxiliary search functions.
  95. #
  96. # If you are a human reading these auxiliary search functions, the "global"
  97. # variables `leftmatches`, `rightmatches`, `distances`, etc. are defined
  98. # below the functions, so that they are initialized close to the initial
  99. # invocation of the search functions.
  100. def breadth_first_search():
  101. for v in left:
  102. if leftmatches[v] is None:
  103. distances[v] = 0
  104. queue.append(v)
  105. else:
  106. distances[v] = INFINITY
  107. distances[None] = INFINITY
  108. while queue:
  109. v = queue.popleft()
  110. if distances[v] < distances[None]:
  111. for u in G[v]:
  112. if distances[rightmatches[u]] is INFINITY:
  113. distances[rightmatches[u]] = distances[v] + 1
  114. queue.append(rightmatches[u])
  115. return distances[None] is not INFINITY
  116. def depth_first_search(v):
  117. if v is not None:
  118. for u in G[v]:
  119. if distances[rightmatches[u]] == distances[v] + 1:
  120. if depth_first_search(rightmatches[u]):
  121. rightmatches[u] = v
  122. leftmatches[v] = u
  123. return True
  124. distances[v] = INFINITY
  125. return False
  126. return True
  127. # Initialize the "global" variables that maintain state during the search.
  128. left, right = bipartite_sets(G, top_nodes)
  129. leftmatches = {v: None for v in left}
  130. rightmatches = {v: None for v in right}
  131. distances = {}
  132. queue = collections.deque()
  133. # Implementation note: this counter is incremented as pairs are matched but
  134. # it is currently not used elsewhere in the computation.
  135. num_matched_pairs = 0
  136. while breadth_first_search():
  137. for v in left:
  138. if leftmatches[v] is None:
  139. if depth_first_search(v):
  140. num_matched_pairs += 1
  141. # Strip the entries matched to `None`.
  142. leftmatches = {k: v for k, v in leftmatches.items() if v is not None}
  143. rightmatches = {k: v for k, v in rightmatches.items() if v is not None}
  144. # At this point, the left matches and the right matches are inverses of one
  145. # another. In other words,
  146. #
  147. # leftmatches == {v, k for k, v in rightmatches.items()}
  148. #
  149. # Finally, we combine both the left matches and right matches.
  150. return dict(itertools.chain(leftmatches.items(), rightmatches.items()))
  151. @nx._dispatchable
  152. def eppstein_matching(G, top_nodes=None):
  153. """Returns the maximum cardinality matching of the bipartite graph `G`.
  154. Parameters
  155. ----------
  156. G : NetworkX graph
  157. Undirected bipartite graph
  158. top_nodes : container
  159. Container with all nodes in one bipartite node set. If not supplied
  160. it will be computed. But if more than one solution exists an exception
  161. will be raised.
  162. Returns
  163. -------
  164. matches : dictionary
  165. The matching is returned as a dictionary, `matching`, such that
  166. ``matching[v] == w`` if node `v` is matched to node `w`. Unmatched
  167. nodes do not occur as a key in `matching`.
  168. Raises
  169. ------
  170. AmbiguousSolution
  171. Raised if the input bipartite graph is disconnected and no container
  172. with all nodes in one bipartite set is provided. When determining
  173. the nodes in each bipartite set more than one valid solution is
  174. possible if the input graph is disconnected.
  175. Notes
  176. -----
  177. This function is implemented with David Eppstein's version of the algorithm
  178. Hopcroft--Karp algorithm (see :func:`hopcroft_karp_matching`), which
  179. originally appeared in the `Python Algorithms and Data Structures library
  180. (PADS) <http://www.ics.uci.edu/~eppstein/PADS/ABOUT-PADS.txt>`_.
  181. See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
  182. for further details on how bipartite graphs are handled in NetworkX.
  183. See Also
  184. --------
  185. hopcroft_karp_matching
  186. """
  187. # Due to its original implementation, a directed graph is needed
  188. # so that the two sets of bipartite nodes can be distinguished
  189. left, right = bipartite_sets(G, top_nodes)
  190. G = nx.DiGraph(G.edges(left))
  191. # initialize greedy matching (redundant, but faster than full search)
  192. matching = {}
  193. for u in G:
  194. for v in G[u]:
  195. if v not in matching:
  196. matching[v] = u
  197. break
  198. while True:
  199. # structure residual graph into layers
  200. # pred[u] gives the neighbor in the previous layer for u in U
  201. # preds[v] gives a list of neighbors in the previous layer for v in V
  202. # unmatched gives a list of unmatched vertices in final layer of V,
  203. # and is also used as a flag value for pred[u] when u is in the first
  204. # layer
  205. preds = {}
  206. unmatched = []
  207. pred = {u: unmatched for u in G}
  208. for v in matching:
  209. del pred[matching[v]]
  210. layer = list(pred)
  211. # repeatedly extend layering structure by another pair of layers
  212. while layer and not unmatched:
  213. newLayer = {}
  214. for u in layer:
  215. for v in G[u]:
  216. if v not in preds:
  217. newLayer.setdefault(v, []).append(u)
  218. layer = []
  219. for v in newLayer:
  220. preds[v] = newLayer[v]
  221. if v in matching:
  222. layer.append(matching[v])
  223. pred[matching[v]] = v
  224. else:
  225. unmatched.append(v)
  226. # did we finish layering without finding any alternating paths?
  227. if not unmatched:
  228. # TODO - The lines between --- were unused and were thus commented
  229. # out. This whole commented chunk should be reviewed to determine
  230. # whether it should be built upon or completely removed.
  231. # ---
  232. # unlayered = {}
  233. # for u in G:
  234. # # TODO Why is extra inner loop necessary?
  235. # for v in G[u]:
  236. # if v not in preds:
  237. # unlayered[v] = None
  238. # ---
  239. # TODO Originally, this function returned a three-tuple:
  240. #
  241. # return (matching, list(pred), list(unlayered))
  242. #
  243. # For some reason, the documentation for this function
  244. # indicated that the second and third elements of the returned
  245. # three-tuple would be the vertices in the left and right vertex
  246. # sets, respectively, that are also in the maximum independent set.
  247. # However, what I think the author meant was that the second
  248. # element is the list of vertices that were unmatched and the third
  249. # element was the list of vertices that were matched. Since that
  250. # seems to be the case, they don't really need to be returned,
  251. # since that information can be inferred from the matching
  252. # dictionary.
  253. # All the matched nodes must be a key in the dictionary
  254. for key in matching.copy():
  255. matching[matching[key]] = key
  256. return matching
  257. # recursively search backward through layers to find alternating paths
  258. # recursion returns true if found path, false otherwise
  259. def recurse(v):
  260. if v in preds:
  261. L = preds.pop(v)
  262. for u in L:
  263. if u in pred:
  264. pu = pred.pop(u)
  265. if pu is unmatched or recurse(pu):
  266. matching[v] = u
  267. return True
  268. return False
  269. for v in unmatched:
  270. recurse(v)
  271. def _is_connected_by_alternating_path(G, v, matched_edges, unmatched_edges, targets):
  272. """Returns True if and only if the vertex `v` is connected to one of
  273. the target vertices by an alternating path in `G`.
  274. An *alternating path* is a path in which every other edge is in the
  275. specified maximum matching (and the remaining edges in the path are not in
  276. the matching). An alternating path may have matched edges in the even
  277. positions or in the odd positions, as long as the edges alternate between
  278. 'matched' and 'unmatched'.
  279. `G` is an undirected bipartite NetworkX graph.
  280. `v` is a vertex in `G`.
  281. `matched_edges` is a set of edges present in a maximum matching in `G`.
  282. `unmatched_edges` is a set of edges not present in a maximum
  283. matching in `G`.
  284. `targets` is a set of vertices.
  285. """
  286. def _alternating_dfs(u, along_matched=True):
  287. """Returns True if and only if `u` is connected to one of the
  288. targets by an alternating path.
  289. `u` is a vertex in the graph `G`.
  290. If `along_matched` is True, this step of the depth-first search
  291. will continue only through edges in the given matching. Otherwise, it
  292. will continue only through edges *not* in the given matching.
  293. """
  294. visited = set()
  295. # Follow matched edges when depth is even,
  296. # and follow unmatched edges when depth is odd.
  297. initial_depth = 0 if along_matched else 1
  298. stack = [(u, iter(G[u]), initial_depth)]
  299. while stack:
  300. parent, children, depth = stack[-1]
  301. valid_edges = matched_edges if depth % 2 else unmatched_edges
  302. try:
  303. child = next(children)
  304. if child not in visited:
  305. if (parent, child) in valid_edges or (child, parent) in valid_edges:
  306. if child in targets:
  307. return True
  308. visited.add(child)
  309. stack.append((child, iter(G[child]), depth + 1))
  310. except StopIteration:
  311. stack.pop()
  312. return False
  313. # Check for alternating paths starting with edges in the matching, then
  314. # check for alternating paths starting with edges not in the
  315. # matching.
  316. return _alternating_dfs(v, along_matched=True) or _alternating_dfs(
  317. v, along_matched=False
  318. )
  319. def _connected_by_alternating_paths(G, matching, targets):
  320. """Returns the set of vertices that are connected to one of the target
  321. vertices by an alternating path in `G` or are themselves a target.
  322. An *alternating path* is a path in which every other edge is in the
  323. specified maximum matching (and the remaining edges in the path are not in
  324. the matching). An alternating path may have matched edges in the even
  325. positions or in the odd positions, as long as the edges alternate between
  326. 'matched' and 'unmatched'.
  327. `G` is an undirected bipartite NetworkX graph.
  328. `matching` is a dictionary representing a maximum matching in `G`, as
  329. returned by, for example, :func:`maximum_matching`.
  330. `targets` is a set of vertices.
  331. """
  332. # Get the set of matched edges and the set of unmatched edges. Only include
  333. # one version of each undirected edge (for example, include edge (1, 2) but
  334. # not edge (2, 1)). Using frozensets as an intermediary step we do not
  335. # require nodes to be orderable.
  336. edge_sets = {frozenset((u, v)) for u, v in matching.items()}
  337. matched_edges = {tuple(edge) for edge in edge_sets}
  338. unmatched_edges = {
  339. (u, v) for (u, v) in G.edges() if frozenset((u, v)) not in edge_sets
  340. }
  341. return {
  342. v
  343. for v in G
  344. if v in targets
  345. or _is_connected_by_alternating_path(
  346. G, v, matched_edges, unmatched_edges, targets
  347. )
  348. }
  349. @nx._dispatchable
  350. def to_vertex_cover(G, matching, top_nodes=None):
  351. """Returns the minimum vertex cover corresponding to the given maximum
  352. matching of the bipartite graph `G`.
  353. Parameters
  354. ----------
  355. G : NetworkX graph
  356. Undirected bipartite graph
  357. matching : dictionary
  358. A dictionary whose keys are vertices in `G` and whose values are the
  359. distinct neighbors comprising the maximum matching for `G`, as returned
  360. by, for example, :func:`maximum_matching`. The dictionary *must*
  361. represent the maximum matching.
  362. top_nodes : container
  363. Container with all nodes in one bipartite node set. If not supplied
  364. it will be computed. But if more than one solution exists an exception
  365. will be raised.
  366. Returns
  367. -------
  368. vertex_cover : :class:`set`
  369. The minimum vertex cover in `G`.
  370. Raises
  371. ------
  372. AmbiguousSolution
  373. Raised if the input bipartite graph is disconnected and no container
  374. with all nodes in one bipartite set is provided. When determining
  375. the nodes in each bipartite set more than one valid solution is
  376. possible if the input graph is disconnected.
  377. Notes
  378. -----
  379. This function is implemented using the procedure guaranteed by `Konig's
  380. theorem
  381. <https://en.wikipedia.org/wiki/K%C3%B6nig%27s_theorem_%28graph_theory%29>`_,
  382. which proves an equivalence between a maximum matching and a minimum vertex
  383. cover in bipartite graphs.
  384. Since a minimum vertex cover is the complement of a maximum independent set
  385. for any graph, one can compute the maximum independent set of a bipartite
  386. graph this way:
  387. >>> G = nx.complete_bipartite_graph(2, 3)
  388. >>> matching = nx.bipartite.maximum_matching(G)
  389. >>> vertex_cover = nx.bipartite.to_vertex_cover(G, matching)
  390. >>> independent_set = set(G) - vertex_cover
  391. >>> print(list(independent_set))
  392. [2, 3, 4]
  393. See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
  394. for further details on how bipartite graphs are handled in NetworkX.
  395. """
  396. # This is a Python implementation of the algorithm described at
  397. # <https://en.wikipedia.org/wiki/K%C3%B6nig%27s_theorem_%28graph_theory%29#Proof>.
  398. L, R = bipartite_sets(G, top_nodes)
  399. # Let U be the set of unmatched vertices in the left vertex set.
  400. unmatched_vertices = set(G) - set(matching)
  401. U = unmatched_vertices & L
  402. # Let Z be the set of vertices that are either in U or are connected to U
  403. # by alternating paths.
  404. Z = _connected_by_alternating_paths(G, matching, U)
  405. # At this point, every edge either has a right endpoint in Z or a left
  406. # endpoint not in Z. This gives us the vertex cover.
  407. return (L - Z) | (R & Z)
  408. #: Returns the maximum cardinality matching in the given bipartite graph.
  409. #:
  410. #: This function is simply an alias for :func:`hopcroft_karp_matching`.
  411. maximum_matching = hopcroft_karp_matching
  412. @nx._dispatchable(edge_attrs="weight")
  413. def minimum_weight_full_matching(G, top_nodes=None, weight="weight"):
  414. r"""Returns a minimum weight full matching of the bipartite graph `G`.
  415. Let :math:`G = ((U, V), E)` be a weighted bipartite graph with real weights
  416. :math:`w : E \to \mathbb{R}`. This function then produces a matching
  417. :math:`M \subseteq E` with cardinality
  418. .. math::
  419. \lvert M \rvert = \min(\lvert U \rvert, \lvert V \rvert),
  420. which minimizes the sum of the weights of the edges included in the
  421. matching, :math:`\sum_{e \in M} w(e)`, or raises an error if no such
  422. matching exists.
  423. When :math:`\lvert U \rvert = \lvert V \rvert`, this is commonly
  424. referred to as a perfect matching; here, since we allow
  425. :math:`\lvert U \rvert` and :math:`\lvert V \rvert` to differ, we
  426. follow Karp [1]_ and refer to the matching as *full*.
  427. Parameters
  428. ----------
  429. G : NetworkX graph
  430. Undirected bipartite graph
  431. top_nodes : container
  432. Container with all nodes in one bipartite node set. If not supplied
  433. it will be computed.
  434. weight : string, optional (default='weight')
  435. The edge data key used to provide each value in the matrix.
  436. If None, then each edge has weight 1.
  437. Returns
  438. -------
  439. matches : dictionary
  440. The matching is returned as a dictionary, `matches`, such that
  441. ``matches[v] == w`` if node `v` is matched to node `w`. Unmatched
  442. nodes do not occur as a key in `matches`.
  443. Raises
  444. ------
  445. ValueError
  446. Raised if no full matching exists.
  447. ImportError
  448. Raised if SciPy is not available.
  449. Notes
  450. -----
  451. The problem of determining a minimum weight full matching is also known as
  452. the rectangular linear assignment problem. This implementation defers the
  453. calculation of the assignment to SciPy.
  454. References
  455. ----------
  456. .. [1] Richard Manning Karp:
  457. An algorithm to Solve the m x n Assignment Problem in Expected Time
  458. O(mn log n).
  459. Networks, 10(2):143–152, 1980.
  460. """
  461. import numpy as np
  462. import scipy as sp
  463. left, right = nx.bipartite.sets(G, top_nodes)
  464. U = list(left)
  465. V = list(right)
  466. # We explicitly create the biadjacency matrix having infinities
  467. # where edges are missing (as opposed to zeros, which is what one would
  468. # get by using toarray on the sparse matrix).
  469. weights_sparse = biadjacency_matrix(
  470. G, row_order=U, column_order=V, weight=weight, format="coo"
  471. )
  472. weights = np.full(weights_sparse.shape, np.inf)
  473. weights[weights_sparse.row, weights_sparse.col] = weights_sparse.data
  474. left_matches = sp.optimize.linear_sum_assignment(weights)
  475. d = {U[u]: V[v] for u, v in zip(*left_matches)}
  476. # d will contain the matching from edges in left to right; we need to
  477. # add the ones from right to left as well.
  478. d.update({v: u for u, v in d.items()})
  479. return d