edited by
16,894 views
77 77 votes

Let a$_{n}$ represent the number of bit strings of length n containing two consecutive $1$s. What is the recurrence relation for $a_{n}$?

  1. $a_{n - 2} + a_{n - 1} + 2^{n - 2}$
  2. $a_{n - 2} + 2a_{n - 1} + 2^{n - 2}$
  3. $2a_{n - 2} + a_{n - 1} + 2^{n - 2}$
  4. $2a_{n - 2} + 2a_{n - 1} + 2^{n - 2}$

5 Answers

Best answer
113 113 votes

Counting the number of bit strings NOT containing two consecutive $1$'s. $($It is easy to derive a recurrence relation for the NOT case as shown below$)$

$0 \quad 1$
$00 \quad 01 \quad 10 - 3$ $($append both $0$ and $1$ to any string ending in $0$, and append $0$ to any string ending in $1$$)$
$000 \quad 001 \quad 010 \quad 100 \quad 101 - 5$ $($all strings ending in $0$ give two strings and those ending in $1$ give $1$ string$)$
$0000 \quad 0001 \quad 0010 \quad 0100 \quad 0101 \quad 1000 \quad 1001 \quad 1010 - 8$
$\vdots$

$a_n' = a_{n-1}' + a_{n-2}' $ $($where $a_n$ denote the number of bit strings of length $n$ containing two consecutive $1$s$)$

$2^n - a_n = (2^{n-1}  - a_{n-1}) + (2^{n-2} - a_{n-2})$

$a_n= 2^{n-2}(4 - 2 - 1) + a_{n-1} +a_{n-2}$

$a_n= a_{n-1} + a_{n-2} + 2^{n-2}$

Correct Option: A

edited by
23 23 votes

we can break this problem into different cases:

Now first assume we already build the string of length n-1 and then,

we want to add 0 at the end of it (we are concatenating at right end),  we know adding a zero can’t increase the number of string with consecutive ones.  

so $a_{n-1}$ is the number of strings of length n-1 with 2 consecutive one, we then add a 0, this number remains the same.

we want to add a 1 at the end, here we need to be little careful about whether last element of string of length n-1 is 1 or 0,  so we have to analyze last 2 bits instead of 1,

For 2 bits, cases for 00 and 10 are covered in case 1 (adding 0 at the end).

We left with two cases 01 and 11

Adding 01 at the end of a string of length can’t add any extra consecutive 1’s, so this gives $a_{n-2}$ possible strings

lastly we are adding 11, which is itself a pair of consecutive ones, so irrespective of what we have in those n-2 bits, adding 11 make all of them a string with two consecutive 1’s, So there are $2^{n-2}$ possible string of length n-2, and we are adding 11 at the end, so this case gives $2^{n-2}$ strings with two consecutive 1’s

Adding all together, $a_{n}$ will be:

$a_{n-1}$ (adding 0 at the end.)
$a_{n-2}$ (adding 01 at the end)
$2^{n-2}$ (adding 11 at the end)
$a_{n} = a_{n-1} + a_{n-2} + 2^{n-2}$

(All these cases are disjoint, string from case 1(adding 0)  and case 2(adding 01) are disjoint because of last bit is different, similarly case1(adding 0) and case 2(adding 11) are different, case 2 and 3 are disjoint by their second last bit)


A similar kind of analysis can be done for all variants of this problem,
For 2 consecutive 0s, everything remains same; just replace 0 with 1, and 1 with 0.

For no consecutive 0s ( or for no consecutive 1s, after doing same replacement here as well)
It goes as follows: (explaining for no consecutive 0’s)

we get a string of length n-1 by recursion, we can add 1 at the end without worrying about anything, because adding 1 can never generate consecutive zeros, so we get $a_{n-1}$ for a string of length n-1,

For adding 0, we have to be careful. Let us move to the last two bits.

last two bits can be 10, 01,11 ( it can’t be 00 because that is generating consecutive 0’s)

01, 11 both are covered in the previous case(adding 1 at last bit)
We are left with only one possible way: 10. This case gives $a_{n-2}$
We are done, as we have seen all the possible cases.

Therefore, for no consecutive 0’s

$a_{n} = a_{n-1} + a_{n-2}$

edited by
16 16 votes
For strings with consecutive 1s,
 

a0=0

a1=0

a2 =11 (total 1) ,

a3= 011,111,110  (3),

a4= 0011,1011,0110,0111,1111,1110,1100,1101 (total 8)...by backtracking,option a and c satisfy a3 and only a satisfies a4..so a is the answer.
1 1 vote
You can just do this question using option elimination but let's understand what's the logic behind it

We want to find recurrence for number of strings of length n having two consecutive 1

Let number of strings of length n having two consecutive 1 be 'an'

Now we will have cases

A) what if string ends with 0

In this case we need to find number of strings of length n-1 having two consecutive 1

So it will 'an-1'

B) what if strings ends with 1

In this case you will have two cases

1) either string ends with 01 or 11

If the string is ending with 01 then we need to find number of strings of length n-2 having two consecutive 1

ie it will be 'an-2'

But if string ends with 11 we already have two consecutive 11 now we just need to find number of strings of length n-2.

Each position will have 2 choices which is total 2^n-2

Finally recurrence is

an= an-1+ an-2+ 2^n-2

 
Answer:
Position:
Show:

Related questions

51 51 votes
3 answers 3 answers
18.9k
18.9k views
Misbah Ghaya asked Feb 11, 2015
18,947 views
Which one of the following is the recurrence equation for the worst case time complexity of the quick sort algorithm for sorting $n\;( \geq 2)$ numbers? In the recurrenc...
93 93 votes
16 answers 16 answers
32.7k
32.7k views
Misbah Ghaya asked Feb 13, 2015
32,650 views
Let $G = (V, E)$ be a simple undirected graph, and $s$ be a particular vertex in it called the source. For $x \in V$, let $d(x)$ denote the shortest distance in $G$ from ...
89 89 votes
7 answers 7 answers
29.5k
29.5k views
Misbah Ghaya asked Feb 13, 2015
29,455 views
The graph shown below has $8$ edges with distinct integer edge weights. The minimum spanning tree (MST) is of weight $36$ and contains the edges: $\{(A, C), (B, C), (B, E...
131 131 votes
7 answers 7 answers
41.2k
41.2k views
Misbah Ghaya asked Feb 13, 2015
41,184 views
An algorithm performs $(\log N)^{\frac{1}{2}}$ find operations , $N$ insert operations, $(\log N)^{\frac{1}{2}}$ delete operations, and $(\log N)^{\frac{1}{2}}$ decrease-...