Skip to content

Commit 8131bd7

Browse files
committed
Update dynamic programming.
1 parent 61da052 commit 8131bd7

File tree

4 files changed

+27
-42
lines changed

4 files changed

+27
-42
lines changed

docs/schedule.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
|11/11| [Homework 2](https://emory.zoom.us/rec/share/1FzBXB8581_8fTwXZz1lCLGPquaFgE13i9TWY-f8u2wpifBzTtq4m31qAyKwTd0m.khjxck1oQ5s4K23R) | | [hw 3](https://canvas.emory.edu/courses/76034/assignments/408681) |
3030
|11/16| [Network Flow](https://emory.zoom.us/rec/share/AyviVMlLixWFDeJzUC_wb__x0AvIhJN8GFlD1u_ffst-V9fIGYvqQnZgSZlJD-jB.84wsBJ68HPBVxogb) | | [quiz 8](https://canvas.emory.edu/courses/76034/assignments/410195) |
3131
|11/18| [Dynamic Programming](https://emory.zoom.us/rec/share/P1k3sxxd-2GSF26EQmPjVoCU6xZ_FOOZ-yQ92yJLZASLqKxVU_0yK8lZyqEN2oCV.tzbLlckOfJrrRAyH) | [book](https://emory.gitbook.io/dsa-java/dynamic-programming), [main](../src/main/java/edu/emory/cs/dynamic), [test](../src/test/java/edu/emory/cs/dynamic) | |
32-
|11/23| [Dynamic Programming]() | | [quiz 9]() |
32+
|11/23| [Dynamic Programming](https://emory.zoom.us/rec/share/bcmHGe3DHQA-VEAzgfmv74SZsiv-NFsPVFJgxneu8akX4dufQwVdN4JxX2PFgF_z.WEJrZfqhrE3OiiAM) | | [quiz 9]() |
3333

3434
<!-- -->
3535
<!-- [Shortest Path Algorithms]() | [md](), [pdf](shortest_path_algorithms.pdf), [main](../src/main/java/edu/emory/cs/graph/path/) | [quiz 8](quiz0.md#quiz-8) | -->

src/main/java/edu/emory/cs/dynamic/lcs/LCS.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
*/
1616
package edu.emory.cs.dynamic.lcs;
1717

18-
/**
19-
* @author Jinho D. Choi ({@code jinho.choi@emory.edu})
20-
*/
18+
/** @author Jinho D. Choi */
2119
public abstract class LCS {
2220
/**
2321
* @param a the first string.
2422
* @param b the second string.
25-
* @return a longest common sequence of the specific strings {@code a} and {@code b}.
23+
* @return a longest common sequence of the two specific strings.
2624
*/
2725
public String solve(String a, String b) {
2826
return solve(a.toCharArray(), b.toCharArray(), a.length() - 1, b.length() - 1);
@@ -31,10 +29,9 @@ public String solve(String a, String b) {
3129
/**
3230
* @param c the first array of characters.
3331
* @param d the second array of characters.
34-
* @param i the index of {@code c} to be compared.
35-
* @param j the index of {@code d} to be compared
36-
* @return a longest common sequence of the specific strings {@code c} and {@code d}.
32+
* @param i the index of the character in {@code c} to be compared.
33+
* @param j the index of the character in {@code d} to be compared.
34+
* @return a longest common sequence of the two specific strings.
3735
*/
3836
protected abstract String solve(char[] c, char[] d, int i, int j);
3937
}
40-

src/main/java/edu/emory/cs/dynamic/lcs/LCSDynamic.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,10 @@ protected String solve(char[] c, char[] d, int i, int j) {
2525
return solve(c, d, i, j, createTable(c, d));
2626
}
2727

28-
private String solve(char[] c, char[] d, int i, int j, int[][] table) {
29-
if (i < 0 || j < 0)
30-
return "";
31-
32-
//Found common sequence
33-
if (c[i] == d[j])
34-
return solve(c, d, i - 1, j - 1, table) + c[i];
35-
36-
//If search on String c has exhausted
37-
if (i == 0) return solve(c, d, i, j - 1, table);
38-
//If search on String d has exhausted
39-
if (j == 0) return solve(c, d, i - 1, j, table);
40-
41-
//Recursively search for the table[i][j] with the largest element
42-
return (table[i - 1][j] > table[i][j - 1]) ? solve(c, d, i - 1, j, table) : solve(c, d, i, j - 1, table);
43-
}
44-
4528
/**
46-
* Populating lcs dynamic table of the (sub)strings c and d
47-
*
48-
* @param c String 1
49-
* @param d String 2
50-
* @return dynamic table populated by finding the lcs of c[0~i] and d[0~j]
29+
* @param c the first string.
30+
* @param d the second string.
31+
* @return the dynamic table populated by estimating the # of LCSs in the grid of the two specific strings.
5132
*/
5233
private int[][] createTable(char[] c, char[] d) {
5334
final int N = c.length, M = d.length;
@@ -59,5 +40,18 @@ private int[][] createTable(char[] c, char[] d) {
5940

6041
return table;
6142
}
43+
44+
private String solve(char[] c, char[] d, int i, int j, int[][] table) {
45+
if (i < 0 || j < 0) return "";
46+
47+
// a common sequence is found
48+
if (c[i] == d[j]) return solve(c, d, i - 1, j - 1, table) + c[i];
49+
// search on the first string has been exhausted
50+
if (i == 0) return solve(c, d, i, j - 1, table);
51+
// search on the second string has been exhausted
52+
if (j == 0) return solve(c, d, i - 1, j, table);
53+
54+
return (table[i - 1][j] > table[i][j - 1]) ? solve(c, d, i - 1, j, table) : solve(c, d, i, j - 1, table);
55+
}
6256
}
6357

src/main/java/edu/emory/cs/dynamic/lcs/LCSRecursive.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,17 @@
1515
*/
1616
package edu.emory.cs.dynamic.lcs;
1717

18-
/**
19-
* @author Jinho D. Choi ({@code jinho.choi@emory.edu})
20-
*/
18+
/** @author Jinho D. Choi */
2119
public class LCSRecursive extends LCS {
2220
@Override
2321
protected String solve(char[] c, char[] d, int i, int j) {
24-
if (i < 0 || j < 0)
25-
return "";
26-
27-
if (c[i] == d[j])
28-
return solve(c, d, i - 1, j - 1) + c[i];
22+
if (i < 0 || j < 0) return "";
23+
if (c[i] == d[j]) return solve(c, d, i - 1, j - 1) + c[i];
2924

30-
//Get the lcs through searching from the right of string c
25+
// get the LCS between c[:i-1] and d[:j]
3126
String c1 = solve(c, d, i - 1, j);
32-
//Get the lcs through searching from the right of string d
27+
// get the LCS between c[:i] and d[:j-1]
3328
String d1 = solve(c, d, i, j - 1);
34-
3529
return (c1.length() > d1.length()) ? c1 : d1;
3630
}
3731
}

0 commit comments

Comments
 (0)