Beth Tableaux
Time limit2sMemory limit64 MB
Parse a propositional formula and either report it valid or print the lexicographically smallest falsifying assignment.
- Level
Medium6 of 10
- Topics
- Backtracking, Implementation, Brute force, Recursion
- Solved
- No attempts yet
Problem
Your program must decide whether a given propositional (boolean) formula is valid — that is, whether it evaluates to (true) under every assignment of boolean values to its variables. If is not valid, then it evaluates to (false) for at least one assignment, and your program must also report such a falsifying assignment.
A naive method tries all assignments of the variables, which is hopeless for large and unlike the way people actually reason about formulas. The classical Beth tableau (semantic tableau) method searches for a counterexample directly: it keeps two collections of sub-formulas — those we are trying to make true and those we are trying to make false — and repeatedly breaks each compound formula down by its outermost connective. If every branch runs into a contradiction (the same formula forced both true and false, or forced true, or forced false), the formula is valid; a branch that survives with no contradiction gives a falsifying assignment (variables forced true get , those forced false get ). You may use any correct method — only the final answer is checked.
Syntax. Formulas are built as follows, with the connectives listed from the highest priority to the lowest:
- Constants (true) and (false).
- Variables: the letters
A–Zanda–z. They are case-sensitive, soAandaare different variables. - Parentheses: if is a formula, then so is .
- Negation: .
- Conjunction , evaluated left to right: .
- Disjunction , also evaluated left to right.
- Implication , evaluated right to left: means .
- Equivalence , defined as .
The connectives have their usual truth tables: is logical NOT, is AND, is OR, equals , and is true exactly when and have the same value.
Input
A single line containing the formula, written as a string of tokens: 0, 1, the letters A–Z and a–z, (, ), ~, &, |, =>, and =. The last five tokens stand for , , , , and respectively. Tokens may be separated by any number of spaces. The line contains at most characters, and the formula is guaranteed to be syntactically correct.
Output
Print exactly one line.
- If the formula is valid, print
true. - Otherwise print
false. If the formula contains at least one variable, append:followed by the lexicographically smallest falsifying assignment. Sort the variables in ascending order by character code (so uppercase letters come before lowercase), and among all assignments that make the formula equal to choose the one whose bit vector is smallest, reading the first variable as the most significant bit and taking . Write the assignment asvar=valuepairs joined by,(a comma and a space), for examplefalse: A=0, B=1. If the formula has no variables at all, print justfalse.