Skip to content

Commit

Permalink
perf: improve algorithm to determine breadth (#50)
Browse files Browse the repository at this point in the history
* perf: improve algorithm to determine breadth

* docs: add comment to new breadth function for clarity
  • Loading branch information
engenmt authored Feb 22, 2023
1 parent 71d63be commit a77f781
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/permpy/permutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,14 +595,18 @@ def noninversions(self):

def breadth(self):
"""Return the minimum taxicab distance among pairs of entries in the permutation."""

min_dist = len(self)
for i, j in itertools.combinations(range(len(self)), 2):
h_dist = abs(i - j)
v_dist = abs(self[i] - self[j])
dist = h_dist + v_dist
if dist < min_dist:
min_dist = dist
for idx_west, val_west in enumerate(self):
for delta_x, val_east in enumerate(
self[idx_west + 1 : idx_west + min_dist - 1], start=1
):
dist = delta_x + abs(val_west - val_east)
if dist < min_dist:
# The minimum breadth attained by a permutation is 2,
# so no smaller breadth will be found.
if dist == 2:
return dist
min_dist = dist
return min_dist

def bonds(self):
Expand Down

0 comments on commit a77f781

Please sign in to comment.