From 76de35471f2f14ce281043db3da7820f57ecdf26 Mon Sep 17 00:00:00 2001 From: Joris van Rantwijk Date: Fri, 12 May 2023 18:12:25 +0200 Subject: [PATCH] Simplify find_path_through_blossom --- python/mwmatching.py | 42 ++++++++++-------------------------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/python/mwmatching.py b/python/mwmatching.py index dae66b3..a2ed016 100644 --- a/python/mwmatching.py +++ b/python/mwmatching.py @@ -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)