Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CS253: Dynamic Programming (2019)

Jinho D. Choi
December 02, 2019

CS253: Dynamic Programming (2019)

Jinho D. Choi

December 02, 2019
Tweet

More Decks by Jinho D. Choi

Other Decks in Technology

Transcript

  1. Fibonacci Sequence 2 • Write a method that returns the

    k’th Fibonacci number. - F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2.
  2. Fibonacci Sequence 2 public abstract class AbstractFibonacci { public int

    get(int k) { if (k < 0) throw new IllegalArgumentException("Invalid: "+k); switch (k) { case 0 : return 0; case 1 : return 1; default: return get2p(k); } } protected abstract int get2p(int k); } • Write a method that returns the k’th Fibonacci number. - F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2.
  3. Fibonacci Sequence 2 public abstract class AbstractFibonacci { public int

    get(int k) { if (k < 0) throw new IllegalArgumentException("Invalid: "+k); switch (k) { case 0 : return 0; case 1 : return 1; default: return get2p(k); } } protected abstract int get2p(int k); } • Write a method that returns the k’th Fibonacci number. - F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2.
  4. Fibonacci Sequence 2 public abstract class AbstractFibonacci { public int

    get(int k) { if (k < 0) throw new IllegalArgumentException("Invalid: "+k); switch (k) { case 0 : return 0; case 1 : return 1; default: return get2p(k); } } protected abstract int get2p(int k); } • Write a method that returns the k’th Fibonacci number. - F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2.
  5. Fibonacci Sequence 2 public abstract class AbstractFibonacci { public int

    get(int k) { if (k < 0) throw new IllegalArgumentException("Invalid: "+k); switch (k) { case 0 : return 0; case 1 : return 1; default: return get2p(k); } } protected abstract int get2p(int k); } • Write a method that returns the k’th Fibonacci number. - F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2.
  6. Fibonacci - Recursive 3 protected int get2p(int k) { switch

    (k) { case 0 : return 0; case 1 : return 1; default: return get2p(k-1) + get2p(k-2); } }
  7. Fibonacci - Loop 5 protected int get2p(int k) { int

    f0 = 0, f1 = 1, f2; for (int i=2; i<k; i++) { f2 = f0 + f1; f0 = f1; f1 = f2; } return f0 + f1; }
  8. Fibonacci - Loop 5 protected int get2p(int k) { int

    f0 = 0, f1 = 1, f2; for (int i=2; i<k; i++) { f2 = f0 + f1; f0 = f1; f1 = f2; } return f0 + f1; } 1 0
  9. Fibonacci - Loop 5 protected int get2p(int k) { int

    f0 = 0, f1 = 1, f2; for (int i=2; i<k; i++) { f2 = f0 + f1; f0 = f1; f1 = f2; } return f0 + f1; } 1 2 0 3 4 5
  10. Fibonacci - Dynamic Programming 7 private int[] createTable(int k) {

    int[] table = new int[k+1]; table[0] = 0; table[1] = 1; Arrays.fill(table, 2, k+1, -1); return table; } public int get2p(int k) { return get2p(k, createTable(k)); }
  11. Fibonacci - Dynamic Programming 7 private int[] createTable(int k) {

    int[] table = new int[k+1]; table[0] = 0; table[1] = 1; Arrays.fill(table, 2, k+1, -1); return table; } public int get2p(int k) { return get2p(k, createTable(k)); }
  12. Fibonacci - Dynamic Programming 7 private int[] createTable(int k) {

    int[] table = new int[k+1]; table[0] = 0; table[1] = 1; Arrays.fill(table, 2, k+1, -1); return table; } public int get2p(int k) { return get2p(k, createTable(k)); }
  13. Fibonacci - Dynamic Programming 7 private int[] createTable(int k) {

    int[] table = new int[k+1]; table[0] = 0; table[1] = 1; Arrays.fill(table, 2, k+1, -1); return table; } private int get2p(int k, int[] table) { if (table[k] < 0) table[k] = get2p(k-1, table) + get2p(k-2, table); return table[k]; } public int get2p(int k) { return get2p(k, createTable(k)); }
  14. Fibonacci - Dynamic Programming 7 private int[] createTable(int k) {

    int[] table = new int[k+1]; table[0] = 0; table[1] = 1; Arrays.fill(table, 2, k+1, -1); return table; } private int get2p(int k, int[] table) { if (table[k] < 0) table[k] = get2p(k-1, table) + get2p(k-2, table); return table[k]; } public int get2p(int k) { return get2p(k, createTable(k)); }
  15. Fibonacci - Recursive vs. Dynamic 8 # of recursive calls

    0 5500 11000 16500 22000 k 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Recursive Dynamic
  16. Towers of Hanoi 11 public abstract class AbstractHanoi { public

    abstract List<String> solve(int n, char source, char intermediate, char destination); protected String getKey(int n, char source, char destination) { return n+":"+source+"->"+destination; } }
  17. Hanoi - Recursive 12 public List<String> solve(int n, char source,

    char intermediate, char destination) { List<String> list = new ArrayList<>(); solve(list, n, source, intermediate, destination); return list; }
  18. Hanoi - Recursive 12 private void solve(List<String> list, int n,


    char source, char intermediate, char destination) { if (n == 0) return; solve(list, n-1, source, destination, intermediate); list.add(getKey(n, source, destination)); solve(list, n-1, intermediate, source, destination); } public List<String> solve(int n, char source, char intermediate, char destination) { List<String> list = new ArrayList<>(); solve(list, n, source, intermediate, destination); return list; }
  19. Hanoi - Recursive 12 private void solve(List<String> list, int n,


    char source, char intermediate, char destination) { if (n == 0) return; solve(list, n-1, source, destination, intermediate); list.add(getKey(n, source, destination)); solve(list, n-1, intermediate, source, destination); } public List<String> solve(int n, char source, char intermediate, char destination) { List<String> list = new ArrayList<>(); solve(list, n, source, intermediate, destination); return list; }
  20. Hanoi - Recursive 12 private void solve(List<String> list, int n,


    char source, char intermediate, char destination) { if (n == 0) return; solve(list, n-1, source, destination, intermediate); list.add(getKey(n, source, destination)); solve(list, n-1, intermediate, source, destination); } public List<String> solve(int n, char source, char intermediate, char destination) { List<String> list = new ArrayList<>(); solve(list, n, source, intermediate, destination); return list; }
  21. Hanoi - Dynamic Programming 13 private void solve(List<String> list, int

    n,
 char source, char intermediate, char destination, Map<String,int[]> map) { if (n == 0) return; int fromIndex = list.size(); int[] sub = map.get(getKey(n-1, source, intermediate)); if (sub != null) addAll(list, sub[0], sub[1]); else solve(list, n-1, source, destination, intermediate, map); String key = getKey(n, source, destination); list.add(key); sub = map.get(getKey(n-1, intermediate, destination)); if (sub != null) addAll(list, sub[0], sub[1]); else solve(list, n-1, intermediate, source, destination, map); if (!map.containsKey(key)) map.put(key, new int[]{fromIndex, list.size()}); }
  22. Hanoi - Dynamic Programming 13 private void solve(List<String> list, int

    n,
 char source, char intermediate, char destination, Map<String,int[]> map) { if (n == 0) return; int fromIndex = list.size(); int[] sub = map.get(getKey(n-1, source, intermediate)); if (sub != null) addAll(list, sub[0], sub[1]); else solve(list, n-1, source, destination, intermediate, map); String key = getKey(n, source, destination); list.add(key); sub = map.get(getKey(n-1, intermediate, destination)); if (sub != null) addAll(list, sub[0], sub[1]); else solve(list, n-1, intermediate, source, destination, map); if (!map.containsKey(key)) map.put(key, new int[]{fromIndex, list.size()}); }
  23. Hanoi - Dynamic Programming 13 private void solve(List<String> list, int

    n,
 char source, char intermediate, char destination, Map<String,int[]> map) { if (n == 0) return; int fromIndex = list.size(); int[] sub = map.get(getKey(n-1, source, intermediate)); if (sub != null) addAll(list, sub[0], sub[1]); else solve(list, n-1, source, destination, intermediate, map); String key = getKey(n, source, destination); list.add(key); sub = map.get(getKey(n-1, intermediate, destination)); if (sub != null) addAll(list, sub[0], sub[1]); else solve(list, n-1, intermediate, source, destination, map); if (!map.containsKey(key)) map.put(key, new int[]{fromIndex, list.size()}); }
  24. Hanoi - Dynamic Programming 13 private void solve(List<String> list, int

    n,
 char source, char intermediate, char destination, Map<String,int[]> map) { if (n == 0) return; int fromIndex = list.size(); int[] sub = map.get(getKey(n-1, source, intermediate)); if (sub != null) addAll(list, sub[0], sub[1]); else solve(list, n-1, source, destination, intermediate, map); String key = getKey(n, source, destination); list.add(key); sub = map.get(getKey(n-1, intermediate, destination)); if (sub != null) addAll(list, sub[0], sub[1]); else solve(list, n-1, intermediate, source, destination, map); if (!map.containsKey(key)) map.put(key, new int[]{fromIndex, list.size()}); }
  25. Hanoi - Dynamic Programming 13 private void solve(List<String> list, int

    n,
 char source, char intermediate, char destination, Map<String,int[]> map) { if (n == 0) return; int fromIndex = list.size(); int[] sub = map.get(getKey(n-1, source, intermediate)); if (sub != null) addAll(list, sub[0], sub[1]); else solve(list, n-1, source, destination, intermediate, map); String key = getKey(n, source, destination); list.add(key); sub = map.get(getKey(n-1, intermediate, destination)); if (sub != null) addAll(list, sub[0], sub[1]); else solve(list, n-1, intermediate, source, destination, map); if (!map.containsKey(key)) map.put(key, new int[]{fromIndex, list.size()}); }
  26. Hanoi - Recursive vs. Dynamic 14 Σ of 100 iterations

    (ms) 0 2000 4000 6000 8000 k 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Recursive Dynamic
  27. • “ABCDE”: - Substring: {“A”, “BC”, “CDE”, …}. - Subsequence:

    {all substrings, “AC”, “ACE”, …}. Longest Common Subsequence 16
  28. • “ABCDE”: - Substring: {“A”, “BC”, “CDE”, …}. - Subsequence:

    {all substrings, “AC”, “ACE”, …}. - Not subsequence: {“BA”, “DAB”, …} Longest Common Subsequence 16
  29. • “ABCDE”: - Substring: {“A”, “BC”, “CDE”, …}. - Subsequence:

    {all substrings, “AC”, “ACE”, …}. - Not subsequence: {“BA”, “DAB”, …} • Longest common subsequence Longest Common Subsequence 16
  30. • “ABCDE”: - Substring: {“A”, “BC”, “CDE”, …}. - Subsequence:

    {all substrings, “AC”, “ACE”, …}. - Not subsequence: {“BA”, “DAB”, …} • Longest common subsequence - The longest subsequence commonly shared by multiple strings. Longest Common Subsequence 16
  31. • “ABCDE”: - Substring: {“A”, “BC”, “CDE”, …}. - Subsequence:

    {all substrings, “AC”, “ACE”, …}. - Not subsequence: {“BA”, “DAB”, …} • Longest common subsequence - The longest subsequence commonly shared by multiple strings. - e.g., “baal” is a LCS of “bilabial” and “balaclava”. Longest Common Subsequence 16
  32. • “ABCDE”: - Substring: {“A”, “BC”, “CDE”, …}. - Subsequence:

    {all substrings, “AC”, “ACE”, …}. - Not subsequence: {“BA”, “DAB”, …} • Longest common subsequence - The longest subsequence commonly shared by multiple strings. - e.g., “baal” is a LCS of “bilabial” and “balaclava”. - Can there be more than one LCS? Longest Common Subsequence 16
  33. • “ABCDE”: - Substring: {“A”, “BC”, “CDE”, …}. - Subsequence:

    {all substrings, “AC”, “ACE”, …}. - Not subsequence: {“BA”, “DAB”, …} • Longest common subsequence - The longest subsequence commonly shared by multiple strings. - e.g., “baal” is a LCS of “bilabial” and “balaclava”. - Can there be more than one LCS? Longest Common Subsequence 16 blal → bilabial balaclava blaa→ bilabial balaclava
  34. • “ABCDE”: - Substring: {“A”, “BC”, “CDE”, …}. - Subsequence:

    {all substrings, “AC”, “ACE”, …}. - Not subsequence: {“BA”, “DAB”, …} • Longest common subsequence - The longest subsequence commonly shared by multiple strings. - e.g., “baal” is a LCS of “bilabial” and “balaclava”. - Can there be more than one LCS? • Application Longest Common Subsequence 16 blal → bilabial balaclava blaa→ bilabial balaclava
  35. • “ABCDE”: - Substring: {“A”, “BC”, “CDE”, …}. - Subsequence:

    {all substrings, “AC”, “ACE”, …}. - Not subsequence: {“BA”, “DAB”, …} • Longest common subsequence - The longest subsequence commonly shared by multiple strings. - e.g., “baal” is a LCS of “bilabial” and “balaclava”. - Can there be more than one LCS? • Application - Find LCSs in DNA (e.g., GAATGTCCTTTCTCTAAGTCCTAAG). Longest Common Subsequence 16 blal → bilabial balaclava blaa→ bilabial balaclava
  36. Abstract LCS 17 public abstract class AbstractLCS { /** @return

    a longest common sequence of the specific strings a and b. */ public String solve(String a, String b) { return solve(a.toCharArray(), b.toCharArray(), a.length()-1, b.length()-1); } protected abstract String solve(char[] c, char[] d, int i, int j); }
  37. Abstract LCS 17 public abstract class AbstractLCS { /** @return

    a longest common sequence of the specific strings a and b. */ public String solve(String a, String b) { return solve(a.toCharArray(), b.toCharArray(), a.length()-1, b.length()-1); } protected abstract String solve(char[] c, char[] d, int i, int j); }
  38. LCS - Recursive 18 A C G T C G

    T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8
  39. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8
  40. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8)
  41. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8)
  42. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G
  43. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G (6, 7)
  44. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G (6, 7) (0, 7) ...
  45. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G (6, 7) (0, 7) ... A
  46. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G (6, 7) (0, 7) ... A (-1, 6)
  47. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G (6, 7) (0, 7) ... A (-1, 6) A
  48. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G (6, 7) (0, 7) ... A (-1, 6) (1, 6) A
  49. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G (6, 7) (0, 7) ... A (-1, 6) (1, 6) (-1, 6) A ...
  50. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G (6, 7) (0, 7) ... A (-1, 6) (1, 6) (-1, 6) A ... (0, 5)
  51. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G (6, 7) (0, 7) ... A (-1, 6) (1, 6) (-1, 6) A ... (0, 5) (-1, 5) ...
  52. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G (6, 7) (0, 7) ... A (-1, 6) (1, 6) (-1, 6) A ... (0, 5) (-1, 5) ... (0, 4)
  53. protected String solve(char[] c, char[] d, int i, int j)

    { if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return solve(c, d, i-1, j-1) + c[i]; String c1 = solve(c, d, i-1, j); String d1 = solve(c, d, i, j-1); return (c1.length() > d1.length()) ? c1 : d1; } LCS - Recursive 18 A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 (8, 8) (7, 8) G (6, 7) (0, 7) ... A (-1, 6) (1, 6) (-1, 6) A ... (0, 5) (-1, 5) ... (0, 4) (-1, 4)
  54. LCS - Dynamic Programming 19 A C G T C

    G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8
  55. LCS - Dynamic Programming 19 final int N = c.length,

    M = d.length; int[][] table = new int[N][M]; A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4
  56. LCS - Dynamic Programming 19 final int N = c.length,

    M = d.length; int[][] table = new int[N][M]; A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 for (int i=1; i<N; i++) for (int j=1; j<M; j++) { table[i][j] = (c[i] == d[j]) ? table[i-1][j-1] + 1 : Math.max(table[i-1][j], table[i][j-1]); }
  57. LCS - Dynamic Programming 19 final int N = c.length,

    M = d.length; int[][] table = new int[N][M]; A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 for (int i=1; i<N; i++) for (int j=1; j<M; j++) { table[i][j] = (c[i] == d[j]) ? table[i-1][j-1] + 1 : Math.max(table[i-1][j], table[i][j-1]); }
  58. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8
  59. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8
  60. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8
  61. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8
  62. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8
  63. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8
  64. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G
  65. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G
  66. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T
  67. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T
  68. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T G
  69. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T G
  70. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T G
  71. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T G
  72. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T G T
  73. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T G T
  74. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T G T
  75. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T G T C
  76. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T G T C
  77. LCS - Dynamic Programming 20 0 1 2 3 4

    5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 1 1 1 1 1 1 3 0 1 1 1 2 2 2 2 2 4 0 1 1 1 2 2 2 2 2 5 0 1 1 2 2 3 3 3 3 6 0 1 1 2 3 3 3 3 3 7 0 1 1 2 3 4 4 4 4 8 0 1 1 2 3 4 4 4 4 if (i < 0 || j < 0) return ""; if (c[i] == d[j]) return get(c, d, i-1, j-1, table) + c[i]; if (i == 0) return get(c, d, i, j-1, table); if (j == 0) return get(c, d, i-1, j, table); return (table[i-1][j] > table[i][j-1]) ? get(c, d, i-1, j, table) : get(c, d, i, j-1, table); A C G T C G T G T C T A G T G G A G 0 1 2 3 4 5 6 7 8 G T G T C Complexity?