-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsat solver.cpp
169 lines (158 loc) · 3.25 KB
/
sat solver.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <vector>
#include <stdlib.h>
using namespace std;
int p,c;//p is no. of propositions ,c is no. of clauses
int lit = 0;
vector<vector<int> > clause;//vector that stores clauses
vector<int> result;//final result vector which gives 1 or -1 for every proposition
vector<int> unit;
vector<int> count;
void input()
{//Takes input
int i,b;
char a;
scanf("%c",&a);
while(a=='c')
{
while(a!='\n')
scanf("%c",&a);
scanf("%c",&a);
}
scanf("%c",&a);
scanf("%c",&a);
scanf("%c",&a);
scanf("%c",&a);
scanf("%d %d",&p,&c);
clause.resize(c);
result.resize(p,0);
unit.resize(p,0);
count.resize(p);
for(i=0;i<c;i++)
{
scanf("%d",&b);
while(b!=0)
{
clause[i].push_back(b);
scanf("%d",&b);
}
}
}
int conflict(){
int i,j;
for(i=0;i<c;i++)
{
int ct = 0;
for(j=0;j<clause[i].size();j++){
if(result[abs(clause[i][j])-1]==0)
break;
else
{
if(result[abs(clause[i][j])-1]*clause[i][j]<0)
ct++;
}
}
if(ct == clause[i].size())
{
return 1;
}
}
return 0;
}
void unitclause(){//gives 1 to a proposition in unit clause if true
int i;
for(i=0;i<c;i++)
{
if(clause[i].size()==1)
{
int x=abs(clause[i][0]);
if(clause[i][0]<0 && result[x-1]<=0)
result[x-1] = -1;
else if(clause[i][0]<0 && result[x-1]>0)
{//if clauses like 1 0 and -1 0 appears
printf("UNSAT");
exit(0);
}
if(clause[i][0]>0 && result[x-1]>=0)
result[x-1] = 1;
else if(clause[i][0]>0 && result[x-1]<0)
{
printf("UNSAT");
exit(0);
}
unit[x-1]++;
}
}
}
int prevlit(int lit){//provides the previous non unit literal
lit--;
if(lit<0)
{
printf("UNSAT");
exit(0);
}
while(unit[lit-1]!=0)
lit--;
return lit;
}
void fun(){//Simply a part of backtrack
if(conflict())
{
result[lit-1] = 0;
lit = prevlit(lit);
if(count[lit-1]%2==0){
result[lit-1] = 0;
lit = prevlit(lit);
}
result[lit-1] = -result[lit-1];//reversing the value of literal
count[lit-1]++;
fun();
}
}
void backtrack(){//a function which backtracks if there is a conflict by given model
while(conflict())
{
if(unit[lit-1]==0){
if(count[lit-1]%2==0)
lit = prevlit(lit);
result[lit-1] = -result[lit-1];
count[lit-1]++;
fun();}
else
lit = prevlit(lit);
}
}
void dpll(){//dpll algorithm
while(1)
{
while(conflict())
{
backtrack();
}
if(lit==p&&!conflict())
{//a satisfiable model is found
printf("SAT\n");
for(int i=0;i<p;i++){
printf("%d ",result[i]*(i+1));
}
exit(0);
}
if(result[lit]==0&&lit<p){
result[lit] = 1;
count[lit]++;
lit++;
}
else if(lit<p) {
lit++;
}
}
}
int main(){
input();
unitclause();
dpll();
for(int i=0;i<p;i++){
printf("%d ",result[i]*(i+1));
}
return 0;
}