Clean up trace_alternating_paths()
This commit is contained in:
parent
f491d6dcec
commit
d1b79c1cde
|
@ -840,18 +840,17 @@ class _MatchingContext:
|
||||||
|
|
||||||
# "xedges" is a list of edges used while tracing from "x".
|
# "xedges" is a list of edges used while tracing from "x".
|
||||||
# "yedges" is a list of edges used while tracing from "y".
|
# "yedges" is a list of edges used while tracing from "y".
|
||||||
xedges: list[tuple[int, int]] = []
|
# Pre-load the edge (x, y) on both lists.
|
||||||
yedges: list[tuple[int, int]] = []
|
xedges: list[tuple[int, int]] = [(x, y)]
|
||||||
|
yedges: list[tuple[int, int]] = [(y, x)]
|
||||||
|
|
||||||
# Pre-load the edge between "x" and "y" so it will end up in the right
|
# "first_common" is the first common ancestor of "x" and "y"
|
||||||
# place in the final path.
|
# in the alternating tree, or None if there is no common ancestor.
|
||||||
xedges.append((x, y))
|
first_common: Optional[_Blossom] = None
|
||||||
|
|
||||||
# Alternate between tracing the path from "x" and the path from "y".
|
# Alternate between tracing the path from "x" and the path from "y".
|
||||||
# This ensures that the search time is bounded by the size of the
|
# This ensures that the search time is bounded by the size of the
|
||||||
# newly found blossom.
|
# newly found blossom.
|
||||||
# TODO : this code is a bit shady; maybe reconsider the swapping trick
|
|
||||||
first_common: Optional[_Blossom] = None
|
|
||||||
while x != -1 or y != -1:
|
while x != -1 or y != -1:
|
||||||
|
|
||||||
# Check if we found a common ancestor.
|
# Check if we found a common ancestor.
|
||||||
|
@ -882,17 +881,16 @@ class _MatchingContext:
|
||||||
b.marker = False
|
b.marker = False
|
||||||
|
|
||||||
# If we found a common ancestor, trim the paths so they end there.
|
# If we found a common ancestor, trim the paths so they end there.
|
||||||
# TODO : also this is just plain ugly - try to rework
|
|
||||||
if first_common is not None:
|
if first_common is not None:
|
||||||
assert self.vertex_top_blossom[xedges[-1][0]] is first_common
|
assert self.vertex_top_blossom[xedges[-1][0]] is first_common
|
||||||
while yedges and (self.vertex_top_blossom[yedges[-1][0]]
|
while self.vertex_top_blossom[yedges[-1][0]] is not first_common:
|
||||||
is not first_common):
|
|
||||||
yedges.pop()
|
yedges.pop()
|
||||||
|
|
||||||
# Fuse the two paths.
|
# Fuse the two paths.
|
||||||
# Flip the order of one path and the edge tuples in the other path
|
# Flip the order of one path, and flip the edge tuples in the other
|
||||||
# to obtain a continuous path with correctly ordered edge tuples.
|
# path to obtain a continuous path with correctly ordered edge tuples.
|
||||||
path_edges = xedges[::-1] + [(y, x) for (x, y) in yedges]
|
# Skip the duplicate edge in one of the paths.
|
||||||
|
path_edges = xedges[::-1] + [(y, x) for (x, y) in yedges[1:]]
|
||||||
|
|
||||||
# Any S-to-S alternating path must have odd length.
|
# Any S-to-S alternating path must have odd length.
|
||||||
assert len(path_edges) % 2 == 1
|
assert len(path_edges) % 2 == 1
|
||||||
|
|
Loading…
Reference in New Issue