1
0
Fork 0

Simplify find_path_through_blossom

This commit is contained in:
Joris van Rantwijk 2023-05-12 18:12:25 +02:00
parent be2b474873
commit 76de35471f
1 changed files with 10 additions and 32 deletions

View File

@ -969,40 +969,18 @@ class _MatchingContext:
Tuple (nodes, edges).
"""
nodes: list[_Blossom] = [sub]
edges: list[tuple[int, int]] = []
# Walk around the blossom from "sub" to its base.
p = blossom.subblossoms.index(sub)
nsub = len(blossom.subblossoms)
while p != 0:
if p % 2 == 0:
# Stepping towards the beginning of the subblossom list.
# Currently at subblossom (p), next position (p-2):
#
# 0 --- 1 === 2 --- 3 === (p-2) --- (p-1) ==(i,j)== (p)
# ^^^ ^^^
# <-------------------
#
# We flip edges from (i,j) to (j,i) to make them fit
# in the path from "s" to base.
edges.append(blossom.edges[p-1][::-1])
nodes.append(blossom.subblossoms[p-1])
edges.append(blossom.edges[p-2][::-1])
nodes.append(blossom.subblossoms[p-2])
p -= 2
else:
# Stepping towards the end of the subblossom list.
# Currently at subblossom (p), next position (p+2):
#
# (p) ==(i,j)== (p+1) --- (p+2) === (p+3) --- 0
# ^^^ ^^^
# ------------------->
edges.append(blossom.edges[p])
nodes.append(blossom.subblossoms[p+1])
edges.append(blossom.edges[p+1])
nodes.append(blossom.subblossoms[(p+2) % nsub])
p = (p + 2) % nsub
if p % 2 == 0:
# Walk backwards around the blossom.
# Flip edges from (i,j) to (j,i) to make them fit
# in the path from "sub" to base.
nodes = blossom.subblossoms[p::-1]
edges = [(j, i) for (i, j) in blossom.edges[:p][::-1]]
else:
# Walk forward around the blossom.
nodes = blossom.subblossoms[p:] + blossom.subblossoms[0:1]
edges = blossom.edges[p:]
return (nodes, edges)