SlideShare a Scribd company logo
1 of 56
Download to read offline
Emory University Logo Guidelines
-
Dynamic Programming:
Longest Common Subsequence
Data Structures and Algorithms
Emory University
Jinho D. Choi
Emory University Logo Guidelines
-
Longest Common Subsequence
2
Emory University Logo Guidelines
-
Longest Common Subsequence
• “ABCDE”:
2
Emory University Logo Guidelines
-
Longest Common Subsequence
• “ABCDE”:
- Substring: {“A”,“BC”,“CDE”, …}.
2
Emory University Logo Guidelines
-
Longest Common Subsequence
• “ABCDE”:
- Substring: {“A”,“BC”,“CDE”, …}.
- Subsequence: {all substrings,“AC”,“ACE”, …}.
2
Emory University Logo Guidelines
-
Longest Common Subsequence
• “ABCDE”:
- Substring: {“A”,“BC”,“CDE”, …}.
- Subsequence: {all substrings,“AC”,“ACE”, …}.
- Not subsequence: {“BA”,“DAB”, …}
2
Emory University Logo Guidelines
-
Longest Common Subsequence
• “ABCDE”:
- Substring: {“A”,“BC”,“CDE”, …}.
- Subsequence: {all substrings,“AC”,“ACE”, …}.
- Not subsequence: {“BA”,“DAB”, …}
• Longest common subsequence
2
Emory University Logo Guidelines
-
Longest Common Subsequence
• “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.
2
Emory University Logo Guidelines
-
Longest Common Subsequence
• “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”.
2
Emory University Logo Guidelines
-
Longest Common Subsequence
• “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?
2
Emory University Logo Guidelines
-
Longest Common Subsequence
• “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?
2
blal → bilabial balaclava
blaa→ bilabial balaclava
Emory University Logo Guidelines
-
Longest Common Subsequence
• “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
2
blal → bilabial balaclava
blaa→ bilabial balaclava
Emory University Logo Guidelines
-
Longest Common Subsequence
• “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).
2
blal → bilabial balaclava
blaa→ bilabial balaclava
Emory University Logo Guidelines
-
Abstract LCS
3
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);
}
Emory University Logo Guidelines
-
Abstract LCS
3
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);
}
Emory University Logo Guidelines
-
LCS - Recursive
4
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
Emory University Logo Guidelines
-
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
4
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
Emory University Logo Guidelines
-
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
4
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)
Emory University Logo Guidelines
-
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
4
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)
Emory University Logo Guidelines
-
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
4
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
Emory University Logo Guidelines
-
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
4
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)
Emory University Logo Guidelines
-
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
4
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)
...
Emory University Logo Guidelines
-
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
4
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
Emory University Logo Guidelines
-
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
4
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)
Emory University Logo Guidelines
-
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
4
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
Emory University Logo Guidelines
-
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
4
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
Emory University Logo Guidelines
-
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
4
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
...
Emory University Logo Guidelines
-
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
4
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)
Emory University Logo Guidelines
-
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
4
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)
...
Emory University Logo Guidelines
-
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
4
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)
Emory University Logo Guidelines
-
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
4
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)
Emory University Logo Guidelines
-
LCS - Dynamic Programming
5
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
Emory University Logo Guidelines
-
LCS - Dynamic Programming
5
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
Emory University Logo Guidelines
-
LCS - Dynamic Programming
5
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]);
}
Emory University Logo Guidelines
-
LCS - Dynamic Programming
5
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]);
}
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GT
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GT
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GTG
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GTG
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GTG
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GTG
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GTGT
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GTGT
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GTGT
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GTGTC
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GTGTC
Emory University Logo Guidelines
-
LCS - Dynamic Programming
6
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
GTGTC
Complexity?
Emory University Logo Guidelines
-
Agenda
• Exercise
- https://github.com/emory-courses/cs323/wiki/Dynamic-Programming
• Reading
- http://en.wikipedia.org/wiki/Knapsack_problem
7

More Related Content

Similar to CS323: Longest Common Subsequences

Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshan
riturajj
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
klyni777
 
lecture 24
lecture 24lecture 24
lecture 24
sajinsc
 
Ramco Sample Paper 2003
Ramco  Sample  Paper 2003Ramco  Sample  Paper 2003
Ramco Sample Paper 2003
ncct
 

Similar to CS323: Longest Common Subsequences (20)

Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)Longest Common Sub-sequence (LCS)
Longest Common Sub-sequence (LCS)
 
Introduction to Polyhedral Compilation
Introduction to Polyhedral CompilationIntroduction to Polyhedral Compilation
Introduction to Polyhedral Compilation
 
Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshan
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
lecture 24
lecture 24lecture 24
lecture 24
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
 
