Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f9779d0

Browse files
committed
change name of add_implication to add_disjunction and g to adj
1 parent 62cbd0e commit f9779d0

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/graph/2SAT.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ $$(a \lor \lnot b) \land (\lnot a \lor b) \land (\lnot a \lor \lnot b) \land (a
2727
The oriented graph will contain the following vertices and edges:
2828

2929
$$\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
3232
\end{array}$$
3333

3434
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
9292
Afterwards we can choose the assignment of $x$ by comparing $\text{comp}[x]$ and $\text{comp}[\lnot x]$.
9393
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.
9494

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).
9696
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.
9797

98-
```cpp
98+
```{.cpp file=2sat}
9999
int n;
100-
vector<vector<int>> g, gt;
100+
vector<vector<int>> adj, adj_t;
101101
vector<bool> used;
102102
vector<int> order, comp;
103103
vector<bool> assignment;
104104

105105
void dfs1(int v) {
106106
used[v] = true;
107-
for (int u : g[v]) {
107+
for (int u : adj[v]) {
108108
if (!used[u])
109109
dfs1(u);
110110
}
@@ -113,7 +113,7 @@ void dfs1(int v) {
113113

114114
void dfs2(int v, int cl) {
115115
comp[v] = cl;
116-
for (int u : gt[v]) {
116+
for (int u : adj_t[v]) {
117117
if (comp[u] == -1)
118118
dfs2(u, cl);
119119
}
@@ -143,20 +143,21 @@ bool solve_2SAT() {
143143
return true;
144144
}
145145

146-
void add_implication(int a, bool na, int b, bool nb) {
146+
void add_disjunction(int a, bool na, int b, bool nb) {
147147
// na and nb signify whether a and b are to be negated
148-
a = 2 * a ^ na;
149-
b = 2 * b ^ nb;
150-
int neg_a = a ^ 1, neg_b = b ^ 1;
151-
g[a].push_back(b);
152-
g[neg_b].push_back(neg_a);
153-
gt[b].push_back(a);
154-
gt[neg_a].push_back(neg_b);
148+
a = 2*a ^ na;
149+
b = 2*b ^ nb;
150+
int neg_a = a ^ 1;
151+
int neg_b = b ^ 1;
152+
adj[neg_a].push_back(b);
153+
adj[neg_b].push_back(a);
154+
adj_t[b].push_back(neg_a);
155+
adj_t[a].push_back(neg_b);
155156
}
156157
```
158+
157159
## Practice Problems
158160
* [UVA: Rectangles](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3081)
159161
* [Codeforces: The Door Problem](http://codeforces.com/contest/776/problem/D)
160162
* [Codeforces : Radio Stations](https://codeforces.com/problemset/problem/1215/F)
161163
* [CSES : Giant Pizza](https://cses.fi/problemset/task/1684)
162-

0 commit comments

Comments
 (0)