covering.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """ Functions related to graph covers."""
  2. from networkx.algorithms.bipartite.matching import hopcroft_karp_matching
  3. from networkx.algorithms.covering import min_edge_cover as _min_edge_cover
  4. from networkx.utils import not_implemented_for
  5. __all__ = ["min_edge_cover"]
  6. @not_implemented_for("directed")
  7. @not_implemented_for("multigraph")
  8. def min_edge_cover(G, matching_algorithm=None):
  9. """Returns a set of edges which constitutes
  10. the minimum edge cover of the graph.
  11. The smallest edge cover can be found in polynomial time by finding
  12. a maximum matching and extending it greedily so that all nodes
  13. are covered.
  14. Parameters
  15. ----------
  16. G : NetworkX graph
  17. An undirected bipartite graph.
  18. matching_algorithm : function
  19. A function that returns a maximum cardinality matching in a
  20. given bipartite graph. The function must take one input, the
  21. graph ``G``, and return a dictionary mapping each node to its
  22. mate. If not specified,
  23. :func:`~networkx.algorithms.bipartite.matching.hopcroft_karp_matching`
  24. will be used. Other possibilities include
  25. :func:`~networkx.algorithms.bipartite.matching.eppstein_matching`,
  26. Returns
  27. -------
  28. set
  29. A set of the edges in a minimum edge cover of the graph, given as
  30. pairs of nodes. It contains both the edges `(u, v)` and `(v, u)`
  31. for given nodes `u` and `v` among the edges of minimum edge cover.
  32. Notes
  33. -----
  34. An edge cover of a graph is a set of edges such that every node of
  35. the graph is incident to at least one edge of the set.
  36. A minimum edge cover is an edge covering of smallest cardinality.
  37. Due to its implementation, the worst-case running time of this algorithm
  38. is bounded by the worst-case running time of the function
  39. ``matching_algorithm``.
  40. """
  41. if G.order() == 0: # Special case for the empty graph
  42. return set()
  43. if matching_algorithm is None:
  44. matching_algorithm = hopcroft_karp_matching
  45. return _min_edge_cover(G, matching_algorithm=matching_algorithm)