Dynamic Programing_LCS.ppt
Dynamic Programing_LCS.pptDynamic Programing_LCS.ppt
Dynamic Programing_LCS.ppt
 
Yoyak ScalaDays 2015
Yoyak ScalaDays 2015Yoyak ScalaDays 2015
Yoyak ScalaDays 2015
 
Ramco Sample Paper 2003
Ramco  Sample  Paper 2003Ramco  Sample  Paper 2003
Ramco Sample Paper 2003
 
LDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdf
LDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdfLDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdf
LDCQ paper Dec21 with answer key_62cb2996afc60f6aedeb248c1d9283e5.pdf
 
Revision1schema C programming
Revision1schema C programmingRevision1schema C programming
Revision1schema C programming
 
Add math may june 2016 p1
Add math may june 2016 p1Add math may june 2016 p1
Add math may june 2016 p1
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
Rsa Signature: Behind The Scenes
Rsa Signature: Behind The Scenes Rsa Signature: Behind The Scenes
Rsa Signature: Behind The Scenes
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
CS.3.Arrays.pdf
CS.3.Arrays.pdfCS.3.Arrays.pdf
CS.3.Arrays.pdf
 
Operators
OperatorsOperators
Operators
 
UNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptxUNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptx
 
Java operators
Java operatorsJava operators
Java operators
 

More from Jinho Choi

More from Jinho Choi (20)

Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
Adaptation of Multilingual Transformer Encoder for Robust Enhanced Universal ...
 
Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...
Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...
Analysis of Hierarchical Multi-Content Text Classification Model on B-SHARP D...
 
Competence-Level Prediction and Resume & Job Description Matching Using Conte...
Competence-Level Prediction and Resume & Job Description Matching Using Conte...Competence-Level Prediction and Resume & Job Description Matching Using Conte...
Competence-Level Prediction and Resume & Job Description Matching Using Conte...
 
Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...
Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...
Transformers to Learn Hierarchical Contexts in Multiparty Dialogue for Span-b...
 
The Myth of Higher-Order Inference in Coreference Resolution
The Myth of Higher-Order Inference in Coreference ResolutionThe Myth of Higher-Order Inference in Coreference Resolution
The Myth of Higher-Order Inference in Coreference Resolution
 
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
Noise Pollution in Hospital Readmission Prediction: Long Document Classificat...
 
Abstract Meaning Representation
Abstract Meaning RepresentationAbstract Meaning Representation
Abstract Meaning Representation
 
Semantic Role Labeling
Semantic Role LabelingSemantic Role Labeling
Semantic Role Labeling
 
CKY Parsing
CKY ParsingCKY Parsing
CKY Parsing
 
CS329 - WordNet Similarities
CS329 - WordNet SimilaritiesCS329 - WordNet Similarities
CS329 - WordNet Similarities
 
CS329 - Lexical Relations
CS329 - Lexical RelationsCS329 - Lexical Relations
CS329 - Lexical Relations
 
Automatic Knowledge Base Expansion for Dialogue Management
Automatic Knowledge Base Expansion for Dialogue ManagementAutomatic Knowledge Base Expansion for Dialogue Management
Automatic Knowledge Base Expansion for Dialogue Management
 
Attention is All You Need for AMR Parsing
Attention is All You Need for AMR ParsingAttention is All You Need for AMR Parsing
Attention is All You Need for AMR Parsing
 
Graph-to-Text Generation and its Applications to Dialogue
Graph-to-Text Generation and its Applications to DialogueGraph-to-Text Generation and its Applications to Dialogue
Graph-to-Text Generation and its Applications to Dialogue
 
Real-time Coreference Resolution for Dialogue Understanding
Real-time Coreference Resolution for Dialogue UnderstandingReal-time Coreference Resolution for Dialogue Understanding
Real-time Coreference Resolution for Dialogue Understanding
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Tries - Put
Tries - PutTries - Put
Tries - Put
 
Multi-modal Embedding Learning for Early Detection of Alzheimer's Disease
Multi-modal Embedding Learning for Early Detection of Alzheimer's DiseaseMulti-modal Embedding Learning for Early Detection of Alzheimer's Disease
Multi-modal Embedding Learning for Early Detection of Alzheimer's Disease
 
Building Widely-Interpretable Semantic Networks for Dialogue Contexts
Building Widely-Interpretable Semantic Networks for Dialogue ContextsBuilding Widely-Interpretable Semantic Networks for Dialogue Contexts
Building Widely-Interpretable Semantic Networks for Dialogue Contexts
 
