-
Notifications
You must be signed in to change notification settings - Fork 1
/
DPSolver.java
47 lines (43 loc) · 1.24 KB
/
DPSolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Jan 27, 2018
*
* @author Noah Tyler
*/
package satSolver;
public class DPSolver{
public DPSolver(String fileName){
Formula formula = new Formula();
formula.read(fileName);
solve(formula);
}
public boolean dpSolver(Formula f){
if(f.isFormulaEmpty())//formula emmpty or solved
return true;
else if(f.hasEmptyClause())//formula has empty clause
return false;
else {
int varAvailable = f.firstAvailable();
f.assign(varAvailable, 1);
if(dpSolver(f))
return true;
else{
f.unset(varAvailable);
f.assign(varAvailable, -1);
if(dpSolver(f))
return true;
else{
f.unset(varAvailable);
}
return false;
}
}
}
public void solve(Formula f) {
if(dpSolver(f)){
System.out.println("The formula is satisfiable with the following variables!");
f.printAssignment();
}
else
System.out.println("The formula is unsatisfiable!");
}
}