For a finite graph without an isolated vertex and with nonnegative edge weights, put and let attain this minimum. Define savings and baseline . A maximum-weight matching with weights , allowing arbitrary cardinality, gives a minimum-weight edge coverIt replaces separate cheapest coverage of two endpoints by a shared edge when doing so saves enough weight. Savings can be negative even though original costs are nonnegative; such edges need not be selected.
Proof. The construction covers all vertices. Duplicate selected edges are counted once, so nonnegative weights give . Take an optimal edge cover and delete redundant edges to make it inclusion-minimal without increasing cost. Its components are star graphs, by the argument for the edge cover and matching identity. Select one edge from each component to obtain a matching in a graph . A component with center , selected leaf and other leaves contributes to , at most its total edge weight since . Thus . Maximizing giveswhich proves both the construction and the displayed identity, including zero-weight cases. One call to a general-graph maximum-weight matching polynomial-time algorithm suffices, together with linear-time preprocessing. Choosing a maximum cardinality matching under these modified weights would impose an unnecessary constraint and can spoil the reduction.[ref]
Maximum-weight matching documentation by NetworkX. max_weight_matching, maxcardinality=False, and Notes on polynomial running time
Use minimum-weight edge cover by matching savings. Reject inputs with an isolated vertex. For every vertex , choose a cheapest incident edge and write . Define a baseline and a saving for each edge byCompute a maximum-weight matching using as its weights, allowing the empty matching in a graph and imposing no maximum-cardinality condition. Negative savings may be ignored. ReturnThis is an edge cover. Since original weights are nonnegative, removing duplicate appearances of an edge cannot increase the total, and henceTo prove optimality, take a minimum-weight edge cover and remove redundant edges until it is inclusion-minimal. Nonnegative weights ensure that its weight does not increase, so it remains optimal. As proved in (i), its components are star graphs. Choose one edge from each star graph, forming a matching in a graph .
In a star graph with center , selected leaf , and other leaves , this choice contributes to . Each other leaf has its edge available, so . Thus this contribution is at most the weight of that star graph. Summing over components yieldsSince maximizes saving, , and consequentlyAll inequalities are equalities. Therefore is a minimum-weight edge cover. This proof also handles zero weights and duplicate cheapest-edge choices.
Finding all and constructing takes time. A general-graph maximum-weight matching polynomial-time algorithm supplies the only substantial step; the documented routine runs in arithmetic time. Exact rational or integer weights avoid floating-point comparisons affecting optimality.[ref]
Maximum-weight matching documentation by NetworkX. max_weight_matching, maxcardinality=False; Notes: O(number_of_nodes ** 3), integer arithmetic for integer weights, and floating-point precision caveat
First test for an isolated vertex, reporting infeasibility if one exists. Otherwise compute a maximum cardinality matching using a general-graph polynomial-time algorithm, then add one arbitrary incident edge for each unmatched vertex in a matching. The proof in (i) shows that the resulting edge cover has minimum cardinality .
The final scan takes time with adjacency lists; the maximum cardinality matching computation is polynomial. One may obtain it from a general-graph maximum-weight matching routine with every weight equal to one.[ref]
Maximum-weight matching documentation by NetworkX. max_weight_matching, unit weights give maximum cardinality; Notes: cubic vertex-count running time