How to make Emora talk about Sports Intelligently
How to make Emora talk about Sports IntelligentlyHow to make Emora talk about Sports Intelligently
How to make Emora talk about Sports Intelligently
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

CS323: Longest Common Subsequences

  • 1. Emory University Logo Guidelines - Dynamic Programming: Longest Common Subsequence Data Structures and Algorithms Emory University Jinho D. Choi
  • 2. Emory University Logo Guidelines - Longest Common Subsequence 2
  • 3. Emory University Logo Guidelines - Longest Common Subsequence • “ABCDE”: 2
  • 4. Emory University Logo Guidelines - Longest Common Subsequence • “ABCDE”: - Substring: {“A”,“BC”,“CDE”, …}. 2
  • 5. Emory University Logo Guidelines - Longest Common Subsequence • “ABCDE”: - Substring: {“A”,“BC”,“CDE”, …}. - Subsequence: {all substrings,“AC”,“ACE”, …}. 2
  • 6. Emory University Logo Guidelines - Longest Common Subsequence • “ABCDE”: - Substring: {“A”,“BC”,“CDE”, …}. - Subsequence: {all substrings,“AC”,“ACE”, …}. - Not subsequence: {“BA”,“DAB”, …} 2
  • 7. Emory University Logo Guidelines - Longest Common Subsequence • “ABCDE”: - Substring: {“A”,“BC”,“CDE”, …}. - Subsequence: {all substrings,“AC”,“ACE”, …}. - Not subsequence: {“BA”,“DAB”, …} • Longest common subsequence 2
  • 8. Emory University Logo Guidelines - Longest Common Subsequence • “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. 2
  • 9. Emory University Logo Guidelines - Longest Common Subsequence • “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”. 2
  • 10. Emory University Logo Guidelines - Longest Common Subsequence • “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? 2
  • 11. Emory University Logo Guidelines - Longest Common Subsequence • “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? 2 blal → bilabial balaclava blaa→ bilabial balaclava
  • 12. Emory University Logo Guidelines - Longest Common Subsequence • “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 2 blal → bilabial balaclava blaa→ bilabial balaclava
  • 13. Emory University Logo Guidelines - Longest Common Subsequence • “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). 2 blal → bilabial balaclava blaa→ bilabial balaclava
  • 14. Emory University Logo Guidelines - Abstract LCS 3 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); }
  • 15. Emory University Logo Guidelines - Abstract LCS 3 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); }
  • 16. Emory University Logo Guidelines - LCS - Recursive 4 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
  • 17. Emory University Logo Guidelines - 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 4 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
  • 18. Emory University Logo Guidelines - 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 4 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)
  • 19. Emory University Logo Guidelines - 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 4 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)
  • 20. Emory University Logo Guidelines - 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 4 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
  • 21. Emory University Logo Guidelines - 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 4 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)
  • 22. Emory University Logo Guidelines - 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 4 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) ...
  • 23. Emory University Logo Guidelines - 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 4 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
  • 24. Emory University Logo Guidelines - 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 4 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)
  • 25. Emory University Logo Guidelines - 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 4 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
  • 26. Emory University Logo Guidelines - 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 4 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
  • 27. Emory University Logo Guidelines - 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 4 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 ...
  • 28. Emory University Logo Guidelines - 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 4 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)
  • 29. Emory University Logo Guidelines - 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 4 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) ...
  • 30. Emory University Logo Guidelines - 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 4 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)
  • 31. Emory University Logo Guidelines - 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 4 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)
  • 32. Emory University Logo Guidelines - LCS - Dynamic Programming 5 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
  • 33. Emory University Logo Guidelines - LCS - Dynamic Programming 5 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
  • 34. Emory University Logo Guidelines - LCS - Dynamic Programming 5 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]); }
  • 35. Emory University Logo Guidelines - LCS - Dynamic Programming 5 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]); }
  • 36. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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
  • 37. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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
  • 38. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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
  • 39. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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
  • 40. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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
  • 41. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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
  • 42. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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
  • 43. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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
  • 44. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GT
  • 45. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GT
  • 46. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GTG
  • 47. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GTG
  • 48. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GTG
  • 49. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GTG
  • 50. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GTGT
  • 51. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GTGT
  • 52. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GTGT
  • 53. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GTGTC
  • 54. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GTGTC
  • 55. Emory University Logo Guidelines - LCS - Dynamic Programming 6 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 GTGTC Complexity?
  • 56. Emory University Logo Guidelines - Agenda • Exercise - https://github.com/emory-courses/cs323/wiki/Dynamic-Programming • Reading - http://en.wikipedia.org/wiki/Knapsack_problem 7