-
Notifications
You must be signed in to change notification settings - Fork 3
/
graph.cpp
executable file
·206 lines (173 loc) · 3.73 KB
/
graph.cpp
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
#include "graph.hpp"
#include <cassert>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <string>
namespace DHBW
{
//////////////////////////////////////
/// class Vertex
EdgeId Vertex::num_edges() const
{
return _edges.size();
}
EdgeId const & Vertex::edge(EdgeId id) const
{
assert(id < num_edges());
return _edges[id];
}
VertexId Vertex::id() const
{
return _id;
}
Vertex::Vertex(VertexId id_, Graph const & graph_)
:
_id(id_),
_graph(graph_)
{}
void Vertex::add_edge(EdgeId id)
{
assert(_graph.edge(id).vertex1() == this->id() or _graph.edge(id).vertex2() == this->id());
_edges.push_back(id);
}
//////////////////////////////////////
/// class Edge
VertexId const & Edge::vertex1() const
{
return _ep1;
}
VertexId const & Edge::vertex2() const
{
return _ep2;
}
VertexId const & Edge::other_vertex(VertexId v) const
{
assert(is_adjiazent(v));
if (v == vertex1())
{
return vertex2();
}
else
{
return vertex1();
}
}
bool Edge::is_adjiazent(VertexId id) const
{
return (id == _ep1 or id == _ep2);
}
Edge::Edge(EdgeId id_, VertexId ep1_, VertexId ep2_, Graph const & graph_)
:
_id(id_),
_ep1(ep1_),
_ep2(ep2_),
_graph(graph_)
{
assert(_id >= 0);
assert(_ep1 < _graph.num_vertices() and _ep2 < _graph.num_vertices());
}
//////////////////////////////////////
/// class Graph
Vertex const & Graph::vertex(VertexId id) const
{
assert(id < num_vertices());
return _vertices[id];
}
Vertex & Graph::vertex(VertexId id)
{
return const_cast<Vertex&>(static_cast<Graph const*>(this)->vertex(id));
}
Edge const & Graph::edge(EdgeId id) const
{
assert(id < num_edges());
return _edges[id];
}
Edge & Graph::edge(EdgeId id)
{
return const_cast<Edge&>(static_cast<Graph const*>(this)->edge(id));
}
VertexId Graph::num_vertices() const
{
return _vertices.size();
}
EdgeId Graph::num_edges() const
{
return _edges.size();
}
VertexId Graph::add_vertex()
{
_vertices.emplace_back(num_vertices(),*this);
return num_vertices()-1;
}
EdgeId Graph::add_edge(VertexId ep1, VertexId ep2)
{
_edges.emplace_back(num_edges(),ep1,ep2,*this);
vertex(ep1).add_edge(num_edges()-1);
vertex(ep2).add_edge(num_edges()-1);
return num_edges()-1;
}
Graph::Graph()
:
_vertices(),
_edges()
{}
Graph::Graph(std::string const & filename,GraphInputFormat format)
:
_vertices(),
_edges()
{
switch (format)
{
case GraphInputFormat::plain: read_from_file_plain(filename); break;
}
}
void Graph::read_from_file_plain(std::string const & filename)
{
assert(_vertices.empty() and _edges.empty());
std::ifstream infile(filename.c_str());
assert(infile.good());
std::string line;
//Read first line, i.e. the number of vertices
size_t num_vertices;
{
std::getline(infile, line);
std::istringstream iss(line);
iss >> num_vertices;
assert(not iss.fail());
}
// Add vertices
_vertices.reserve(num_vertices);
for(size_t i = 0; i < num_vertices; ++i)
{
add_vertex();
}
// Read and add edges
while(std::getline(infile,line))
{
size_t a,b;
std::istringstream iss(line);
iss >> a >> b;
assert(not iss.fail());
add_edge(VertexId(a),VertexId(b));
}
}
void Graph::write_to_file(std::string const & filename, GraphInputFormat format)
{
switch (format)
{
case GraphInputFormat::plain: write_to_file_plain(filename); break;
}
}
void Graph::write_to_file_plain(std::string const & filename)
{
std::ofstream outfile(filename.c_str());
// Write cities
outfile << num_vertices();
// Write edges
for (Edge const & edge : _edges)
{
outfile << "\n" << edge.vertex1() << " " << edge.vertex2();
}
}
} // namespace DHBW