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