You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/graph/2SAT.md
+17-16Lines changed: 17 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,8 +27,8 @@ $$(a \lor \lnot b) \land (\lnot a \lor b) \land (\lnot a \lor \lnot b) \land (a
27
27
The oriented graph will contain the following vertices and edges:
28
28
29
29
$$\begin{array}{cccc}
30
-
\lnot a \Rightarrow \lnot b & a \Rightarrow b & a \Rightarrow \lnot b & \lnot a \Rightarrow \lnot c\\\\
31
-
b \Rightarrow a & \lnot b \Rightarrow \lnot a & b \Rightarrow \lnot a & c \Rightarrow a\\\\
30
+
\lnot a \Rightarrow \lnot b & a \Rightarrow b & a \Rightarrow \lnot b & \lnot a \Rightarrow \lnot c\\
31
+
b \Rightarrow a & \lnot b \Rightarrow \lnot a & b \Rightarrow \lnot a & c \Rightarrow a
32
32
\end{array}$$
33
33
34
34
You can see the implication graph in the following image:
@@ -92,19 +92,19 @@ In the second traversal of the graph Kosaraju's algorithm visits the strongly co
92
92
Afterwards we can choose the assignment of $x$ by comparing $\text{comp}[x]$ and $\text{comp}[\lnot x]$.
93
93
If $\text{comp}[x] = \text{comp}[\lnot x]$ we return $\text{false}$ to indicate that there doesn't exist a valid assignment that satisfies the 2-SAT problem.
94
94
95
-
Below is the implementation of the solution of the 2-SAT problem for the already constructed graph of implication $g$ and the transpose graph $g^{\intercal}$ (in which the direction of each edge is reversed).
95
+
Below is the implementation of the solution of the 2-SAT problem for the already constructed graph of implication $adj$ and the transpose graph $adj^{\intercal}$ (in which the direction of each edge is reversed).
96
96
In the graph the vertices with indices $2k$ and $2k+1$ are the two vertices corresponding to variable $k$ with $2k+1$ corresponding to the negated variable.
97
97
98
-
```cpp
98
+
```{.cpp file=2sat}
99
99
int n;
100
-
vector<vector<int>> g, gt;
100
+
vector<vector<int>> adj, adj_t;
101
101
vector<bool> used;
102
102
vector<int> order, comp;
103
103
vector<bool> assignment;
104
104
105
105
voiddfs1(int v) {
106
106
used[v] = true;
107
-
for (int u : g[v]) {
107
+
for (int u : adj[v]) {
108
108
if (!used[u])
109
109
dfs1(u);
110
110
}
@@ -113,7 +113,7 @@ void dfs1(int v) {
113
113
114
114
void dfs2(int v, int cl) {
115
115
comp[v] = cl;
116
-
for (int u : gt[v]) {
116
+
for (int u : adj_t[v]) {
117
117
if (comp[u] == -1)
118
118
dfs2(u, cl);
119
119
}
@@ -143,20 +143,21 @@ bool solve_2SAT() {
143
143
return true;
144
144
}
145
145
146
-
void add_implication(int a, bool na, int b, bool nb) {
146
+
void add_disjunction(int a, bool na, int b, bool nb) {
147
147
// na and nb signify whether a and b are to be negated
0 commit comments