-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSAT_Solver.c
47 lines (43 loc) · 1.61 KB
/
SAT_Solver.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Function to evaluate a CNF formula with a given variable assignment
bool evaluate_formula(int **formula, int *assignment, int num_clauses, int num_variables) {
for (int i = 0; i < num_clauses; i++) {
bool clause_satisfied = false;
for (int j = 0; j < 3; j++) {
int var = formula[i][j];
if (var > 0 && assignment[var - 1] == 1) {
clause_satisfied = true;
break;
}
if (var < 0 && assignment[-var - 1] == 0) {
clause_satisfied = true;
break;
}
}
if (!clause_satisfied) {
return false; // If one clause is not satisfied, formula is unsatisfied
}
}
return true; // If all clauses are satisfied
}
// Backtracking solver using the PMLL algorithm
bool PMLL_SAT_Solver(int **formula, int num_clauses, int num_variables) {
int *assignment = (int *)calloc(num_variables, sizeof(int)); // All variables are initially unassigned (0)
// Recursive backtracking
for (int i = 0; i < num_variables; ++i) {
assignment[i] = 1; // Try true (1)
if (evaluate_formula(formula, assignment, num_clauses, num_variables)) {
free(assignment);
return true;
}
assignment[i] = 0; // Try false (0)
if (evaluate_formula(formula, assignment, num_clauses, num_variables)) {
free(assignment);
return true;
}
}
free(assignment);
return false; // If no assignment satisfies the formula, return unsatisfiable
}