コード例 #1
0
  /**
   * Recursive backtracking solution
   *
   * @param formula 3sat formula to solve.
   * @returns true iff formula is satisfiable
   */
  public boolean dp(Formula formula) {

    if (formula.isEmpty()) // First base case: solution found
    return true;
    else if (formula.hasEmptyClause()) { // Second base case: dead end found

      return false;
    } else {
      // Pick a branch variable
      int var = formula.selectBranchVar();
      formula.setVar(var, true);
      if (dp(formula)) return true;
      else {
        // Unset var in the formula
        formula.unset(var);
        // Setting var to true did not work, so now try var = false
        formula.setVar(var, false);

        if (dp(formula)) return true;
        else {
          // Neither true nor false worked, so unset the branch
          // variable and backtrack
          formula.unset(var);
          return false;
        }
      }
    }
  }
コード例 #2
0
 @Test
 public void testIsEmpty() {
   Formula f = new Formula();
   assertTrue(f.isEmpty());
 }