-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.hpp
226 lines (143 loc) · 4.09 KB
/
Graph.hpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#ifndef GRAPH_ADJ_HPP
#define GRAPH_ADJ_HPP
#include<vector>
#include<set>
#include<iostream>
#include<fstream>
#include<string>
#include <algorithm> // std::next_permutation, std::sort
size_t factorial(int n)
{
size_t fac = 1;
if(n >1)
for(size_t i = 2; i <=n; i++)
{
fac*= i;
}
return fac;
}
class Graph
{
public:
typedef int Vertex;
typedef std::vector<std::vector<Vertex>> Adjacency_List;
typedef std::pair<Vertex,Vertex> Edge;
typedef std::set<Edge> Edges;
Graph(); //empty graph.
Graph(size_t k_nodes); // random graph with size k. // not implemented
Graph(Edges); // construct a graph given a set of Edges. // not implemented
Graph(const char*); // contruct a graph given a file that represent the edges.
Graph(const Graph&); //not implemented
Graph& operator =(const Graph&); //not implemented
int num_vertices(){ return n_vertices_;};
int num_edges(){return n_edges_;}
const int num_vertices()const{ return n_vertices_;};
const int num_edges()const{return n_edges_;}
Adjacency_List& adlist(){ return adl_;}
const Adjacency_List& adlist()const{return adl_;};
friend std::ostream &operator<<(std::ostream& os, const Graph& g);
friend void ncol2SAT(const Graph& g,size_t ncol);
private:
Adjacency_List adl_;
size_t n_vertices_;
size_t n_edges_;
};
Graph::Graph(const char* name_file)
{
std::ifstream myfile(name_file);
std::string line;
int a;
size_t pos1,pos2, end;
const char* b="\0";
int i =0;
n_edges_ = 0;
n_vertices_ =0;
if(myfile.is_open())
{
while ( getline (myfile,line) )
{
pos1 = line.find(":")+1;
end =line.find(";");
adl_.push_back(std::vector<Vertex>());
do
{
pos2 = line.find(":", pos1);
if(pos2== std::string::npos)
pos2 =end;
std::string substring = line.substr(pos1, pos2-pos1);
adl_[i].push_back(std::stoi(substring));
pos1 = pos2+1;
n_edges_++;
}while(pos2!=end);
pos1 =0;
i++;
}
n_vertices_ =i;
}
else
{
std::cout<<"Error opening the file";
}
myfile.close();
}
std::ostream &operator<<(std::ostream& os, const Graph& g)
{
int i=0;
for(auto lista:g.adl_)
{
for(auto nodo:lista)
{
os<<"< "<<i<<" , "<<nodo<<" >"<<" ";
}
os<<'\n'<<std::endl;
i++;
}
return os;
}
void ncol2SAT(const Graph& g, size_t n_col)
{
std::ofstream myfile;
myfile.open ("dimacsformatSAT.txt");
int i=0;
int n_vertices = g.adl_.size();
int n_edges = 0;
int sub_index =0;
int vert=0;
int nodes = n_col;
int n_combinations = factorial(n_col)/(factorial(2)*factorial(n_col-2));
bool mask[nodes];
std::fill(mask,mask+nodes,false);
for(int i = nodes-1; i> nodes-1-2; i--)
mask[i] = true;
myfile << "p cnf "<<n_col*g.n_vertices_<<' '<<(1+n_combinations)*g.n_vertices_+n_col*g.n_edges_<<'\n';
for(auto lista:g.adl_)
{
myfile<<"c For each vertex "<<i<<std::endl;
for(size_t j = 1; j <= n_col; j++)
{
myfile<<j+sub_index<<' ';
}
myfile<<0<<'\n';
do
{
for(int i = 0; i < n_col; i++)
{
if(mask[i])
myfile<<-(i+1+sub_index)<<' ';
}
myfile<<0<<'\n';
} while ( std::next_permutation(mask,mask+nodes) );
myfile<<"c For each edge "<<i<<std::endl;
for(auto nodo:lista)
{
for(size_t j = 1; j <= n_col; j++)
myfile<<'-'<<(i * n_col+j)<<' '<<'-'<<nodo*n_col+j<<' '<<"0\n";
myfile<<'\n';
}
myfile<<'\n';
i++;
sub_index+=n_col;
}
myfile.close();
}
#endif