matching.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """
  2. **************
  3. Graph Matching
  4. **************
  5. Given a graph G = (V,E), a matching M in G is a set of pairwise non-adjacent
  6. edges; that is, no two edges share a common vertex.
  7. `Wikipedia: Matching <https://en.wikipedia.org/wiki/Matching_(graph_theory)>`_
  8. """
  9. import networkx as nx
  10. __all__ = ["min_maximal_matching"]
  11. @nx._dispatchable
  12. def min_maximal_matching(G):
  13. r"""Returns the minimum maximal matching of G. That is, out of all maximal
  14. matchings of the graph G, the smallest is returned.
  15. Parameters
  16. ----------
  17. G : NetworkX graph
  18. Undirected graph
  19. Returns
  20. -------
  21. min_maximal_matching : set
  22. Returns a set of edges such that no two edges share a common endpoint
  23. and every edge not in the set shares some common endpoint in the set.
  24. Cardinality will be 2*OPT in the worst case.
  25. Notes
  26. -----
  27. The algorithm computes an approximate solution for the minimum maximal
  28. cardinality matching problem. The solution is no more than 2 * OPT in size.
  29. Runtime is $O(|E|)$.
  30. References
  31. ----------
  32. .. [1] Vazirani, Vijay Approximation Algorithms (2001)
  33. """
  34. return nx.maximal_matching(G)