Question

In: Computer Science

Design an efficient algorithm to compute the number of binary strings with length n that satisfy...

Design an efficient algorithm to compute the number of binary strings with length n that satisfy 1 the regular expression ((0 + 11 + 101)∗ (1101))∗ .

Solutions

Expert Solution

ANSWER :

GIVEN THAT :

We can solve this question with the help of constructing a finite state machine and then designing a dynamic programming approach to solve the same.

Look at the below Finite state machine representing the regular expresion ( (0+10+101)(1101)).

Here, S,S1,F are three states where S is the start state and F is the final state.
Points to notice,We can create one 0 length string but we cannot create strings of length 1, 2 and 3. as (1101) is present.
From S, on having a string of length 1(0) or length 2(11) or length 3(101) we go to S1. In S1, we add (1101) to it and move to final state(if that is the termination of string) or move to S again to repeat the string (to perform the kleene star {*} operation).

Now, we will design a dynammic programming approach. We know number of string of length 0 that can be formed is 1 and number of strings of length 1,2 and 3 are 0. So,we will find out the number of strings of length l that can be formed in all the three states. i.e S,S1 and F. In the end, we will be intersted in the answer produced by F as it is the final state.

Consider,three arrays of length N+1:
DP_S[L] which gives the number of strings that can be formed in state S having length L.
Similarly, DP_S1[L] which gives the number of strings that can be formed in state S having length L.
and DP_F[L] which gives the number of strings that can be formed in state S having length L.

Algorithm:

1. Initialize DP_S[0] = 1 , DP_S1[0] = 1 and DP_F[0] = 1 (as number of strings of length 0 that can be formed is 1)

2. If L-4 > 0
DP_S[L] = DP_S1[L]
else
DP_S[L] = 0 (as number of strings of length 1,2,3 are 0)

3. DP_S1[L] = DP_S[L] + DP_S[L-1] + DP_S[L-2] + DP_S[L-3]
(number of strings in S1 of length L is sum of the number of strings of length L,L-1,L-2 and L-3 in state S as from state S, on generating a string of length 0 (null) or 1 (string 0) or 2 (string 11) or 3 (string 101) we move to state S1)

4. DP_F[L] = DP_S1[L-4] + DP_S[L]
(Similarly, from state S we can go to state F on null (same length string) or S1 we can go to F upon string 1101 (string of length 4). Hence, number of strings in F of length L is sum of the number of strings of length L in state S and number of strings of length L-4 in state S1)

5. DP_F[N] will return the total number of strings of length N that can be formed .


Related Solutions

Find a recurrence relation for the number of binary strings of length n which do not...
Find a recurrence relation for the number of binary strings of length n which do not contain the substring 010
With being given two n-bit binary strings, verify if the strings are identical or not. Design...
With being given two n-bit binary strings, verify if the strings are identical or not. Design a procedure showing all steps and write a pseudo-code. What is the complexity of the procedure? If it can tolerate some error, is there a better than linear time solution? If so, write the pseudo-code.
Use inclusion-exclusion to find the number of binary strings of length 5 that have at least...
Use inclusion-exclusion to find the number of binary strings of length 5 that have at least one of the following characteristics: start with a 1, end with a 0, or contain exactly two
find a recurrence relation for the number of bit strings of length n that contain the...
find a recurrence relation for the number of bit strings of length n that contain the string 10. What are the initial conditions? How many bit strings of length eight contain the string 10
Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:
Chapter 3 Exercise 1Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:      1. Make a guess at the answer (you can pick n/2 as your initial guess).      2. Computer = n / guess.      3. Set guess = (guess +r) / 2.      4. Go back to step 2 until the last two guess values are within 1% of each other.Write a program that inputs an integer for n, iterates through the Babylonian algorithm until the guess...
find a recurrence relation for the number of bit strings of length n that contain two...
find a recurrence relation for the number of bit strings of length n that contain two consecutive 1s. What are the initial conditions? How many bit strings of length eight contain two consecutive 1s
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
1. Write a Post system that defines the set of binary strings of odd length that...
1. Write a Post system that defines the set of binary strings of odd length that have a “x” as the middle character and "x" is a string
Design a Turing machine that, given a positive binary number n greater than or equal to...
Design a Turing machine that, given a positive binary number n greater than or equal to 2, subtracts 2 from this number. Test it, step-by-step, on the example of n = 2.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT