-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathLengthOfShortestCommonSupersequence.java
105 lines (83 loc) · 3.16 KB
/
LengthOfShortestCommonSupersequence.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
https://www.techiedelight.com/shortest-common-supersequence-introduction-scs-length/
Shortest Common Supersequence is the problem of finding the the shortest sequence Z, such that sequences X and Y are subsequences of Z.
X: ABCBDAB
Y: BDCABA
Supersequences: ABCBDCABA, ABDCABDAB, ABDCBDABA
OPTIMAL SUBSTRUCTURE OF SUBPROBLEMS -
Let length(X) = n and length(Y) = m
1. If X[n] == Y[m]
We find the SCS of shortened subsequences X[n-1] & Y[m-1] and append the common element at it's end.
SCS(X[n], Y[m]) = SCS(X[n-1],Y[m-1]) + X[n]
2. If X[n] != Y[m]
The shortest supersequence will either end in X[n] or Y[m]. So, we explore both and consider the one giving us the shortest
supersequence.
SCS(X[n], Y[m]) = min( SCS(X[n-1],Y[m]) + X[n], SCS(X[n],Y[m-1])+Y[m] )
OVERLAPPING SUBPROBLEMS
*/
//RECURSION
public int scsLength(String X, String Y, int n, int m){
if(n==0 || m==0) return (n+m);
else if(X.charAt(n-1) == Y.charAt(m-1)) return scsLength(X, Y, n-1, m-1) + 1;
else return Math.min(scsLength(X, Y, n, m-1), scsLength(X, Y, n-1, m))+1;
}
/*
Time Complexity - O(2^(n+m))
Worst case occurs when none of the characters of the two sequences match - each call leads to two more calls
*/
//MEMOIZED RECURSION
public int scsLength(String X, String Y, int n, int m, HashMap<String, Integer>){
if(n==0 || m==0) return n+m;
String key = n+"|"+m;
if(!map.containsKey(key)){
if(X.charAt(n-1) == Y.charAt(m-1)) map.put(key, scsLength(X, Y, n-1, m-1, map)+1);
else map.put(key, Math.min(scsLength(X, Y, n, m-1, map), scsLength(X, Y, n-1, m, map))+1);
}
return map.get(key);
}
/*
Time Complexity and Space Complexity - O(n*m)
*/
//BOTTOM UP DYNAMIC PROGRAMMING
/*
SCS[i][j] = i if j==0
= j if i==0
= SCS[i-1][j-1] + 1 if(X[i] == Y[j])
= Math.min(SCS[i-1][j], SCS[i][j-1]) + 1 if(X[i] != Y[j])
*/
public int scsLength(String X, String Y){
int n = X.length();
int m = Y.length();
int T[][] = new int[n+1][m+1];
for(int i=0 ; i <=n ; i++) T[i][0] = i;
for(int j=0 ; j <= m ; j++) T[0][j] = j;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(X.charAt(i-1) == Y.charAt(j-1)) T[i][j] = T[i-1][j-1]+1;
else T[i][j] = Math.min(T[i-1][j], T[i][j-1])+1
}
}
return T[n][m];
}
/*
Time Complexity & Space Complexity - O(n*m)
*/
//SPACE OPTIMIZED DYNAMIC PROGRAMMING - BOTTOM UP
public int scsLength(String X, String Y){
int n = X.length();
int m = Y.length();
int T[][] = new int[2][m+1];
for(int i=0 ; i <=m ; i++) T[0][i] = i;
for(int i=1;i<=n;i++){
for(int j=0;j<=m;j++){
if(j==0) T[i % 2][j] = i;
else if(X.charAt(i-1) == Y.charAt(j-1)) T[i % 2][j] = T[(i-1) % 2][j-1]+1;
else T[i % 2][j] = Math.min(T[(i-1) % 2][j], T[i % 2][j-1])+1
}
}
return T[n % 2][m];
}
/*
Time Complexity - O(n*m)
Space Complexity - (2*m)
*/