-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterfaces.cpp
More file actions
263 lines (234 loc) · 6.8 KB
/
interfaces.cpp
File metadata and controls
263 lines (234 loc) · 6.8 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "interfaces.hpp"
//----------------Adjacency lists interface
AdjacencyListsInterface::AdjacencyListsInterface(const AdjacencyListsInterface& other)
{
adjLists = other.adjLists;
}
AdjacencyListsInterface& AdjacencyListsInterface::operator=(const AdjacencyListsInterface& other)
{
adjLists = other.adjLists;
return *this;
}
// O(1) (average)
bool AdjacencyListsInterface::hasEdge(Vertex::Number v1, Vertex::Number v2) const
{
auto fromVertexList = adjLists.at(v1);
bool contains = std::find(fromVertexList.begin(), fromVertexList.end(), v2) != fromVertexList.end();
return contains;
}
// adding 'count' vertices to interface
// O(1)
void AdjacencyListsInterface::addVertex(Vertex::Number count)
{
// adding new vertex into adjacency lists vector
adjLists.resize(adjLists.size() + count);
}
// O(1)
void AdjacencyListsInterface::addEdge(Vertex::Number v1, Vertex::Number v2, bool directed)
{;
adjLists[v1].insert(v2);
// for not directed edges adding second direction
if(!directed)
{
adjLists[v2].insert(v1);
}
}
// O(1)
void AdjacencyListsInterface::deleteEdge(Vertex::Number v1, Vertex::Number v2, bool directed)
{
adjLists[v1].erase(v2);
// for not directed edges erasing second direction
if(!directed)
{
adjLists[v2].erase(v1);
}
}
//O(|V| + |E|) - because we have no more iterations, that we have edges(but some vertices can not have neighboors).
void AdjacencyListsInterface::transpose()
{
// creating new adjacency lists
AdjacencyLists newAdjLists;
newAdjLists.resize(adjLists.size());
// newAdjLists[i][j] = adjLists[j][i]
for(size_t list = 0; list < adjLists.size(); ++list)
{
for(auto item: adjLists[list])
{
newAdjLists[item].insert(list);
}
}
adjLists = newAdjLists;
}
// O(|E|)
Vertex::Number AdjacencyListsInterface::inDegree(Vertex::Number v) const
{
Vertex::Number indeg = 0;
for(auto list: adjLists)
{
indeg += list.count(v);
}
return indeg;
}
// O(1)
Vertex::Number AdjacencyListsInterface::outDegree(Vertex::Number v) const
{
return adjLists[v].size();
}
// O(|E|)
Vertex::Number AdjacencyListsInterface::degree(Vertex::Number v) const
{
return inDegree(v) + outDegree(v);
}
// grabs degrees of ALL vertices
// Complexity: O(|E|).
void AdjacencyListsInterface::getDegreesList(VertexNumbers& inDegContainer, VertexNumbers& outDegContainer, VertexNumbers& degContainer) const
{
inDegContainer.resize(adjLists.size(), 0);
outDegContainer.resize(adjLists.size(), 0);
degContainer.resize(adjLists.size(), 0);
for(size_t adjList = 0; adjList < adjLists.size(); ++adjList)
{
for(auto vNum: adjLists[adjList])
{
++inDegContainer[vNum];
++outDegContainer[adjList];
++degContainer[vNum];
++degContainer[adjList];
}
}
}
// O(1) - adjacent vertex order is NOT DETERMINED
typename AdjacencyListsInterface::VertexNumbers AdjacencyListsInterface::getAdjacentVerticesFor(Vertex::Number v) const
{
std::vector<Vertex::Number> adjVector;
adjVector.reserve(adjLists[v].size());
adjVector.insert(adjVector.begin(), adjLists[v].begin(), adjLists[v].end());
return adjVector;
}
//----------------/Adjacency lists interface
//----------------Adjacency matrix interface
AdjacencyMatrixInterface::AdjacencyMatrixInterface(const AdjacencyMatrixInterface& other)
{
adjMatrix = other.adjMatrix;
}
AdjacencyMatrixInterface& AdjacencyMatrixInterface::operator=(const AdjacencyMatrixInterface& other)
{
adjMatrix = other.adjMatrix;
return *this;
}
// O(1)
bool AdjacencyMatrixInterface::hasEdge(Vertex::Number v1, Vertex::Number v2) const
{
return adjMatrix[v1][v2] != NotPresented;
}
// adding 'count' vertices to interface
// O(|V|)
void AdjacencyMatrixInterface::addVertex(Vertex::Number count)
{
// adding new vertex as new row in the matrix and as new column in each row of the matrix
adjMatrix.resize(adjMatrix.size() + count);
for(size_t i = 0; i < adjMatrix.size(); ++i)
{
adjMatrix[i].resize(adjMatrix.size(), NotPresented);
}
}
// O(1)
void AdjacencyMatrixInterface::addEdge(Vertex::Number v1, Vertex::Number v2, bool directed)
{
adjMatrix[v1][v2] = Presented;
if(!directed)
{
adjMatrix[v2][v1] = Presented;
}
}
// O(1)
void AdjacencyMatrixInterface::deleteEdge(Vertex::Number v1, Vertex::Number v2, bool directed)
{
adjMatrix[v1][v2] = NotPresented;
if(!directed)
{
adjMatrix[v2][v1] = NotPresented;
}
}
// O(|V|^2)
void AdjacencyMatrixInterface::transpose()
{
// transposing by diagonal
for(size_t i = 0; i < adjMatrix.size(); ++i)
{
for(size_t j = i; j < adjMatrix[i].size(); ++j)
{
std::swap(adjMatrix[i][j], adjMatrix[j][i]);
}
}
}
// O(|V|)
Vertex::Number AdjacencyMatrixInterface::inDegree(Vertex::Number v) const
{
Vertex::Number indeg = 0;
for(size_t row = 0; row < adjMatrix.size(); ++row)
{
if(adjMatrix[row][v] == Presented)
{
++indeg;
}
}
return indeg;
}
// O(|V|)
Vertex::Number AdjacencyMatrixInterface::outDegree(Vertex::Number v) const
{
Vertex::Number outdeg = 0;
for(size_t col = 0; col < adjMatrix[v].size(); ++col)
{
if(adjMatrix[v][col] == Presented)
{
++outdeg;
}
}
return outdeg;
}
// O(|V|)
Vertex::Number AdjacencyMatrixInterface::degree(Vertex::Number v) const
{
return inDegree(v) + outDegree(v);
}
// grabs degrees of ALL vertices
// Complexity: O(|V|^2).
// WARNING! Only has sense for adjacency lists, but as we need to provide unified functionality, also done it here.
void AdjacencyMatrixInterface::getDegreesList(VertexNumbers& inDegContainer, VertexNumbers& outDegContainer, VertexNumbers& degContainer) const
{
inDegContainer.resize(adjMatrix.size(), 0);
outDegContainer.resize(adjMatrix.size(), 0);
degContainer.resize(adjMatrix.size(), 0);
for(size_t i = 0; i < adjMatrix.size(); ++i)
{
for(size_t j = 0; j < adjMatrix.size(); ++j)
{
if(adjMatrix[i][j] == Presented)
{
++inDegContainer[j];
++outDegContainer[i];
++degContainer[i];
++degContainer[j];
}
}
}
}
// O(|V|)
// resulting vector is already sorted
typename AdjacencyMatrixInterface::VertexNumbers AdjacencyMatrixInterface::getAdjacentVerticesFor(Vertex::Number v) const
{
std::vector<Vertex::Number> adjVector;
adjVector.reserve(adjMatrix[v].size());
for(size_t j = 0; j < adjMatrix[v].size(); ++j)
{
if(adjMatrix[v][j] == Presented)
{
adjVector.push_back(j);
}
}
adjVector.shrink_to_fit();
return adjVector;
}
//----------------/Adjacency matrix interface