1
0
Fork 0

Eliminate edges with negative weight

The base algorithm transparently ignores edges, but only if
the graph contains at least 1 edge with positive weight.
If ALL edges have negative weight, dual variables may be
initialized to negative values which leads to failure.
This commit is contained in:
Joris van Rantwijk 2023-02-06 12:35:35 +01:00
parent 99bc2912d8
commit 23c3e35865
1 changed files with 7 additions and 3 deletions

View File

@ -28,9 +28,7 @@ def maximum_weight_matching(
Isolated vertices (not incident to any edge) are allowed, but not Isolated vertices (not incident to any edge) are allowed, but not
recommended since such vertices consume time and memory but have recommended since such vertices consume time and memory but have
no effect on the maximum-weight matching. no effect on the maximum-weight matching.
Edges with negative weight are ignored.
Negative edge weights are allowed, but not recommended since such edges
consume time and memory but have no effect on the maximum-weight matching.
This function takes time O(n**3), where "n" is the number of vertices. This function takes time O(n**3), where "n" is the number of vertices.
This function uses O(n + m) memory, where "m" is the number of edges. This function uses O(n + m) memory, where "m" is the number of edges.
@ -53,6 +51,12 @@ def maximum_weight_matching(
_check_input_types(edges) _check_input_types(edges)
_check_input_graph(edges) _check_input_graph(edges)
# Eliminate edges with negative weight.
# This does not change the solution but prevents complications
# in the algorithm.
if any(e[2] < 0 for e in edges):
edges = [e for e in edges if e[2] >= 0]
# Special case for empty graphs. # Special case for empty graphs.
if not edges: if not edges:
return [] return []