Beispiel #1
0
def __path(V, D, k):
  if D == 0:
    return [(xy, xy) for xy in irange(V[0][0])]

  tx = V[D][k]

  if k == -D or (k != D and V[D][k - 1] < V[D][k + 1]):
    x = V[D][k + 1]
    y = x - (k + 1)
    k = k + 1
    y = y + 1
  else:
    x = V[D][k - 1]
    y = x - (k - 1)
    k = k - 1
    x = x + 1

  return __path(V, D - 1, k) + [(x + d, y + d) for d in irange(tx - x)]
Beispiel #2
0
def path(a, b, eq = __eq):
  """
  Finds the path through the match grid of sequence a and b,
  using the function eq to determine equality.
  Returns path
  """

  m = len(a)
  n = len(b)
  mn = m + n

  if mn == 0: # two empty sequences
    return [(0, 0)]
  
  Vd = []
  V = {1: 0}

  for D in irange(mn):
    for k in irange(-D, D, 2):
      if k == -D or (k != D and V[k - 1] < V[k + 1]):
        x = V[k + 1]
      else:
        x = V[k - 1] + 1
      y = x - k

      while x < m and y < n and eq(a[x], b[y]):
        x += 1
        y += 1

      V[k] = x
    
      if x >= m and y >= n:
        Vd.append(V.copy())
        return __path(Vd, D, k)

    Vd.append(V.copy())

  raise Exception("lcs should not reach here")