I was wondering if anyone can answer me which is number of results generated in the backtracking solution for the next problem:
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"
There is a related post in stackoverflow: Generate balanced parentheses in java
My doubt if that if there is a formula that can give me the number of valid parentheses I can generate before compute them.
for example:
- f(n):
- f(1) = 1
- f(2) = 2
- f(3) = 5
and so on..
Thank you.
The number expressions containing n pairs of parentheses which are correctly matched can be calculated via Catalan numbers. Quoting the relevant link from Wikipedia:
There are many counting problems in combinatorics whose solution is given by the Catalan numbers … Cn is the number of Dyck words of length 2n. A Dyck word is a string consisting of n X's and n Y's such that no initial segment of the string has more Y's than X's … Re-interpreting the symbol X as an open parenthesis and Y as a close parenthesis, Cn counts the number of expressions containing n pairs of parentheses which are correctly matched.
The nth Catalan number is given directly in terms of binomial coefficients by:
Related
Consider a valid string with bit-wise operands (that is "xor" ,"or","and") and also "X" where X is
a integer from {0,1}. Let us consider their are n number of "X" then their are 2**n possibles of strings .we need to find number of strings that have value as "0" and value as "1".
Example :
Consider "X&X" out of 4 possible,only 3 have value 0 and only one has value 1.
Can anybody help me when their are more than one bit wise operand in string.
You don't say, but I assume that the interpretation of strings is done by associating left-to-right (that is 0^1&0 means (0^1)&0). I'll use |, & and ^ for or, and, xor.
Let s be an arbitrary string with n Xs that evaluates to 0, and t be an arbitrary string with n Xs that evaluates to 1.
Then, the strings with n+1 Xs that evaluate to 1 look like one of: t|0, s|1, t|1, t&1, s^1, t^0. The ones that evaluate to 0 are s|0, s&1, s&0, t&0, s^0, t^1.
Fixing a sequence of operators, let S(n) be the number of strings with n Xs that evaluate to 0 with the given operators, and T(n) be the number that evaluate to 1. Suppose the i'th operator that you're given is O(i). Then, just counting the combinations in the previous paragraph for each operator, you have recurrence relations:
S(1)=T(1)=1
S(n+1), T(n+1) = S(n), S(n)+2T(n) if O(i)=|
S(n+1), T(n+1) = 2S(n)+T(n), T(n) if O(i)=&
S(n+1), T(n+1) = S(n)+T(n), S(n)+T(n) if O(i)=^
These recurrence relations are easy to encode in an O(n) dynamic program.
Assume we have n three letter substrings. It is possible to make a string of length n+2 out of these N substrings by concatenating them (Where overlapping letters are written only once) . Whereby this string must have the form a1,a2,a3,a4...
So it is only allowed to link two substrings if they overlap at two adjacent places: 'yxz' + 'xzw' = 'yxzw' , but 'yxz' + 'aby' is for example not allowed.
Example 1: The n = 3 three letter substrings are 'abc','cde','bcd' Output: YES
. Because 'abc' + 'bcd'+ 'cde' = 'abcde' is a valid String with n+2 = 5 letters.
Example 2: The n = 3 three letter substrings are 'abc','bca','bcd' Output: NO. Because its not possible to concatenating them all.
How can i finde an efficient algorithm for this problem? Trying all possible combinations takes far too long with O(n!)
One of the popular approaches to solving this kind of problems is to build the overlap graph of the input sequences, whose vertices are your triplets and where an arc a_i -> a_j between two triplets means that the last two letters of a_i are the first two letters of a_j; and then to find a Hamiltonian path in the resulting graph.
A naïve search would of course not outperform the exhaustive search you mention, but the linked Wikipedia article gives some leads on how to do this more efficiently.
Suppose we have a boolean array of length X. The only rule is, TRUE must not occur twice in adjacent places. Especially the array with only false values is allowed. E.g. this is forbidden: [1,1,0,0,0] and these is allowed: [1,0,0,0,0], [0,0,0,0,0], [1,0,1,0,1] etc. How can I use dynamic programming to determine how many different valid arrays of length X there are?
Let Ti be the number of arrays of length i that meet your criterion and end in 1, and let Fi be the number of arrays of length i that meet your criterion and do not end in 1.
Then:
T0 = 0
F0 = 1
Ti+1 = Fi. (Each array of length i+1 that meets your criterion and ends in 1 consists of an array of length i that meets your criterion and does not end in 1, plus an extra 1 at the end.)
Fi+1 = Fi + Ti. (Each array of length i+1 that meets your criterion and does not end in 1 consists of an array of length i that meets your criterion, plus an extra 0 at the end.)
You want FX + TX.
So you can just write a loop that calculates Fi and Ti for each i from 0 to X, and then return FX + TX.
(This isn't even dynamic programming, per se, because you don't need to store partial values; Fi+1 and Ti+1 depend only on Fi and Ti. So this is O(X) time and O(1) space.)
I think you can calculate the number without using DP. Since you know the total number of arrays for length N, that is `2^N'.
Now you need to deduct those bad arrays that do not qualify if they have adjacent 1's. For an array of length N, there are these cases
1. the array has no 1's, only one case, and it is a valid array
2. the array has one 1's, all cases are valid
3. the array has two 1's, there are N - 1 cases which are not valid
4. the array has three 1's, there are (N-1) * (N-2) / 2 cases which are not valid
...
The dp solution would have two state parameters. One is the position of the array and the other is the previous position’s value. If the previous position’s value is 1 then you can only choose 0. If the previous position’s value is 0 then you can choose either 1 or 0. Hope this helps.
You don't really need dynamic programming.
For array length X the number of valid arrays is Fib(X+1), where Fib is the array of fibonacci numbers.
X=1: valid arrays: 2
X=2: valid arrays: 3
X=3: valid arrays: 5
X=4: valid arrays: 8
and so on...
Demonstration:
Let's assume we are looking for the arrays for X and we know the number of valid arrays for X-1. We can freely add a zero to the end of each of these X-1 length arrays, so that's F(X-1) so far. We can also add a '1' to the end of each X-1 arrays which ends with 0. But how many of those arrays are? Well, it's exactly F(X-2) because we could generate the zero ending X-1 length arrays exactly the same way: by adding a zero at the end of each X-2 length arrays. So F(X) = F(X-1) + F(X-2)
And that's exactly the definition of the Fibonacci array.
All we have to do is manually calculate the first two element to determinate if it's exactly the Fibonacci array or is it shifted.
You can even find a formula to calculate the Nth element of the fibonacci array, so it can be solved in O(1).
When we have a recursive function to generate parentheses with N valid parentheses, the time complexity is that of the Catalan number. This doesn't make sense to me.
My analysis of the time complexity is that, we have two operations at every node of the recursion tree. We can either add a close bracket or an opening bracket. So we make two recursive calls.
T(n) = 2 * T(N - 1) = O(2^N)
I get O(2^N) as my time complexity -- not the Catalan number. The Catalan number is so arbitrary to me -- it doesn't make sense. Could anyone explain it a bit further?
In your assumption, you explore all cases that can be formed by the characters '(' and ')'. However, it is possible to eliminate some of those cases, isn't it? For instance, we know that for an input N = 4, "))((" is not a valid/balanced string. In fact, we know this to be true from the moment we put the first character of that string. Here's a recursive implementation in Python, just so that we can observe it through an example.
def generate(index, N, s, depth):
if index == N:
print s
if depth > 0:
generate(index + 1, N, s + ')', depth - 1)
if depth < N:
generate(index + 1, N, s + '(', depth + 1)
Essentially, in a recursive implementation, you keep a score of the current depth. Whenever that score is less than 0, you know that your string becomes unbalanced, thus there is no point in exploring further. So, contrary to what you assumed, you do not explore both the subproblems.
If you think about it, the problem is simply finding the number of valid permutations of N = 2 * K different characters. At the first(leftmost) position, you can place K characters. (i.e. all the '(') In the second position, you can either place one of the ')' characters, or you can place one of the remaining K-1 '(' characters. With this approach, using permutation with repetition, you can find that the complexity of the problem you mentioned is, indeed, equivalent to the Kth Catalan number.
Basically, for a string of length 2N, you have two different characters of which you have N, each. Using permutation with repetition, all the possible permutations for this string would be (2N)! / (N! N!). Well, the formula for the Nth Catalan number is just that value, divided by an additional (N+1), as you can see in the relevant Wikipedia article. If you consider the cases where you do not handle the unbalanced strings I mentioned above, you can see that (N+1) factor is due to the cases where you don't compute both the subproblems.
I read that the total number of substrings that can be formed from a given string is n^2 but I don't understand how to count this.
By substrings, I mean, given a string CAT, the substrings would be:
C
CA
CAT
A
AT
T
The total number of (nonempty) substrings is n + C(n,2). The leading n counts the number of substrings of length 1 and C(n,2) counts the number of substrings of length > 1 and is equal to the number of ways to choose 2 indices from the set of n. The standard formula for binomial coefficients yields C(n,2) = n*(n-1)/2. Combining these two terms and simplifying gives that the total number is (n^2 + n)/2. #rici in the comments notes that this is the same as C(n+1,2) which makes sense if you e.g. think in terms of Python string slicing where substrings of s can always be written in the form s[i:j] where 0 <= i < j <= n (with j being 1 more than the final index). For n = 3 this works out to (9 + 3)/2 = 6.
In the sense of complexity theory the number of substrings is O(n^2), which might be what you read somewhere.
You have a starting point and and end point - if each could point to anywhere along the word, each would have n possible values, and therefor an overall of n^2, so that's an upper limit.
However, we need a constraint saying that the substring cannot end before it started, so end - start >=0. This cuts the possible count in about half, but on asymptotic terms it's still O(n^2)
Substring calculation is logically
selecting 2 blank spaces atleast one letter apart.
a| b c | d = substring bc
| a b c |d = substring abc.
Now how many ways can you chose these 2 blankspace. For n letter word there are n+1.
Then first select one = n+1 ways
Select another (not the same)= n
So total n(n+1). But you have calculated everything twice. So n*(n+1)/2.
Programmatically, without applying any special algorithms(like Z algo etc) you can use a map to calculate no of distinct substrings.(O(n^3)).
You can use suffix tree to get O(n^2) substring calculaton.
To get a substring of a given string s, you just need to select two different points in the string. Let s contain n characters,
|s[0]|s[1]|...|s[n-1]|
You want to choose two vertical bars to get a substring. How many vertical bars do you have? Exactly n+1. So the number of sustrings is C(n+1,2) = n(n+1)/2, which is to choose 2 items from n+1. Of course, it could be denoted as O(n^2).