Skip to content

Commit

Permalink
Merge pull request gzc#214 from ravijoshiBITS/master
Browse files Browse the repository at this point in the history
made FA code more readable, added English solution to a question of exercise 15.4
  • Loading branch information
Zhenchao Gan authored Apr 1, 2018
2 parents 2bfdbd5 + a1b9f01 commit 4aa54f0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
35 changes: 27 additions & 8 deletions C15-Dynamic-Programming/15.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand All @@ -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 = <x1,x2,...,xn> we wish to find the longest monotonically increasing subsequence.

1. First we sort the string X which produces sequence X'.
Expand All @@ -78,6 +83,20 @@ Given a sequence X = <x1,x2,...,xn> 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.)
Expand Down
29 changes: 19 additions & 10 deletions C32-String-Matching/FA.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 4aa54f0

Please sign in to comment.