{"id":27185,"date":"2018-01-03T22:14:29","date_gmt":"2018-01-03T16:44:29","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27185"},"modified":"2018-01-03T22:14:29","modified_gmt":"2018-01-03T16:44:29","slug":"boolean-parenthesization-problem","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/boolean-parenthesization-problem\/","title":{"rendered":"Boolean Parenthesization Problem"},"content":{"rendered":"<p>Given a boolean expression with following symbols.<span id=\"more-129692\"><\/span><\/p>\n<pre><strong>Symbols<\/strong>\r\n    'T' ---&gt; true \r\n    'F' ---&gt; false<\/pre>\n<p>And following operators filled between symbols<\/p>\n<pre><strong>Operators<\/strong>\r\n    &amp;   ---&gt; boolean AND\r\n    |   ---&gt; boolean OR\r\n    ^   ---&gt; boolean XOR<\/pre>\n<p>Count the number of ways we can parenthesize the expression so that the value of expression evaluates to true.<\/p>\n<p>Let the input be in form of two arrays one contains the symbols (T and F) in order and other contains operators (&amp;, | and ^}<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre>Input: symbol[]    = {T, F, T}\r\n       operator[]  = {^, &amp;}\r\nOutput: 2\r\nThe given expression is \"T ^ F &amp; T\", it evaluates true\r\nin two ways \"((T ^ F) &amp; T)\" and \"(T ^ (F &amp; T))\"\r\n\r\nInput: symbol[]    = {T, F, F}\r\n       operator[]  = {^, |}\r\nOutput: 2\r\nThe given expression is \"T ^ F | F\", it evaluates true\r\nin two ways \"( (T ^ F) | F )\" and \"( T ^ (F | F) )\". \r\n\r\nInput: symbol[]    = {T, T, F, T}\r\n       operator[]  = {|, &amp;, ^}\r\nOutput: 4\r\nThe given expression is \"T | T &amp; F ^ T\", it evaluates true\r\nin 4 ways ((T|T)&amp;(F^T)), (T|(T&amp;(F^T))), (((T|T)&amp;F)^T) \r\nand (T|((T&amp;F)^T)). \r\n<\/pre>\n<p><strong>Solution:<\/strong><br \/>\nLet <u><strong>T(i, j)<\/strong><\/u> 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.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignleft size-full wp-image-27191\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/True-Equations.png\" alt=\"True Equations\" width=\"643\" height=\"96\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/True-Equations.png 643w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/True-Equations-300x45.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/True-Equations-640x96.png 640w\" sizes=\"(max-width: 643px) 100vw, 643px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Let <u><strong>F(i, j)<\/strong><\/u> 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.<\/p>\n<p><img decoding=\"async\" class=\"alignleft size-full wp-image-27192\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/False-Equations.png\" alt=\"False Equations\" width=\"651\" height=\"96\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/False-Equations.png 651w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/False-Equations-300x44.png 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Base Cases:<\/p>\n<pre>T(i, i) = 1 if symbol[i] = 'T' \r\nT(i, i) = 0 if symbol[i] = 'F' \r\n\r\nF(i, i) = 1 if symbol[i] = 'F' \r\nF(i, i) = 0 if symbol[i] = 'T'<\/pre>\n<p>If we draw recursion tree of above recursive solution, we can observe that it many overlapping subproblems. Like other dynamic programming problems, it can be solved by filling a table in bottom up manner. Following is C++ implementation of dynamic programming solution.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">#include&lt;iostream&gt;<br\/>#include&lt;cstring&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ Returns count of all possible parenthesizations that lead to<br\/>\/\/ result true for a boolean expression with symbols like true<br\/>\/\/ and false and operators like &amp;, | and ^ filled between symbols<br\/>int countParenth(char symb[], char oper[], int n)<br\/>{<br\/>    int F[n][n], T[n][n];<br\/> <br\/>    \/\/ Fill diaginal entries first<br\/>    \/\/ All diagonal entries in T[i][i] are 1 if symbol[i]<br\/>    \/\/ is T (true).  Similarly, all F[i][i] entries are 1 if<br\/>    \/\/ symbol[i] is F (False)<br\/>    for (int i = 0; i &lt; n; i++)<br\/>    {<br\/>        F[i][i] = (symb[i] == &#039;F&#039;)? 1: 0;<br\/>        T[i][i] = (symb[i] == &#039;T&#039;)? 1: 0;<br\/>    }<br\/> <br\/>    \/\/ Now fill T[i][i+1], T[i][i+2], T[i][i+3]... in order<br\/>    \/\/ And F[i][i+1], F[i][i+2], F[i][i+3]... in order<br\/>    for (int gap=1; gap&lt;n; ++gap)<br\/>    {<br\/>        for (int i=0, j=gap; j&lt;n; ++i, ++j)<br\/>        {<br\/>            T[i][j] = F[i][j] = 0;<br\/>            for (int g=0; g&lt;gap; g++)<br\/>            {<br\/>                \/\/ Find place of parenthesization using current value<br\/>                \/\/ of gap<br\/>                int k = i + g;<br\/> <br\/>                \/\/ Store Total[i][k] and Total[k+1][j]<br\/>                int tik = T[i][k] + F[i][k];<br\/>                int tkj = T[k+1][j] + F[k+1][j];<br\/> <br\/>                \/\/ Follow the recursive formulas according to the current<br\/>                \/\/ operator<br\/>                if (oper[k] == &#039;&amp;&#039;)<br\/>                {<br\/>                    T[i][j] += T[i][k]*T[k+1][j];<br\/>                    F[i][j] += (tik*tkj - T[i][k]*T[k+1][j]);<br\/>                }<br\/>                if (oper[k] == &#039;|&#039;)<br\/>                {<br\/>                    F[i][j] += F[i][k]*F[k+1][j];<br\/>                    T[i][j] += (tik*tkj - F[i][k]*F[k+1][j]);<br\/>                }<br\/>                if (oper[k] == &#039;^&#039;)<br\/>                {<br\/>                    T[i][j] += F[i][k]*T[k+1][j] + T[i][k]*F[k+1][j];<br\/>                    F[i][j] += T[i][k]*T[k+1][j] + F[i][k]*F[k+1][j];<br\/>                }<br\/>            }<br\/>        }<br\/>    }<br\/>    return T[0][n-1];<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    char symbols[] = &quot;TTFT&quot;;<br\/>    char operators[] = &quot;|&amp;^&quot;;<br\/>    int n = strlen(symbols);<br\/> <br\/>    \/\/ There are 4 ways<br\/>    \/\/ ((T|T)&amp;(F^T)), (T|(T&amp;(F^T))), (((T|T)&amp;F)^T) and (T|((T&amp;F)^T))<br\/>    cout &lt;&lt; countParenth(symbols, operators, n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output :<\/strong><\/p>\n<pre>4<\/pre>\n<p>Time Complexity: O(n<sup>3<\/sup>)<br \/>\nAuxiliary Space: O(n<sup>2<\/sup>)<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Boolean Parenthesization Problem &#8211; Dynamic Programming Count the number of ways we can parenthesize the expression so that the value of expression<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83847,70145],"tags":[81034,81029,81035,81038,81030,81032,81031,72848,72840,72846,72985,72844,81041],"class_list":["post-27185","post","type-post","status-publish","format-standard","hentry","category-boolean-parenthesization","category-dynamic-programming","tag-2-dimensional-algorithms-practice-problems","tag-boolean-parenthesisation-problem-dynamic-programming","tag-boolean-parenthesization-in-c-code","tag-boolean-parenthesization-problem-java","tag-counting-boolean-parenthesization-problem","tag-counting-boolean-parenthesizations","tag-counting-boolean-parenthesizations-implementation","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-java","tag-dynamic-programming-software","tag-evaluate-expression-to-in-c-program"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27185"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27185\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}