{"record":{"id":"8904dedec3b768e9","repo":"TheAlgorithms/Java","slug":"no-satisfying-assignment-exists-for-the-given-expr","errorCode":null,"errorMessage":"No satisfying assignment exists for the given expression.","messagePattern":"No satisfying assignment exists for the given expression\\.","errorType":"panic","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java","lineNumber":217,"sourceCode":"        if (!isSolved) {\n            throw new Error(\"Please call solve() before checking for a solution.\");\n        }\n        return hasSolution;\n    }\n\n    /**\n     * Returns one valid assignment of variables that satisfies the boolean formula.\n     *\n     * @return a boolean array where {@code result[i]} represents the truth value of\n     *         variable {@code xᵢ}\n     * @throws Error if called before {@link #solve()} or if no solution exists\n     */\n    boolean[] getSolutions() {\n        if (!isSolved) {\n            throw new Error(\"Please call solve() before fetching the solution.\");\n        }\n        if (!hasSolution) {\n            throw new Error(\"No satisfying assignment exists for the given expression.\");\n        }\n        return variableAssignments.clone();\n    }\n\n    /** Performs DFS to compute topological order. */\n    private void dfsForTopologicalOrder(int u, boolean[] visited, Stack<Integer> topologicalOrder) {\n        visited[u] = true;\n        for (int v : graph[u]) {\n            if (!visited[v]) {\n                dfsForTopologicalOrder(v, visited, topologicalOrder);\n            }\n        }\n        topologicalOrder.push(u);\n    }\n\n    /** Performs DFS on the transposed graph to identify SCCs. */\n    private void dfsForScc(int u, boolean[] visited, int[] component, int sccId) {\n        visited[u] = true;","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/graphs/TwoSat.java#L199-L235","documentation":"Thrown by `TwoSat.getSolutions` when the formula is unsatisfiable (`hasSolution == false`). After `solve()` runs, if any variable and its negation share an SCC the formula has no satisfying assignment, so returning one is impossible. This is an `Error` reflecting that the problem instance, not the call, is contradictory.","triggerScenarios":"Calling `getSolutions()` on a solved TwoSat instance whose clauses are contradictory (e.g. clauses forcing `(x1) and (not x1)`).","commonSituations":"Over-constrained formulas; clauses generated from mutually exclusive requirements; feeding an inherently unsatisfiable problem to the solver.","solutions":["Check `isSolutionExists()` before calling `getSolutions()`","Relax/remove contradictory clauses so the formula becomes satisfiable","Catch the Error and report unsatisfiability to the user"],"exampleFix":"// before\nboolean[] s = ts.getSolutions(); // throws if unsat\n// after\nts.solve();\nif (ts.isSolutionExists()) {\n    boolean[] s = ts.getSolutions();\n} else {\n    // report unsatisfiable\n}","handlingStrategy":"validation","validationCode":"ts.solve();\nif (!ts.isSolutionExists()) {\n    // no solution; do not call getSolutions()\n}","typeGuard":null,"tryCatchPattern":"try {\n    ts.getSolutions();\n} catch (Error e) {\n    // formula is unsatisfiable\n}","preventionTips":["Always guard getSolutions() with isSolutionExists()","Treat unsatisfiability as an expected outcome, not an exception"],"tags":["sat","unsatisfiable","algorithm-invariant"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}