Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
TwoSAT.h
Go to the documentation of this file.
1
31#pragma once
32
33#include <ogdf/basic/Graph.h>
35#include <ogdf/basic/basic.h>
37
38#include <utility>
39#include <vector>
40
41namespace ogdf {
42
43#ifdef OGDF_DEBUG
44
47 int m_val;
48
49public:
50 OGDF_DEPRECATED("uninitialized value")
51
52 twosat_var() : m_val(0) { }
53
54 explicit twosat_var(int val) : m_val(val) { }
55
56 explicit operator int() { return m_val; }
57
58 bool operator==(const twosat_var& rhs) const { return m_val == rhs.m_val; }
59
60 bool operator!=(const twosat_var& rhs) const { return m_val != rhs.m_val; }
61};
62
64
65#else
66using twosat_var = int;
68#endif
69
71class OGDF_EXPORT TwoSAT : protected Graph {
72 std::vector<bool> m_assignment;
73 std::vector<node> m_node_map;
74
75public:
76 TwoSAT() {};
77
79 node pos = newNode(), neg = newNode();
80 OGDF_ASSERT(pos->index() % 2 == 0);
81 OGDF_ASSERT(pos->index() + 1 == neg->index());
82 m_assignment.push_back(false);
83 OGDF_ASSERT(m_node_map.size() == pos->index());
84 m_node_map.push_back(pos);
85 OGDF_ASSERT(m_node_map.size() == neg->index());
86 m_node_map.push_back(neg);
87 return (twosat_var)(pos->index() / 2);
88 }
89
90 int variableCount() const {
91 OGDF_ASSERT(nodes.size() % 2 == 0);
92 return nodes.size() / 2;
93 }
94
95 int newClause(twosat_var var1, bool sign1, twosat_var var2, bool sign2) {
96 auto [p1, n1] = getNodes(var1, !sign1);
97 auto [p2, n2] = getNodes(var2, !sign2);
98 edge e1 = newEdge(n1, p2);
99 edge e2 = newEdge(n2, p1);
100 OGDF_ASSERT(e1->index() % 2 == 0);
101 OGDF_ASSERT(e1->index() + 1 == e2->index());
102 return e1->index();
103 }
104
105 int clauseCount() const {
106 OGDF_ASSERT(edges.size() % 2 == 0);
107 return edges.size() / 2;
108 }
109
110 bool solve() {
111 NodeArray<int> components(*this, -1);
112 strongComponents(*this, components);
113
114 OGDF_ASSERT(nodes.size() % 2 == 0);
115 OGDF_ASSERT(edges.size() % 2 == 0);
116 for (auto it = nodes.begin(); it != nodes.end(); it++) {
117 node pos = *(it);
118 it++;
119 node neg = *(it);
120 OGDF_ASSERT(pos->index() % 2 == 0);
121 OGDF_ASSERT(pos->index() + 1 == neg->index());
122
123 if (components[pos] == components[neg]) {
124 return false;
125 }
126 m_assignment[pos->index() / 2] = (components[pos] > components[neg]);
127 }
128 return true;
129 }
130
131 bool getAssignment(twosat_var var) const { return m_assignment[(int)var]; }
132
133 void clear() override {
134 Graph::clear();
135 m_assignment.clear();
136 }
137
138 using const_iterator = typename std::vector<bool>::const_iterator;
139 using const_reverse_iterator = typename std::vector<bool>::const_reverse_iterator;
140
141 const_iterator begin() const { return m_assignment.begin(); }
142
143 const_iterator end() const { return m_assignment.end(); }
144
145 const_reverse_iterator rbegin() const { return m_assignment.rbegin(); }
146
147 const_reverse_iterator rend() const { return m_assignment.rend(); }
148
149protected:
150 std::pair<node, node> getNodes(twosat_var var, bool flip = false) const {
151 int idx = getNodeIndex(var, true);
152 node pos = m_node_map[idx];
153 OGDF_ASSERT(pos->index() == idx);
154 OGDF_ASSERT(pos->index() / 2 == (int)var);
155 OGDF_ASSERT(pos->index() % 2 == 0);
156 node neg = m_node_map[idx + 1];
157 OGDF_ASSERT(neg->index() == idx + 1);
158 if (flip) {
159 return std::pair(neg, pos);
160 } else {
161 return std::pair(pos, neg);
162 }
163 }
164
165 node getNode(twosat_var var, bool sign) const {
166 int idx = getNodeIndex(var, sign);
167 node n = m_node_map[idx];
168 OGDF_ASSERT(n->index() == idx);
169 return n;
170 }
171
172 int getNodeIndex(twosat_var var, bool sign) const {
173 int idx = ((int)var) * 2 + (sign ? 0 : 1);
174 OGDF_ASSERT(0 <= idx);
175 OGDF_ASSERT(idx <= Graph::maxNodeIndex());
176 return idx;
177 }
178};
179
180}
Includes declaration of graph class.
Decralation of GraphElement and GraphList classes.
Basic declarations, included by all source files.
Class for the representation of edges.
Definition Graph_d.h:364
int index() const
Returns the index of the edge.
Definition Graph_d.h:396
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
Class for the representation of nodes.
Definition Graph_d.h:241
int index() const
Returns the (unique) node index.
Definition Graph_d.h:275
A simple solver for TwoSAT instances, representing the instance as implication graph and solving it v...
Definition TwoSAT.h:71
int clauseCount() const
Definition TwoSAT.h:105
bool getAssignment(twosat_var var) const
Definition TwoSAT.h:131
const_iterator end() const
Definition TwoSAT.h:143
const_iterator begin() const
Definition TwoSAT.h:141
const_reverse_iterator rend() const
Definition TwoSAT.h:147
node getNode(twosat_var var, bool sign) const
Definition TwoSAT.h:165
bool solve()
Definition TwoSAT.h:110
void clear() override
Removes all nodes and all edges from the graph.
Definition TwoSAT.h:133
typename std::vector< bool >::const_reverse_iterator const_reverse_iterator
Definition TwoSAT.h:139
int getNodeIndex(twosat_var var, bool sign) const
Definition TwoSAT.h:172
std::vector< bool > m_assignment
Definition TwoSAT.h:72
typename std::vector< bool >::const_iterator const_iterator
Definition TwoSAT.h:138
twosat_var newVariable()
Definition TwoSAT.h:78
const_reverse_iterator rbegin() const
Definition TwoSAT.h:145
int variableCount() const
Definition TwoSAT.h:90
std::vector< node > m_node_map
Definition TwoSAT.h:73
int newClause(twosat_var var1, bool sign1, twosat_var var2, bool sign2)
Definition TwoSAT.h:95
std::pair< node, node > getNodes(twosat_var var, bool flip=false) const
Definition TwoSAT.h:150
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
In debug mode, twosat_var is a class instead of a simple int to prevent unintened use of the default ...
Definition TwoSAT.h:46
bool operator!=(const twosat_var &rhs) const
Definition TwoSAT.h:60
bool operator==(const twosat_var &rhs) const
Definition TwoSAT.h:58
twosat_var(int val)
Definition TwoSAT.h:54
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
int strongComponents(const Graph &G, NodeArray< int > &component)
Computes the strongly connected components of the digraph G.
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
Definition config.h:206
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
The namespace for all OGDF objects.
const twosat_var TwoSAT_Var_Undefined(-1)
Declaration of simple graph algorithms.