Data Structure Multiple Choice Questions on “Counting Boolean Parenthesizations”.
1. You are given a boolean expression which consists of operators &, | and ∧ (AND, OR and XOR) and symbols T or F (true or false). You have to find the number of ways in which the symbols can be parenthesized so that the expression evaluates to true. This is the boolean parenthesization problem. Which of the following methods can be used to solve the problem? Answer: d 2. Consider the expression T & F | T. What is the number of ways in which the expression can be parenthesized so that the output is T (true)? Answer: c 3. Consider the expression T & F ∧ T. What is the number of ways in which the expression can be parenthesized so that the output is T (true)? Answer: c 4. Consider the expression T | F ∧ T. In how many ways can the expression be parenthesized so that the output is F (false)? Answer: b 5. Which of the following gives the total number of ways of parenthesizing an expression with n + 1 terms? Answer: d 6. What is the maximum number of ways in which a boolean expression with n + 1 terms can be parenthesized, such that the output is true? Answer: a 7. Consider the following dynamic programming implementation of the boolean parenthesization problem: Which of the following lines should be added to complete the “if(op[pos] == ‘|’)” part of the code? Answer: d 8. Which of the following lines should be added to complete the “if(op[k] == ‘&’)” part of the following code? a) True[row][col] += False[row][pos] * False[pos+1][col]; Answer: b 9. What is the time complexity of the following dynamic programming implementation of the boolean parenthesization problem? a) O(1) 10. What is the space complexity of the following dynamic programming implementation of the boolean parenthesization problem? a) O(1) Answer: c 11. What is the output of the following code? a) 1 Answer: d
a) Dynamic programming
b) Recursion
c) Brute force
d) Dynamic programming, Recursion and Brute force
Clarification: Dynamic programming, Recursion and Brute force can be used to solve the Boolean parenthesization problem.
a) 0
b) 1
c) 2
d) 3
Clarification: The expression can be parenthesized as T & (F | T) and (T & F) | T so that the output is T.
a) 0
b) 1
c) 2
d) 3
Clarification: The expression can be parenthesized as (T & F) ∧ T or T & (F ∧ T), so that the output is T.
a) 0
b) 1
c) 2
d) 3
Clarification: The expression can be parenthesized as (T | F) ∧ T, so that the output is F (false).
a) n factorial
b) n square
c) n cube
d) nth catalan number
Clarification: The nth Catalan number gives the total number of ways of parenthesizing an expression with n + 1 terms.
a) nth catalan number
b) n factorial
c) n cube
d) n square
Clarification: The number of ways will be maximum when all the possible parenthesizations result in a true value. The number of possible parenthesizations is given by the nth catalan number.int count_bool_parenthesization(char *sym, char *op)
{
int str_len = strlen(sym);
int True[str_len][str_len],False[str_len][str_len];
int row,col,length,l;
for(row = 0, col = 0; row < str_len; row++,col++)
{
if(sym[row] == 'T')
{
True[row][col] = 1;
False[row][col] = 0;
}
else
{
True[row][col] = 0;
False[row][col] = 1;
}
}
for(length = 1; length < str_len; length++)
{
for(row = 0, col = length; col < str_len; col++, row++)
{
True[row][col] = 0;
False[row][col] = 0;
for(l = 0; l < length; l++)
{
int pos = row + l;
int t_row_pos = True[row][pos] + False[row][pos];
int t_pos_col = True[pos+1][col] + False[pos+1][col];
if(op[pos] == '|')
{
_______________;
}
if(op[pos] == '&')
{
_______________;
}
if(op[pos] == '^')
{
_______________;
}
}
}
}
return True[0][str_len-1];
}
a) False[row][col] += True[row][pos] * False[pos+1][col];
True[row][col] += t_row_pos * t_pos_col + False[row][pos] * False[pos+1][col];
b) False[row][col] += False[row][pos] * True[pos+1][col];
True[row][col] += t_row_pos * t_pos_col – True[row][pos] * True[pos+1][col];
c) False[row][col] += True[row][pos] * True[pos+1][col];
True[row][col] += t_row_pos * t_pos_col + True[row][pos] * True[pos+1][col];
d) False[row][col] += False[row][pos] * False[pos+1][col];
True[row][col] += t_row_pos * t_pos_col – False[row][pos] * False[pos+1][col];
Clarification: The following lines should be added:
False[row][col] += False[row][pos] * False[pos+1][col];
True[row][col] += t_row_pos * t_pos_col + False[row][pos] * False[pos+1][col];int count_bool_parenthesization(char *sym, char *op)
{
int str_len = strlen(sym);
int True[str_len][str_len],False[str_len][str_len];
int row,col,length,l;
for(row = 0, col = 0; row < str_len; row++,col++)
{
if(sym[row] == 'T')
{
True[row][col] = 1;
False[row][col] = 0;
}
else
{
True[row][col] = 0;
False[row][col] = 1;
}
}
for(length = 1; length < str_len; length++)
{
for(row = 0, col = length; col < str_len; col++, row++)
{
True[row][col] = 0;
False[row][col] = 0;
for(l = 0; l < length; l++)
{
int pos = row + l;
int t_row_pos = True[row][pos] + False[row][pos];
int t_pos_col = True[pos+1][col] + False[pos+1][col];
if(op[pos] == '|')
{
_______________;
}
if(op[pos] == '&')
{
_______________;
}
if(op[pos] == '^')
{
_______________;
}
}
}
}
return True[0][str_len-1];
}
False[row][col] += t_row_pos * t_pos_col – True[row][pos] * True[pos+1][col];
b) True[row][col] += True[row][pos] * True[pos+1][col];
False[row][col] += t_row_pos * t_pos_col – True[row][pos] * True[pos+1][col];
c) True[row][col] += True[row][pos] * False[pos+1][col];
False[row][col] += t_row_pos * t_pos_col – False[row][pos] * True[pos+1][col];
d) True[row][col] += False[row][pos] * True[pos+1][col];
False[row][col] += t_row_pos * t_pos_col – False[row][pos] * True[pos+1][col];
Clarification: The following lines should be added:
True[row][col] += True[row][pos] * True[pos+1][col];
False[row][col] += t_row_pos * t_pos_col – True[row][pos] * True[pos+1][col];int count_bool_parenthesization(char *sym, char *op)
{
int str_len = strlen(sym);
int True[str_len][str_len],False[str_len][str_len];
int row,col,length,l;
for(row = 0, col = 0; row < str_len; row++,col++)
{
if(sym[row] == 'T')
{
True[row][col] = 1;
False[row][col] = 0;
}
else
{
True[row][col] = 0;
False[row][col] = 1;
}
}
for(length = 1; length < str_len; length++)
{
for(row = 0, col = length; col < str_len; col++, row++)
{
True[row][col] = 0;
False[row][col] = 0;
for(l = 0; l < length; l++)
{
int pos = row + l;
int t_row_pos = True[row][pos] + False[row][pos];
int t_pos_col = True[pos+1][col] + False[pos+1][col];
if(op[pos] == '|')
{
False[row][col] += False[row][pos] * False[pos+1][col];
True[row][col] += t_row_pos * t_pos_col - False[row][pos] * False[pos+1][col];;
}
if(op[pos] == '&')
{
True[row][col] += True[row][pos] * True[pos+1][col];
False[row][col] += t_row_pos * t_pos_col - True[row][pos] * True[pos+1][col];
}
if(op[pos] == '^')
{
True[row][col] += True[row][pos] * False[pos+1][col]
+ False[row][pos] * True[pos + 1][col];
False[row][col] += True[row][pos] * True[pos+1][col]
+ False[row][pos] * False[pos+1][col];
}
}
}
}
return True[0][str_len-1];
}
b) O(n)
c) O(n2)
d) O(n3)
Answer: d
Clarification: The time complexity of the above dynamic programming implementation is O(n3).int count_bool_parenthesization(char *sym, char *op)
{
int str_len = strlen(sym);
int True[str_len][str_len],False[str_len][str_len];
int row,col,length,l;
for(row = 0, col = 0; row < str_len; row++,col++)
{
if(sym[row] == 'T')
{
True[row][col] = 1;
False[row][col] = 0;
}
else
{
True[row][col] = 0;
False[row][col] = 1;
}
}
for(length = 1; length < str_len; length++)
{
for(row = 0, col = length; col < str_len; col++, row++)
{
True[row][col] = 0;
False[row][col] = 0;
for(l = 0; l < length; l++)
{
int pos = row + l;
int t_row_pos = True[row][pos] + False[row][pos];
int t_pos_col = True[pos+1][col] + False[pos+1][col];
if(op[pos] == '|')
{
False[row][col] += False[row][pos] * False[pos+1][col];
True[row][col] += t_row_pos * t_pos_col - False[row][pos] * False[pos+1][col];;
}
if(op[pos] == '&')
{
True[row][col] += True[row][pos] * True[pos+1][col];
False[row][col] += t_row_pos * t_pos_col - True[row][pos] * True[pos+1][col];
}
if(op[pos] == '^')
{
True[row][col] += True[row][pos] * False[pos+1][col]
+ False[row][pos] * True[pos + 1][col];
False[row][col] += True[row][pos] * True[pos+1][col]
+ False[row][pos] * False[pos+1][col];
}
}
}
}
return True[0][str_len-1];
}
b) O(n)
c) O(n2)
d) O(n3)
Clarification: The space complexity of the above dynamic programming implementation is O(n2).#include
b) 2
c) 3
d) 4
View Answer
Clarification: The output of the above program is 4.