Docsity

Review Sheet Outlines - Data Structure | EECS 281, Study notes of Algorithms and Programming

Partial preview of the text

Download Review Sheet Outlines - Data Structure | EECS 281 and more Study notes Algorithms and Programming in PDF only on Docsity!

Outline

Last time:

  • Review of P/NP and 2-approximate solution to TSP
  • Dynamic Programming: o 0/1 Knapsack o Travelling Salesperson

Today:

  • finish up Bitonic merge
  • Longest Common Subsequence problem

Longest Common Subsequence (LCS)

Examples of LCS applications:

  • approximate matching, e.g., spell checker
  • file comparison (used in diff, cvs, cheater finder)
  • searching for proteins with similar DNA sequences, etc.

For string GDV EGT A, GV EA is an example subsequence of the string

Contrast:

  • substring : elements in the substring must be consecutive in the string
  • subsequence : not

Example: given 2 strings, GCVEGTA and GVCEKST,

the longest common subsequence is GV ET , note CET is also a common subsequence

Brute Force LCS

Given strings a and b, of length n and m respectively:

  • each element of a is either in or not in a subsequence
  • there are 2 n^ different subsequences
  • each subsequence must be matched against b’s m elements
  • Running time: O(m 2 n)

Dynamic Programming for LCS

Define L[i, j] = k to be the length of the LCS of

a[1,... , i] and b[1,... , j], where 1 ≤ i ≤ n and 1 ≤ j ≤ m

Question: Can we find overlapping subproblems such that the optimal

solution to the LCS problem consists of optimal solutions to the

subproblems?

Would the optimal solution for L[i, j] consists of optimal solutions for

L[i′, j′], where i′^ ≤ i and j′^ ≤ j?

Use similar approach to solving for the 0/1 Knapsack problem:

consider adding or not adding the last item to the solution of the

subproblem

DP for LCS

Let L[i, 0] = L[0, j] = 0, k the length of the LCS

Case 1: if a[i] 6 = b[j], one of them cannot be in the LCS of L[i, j], so either L[i, j] = L[i, j − 1] or L[i, j] = L[i − 1 , j] Since we want the longest common subsequence, L[i, j] = MAX(L[i, j − 1], L[i − 1 , j])

Case 2: if a[i] == b[j], then L[i − 1 , j − 1] must be k − 1

(Thus L[i, j] is optimal and consists of optimal solutions to the subproblems)

Proof by contradiction: assume L[i − 1 , j − 1] ≥ k, then adding a[i] to a[1,... , i − 1] and b[j] to b[1,... , j − 1] makes L[i, j] ≥ k + 1 ⇒ contradiction So:

L[i, j] =

  

0 if i = 0 or j = 0, L[i − 1 , j − 1] + 1 if i, j > 0 and a[i] == b[j], MAX(L[i − 1 , j], L[i, j − 1]) otherwise

(1)

LCS Extraction

Use stack to accummulate LCS in reversed order

Start from L[n, m]:

  • if L[i] == L[j], found a match, push L[i], move to L[i − 1][j − 1]
  • otherwise, move to the larger of L[i − 1][j] and L[i][j − 1], if L[i − 1][j] == L[i][j − 1], always move horizontal or vertical

Alternative algorithm: again, start from L[n, m]:

  • if L[i, j] == L[i − 1 , j] move to L[i − 1 , j]
  • else if L[i, j] == L[i, j − 1] move to L[i, j − 1]
  • (the above two steps can be swapped)
  • else push L[i], move to L[i − 1 , j − 1]

Pop stack (if there are multiple common subsequences with the same length, each of the above extracts only one)