From a1b9f01948a307b78e45606d4cb7e0968c0d7f7b Mon Sep 17 00:00:00 2001 From: "f2016006@goa.bits-pilani.ac.in" Date: Wed, 14 Mar 2018 19:11:42 +0530 Subject: [PATCH] made FA more readable, added English solution to 15.4 --- C15-Dynamic-Programming/15.4.md | 35 +++++++++++++++++++++++++-------- C32-String-Matching/FA.c | 29 +++++++++++++++++---------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/C15-Dynamic-Programming/15.4.md b/C15-Dynamic-Programming/15.4.md index 78b2148a..7aacc65f 100644 --- a/C15-Dynamic-Programming/15.4.md +++ b/C15-Dynamic-Programming/15.4.md @@ -32,11 +32,11 @@ Give a memoized version of LCS-LENGTH that runs in O(mn) time. ### `Answer` LCS-LENGTH(X, Y) - m <- length[X] - n <- length[Y] - for i <- 1 to m do - for j <- 1 to n do - c[i,j] <- -1 + m ← length[X] + n ← length[Y] + for i ← 1 to m do + for j ← 1 to n do + c[i,j] ← -1 end for end for return LOOKUP-LENGTH(X,Y,m,n) @@ -46,12 +46,12 @@ Give a memoized version of LCS-LENGTH that runs in O(mn) time. return c[i,j] end if if i = 0 or j = 0 then - c[i,j] <- 0 + c[i,j] ← 0 else if X[i] = Y[j] then - c[i,j] <- LOOKUP-LENGTH(X,Y,i-1,j-1)+1 + c[i,j] ← LOOKUP-LENGTH(X,Y,i-1,j-1)+1 else - c[i,j] <- max(LOOKUP-LENGTH(X,Y,i,j-1),LOOKUP-LENGTH(X,Y,i-1,j)) + c[i,j] ← max(LOOKUP-LENGTH(X,Y,i,j-1),LOOKUP-LENGTH(X,Y,i-1,j)) end if end if return c[i,j] @@ -65,11 +65,16 @@ Show how to compute the length of an LCS using only 2 · min(m, n) entries in th 那如何做到只利用min(m, n) entries plus O(1) additional space呢? 其实我们只需要用一行保存状态即可. c[i,j-1]已经保存在该行中. 然后用一个常量维护c[i-1,j-1]即可.每次更新c[i,j]的时候将old valuy保存下来,因为下次要用到. +Since we need only c[i-1,j-1], c[i,j-1], c[i-1,j] to compute c[i,j], we just need to save the previous row and the current row of the dp table. We will maintain the row parallel to the shorter one of the X and Y strings. So we can run the algorithm with 2 · min(m, n) space. + +In fact, we only need to save only one row. c[i,j-1] is already stored in this row. Then, we use an extra variable to maintain c[i-1,j-1]. Every time c[i,j] is updated, the value of c[i-1,j] is saved into the extra variable because it will be used next time. + ### Exercises 15.4-5 *** Give an O(n^2)-time algorithm to find the longest monotonically increasing subsequence of a sequence of n numbers. ### `Answer` +Method 1: Given a sequence X = we wish to find the longest monotonically increasing subsequence. 1. First we sort the string X which produces sequence X'. @@ -78,6 +83,20 @@ Given a sequence X = we wish to find the longest monotonically in The running time is O(n^2) since sorting can be done in O(nlgn) and the call to LCS-LENGTH is O(n^2). +Method 2: + LONGEST-INC-SEQUENCE(Arr, n) + len [1...n] be a new array + for i←1 to n + len[i] ← 1 // length of longest increasing sequence ending at i. + for i←2 to n do + for j←1 to i-1 do + if Arr[j] < Arr[i] + len [i] ← max(len[i], len[j] +1) + end if + end for + end for + return len [n] + ### Exercises 15.4-6 * *** Give an O(n lg n)-time algorithm to find the longest monotonically increasing sub-sequence of a sequence of n numbers. (Hint: Observe that the last element of a candidate subsequence of length i is at least as large as the last element of a candidate subsequence of length i - 1. Maintain candidate subsequences by linking them through the input sequence.) diff --git a/C32-String-Matching/FA.c b/C32-String-Matching/FA.c index e30156a1..93b90c00 100644 --- a/C32-String-Matching/FA.c +++ b/C32-String-Matching/FA.c @@ -57,7 +57,7 @@ int FA_match(char *text,int *array,int numchars,int receive) int i; for(i = 0;i < n;i++) { - int index = numchars*q+text[i]-97; + int index = numchars*q+text[i]-'a'; q = array[index]; if(q == receive) return i+1-receive; @@ -67,23 +67,32 @@ int FA_match(char *text,int *array,int numchars,int receive) int main() { - char *text = "abababacaba"; - char *pattern = "ababaca"; + char *text = "aaababaabaababaab"; + char *pattern = "aabab"; + const int num_chars_alphabet = 2; int length = strlen(pattern); - int *array = (int*)malloc(sizeof(int)*3*(length+1)); + int *array = (int*)malloc(sizeof(int)*num_chars_alphabet*(length+1)); - compute_transition_function(pattern,array,3); + compute_transition_function(pattern,array,num_chars_alphabet); - int i; - printf("This is our chart\n"); + + //This prints a chart showing present state and next states given + //the corresponding inputs. + printf("This is our chart\nstate\t"); + for(char j = 'a'; j < 'a'+num_chars_alphabet; j++) + printf("%c\t", j); + printf("\n"); + + int i; for(i = 0;i <= length; i++) { + printf("%d\t",i); int j; - for(j = 0; j < 3; j++) - printf("%d ",array[i*3+j]); + for(j = 0; j < num_chars_alphabet; j++) + printf("%d\t",array[i*num_chars_alphabet+j]); printf("\n"); } - int offset = FA_match(text,array,3,length); + int offset = FA_match(text,array,num_chars_alphabet,length); printf("offset is %d\n",offset); free(array); return 0;