matching.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. def min_maximal_matching(G):
  12. r"""Returns the minimum maximal matching of G. That is, out of all maximal
  13. matchings of the graph G, the smallest is returned.
  14. Parameters
  15. ----------
  16. G : NetworkX graph
  17. Undirected graph
  18. Returns
  19. -------
  20. min_maximal_matching : set
  21. Returns a set of edges such that no two edges share a common endpoint
  22. and every edge not in the set shares some common endpoint in the set.
  23. Cardinality will be 2*OPT in the worst case.
  24. Notes
  25. -----
  26. The algorithm computes an approximate solution fo the minimum maximal
  27. cardinality matching problem. The solution is no more than 2 * OPT in size.
  28. Runtime is $O(|E|)$.
  29. References
  30. ----------
  31. .. [1] Vazirani, Vijay Approximation Algorithms (2001)
  32. """
  33. return nx.maximal_matching(G)