-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigraph.rs
More file actions
145 lines (129 loc) · 4.53 KB
/
digraph.rs
File metadata and controls
145 lines (129 loc) · 4.53 KB
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
use std::collections::HashSet;
use crate::cnf::{CNFFormula, CNFLiteral};
/// Determine satisfiability of a 2-SAT problem
///
/// This function will determine the satisfiability
/// of a CNF formula with clauses of size 2, using
/// the directed graph of implications for the formula,
/// as described in <https://cp-algorithms.com/graph/2SAT.html>
pub fn digraph_2sat(formula: &CNFFormula) -> bool {
assert!(formula.clauses.iter().all(|clause| clause.literals.len() == 2));
// Number of variables (actually an upper bound)
let n = formula
.clauses
.iter()
.flat_map(|clause| clause.literals.iter())
.map(|literal| literal.variable)
.max()
.map_or_else(|| 0, |x| x + 1) as usize;
let (adj, adj_t) = build_digraph(formula, 2 * n);
// See <https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm>
let mut ordering: Vec<usize> = vec![];
let mut visited: HashSet<usize> = HashSet::new();
for vertex in 0..(2 * n) {
dfs1(&adj, vertex, &mut visited, &mut ordering);
}
let mut components = vec![None::<usize>; 2 * n];
for vertex in ordering.into_iter().rev() {
dfs2(&adj_t, vertex, vertex, &mut components);
}
return (0..n).all(|i| components[2 * i] != components[2 * i + 1]);
}
#[inline(always)]
fn compute_idx(literal: &CNFLiteral, negate: bool) -> usize {
if negate == literal.negate {
2 * literal.variable as usize
} else {
(2 * literal.variable + 1) as usize
}
}
fn build_digraph(formula: &CNFFormula, vertices: usize) -> (Vec<HashSet<usize>>, Vec<HashSet<usize>>) {
let mut adj = vec![HashSet::new(); vertices];
let mut adj_t = vec![HashSet::new(); vertices];
for clause in formula.clauses.iter() {
adj[compute_idx(&clause.literals[0], true)].insert(compute_idx(&clause.literals[1], false));
adj_t[compute_idx(&clause.literals[1], false)].insert(compute_idx(&clause.literals[0], true));
adj[compute_idx(&clause.literals[1], true)].insert(compute_idx(&clause.literals[0], false));
adj_t[compute_idx(&clause.literals[0], false)].insert(compute_idx(&clause.literals[1], true));
}
return (adj, adj_t);
}
fn dfs1(adj: &Vec<HashSet<usize>>, vertex: usize, visited: &mut HashSet<usize>, ordering: &mut Vec<usize>) {
if visited.contains(&vertex) {
return;
}
visited.insert(vertex);
for neighbour in adj[vertex].iter() {
dfs1(adj, *neighbour, visited, ordering);
}
ordering.push(vertex);
}
fn dfs2(adj_t: &Vec<HashSet<usize>>, root: usize, vertex: usize, components: &mut Vec<Option<usize>>) {
if components[vertex].is_some() {
return;
}
components[vertex] = Some(root);
for neighbour in adj_t[vertex].iter() {
dfs2(adj_t, root, *neighbour, components);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cnf::CNFClause;
use crate::rand_cnf::generate_cnf;
#[test]
fn test_simple_satisfiable() {
let formula = CNFFormula {
clauses: vec![
CNFClause {
literals: vec![
CNFLiteral {
negate: false,
variable: 0,
},
CNFLiteral {
negate: true,
variable: 1,
},
],
},
CNFClause {
literals: vec![
CNFLiteral {
negate: false,
variable: 1,
},
CNFLiteral {
negate: false,
variable: 2,
},
],
},
CNFClause {
literals: vec![
CNFLiteral {
negate: true,
variable: 2,
},
CNFLiteral {
negate: true,
variable: 0,
},
],
},
],
};
assert!(digraph_2sat(&formula));
}
#[test]
fn test_random_satisfiable() {
let formula = generate_cnf(2, 25, 0.5, Some(42));
assert!(digraph_2sat(&formula));
}
#[test]
fn test_random_unsatisfiable() {
let formula = generate_cnf(2, 25, 2., Some(42));
assert!(!digraph_2sat(&formula));
}
}