1 / 11

LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0;

LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return max(lcs(x, y, i-1, j), lcs(x, y, i, j-1)); end ; int x = lcs(“abdabcd”,”cabdaaxy”,7,8);. LCS Dynamic Programming Version

rio
Download Presentation

LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0;

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return max(lcs(x, y, i-1, j), lcs(x, y, i, j-1)); end; int x = lcs(“abdabcd”,”cabdaaxy”,7,8);

  2. LCS Dynamic Programming Version int A{0..m,0..n); A{1..m,1..n) = -1; A(0,0..n) = 0; A(1..m,0) = 0; int function lcs (x, y, i, j) begin if (A[i,j] < 0) if (x[i] = y[j]) A[i,j] = lcs(x, y, i-1, j-1) + 1; else A[i,j] = max(lcs(x, y, i-1, j), lcs(x, y, i, j-1)); return A[i,j]; end;

  3. LCS Dynamic Programming Version int c(0..m,0..n); void function lcs (x, y) begin m = length(x); n = length(y); c[1..m,0] = 0; c[0,0..n] = 0; for i = 1 to m for j = 1 to n if (x[i] = y[j]) c[i,j] = c[i-1,j-1] + 1; elseif c[i-1,j] >= c[i,j-1] c[i,j] = c[i-1,j]; else c[i,j] = c[i,j-1]; end; // After execution, c[m,n] contains the length of the lcs

  4. LCS Dynamic Programming Version int c(0..m,0..n); char b(0..m,0..n); void function lcs (x, y) begin m = length(x); n = length(y); c[1..m,0] = 0; c[0,0..n] = 0; for i = 1 to m for j = 1 to n if (x[i] = y[j]) c[i,j] = c[i-1,j-1] + 1; b[i,j] = “\”; elseif c[i-1,j] >= c[i,j-1] c[i,j] = c[i-1,j]; b[i,j] = “|”; else c[i,j] = c[i,j-1]; b[i,j] = “--”; // After execution, b contains a description of the subsequence end;

  5. LCS Print function print-lcs (b, x, i, j) begin if (i = 0) or (j =0) return; elseif b[i,j] = “\” print-lcs (b,x, i-1, j-1); elseif b[i,j] = “|” print-lcs(b, x, i-1,j) else // b[i,j] = “--” print-lcs(b, x, i, j-1) end; // Technically b is not necessary

  6. Assembly Line Scheduling Non-Dynamic Version array e[1..2] = {2,4}; // Entry times for each assembly line array x[1..2] = {3,2}; // Exit times for each assembly line array a[1..2,1..6] = {{7,9,3,4,8,4}, {8,5,6,4,5,7}}; // Station times array t[1..2, 1..5] = {{2,3,1,3,4}, {2,1,2,2,1}}; // Transfer times

  7. ALS Non-Dynamic Version int function shortest_schedule_time (int i, int j) // i is 1..2, j is 1..n begin if (j = 1) return e[i] + a[i,j]; else int f1 = shortest_schedule_time(1, j-1); int f2 = shortest_schedule_time(2, j-1); if (i = 1) // could be eliminated with a two-element array return min( f1 + a[1,j], f2 + t[2,j-1] + a[1,j]); else // i =2 return min( f2 + a[2,j], f1 + t[1,j-1] + a[2,j])); end; void main() { int f1 = shortest_schedule_time(1, n); int f2 = shortest_schedule_time(2, n); print(min(f1 + x[1], f2 + x[2])); }

  8. ALS Dynamic Version #1 struct f_type = {int f1, int f2}; f_type function shortest_schedule_time (j) begin if (j = 1) return (e[1] + a[1,j], e[2] + a[2,j]); else f_type f; f = shortest_schedule_time(j-1); return (min( f.f1 + a[1,j], f.f2 + t[2,j-1] + a[1,j]), min( f.f2 + a[2,j], f.f1 + t[1,j-1] + a[2,j])); end; void main() { f_type f = shortest_schedule_time(n); print(min(f.f1 + x[1], f.f2 + x[2])); }

  9. Assembly Line Scheduling Non-Dynamic Version array e[1..3] = {2,4}; // Entry times for each assembly line array x[1..3] = {3,2}; // Exit times for each assembly line array a[1..3,1..6] = {{7,9,3,4,8,4}, {8,5,6,4,5,7}}; // Station times array t[1..3, 1..3, 1..5] = {{2,3,1,3,4}, {2,1,2,2,1}}; // Transfer times

  10. ALS Non-Dynamic Version int function shortest_schedule_time (int i, int j) // i is 1..3, j is 1..n begin if (j = 1) return e[i] + a[i,j]; else int f1 = shortest_schedule_time(1, j-1); int f2 = shortest_schedule_time(2, j-1); int f3 = shortest_schedule_time(3, j-1); if (i = 1) // could be eliminated with a two-element array return min( f1 + a[1,j], f2 + t[2,1,j-1] + a[1,j], f3 + t[3,1,j-1] + a[1,j]); else if (i = 2) return min( f2 + a[2,j], f1 + t[1,2, j-1] + a[2,j], f3 + t[3,2,j-1] + a[2,j]); else // i = 3 return min( f3 + a[3,j], f1 + t[1,3, j-1] + a[3,j], f2 + t[2,3,j-1] + a[3,j]); end; void main() { int f1 = shortest_schedule_time(1, n); int f2 = shortest_schedule_time(2, n); int f3 = shortest_schedule_time(3, n); print(min(f1 + x[1], f2 + x[2], f3 + x[3])); }

  11. ALS Dynamic Version #1 struct f_type = {int f1, int f2, int f3}; f_type function shortest_schedule_time (j) begin if (j = 1) return (e[1] + a[1,j], e[2] + a[2,j], e[3] + a[3,j]); else f_type f; f = shortest_schedule_time(j-1); return (min( f.f1 + a[1,j], f.f2 + t[2,1,j-1] + a[1,j], f.f3 + t[3,1,j-1] + a[1,j]), min( f.f2 + a[2,j], f.f1 + t[1,2,j-1] + a[2,j], f.f3 + t[3,2,j-1] + a[3,j]), min( f.f3 + a[3,j], f.f1 + t[1,3,j-1] + a[3,j], f.f2 + t[2,3,j-1] + a[3,j]), end; void main() { f_type f = shortest_schedule_time(n); print(min(f.f1 + x[1], f.f2 + x[2], f.f3 + x[3])); }

More Related