def pruesse_ruskey_local(poset, pi, inv, a, b): def extended_poset(x, y): # Extend poset so that a < b return (x, y) == (a, b) or poset(x, y) while True: mrb = 0 typical = False while move(pi, inv, b, RIGHT, extended_poset): mrb += 1 yield True mra = 0 while move(pi, inv, a, RIGHT, extended_poset): typical = True mra += 1 yield True if typical: yield SWITCH_SIGN mla = mra + (-1 if mrb % 2 else 1) # a left moves for __ in range(mla): move(pi, inv, a, LEFT) yield True if typical and mrb % 2 == 1: move(pi, inv, a, LEFT) yield True else: yield SWITCH_SIGN for __ in range(mrb): move(pi, inv, b, LEFT) yield True yield False
def varol_rotem_local(poset, pi, inv, x): # Move i to the left while maintaining pi as a linear extension of poset. # When i can no longer move to the left, do a cyclic shift to put i back to # its starting position. while True: while move(pi, inv, x, LEFT, poset): yield True left_cyclic_shift(pi, inv, inv[x], x) yield False
def gen_all(n, poset): # 0 and n + 1 will be used as the minimum and maximum poset = add_min_max(poset, 0, n + 1) pi = list(range(n + 2)) inv = pi[:] yield pi[1:-1] x = n while x > 1: if move(pi, inv, x, LEFT, poset): yield pi[1:-1] x = n else: left_cyclic_shift(pi, inv, inv[x], x) x -= 1