-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.hpp
665 lines (592 loc) · 18.6 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
#pragma once
#include <cstdint>
#include <utility>
#include <vector>
#include <iostream>
#include <sstream>
#include <optional>
#include <stdexcept>
#include <stack>
#include <queue>
// Uwaga! Kod powinien być odporny na błędy i każda z metod jeżeli zachodzi niebezpieczeństwo wywołania z niepoprawnymi parametrami powinna zgłaszac odpowiednie wyjątki!
// klasa reprezentująca graf skierowany oparty na MACIERZY SĄSIEDZTWA
// V - dane przechowywane przez wierzcholki
// E - dane przechowywane przez krawedzie (etykiety)
template <typename V, typename E>
class Graph
{
public:
// iterator po wierzchołkach (rosnąco po id wierzchołków)
class VerticesIterator: public std::iterator
<std::input_iterator_tag,V>
{
// ...
friend class Graph;
std::size_t index;
std::vector<V>* vecPtr;
VerticesIterator(std::size_t index,std::vector<V>* vecPtr)
:index(index), vecPtr(vecPtr)
{
}
public:
bool operator==(const VerticesIterator &vi2) const
{
return this->index==vi2.index&&this->vecPtr==vi2.vecPtr;
}
bool operator!=(const VerticesIterator &vi2) const
{
return !this->operator==(vi2);
}
VerticesIterator& operator++()
{
++this->index;
return *this;
}
VerticesIterator operator++(int)
{
VerticesIterator copy = *this;
this->operator++();
return copy;
}
operator bool()const
{
return this->index<this->vecPtr->size();
}
V& operator*() const
{
return (*this->vecPtr)[this->index];
}
V* operator->() const
{
return &this->operator*();
}
// zwraca id aktualnego wierzchołka
std::size_t id() const
{
return this->index;
}
};
// iterator po istniejących krawędziach
class EdgesIterator: public std::iterator
<std::input_iterator_tag,E>
{
friend class Graph;
EdgesIterator(std::size_t y, std::size_t x,
std::vector<std::vector<std::optional<E>>>*vecPtr)
:y(y),x(x), vecPtr(vecPtr)
{
}
void validate();
std::size_t y,x;
std::vector<std::vector<std::optional<E>>>* vecPtr;
public:
bool operator==(const EdgesIterator &ei) const
{
return this->x==ei.x&&this->y==ei.y&&this->vecPtr==ei.vecPtr;
}
bool operator!=(const EdgesIterator &ei) const
{
return !(this->operator==(ei));
}
EdgesIterator& operator++();
EdgesIterator operator++(int)
{
EdgesIterator copy = *this;
this->operator++();
return copy;
}
operator bool()const;
E& operator*() const
{
return (*this->vecPtr)[y][x].value();
}
E* operator->() const
{
return &this->operator*();
}
// zwraca id wierzchołka startowego
std::size_t v1id() const
{
return this->y;
}
// zwraca id wierzchołka koncowego
std::size_t v2id() const
{
return this->x;
}
};
template<bool BFS>
class FSIterator: public std::iterator
<std::input_iterator_tag,V>
{
using collection_type = typename
std::conditional<BFS,std::queue<std::size_t>,std::stack<std::size_t>>::type;
// ...
FSIterator(Graph* graphIn)
:mGraph(graphIn),
mVertexIndex(graphIn->nrOfVertices()),
mVertexIndexStart(graphIn->nrOfVertices())
{
}
FSIterator(Graph* graphIn,std::size_t vertexIndexIn)
:mGraph(graphIn),
mVertexIndex(vertexIndexIn),
mVertexIndexStart(vertexIndexIn),
mVisitedVertices(graphIn->nrOfVertices(),false)
{
this->mCollection.push(vertexIndexIn);
}
public:
bool operator==(const FSIterator& dfsi) const
{
return this->mVertexIndex==dfsi.mVertexIndex && this->mGraph == dfsi.mGraph
&&this->mVertexIndexStart==dfsi.mVertexIndexStart;
}
bool operator!=(const FSIterator& dfsi) const
{
return !(*this==dfsi);
}
FSIterator& operator++();
FSIterator operator++(int)
{
FSIterator copy = *this;
this->operator++();
return copy;
}
V& operator*() const
{
return this->mGraph->vertexData(mVertexIndex);
}
V* operator->() const
{
return &this->operator*();
}
operator bool() const
{
return this->mVertexIndex < this->mGraph->nrOfVertices();
}
private:
friend class Graph;
Graph* mGraph;
std::size_t mVertexIndex;
std::size_t mVertexIndexStart;
std::vector<bool> mVisitedVertices;
collection_type mCollection;
std::size_t mCollectionTop()const
{
if constexpr (BFS)
{
return this->mCollection.front();
}
else
{
return this->mCollection.top();
}
}
};
public:
Graph()
:mEdgesNumber(0)
{
}
Graph(const Graph<V, E> &source) = default;
Graph(Graph<V, E> &&source) = default;
Graph& operator=(const Graph<V, E> &source) = default;
Graph& operator=(Graph<V, E> &&source) = default;
~Graph() = default;
// dodaje nowy wierzchołek z danymi przyjętymi w argumencie (wierzchołek powinien posiadać kopie danych) i zwraca "VerticesIterator" na nowo utworzony wierzchołek
VerticesIterator insertVertex(const V &vertex_data);
// dodaje nową krawędź między wierzchołkami o id "vertex1_id" i "vertex2_id" i zwraca "EdgesIterator" na nowo dodaną krawędź, oraz informację o tym czy została dodana nowa krawędź, czy nie
// jeśli krawędź między podanymi wierzchołkami już istnieje to działanie funkcji zależy od ostatniego argumentu
// jeśli ostatni argument przyjmuje wartość "true" to krawędź zostaje zastąpiona, jeśli "false" to nie
std::pair<EdgesIterator, bool> insertEdge(std::size_t y, std::size_t x, const E &label = E(), bool replace = true);
std::pair<EdgesIterator, bool> insertEdge(VerticesIterator vi1, VerticesIterator vi2,
const E &label = E(), bool replace = true)
{
this->mCheckIterator(vi1);
this->mCheckIterator(vi2);
return this->insertEdge(vi1.index,vi2.index,label,replace);
}
// usuwa wierzchołek o podanym id i zwraca "VerticesIterator" na kolejny wierzchołek, lub to samo co "endVertices()" w przypadku usunięcia ostatniego wierzchołka, lub braku wierzchołka o podanym id
VerticesIterator removeVertex(std::size_t vertex_id);
VerticesIterator removeVertex(VerticesIterator vi)
{
this->mCheckIterator(vi);
return this->removeVertex(vi.index);
}
// usuwa krawedź między dwoma wierzchołkami o podanych id i zwraca "EdgesIterator" na kolejną krawędź, lub to samo co "endEdges()" w przypadku usunięcia ostatniej krawedzi, lub braku krawędzi między wierzchołkami o podanych id
EdgesIterator removeEdge(std::size_t y, std::size_t x);
EdgesIterator removeEdge(VerticesIterator vi1,VerticesIterator vi2)
{
this->mCheckEdge(vi1);
this->mCheckEdge(vi2);
return this->removeEdge(vi1.index,vi2.index);
}
EdgesIterator removeEdge(EdgesIterator ei)
{
this->mCheckIterator(ei);
return this->removeEdge(ei.x,ei.y);
}
// zwraca true jeśli istnieje krawędź między wierzchołkami o podanych id, false w przeciwnym razie
// O(1)
bool edgeExist(std::size_t y, std::size_t x) const;
bool edgeExist(VerticesIterator vi1,VerticesIterator vi2)const
{
this->mCheckIterator(vi1);
this->mCheckIterator(vi2);
return edgeExist(vi1.index,vi2.index);
}
// zwraca ilość wierzchołków w grafie
// O(1)
std::size_t nrOfVertices() const
{
return this->mVertices.size();
}
// zwraca ilość krawędzi w grafie
// O(1)
std::size_t nrOfEdges() const
{
return this->mEdgesNumber;
}
// drukuje macierz sąsiedztwa na konsoli (debug)
void printNeighborhoodMatrix() const;
// zwraca "VerticesIterator" do wierzchołka o podanym id, lub to samo co "endVertices()" w przypadku braku wierzchołka o podanym id
VerticesIterator vertex(std::size_t vertex_id)
{
return vertex_id < this->mVertices.size()
? VerticesIterator(vertex_id,&this->mVertices)
: this->endVertices();
}
// zwraca referencję do danych wierzchołka o podanym id
const V& vertexData(std::size_t vertex_id) const
{
this->mCheckVertex(vertex_id);
return this->mVertices[vertex_id];
}
// zwraca referencję do danych wierzchołka o podanym id
V& vertexData(std::size_t vertex_id)
{
this->mCheckVertex(vertex_id);
return this->mVertices[vertex_id];
}
// zwraca "EdgesIterator" do krawędzi pomiędzy wierzchołkami o podanych id, lub to samo co "endEdges()" w przypadku braku krawędzi między wierzchołkami o podanych id
EdgesIterator edge(std::size_t y, std::size_t x)
{
return this->edgeExist(y,x)
?EdgesIterator(y,x,&this->mEdges):this->endEdges();
}
// zwraca referencję do danych (etykiety) krawędzi pomiędzy wierzchołkami o podanych id
const E& edgeLabel(std::size_t y, std::size_t x) const
{
this->mCheckEdge(y,x);
return this->mEdges[y][x].value();
}
// zwraca referencję do danych (etykiety) krawędzi pomiędzy wierzchołkami o podanych id
E& edgeLabel(std::size_t y, std::size_t x)
{
this->mCheckEdge(y,x);
return this->mEdges[y][x].value();
}
VerticesIterator begin() { return beginVertices(); }
VerticesIterator end() { return endVertices(); }
// zwraca "VerticesIterator" na pierwszy wierzchołek (o najmniejszym id)
VerticesIterator beginVertices()
{
return VerticesIterator(0, &this->mVertices);
}
// zwraca "VerticesIterator" "za ostatni" wierzchołek
VerticesIterator endVertices()
{
return VerticesIterator(this->mVertices.size(),&this->mVertices);
}
// zwraca "EdgesIterator" na pierwszą krawędz
EdgesIterator beginEdges()
{
EdgesIterator it(0,0,&this->mEdges);
it.validate();
return it;
}
// zwraca "EdgesIterator" "za ostatnią" krawędz
EdgesIterator endEdges()
{
return EdgesIterator(this->mVertices.size(),0,&this->mEdges);
}
FSIterator<false> beginDFS(std::size_t vertexIndex)
{
return FSIterator<false>(this,vertexIndex);
}
FSIterator<false> endDFS()
{
return FSIterator<false>(this);
}
FSIterator<true> beginBFS(std::size_t vertexIndex)
{
return FSIterator<true>(this,vertexIndex);
}
FSIterator<true> endBFS()
{
return FSIterator<true>(this);
}
void clear();
private:
std::vector<V> mVertices;
std::vector<std::vector<std::optional<E>>> mEdges;
std::size_t mEdgesNumber;
void mCheckVertex(size_t vertex_id)const;
void mCheckEdge(size_t y, size_t x)const;
void mCheckIterator(const VerticesIterator& vi)const;
void mCheckIterator(const EdgesIterator& vi)const;
};
template<typename V, typename E>
typename Graph<V,E>::EdgesIterator& Graph<V,E>::EdgesIterator::operator++()
{
std::size_t verticesNumber = this->vecPtr->size();
do
{
++x;
if(x>=verticesNumber)
{
x=0;
++y;
}
}while(y<verticesNumber && !(*this->vecPtr)[y][x].has_value());
return *this;
}
template<typename V, typename E>
void Graph<V,E>::EdgesIterator::validate()
{
std::size_t verticesNumber = this->vecPtr->size();
while(y<verticesNumber && !(*this->vecPtr)[y][x].has_value())
{
++x;
if(x>=verticesNumber)
{
x=0;
++y;
}
}
}
template<typename V, typename E>
Graph<V,E>::EdgesIterator::operator bool()const
{
std::size_t verticesNumber = this->vecPtr->size();
if(this->x<verticesNumber&&this->y<verticesNumber)
{
return (*this->vecPtr)[y][x].has_value();
}
return false;
}
template<typename V, typename E>
typename Graph<V,E>::VerticesIterator Graph<V,E>::insertVertex(const V&vertexData)
{
size_t index = this->mVertices.size();
this->mVertices.push_back(vertexData);
size_t s1 = index+1;
this->mEdges.resize(s1);
for(std::vector<std::optional<E>>& v1: this->mEdges)
{
v1.resize(s1);
}
return VerticesIterator(s1,&this->mVertices);
}
template<typename V, typename E>
std::pair<typename Graph<V,E>::EdgesIterator, bool> Graph<V,E>::
insertEdge(std::size_t y, std::size_t x, const E &label,bool replace)
{
std::size_t verticesNumber = this->mVertices.size();
if(y<verticesNumber&&x<verticesNumber)
{
if(this->mEdges[y][x].has_value())
{
if(!replace)return std::make_pair(
EdgesIterator(y,x,&this->mEdges),false);
}
else
{
++this->mEdgesNumber;
}
this->mEdges[y][x] = label;
return std::make_pair(EdgesIterator(y,x,&this->mEdges),true);
}
return std::make_pair(this->endEdges(),false);
}
template<typename V,typename E>
typename Graph<V,E>::VerticesIterator Graph<V,E>::removeVertex(std::size_t vertex_id)
{
if(vertex_id < this->mVertices.size())
{
this->mVertices.erase(this->mVertices.begin()+vertex_id);
for(std::optional<E>& op: this->mEdges[vertex_id])
{
if(op.has_value()) --this->mEdgesNumber;
}
this->mEdges.erase(this->mEdges.begin()+vertex_id);
for(std::vector<std::optional<E>>& v1:this->mEdges)
{
if(v1[vertex_id].has_value()) --this->mEdgesNumber;
v1.erase(v1.begin()+vertex_id);
}
return VerticesIterator(vertex_id,&this->mVertices);
}
return this->endVertices();
}
template<typename V,typename E>
typename Graph<V,E>::EdgesIterator Graph<V,E>::removeEdge(std::size_t y, std::size_t x)
{
if(this->edgeExist(y,x))
{
--this->mEdgesNumber;
this->mEdges[y][x].reset();
EdgesIterator iter(y,x,&this->mEdges);
iter.validate();
return iter;
}
return this->endEdges();
}
template<typename V,typename E>
bool Graph<V,E>::edgeExist(std::size_t y, std::size_t x)const
{
if(y<this->mVertices.size()&&x<this->mVertices.size())
{
return this->mEdges[y][x].has_value();
}
return false;
}
template<typename V,typename E>
void Graph<V,E>::mCheckEdge(size_t y, size_t x)const
{
if(!this->edgeExist(y,x))
{
std::ostringstream os;
os<<"[Graph] Edge["<<y<<"]["<<x<<"] does not exist!";
throw std::runtime_error(os.str());
}
}
template<typename V,typename E>
void Graph<V,E>::clear()
{
this->mEdges.clear();
this->mVertices.clear();
this->mEdgesNumber=0;
}
template<typename V,typename E>
void Graph<V,E>::mCheckVertex(size_t vertex_id)const
{
if(vertex_id>=this->mVertices.size())
{
std::ostringstream os;
os<<"[Graph] Vertex["<<vertex_id<<"] does not exist!";
throw std::runtime_error(os.str());
}
}
template<typename V,typename E>
void Graph<V,E>::mCheckIterator(const VerticesIterator& vi)const
{
if(vi.vecPtr!=&this->mVertices)
{
throw std::runtime_error("[Graph] Incorrect iterator");
}
}
template<typename V,typename E>
void Graph<V,E>::mCheckIterator(const EdgesIterator& ei)const
{
if(ei.vecPtr != &this->mEdges)
{
throw std::runtime_error("[Graph] Incorrect iterator");
}
}
template<typename V,typename E>
void Graph<V,E>::printNeighborhoodMatrix()const
{
std::size_t x,y;
std::size_t vertices_number = this->nrOfVertices();
std::cout<<"NrOfVertices:"<<vertices_number<<std::endl;
std::cout<<"NrOfEdges:"<<mEdgesNumber<<std::endl;
{
std::size_t width = this->mEdges.size();
std::size_t height = width>0?this->mEdges[0].size():0;
if(width!=vertices_number || height!=vertices_number)
{
std::cout<<"[Incorrect matrix]"<<std::endl;
std::cout<<"Width:"<<width<<std::endl;
std::cout<<"Height:"<<height<<std::endl;
return;
}
}
if(vertices_number==0)
{
std::cout<<"+\n";
return;
}
std::cout<<"\n";
for(y=0;y<vertices_number;++y)
{
for(x=0;x<vertices_number;++x)
{
std::cout<<"+---";
}
std::cout<<"+\n";
for(x=0;x<vertices_number;++x)
{
std::cout<<"| "<<this->mEdges[y][x].has_value()<<" ";
}
std::cout<<"|\n";
}
for(x=0;x<vertices_number;++x)
{
std::cout<<"+---";
}
std::cout<<"+\n\n";
}
template<typename V,typename E>
template<bool BFS>
typename Graph<V,E>::template FSIterator<BFS>& Graph<V,E>::FSIterator<BFS>::operator++()
{
if(!this->mCollection.empty())
{
std::size_t verticesNumber = this->mGraph->nrOfVertices();
std::size_t vId = mCollectionTop();
this->mVisitedVertices[vId] = true;
this->mCollection.pop();
if constexpr (BFS)
{
for(std::size_t iter=0; iter < verticesNumber; ++iter)
{
if(!mVisitedVertices[iter])
{
if(this->mGraph->edgeExist(vId,iter))
{
this->mVisitedVertices[iter] = true;
this->mCollection.push(iter);
}
}
}
}
else
{
std::size_t iter = verticesNumber;
while(iter>0)
{
--iter;
if(!mVisitedVertices[iter])
{
if(this->mGraph->edgeExist(vId,iter))
{
this->mVisitedVertices[iter] = true;
this->mCollection.push(iter);
}
}
}
}
}
if(this->mCollection.empty())
{
this->mVertexIndex = this->mGraph->nrOfVertices();;
this->mVertexIndexStart = mVertexIndex;
}
else
{
this->mVertexIndex = mCollectionTop();
}
return *this;
}