/
Graph.hpp
1046 lines (864 loc) · 27.7 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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef CS207_GRAPH_HPP
#define CS207_GRAPH_HPP
/** @file Graph.hpp
* @brief An undirected graph type
*/
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include "CS207/Util.hpp"
#include "Point.hpp"
/** @class Graph
* @brief A template for 3D undirected graphs.
*
* RI(graph): All i in [0, i2u_nodes_.size) then i == internal_nodes_[i2u_nodes_[i]].idx
*
* Users can add and retrieve nodes and edges. Edges are unique (there is at
* most one edge between any pair of distinct nodes).
*/
template <typename V, typename E>
class Graph {
public:
/////////////////////////////
// PUBLIC TYPE DEFINITIONS //
/////////////////////////////
/** Type of this graph. */
typedef Graph graph_type;
/** Predeclaration of Node type. */
class Node;
/** Synonym for Node (following STL conventions). */
typedef Node node_type;
/** Predeclaration of Edge type. */
class Edge;
/** Synonym for Edge (following STL conventions). */
typedef Edge edge_type;
/** Synonym for Point */
typedef Point point_type;
/** Type of indexes and sizes.
Return type of Graph::Node::index(), Graph::num_nodes(),
Graph::num_edges(), and argument type of Graph::node(size_type) */
typedef unsigned size_type;
/** Type value for idexes */
typedef unsigned idx_type;
/** Type value for nodes custom data */
typedef V node_value_type;
/** Type value for edges custom data */
typedef E edge_value_type;
/** Type of node iterators, which iterate over all graph nodes. */
class NodeIterator;
/** Synonym for NodeIterator */
typedef NodeIterator node_iterator;
/** Type of edge iterators, which iterate over all graph edges. */
class EdgeIterator;
/** Synonym for EdgeIterator */
typedef EdgeIterator edge_iterator;
/** Type of incident iterators, which iterate incident edges to a node. */
class IncidentIterator;
/** Synonym for IncidentIterator */
typedef IncidentIterator incident_iterator;
/** @struct InternalNode */
struct InternalNode
{
point_type point;
node_value_type value;
idx_type idx;
InternalNode(Point point, node_value_type value, idx_type idx) : point(point), value(value), idx(idx) {
}
};
/** @struct InternalEdge */
struct InternalEdge
{
size_type uid2;
edge_value_type value;
idx_type idx;
InternalEdge(size_type uid2, edge_value_type value, idx_type idx) : uid2(uid2), value(value), idx(idx) {
}
};
/** Type of InternalNode */
typedef InternalNode internal_node;
/** Type of InternalEdge */
typedef InternalEdge internal_edge;
/** Inner vector of internal edges for the adjacency list. */
typedef std::vector<internal_edge> adj_list_edges;
/** Inner vector of valid edges adjacency list. */
typedef std::vector<size_type> adj_list_valid_edges;
////////////////////////////////
// CONSTRUCTOR AND DESTRUCTOR //
////////////////////////////////
/** Construct an empty graph. */
Graph(){
}
/** Default destructor */
~Graph() = default;
/////////////
// General //
/////////////
/** Return the number of nodes in the graph.
*
* Complexity: O(1).
*/
size_type size() const {
return i2u_nodes_.size();
}
/** Remove all nodes and edges from this graph.
* @post num_nodes() == 0 && num_edges() == 0
*
* Invalidates all outstanding Node and Edge objects.
*/
void clear() {
std::cout << "Graph.clean()" << std::endl;
internal_nodes_.clear();
internal_edges_.clear();
i2u_nodes_.clear();
i2u_edges_.clear();
num_edges_ = 0;
assert(num_nodes() == 0);
assert(num_edges() == 0);
}
/////////////////
// GRAPH NODES //
/////////////////
/** @class Graph::Node
* @brief Class representing the graph's nodes.
*
* RI(node): uid_ == i2u_nodes_[ internal_nodes_[uid_].idx ]
* uid_ < internal_nodes_.size()
* internal_nodes_[uid_].idx < i2u_nodes_.size()
* uid >= 0
*
* Node objects are used to access information about the Graph's nodes.
*/
class Node : private totally_ordered<Node> {
public:
/** Construct an invalid node.
*
* Valid nodes are obtained from the Graph class, but it
* is occasionally useful to declare an @i invalid node, and assign a
* valid node to it later. For example:
*
* @code
* Graph::node_type x;
* if (...should pick the first node...)
* x = graph.node(0);
* else
* x = some other node using a complicated calculation
* do_something(x);
* @endcode
*/
Node() {
}
/** Return this node's position. */
const Point& position() const {
assert(valid());
return g_->internal_nodes_[uid_].point;
}
/** Return this node's position. */
Point& position() {
assert(valid());
return g_->internal_nodes_[uid_].point;
}
/** Return this node's index, a number in the range [0, graph_size). */
size_type index() const {
assert(valid());
return g_->internal_nodes_[uid_].idx;
}
/** Test whether this node and @a x are equal.
*
* Equal nodes have the same graph and the same index.
*/
bool operator==(const Node& x) const {
return std::tie(uid_, g_) == std::tie(x.uid_, x.g_);
}
/** Test whether this node is less than @a x in the global order.
*
* This ordering function is useful for STL containers such as
* std::map<>. It need not have any geometric meaning.
*
* The node ordering relation must obey trichotomy: For any two nodes x
* and y, exactly one of x == y, x < y, and y < x is true.
*/
bool operator<(const Node& x) const {
return std::tie(uid_, g_) < std::tie(x.uid_, x.g_);
}
/**
* Returns a reference to this node's value of type V.
*
* @return Object of type V by reference
*/
node_value_type& value() {
assert(g_->internal_nodes_.size() > uid_);
return g_->internal_nodes_[uid_].value;
}
/**
* Returns a reference to this node's value of type V as a constant.
*
* @return Object of type V by reference as a constant.
*/
const node_value_type& value() const {
assert(g_->internal_nodes_.size() > uid_);
return g_->internal_nodes_[uid_].value;
}
/**
* Set value of type node_value_type for this node
* @param v node_value_type
*/
void value(node_value_type v){
g_->internal_nodes_[uid_].value = v;
}
/**
* Return the degree. The number of edges incident to this node
* @return size_type
*/
size_type degree() const {
return g_->i2u_edges_[index()].size();
}
/**
* Returns incident_iterator poiting to the first element.
* @return incident_iterator
*/
incident_iterator edge_begin() const {
return IncidentIterator(g_, uid_, 0);
}
/**
* Returns incident_iterator poiting to one elemnt past the last valid element.
* @return incident_iterator
*/
incident_iterator edge_end() const {
return IncidentIterator(g_, uid_, g_->i2u_edges_[index()].size());
}
private:
// Reference to the Graph object
Graph* g_;
// This element's unique identification number
size_type uid_;
Node(const Graph* g, size_type uid)
: g_(const_cast<Graph*>(g)), uid_(uid) {
assert(g_ != nullptr);
}
bool valid() const {
return uid_ >= 0 && uid_ < g_->internal_nodes_.size()
&& g_->internal_nodes_[uid_].idx < g_->i2u_nodes_.size()
&& g_->i2u_nodes_[g_->internal_nodes_[uid_].idx] == uid_;
}
// Allow Graph to access Node's private member data and functions.
friend class Graph;
};
/** Synonym for size(). */
size_type num_nodes() const {
return size();
}
/** Add a node to the graph, returning the added node.
* @param[in] position The new node's position
* @post new size() == old size() + 1
* @post result_node.index() == old size()
*
* Complexity: O(1) amortized operations.
*/
Node add_node(const Point& position, const node_value_type& value = node_value_type()) {
idx_type idx = i2u_nodes_.size();
size_type uid = internal_nodes_.size();
// Create node and store it in the points vector.
internal_nodes_.push_back( InternalNode(position, value, idx) );
// Add the uid to the vector of valid nodes.
i2u_nodes_.push_back(uid);
// Push back another adjacency list for this node
internal_edges_.push_back({});
i2u_edges_.push_back({});
assert(internal_edges_.size() == internal_nodes_.size());
return Node(this, uid);
}
/** Remove a node from the graph.
* @param[in] n Node to be removed
* @return 1 if old has_node(n), 0 otherwise
*
* @post new size() == old size() - result.
*
* Can invalidate outstanding iterators.
* If old has_node(@a n), then @a n becomes invalid, as do any
* other Node objects equal to @a n. All other Node objects remain valid.
*
* Complexity: O(i2u_edges_[i].size()^2)
*/
size_type remove_node ( const Node & n){
if( !has_node(n) )
return 0;
idx_type idx = n.index();
size_type uid = i2u_nodes_[idx];
assert( internal_nodes_[ i2u_nodes_[idx] ].idx == idx );
// Remove all incident edges before the node is removed.
idx_type x = 0;
adj_list_valid_edges v = i2u_edges_[idx];
while(x < v.size()){
// uid of each element in v
size_type v_uid = v[x];
adj_list_valid_edges adj = i2u_edges_[ internal_nodes_[v_uid].idx ];
for(size_type adj_uid : adj)
if(adj_uid == uid){
remove_edge(Node(this, v_uid), Node(this, adj_uid));
}
++x;
}
// Remove the vector from i2i_edges, so i2u_nodes and i2i_edges are in sync by idx
i2u_edges_.erase(i2u_edges_.begin() + idx);
// Remove the uid from the list of valid nodes
i2u_nodes_.erase(i2u_nodes_.begin() + idx);
// Update the idxs for all nodes subsequent to the removed node
x = idx;
while(x < i2u_nodes_.size()){
internal_nodes_[ i2u_nodes_[x] ].idx = x;
++x;
}
return 1;
}
/** Remove a node from the graph.
* @param[in] n NodeIterator pointing to the node to be removed
* @return NodeIterator
*
* @post new size() == old size() - result.
*
* Can invalidate outstanding iterators.
* If old has_node(@a n), then @a n becomes invalid, as do any
* other Node objects equal to @a n. All other Node objects remain valid.
*
* Complexity: O(i2u_edges_[i].size()^2)
*/
node_iterator remove_node ( node_iterator n_it ){
remove_node(*n_it);
return *this;
}
/** Determine if this Node belongs to this Graph
* @return True if @a n is currently a Node of this Graph
*
* Complexity: O(1).
*/
bool has_node(const Node& n) const {
if(this != n.g_)
return false;
return n.uid_ == i2u_nodes_[n.index()];
}
/** Return the node with index @a i.
* @pre 0 <= @a i < num_nodes()
* @post result_node.index() == i
*
* Complexity: O(1).
*/
Node node(size_type i) const {
assert(i >= 0 && i < num_nodes());
return Node(this, i2u_nodes_[i]);
}
/////////////////
// GRAPH EDGES //
/////////////////
/** @class Graph::Edge
* @brief Class representing the graph's edges.
*
* Edges are order-insensitive pairs of nodes. Two Edges with the same nodes
* are considered equal if they connect the same nodes, in either order.
*/
class Edge : private totally_ordered<Edge> {
public:
/** Construct an invalid Edge. */
Edge() {
}
/** Return a node of this Edge */
Node node1() const {
return Node(g_, node1_uid);
}
/** Return the other node of this Edge */
Node node2() const {
return Node(g_, node2_uid);
}
/** Test whether this edge and @a x are equal.
*
* Equal edges are from the same graph and have the same nodes.
*/
bool operator==(const Edge& x) const {
return std::tie(g_, node1_uid, node2_uid) == std::tie(x.g_, x.node1_uid, x.node2_uid);
}
/** Test whether this edge is less than @a x in the global order.
*
* This ordering function is useful for STL containers such as
* std::map<>. It need not have any geometric meaning.
*
* The edge ordering relation must obey trichotomy: For any two edges x
* and y, exactly one of x == y, x < y, and y < x is true.
*/
bool operator<(const Edge& x) const {
return std::tie(g_, node1_uid, node2_uid) < std::tie(x.g_, x.node1_uid, x.node2_uid);
}
double length () const {
return norm(node1().position() - node2().position());
}
/**
* Returns a reference to this edge's value of type E.
*
* @return Object of type E by reference
*/
edge_value_type& value() {
assert(g_->internal_nodes_.size() > node1_uid);
size_type min_uid = std::min( node1_uid, node2_uid );
size_type max_uid = std::max( node1_uid, node2_uid );
adj_list_edges adjl = g_->internal_edges_[min_uid];
int z = 0;
for(internal_edge ie : adjl){
if(ie.uid2 == max_uid){
return g_->internal_edges_[min_uid][z].value;
}
++z;
}
assert(false);
}
/**
* Returns a reference to this edge's value of type E as a constant.
*
* @return Object of type E by reference as a constant.
*/
const edge_value_type& value() const {
size_type min_uid = std::min( node1_uid, node2_uid );
size_type max_uid = std::max( node1_uid, node2_uid );
adj_list_edges adjl = g_->internal_edges_[min_uid];
for(internal_edge ie : adjl)
if(ie.uid2 == max_uid)
return ie.value;
assert(false);
}
private:
Edge(const Graph* g, size_type n1, size_type n2)
: g_(const_cast<Graph*>(g)), node1_uid(n1), node2_uid(n2) {
}
// Reference to the graph object
Graph* g_;
// Node1's uid
size_type node1_uid;
// Node2's uid
size_type node2_uid;
// Allow Graph to access Edge's private member data and functions.
friend class Graph;
};
/** Return the total number of edges in the graph.
*
* Complexity: No more than O(num_nodes() + num_edges()), hopefully less
*/
size_type num_edges() const {
return num_edges_;
}
/** Add an edge to the graph, or return the current edge if it already exists.
* @pre @a a and @a b are distinct valid nodes of this graph
* @return an Edge object e with e.node1() == @a a and e.node2() == @a b
* @post has_edge(@a a, @a b) == true
* @post If old has_edge(@a a, @a b), new num_edges() == old num_edges().
* Else, new num_edges() == old num_edges() + 1.
*
* Can invalidate edge indexes -- in other words, old edge(@a i) might not
* equal new edge(@a i). Must not invalidate outstanding Edge objects.
*
* Complexity: No more than O(num_nodes() + num_edges()), hopefully less
*/
Edge add_edge(const Node& a, const Node& b, const edge_value_type& value = edge_value_type()) {
// Nodes a and b must be different by precondition.
assert( !(a == b) );
assert( a.index() != b.index() );
size_type n1_uid = i2u_nodes_[a.index()];
size_type n2_uid = i2u_nodes_[b.index()];
if(has_edge(a, b))
return Edge (this, n1_uid, n2_uid);
// Insert node b uid in the node a inner vector
internal_edges_[a.index()].push_back( internal_edge(b.index(), value, i2u_edges_[a.index()].size()) );
// Insert node a uid in the node b inner vector
internal_edges_[b.index()].push_back( internal_edge(a.index(), value, i2u_edges_[b.index()].size()) );
i2u_edges_[a.index()].push_back(n2_uid);
i2u_edges_[b.index()].push_back(n1_uid);
assert( a.index() < i2u_nodes_.size() );
assert( b.index() < i2u_nodes_.size() );
// keep track of number of edges
++num_edges_;
return Edge (this, n1_uid, n2_uid);
}
/** Remove an edge from the graph, or return 1 if removed, 0 otherwise.
* Updates idx of remaining edges.
*
* @pre @a a and @a b are distinct valid nodes of this graph and confirm an edge
* @pre @a a.index() < valid_edges.size
* @pre @a b.index() < valid_edges.size
*
* @return an int, 1 if removed, 0 otherwise.
* @post has_edge(@a a, @a b) == false
* @post If old !has_edge(@a a, @a b), new num_edges() == old num_edges().
* Else, new num_edges() + 1 == old num_edges().
*
*
* Complexity: O(i2u_edges_[a.index()].size() + i2u_edges_[b.index()].size())
*/
size_type remove_edge ( const Node& a, const Node& b){
if( !has_edge(a, b) )
return 0;
size_type n1_uid = i2u_nodes_[a.index()];
size_type n2_uid = i2u_nodes_[b.index()];
// Remove the uid from the list of valid adjacent nodes
assert( i2u_edges_.size() > a.index() );
assert( i2u_edges_.size() > b.index() );
idx_type x = 0;
while(x < i2u_edges_[a.index()].size()){
if(i2u_edges_[a.index()][x] == n2_uid){
i2u_edges_[a.index()].erase( i2u_edges_[a.index()].begin() + x);
--num_edges_;
break;
}
++x;
}
x = 0;
while(x < i2u_edges_[b.index()].size()){
if(i2u_edges_[b.index()][x] == n1_uid){
i2u_edges_[b.index()].erase( i2u_edges_[b.index()].begin() + x);
break;
}
++x;
}
//Update idxs
x = a.index();
while(x < i2u_edges_[a.index()].size()){
internal_edges_[n1_uid][x].idx = x;
++x;
}
//Update idxs
x = b.index();
while(x < i2u_edges_[b.index()].size()){
internal_edges_[n2_uid][x].idx = x;
++x;
}
return 1;
}
size_type remove_edge ( const Edge& e){
return remove_edge(e.node1(), e.node2());
}
edge_iterator remove_edge ( edge_iterator e_it ){
remove_edge(*e_it);
return *this;
}
/** Test whether two nodes are connected by an edge.
* @pre @a a and @a b are valid nodes of this graph
* @return true if, for some @a i, edge(@a i) connects @a a and @a b.
*
* Complexity: No more than O(num_nodes() + num_edges()), hopefully less
*/
bool has_edge(const Node& a, const Node& b) const {
assert(i2u_edges_.size() > a.index());
assert(i2u_edges_.size() > b.index());
//size_type n1_uid = i2u_nodes_[a.index()];
size_type n2_uid = i2u_nodes_[b.index()];
adj_list_valid_edges adj_edges = i2u_edges_[a.index()];
// Check if the edge already exists, by testing if node b is present.
for(size_type uid2 : adj_edges)
if( uid2 == n2_uid ){
//std::cout << "has_edge TRUE " << a.index() << " , " << b.index() << std::endl;
return true;
}
return false;
}
/** Return the edge with index @a i.
* @pre 0 <= @a i < num_edges()
*
* Complexity: No more than O(num_nodes() + num_edges()), hopefully less
*/
Edge edge(size_type i) const {
// Make sure i is valid.
assert(i <= num_edges_);
// https://piazza.com/class/hyf4iomlwgj542?cid=124
edge_iterator it = edge_begin();
for ( ; i != 0; --i)
++it;
return *it;
}
///////////////
// Iterators //
///////////////
/** @class Graph::NodeIterator
* @brief Iterator class for nodes. A forward iterator. */
class NodeIterator : private totally_ordered<NodeIterator> {
public:
// These type definitions help us use STL's iterator_traits.
/** Element type. */
typedef Node value_type;
/** Type of pointers to elements. */
typedef Node* pointer;
/** Type of references to elements. */
typedef Node& reference;
/** Iterator category. */
typedef std::input_iterator_tag iterator_category;
/** Difference between iterators */
typedef std::ptrdiff_t difference_type;
/** Construct an invalid NodeIterator. */
NodeIterator() {
}
/**
* Reference operator for NodeIterator.
* Complexity: O(1).
*
* @Return Node object.
*/
Node operator*() const {
return Node(g_, g_->i2u_nodes_[idx_]);
}
/**
* Incremental Operator for NodeIterator.
* Complexity: O(1).
*
* @Return NodeIterator object by reference.
*/
NodeIterator& operator++() {
++idx_;
return *this;
}
/**
* Equialy Operator for NodeIterator.
* Complexity: O(1).
*
* @Return bool, true if both NodeIterator's are equial.
*/
bool operator==(const NodeIterator& other) const {
return std::tie(g_, idx_) == std::tie(other.g_, other.idx_);
}
private:
NodeIterator(const Graph* g, idx_type idx)
: g_(const_cast<Graph*>(g)), idx_(idx) {
}
/** Reference to the graph */
Graph* g_;
/** Node idx */
idx_type idx_;
friend class Graph;
};
/**
* Return a node_iterator pointing to the begining
* Complexity: O(1).
*
* @return NodeIterator
*/
node_iterator node_begin() const {
return NodeIterator( this, 0 );
}
/**
* Return a node_iterator pointing to one pass the last valid position.
* Complexity: O(1).
*
* @return NodeIterator
*/
node_iterator node_end() const {
return NodeIterator( this, i2u_nodes_.size() );
}
/** @class Graph::EdgeIterator
* @brief Iterator class for edges. A forward iterator. */
class EdgeIterator : private totally_ordered<EdgeIterator> {
public:
// These type definitions help us use STL's iterator_traits.
/** Element type. */
typedef Edge value_type;
/** Type of pointers to elements. */
typedef Edge* pointer;
/** Type of references to elements. */
typedef Edge& reference;
/** Iterator category. */
typedef std::input_iterator_tag iterator_category;
/** Difference between iterators */
typedef std::ptrdiff_t difference_type;
/** Construct an invalid EdgeIterator. */
EdgeIterator() {
}
EdgeIterator(const Graph* g, size_type idx1_, size_type idx2_)
: g_(const_cast<Graph*>(g)), idx1_(idx1_), idx2_(idx2_) {
assert(g_ != nullptr);
assert(g_->internal_edges_.size() > 0);
assert(g_->i2u_edges_.size() >= idx1_);
}
/**
* Reference operator for Edge.
* Complexity: O(1).
*
* @Return Edge object.
*/
Edge operator*() const {
return Edge(g_, g_->i2u_nodes_[idx1_], g_->i2u_edges_[idx1_][idx2_]);
}
/**
* Incremental operator. It will get to the next edge.
*
* @Return edge_iterator object.
*/
edge_iterator& operator++() {
go_to_next();
return *this;
}
/**
* Compare Edge Iterators.
* Complexity: O(1).
*
* @return bool if both iterators are equal.
*/
bool operator==(const edge_iterator& eit) const {
return (idx1_ == eit.idx1_ && idx2_ == eit.idx2_ ) || (idx1_ == eit.idx1_ && idx2_ == eit.idx2_ );
}
private:
friend class Graph;
void go_to_next() {
++idx2_;
if(idx2_ < g_->i2u_edges_[idx1_].size())
return;
while(idx1_ < g_->num_nodes()){
while(idx2_ < g_->i2u_edges_[idx1_].size()){
if(idx2_ < g_->i2u_edges_[idx1_].size())
return;
++idx2_;
}
// the end of the horzontal vector, move one down
++idx1_;
idx2_ = 0;
}
}
/** Reference to the graph */
Graph* g_;
/** Node1 index */
size_type idx1_;
/** Node2 index */
size_type idx2_;
};
/**
* Return a edge_iterator pointing to the begining
* Complexity: O(1).
*
* @return EdgeIterator
*/
edge_iterator edge_begin() const {
return EdgeIterator( this, 0, 0);
}
/**
* Return a edge_iterator pointing to one pass the last valid position.
* Complexity: O(1).
*
* @return EdgeIterator
*/
edge_iterator edge_end() const {
return EdgeIterator ( this, num_nodes(), 0);
}
void echo(){
// print internal_edges_
std::cout << ".........................." << std::endl;
std::cout << "internal_nodes:" << std::endl;
int z = 0;
for(internal_node v : internal_nodes_){
std::cout << "uid: " << z << " -> idx: " << v.idx << " - Pos: " << v.point << std::endl;
++z;
}
std::cout << ".........................." << std::endl << std::endl;
std::cout << "i2u_nodes:" << std::endl;
z = 0;
for(size_type v : i2u_nodes_){
std::cout << "idx: " << z << "-> uid: " << v << std::endl;
++z;
}
std::cout << ".........................." << std::endl << std::endl;
std::cout << "internal_edges_ " << std::endl;
z = 0;
for(adj_list_edges adj_edges : internal_edges_){
std::cout << "uid: " << z << " -> ";
for(internal_edge v : adj_edges){
std::cout << " [uid2: " << v.uid2 << " idx: " << v.idx << "] ";
}
++z;
std::cout <<std::endl ;
}
std::cout << ".........................." << std::endl << std::endl;
std::cout << "i2u_edges_: " << i2u_edges_.size() << std::endl;
z = 0;
for(adj_list_valid_edges vv : i2u_edges_){
std::cout << "idx: " << z << " -> ";
for(size_type v : vv){
std::cout << " [uid2: " << v << "]";
}
++z;
std::cout <<std::endl ;
}
std::cout << ".........................." << std::endl << std::endl;
}
/** @class Graph::IncidentIterator
* @brief Iterator class for edges incident to a node. A forward iterator. */
class IncidentIterator : private totally_ordered<IncidentIterator> {
/** Reference to the graph */
Graph* g_;
/** Node uid */
size_type uid_;
/** Inner vector iterator */
size_type idx2_;
public:
// These type definitions help us use STL's iterator_traits.
/** Element type. */
typedef Edge value_type;
/** Type of pointers to elements. */
typedef Edge* pointer;
/** Type of references to elements. */
typedef Edge& reference;
/** Iterator category. */
typedef std::input_iterator_tag iterator_category;
/** Difference between iterators */
typedef std::ptrdiff_t difference_type;
/** Construct an invalid IncidentIterator. */
IncidentIterator(const Graph* g, size_type uid, size_type idx2)
: g_(const_cast<Graph*>(g)), uid_(uid), idx2_(idx2) {
assert(g_ != nullptr);
assert(g_->i2u_edges_.size() > 0);
assert(g_->internal_edges_.size() > uid_);
assert(g_->i2u_edges_[ g_->internal_nodes_[uid_].idx ].size() >= idx2_);
}
/**
* Reference operator for Edge.
* Complexity: O(1).
*
* @return Edge object.
*/
Edge operator*() const {
// get idx for uid
idx_type i = g_->internal_nodes_[uid_].idx;
assert(idx2_ < g_->i2u_edges_[i].size());
return Edge(g_, uid_, g_->i2u_edges_[i][idx2_]);
}
/**
* Incremental operator. It will get to the next edge.
*
* @return incident_iterator object.
*/