|
| 1 | +/* |
| 2 | +https://www.geeksforgeeks.org/boolean-parenthesization-problem-dp-37/ |
| 3 | +
|
| 4 | +NOTE - The problem is solved similar to Longest Palindromic Subsequence. Only the space above the principal diaognal is utilized |
| 5 | +
|
| 6 | +Given a boolean expression with following symbols. |
| 7 | +
|
| 8 | +Symbols |
| 9 | + 'T' ---> true |
| 10 | + 'F' ---> false |
| 11 | +
|
| 12 | +And following operators filled between symbols |
| 13 | +
|
| 14 | +Operators |
| 15 | + & ---> boolean AND |
| 16 | + | ---> boolean OR |
| 17 | + ^ ---> boolean XOR |
| 18 | +
|
| 19 | +Count the number of ways we can parenthesize the expression so that the value of expression evaluates to true. |
| 20 | +
|
| 21 | +
|
| 22 | +Let the input be in form of two arrays one contains the symbols (T and F) in order and other contains operators (&, | and ^} |
| 23 | +
|
| 24 | +Examples: |
| 25 | +
|
| 26 | +Input: symbol[] = {T, F, T} |
| 27 | + operator[] = {^, &} |
| 28 | +Output: 2 |
| 29 | +The given expression is "T ^ F & T", it evaluates true |
| 30 | +in two ways "((T ^ F) & T)" and "(T ^ (F & T))" |
| 31 | +
|
| 32 | +Input: symbol[] = {T, F, F} |
| 33 | + operator[] = {^, |} |
| 34 | +Output: 2 |
| 35 | +The given expression is "T ^ F | F", it evaluates true |
| 36 | +in two ways "( (T ^ F) | F )" and "( T ^ (F | F) )". |
| 37 | +
|
| 38 | +Input: symbol[] = {T, T, F, T} |
| 39 | + operator[] = {|, &, ^} |
| 40 | +Output: 4 |
| 41 | +The given expression is "T | T & F ^ T", it evaluates true |
| 42 | +in 4 ways ((T|T)&(F^T)), (T|(T&(F^T))), (((T|T)&F)^T) |
| 43 | +and (T|((T&F)^T)). |
| 44 | +
|
| 45 | +
|
| 46 | +
|
| 47 | +
|
| 48 | +Let T(i, j) represents the number of ways to parenthesize the symbols between i and j (both inclusive) such that the subexpression between i and j evaluates to true. |
| 49 | +1. Using a variable gap we fix the number of symbols we want to evaluate. |
| 50 | +2. Then, we fix the start and end of the window which is slid by one each time the internal processing is done. |
| 51 | +3. Internal Processing - we take a variable to divide the symbols between start and end, to consider all possible combinations. |
| 52 | + This variable represents the input operators. |
| 53 | + - - [start-] k [- - - - - - end]- - |
| 54 | + - - [start- -] k [- - - - - end]- - |
| 55 | + - - [start- - -] k [- - - - end]- - |
| 56 | + ..... |
| 57 | + |
| 58 | +Formula to compute T(i,j) |
| 59 | +T(i,j) = Summation of k(i .... j-1) | T(i,k) * T(k+1,j) if operator[k] = & |
| 60 | + | Total(i,k)*Total(k+1,j) - F(i,k)*F(k+1,j) if operator[k] = | |
| 61 | + | T(i,k)*F(k+1,j) + F(i,k)*T(k+1,j) if operator[k] = ^ |
| 62 | + where Total(i,k) = T(i,k) + F(i,k) |
| 63 | +
|
| 64 | +Let F(i, j) represents the number of ways to parenthesize the symbols between i and j (both inclusive) such that the subexpression between i and j evaluates to false. |
| 65 | +
|
| 66 | +Formula to compute F(i,j) |
| 67 | +F(i,j) = Summation of k(i .... j-1) | Total(i,k)*Total(k+1,j) - T(i,k)*T(k+1,j) if operator[k] = & |
| 68 | + | F(i,k)*F(k+1,j) if operator[k] = | |
| 69 | + | T(i,k)*T(k+1,j) + F(i,k)*F(k+1,j) if operator[k] = ^ |
| 70 | + where Total(i,k) = T(i,k) + F(i,k) |
| 71 | +
|
| 72 | +*/ |
| 73 | + |
| 74 | + |
| 75 | +public int countBooleanParenthesization(char sym[], char op[], int n){ |
| 76 | + int T[][] = new int[n][n]; |
| 77 | + int F[][] = new int[n][n]; |
| 78 | + |
| 79 | + for(int i=0;i<n;i++){ |
| 80 | + if(sym[i] == 'T') T[i][i] = 1; |
| 81 | + else T[i][i] = 0; |
| 82 | + |
| 83 | + if(sym[i] == 'F') F[i][i] = 1; |
| 84 | + else F[i][i] = 0; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + for(int gap = 1 ; gap < n ; gap++){ |
| 89 | + |
| 90 | + for(int start = 0, end = gap ; end < n ; start++ , end++){ |
| 91 | + |
| 92 | + T[start][end] = F[start][end] = 0; |
| 93 | + |
| 94 | + for(int k = start ; k < end ; k++){ |
| 95 | + |
| 96 | + int tot_start_k = T[start][k] + F[start][k]; |
| 97 | + int tot_k_end = T[k+1][end] + F[k+1][end]; |
| 98 | + |
| 99 | + if(op[k] == '&'){ |
| 100 | + T[start][end] += (T[start][k] * T[k+1][end]); |
| 101 | + F[start][end] += (tot_start_k * tot_k_end - T[start][k] * T[k+1][end]); |
| 102 | + } |
| 103 | + |
| 104 | + else if(op[k] == '|'){ |
| 105 | + T[start][end] += (tot_start_k * tot_k_end - F[start][k] * F[k+1][end]); |
| 106 | + F[start][end] += (F[start][k] * F[k+1][end]); |
| 107 | + } |
| 108 | + |
| 109 | + else{ //op[k] == '^' |
| 110 | + T[start][end] += (T[start][k] * F[k+1][end]) + (F[start][k] * T[k+1][end]) |
| 111 | + F[start][end] += (F[start][k] * F[k+1][end]) + (T[start][k] * T[k+1][end]); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return T[0][n-1]; |
| 119 | +} |
| 120 | + |
| 121 | +/* |
| 122 | +Time Complexity - O(n^3) |
| 123 | +Number of subproblems - n*n |
| 124 | +Time required to solve each subproblem - n |
| 125 | +
|
| 126 | +Space Complexity - O(n^2) |
| 127 | +*/ |
| 128 | + |
0 commit comments