00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef OR_TOOLS_GRAPH_EBERT_GRAPH_H_
00015 #define OR_TOOLS_GRAPH_EBERT_GRAPH_H_
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 #include <stddef.h>
00113 #include <algorithm>
00114 #include <limits>
00115 #include <string>
00116
00117 #include "base/integral_types.h"
00118 #include "base/logging.h"
00119 #include "base/scoped_ptr.h"
00120 #include "base/stringprintf.h"
00121 #include "util/permutation.h"
00122 #include "util/zvector.h"
00123
00124 using std::string;
00125
00126 namespace operations_research {
00127
00128
00129 template<typename NodeIndexType, typename ArcIndexType> class EbertGraph;
00130 template<typename NodeIndexType, typename ArcIndexType> class ForwardEbertGraph;
00131
00132
00133
00134
00135
00136
00137
00138 typedef int32 NodeIndex;
00139 typedef int32 ArcIndex;
00140 typedef int64 FlowQuantity;
00141 typedef int64 CostValue;
00142 typedef EbertGraph<NodeIndex, ArcIndex> StarGraph;
00143 typedef ForwardEbertGraph<NodeIndex, ArcIndex> ForwardStarGraph;
00144 typedef ZVector<NodeIndex> NodeIndexArray;
00145 typedef ZVector<ArcIndex> ArcIndexArray;
00146 typedef ZVector<FlowQuantity> QuantityArray;
00147 typedef ZVector<CostValue> CostArray;
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 template<typename NodeIndexType, typename ArcIndexType,
00167 typename DerivedGraph> class EbertGraphCore {
00168 public:
00169
00170 static const NodeIndexType kNilNode;
00171
00172
00173 static const ArcIndexType kNilArc;
00174
00175
00176 static const NodeIndexType kFirstNode;
00177
00178
00179 static const ArcIndexType kFirstArc;
00180
00181
00182
00183
00184 static const NodeIndexType kMaxNumNodes;
00185
00186
00187
00188
00189 static const ArcIndexType kMaxNumArcs;
00190
00191
00192
00193
00194
00195 bool Reserve(NodeIndexType new_max_num_nodes, ArcIndexType new_max_num_arcs) {
00196 if (new_max_num_nodes < 1 ||
00197 new_max_num_nodes > kMaxNumNodes) {
00198 return false;
00199 }
00200 if (new_max_num_arcs < 1 ||
00201 new_max_num_arcs > kMaxNumArcs) {
00202 return false;
00203 }
00204 ThisAsDerived()->ReserveNextAdjacentArcAndHeadEntries(new_max_num_arcs);
00205 first_incident_arc_.Reserve(kFirstNode,
00206 new_max_num_nodes - 1);
00207 for (NodeIndexType node = max_num_nodes_;
00208 node < new_max_num_nodes; ++node) {
00209 first_incident_arc_.Set(node, kNilArc);
00210 }
00211 max_num_nodes_ = new_max_num_nodes;
00212 max_num_arcs_ = new_max_num_arcs;
00213 ThisAsDerived()->ReserveInternal(new_max_num_nodes, new_max_num_arcs);
00214 return true;
00215 }
00216
00217
00218 NodeIndexType num_nodes() const { return num_nodes_; }
00219
00220
00221
00222 ArcIndexType num_arcs() const { return num_arcs_; }
00223
00224
00225
00226
00227 NodeIndexType end_node_index() const {
00228 return kFirstNode + num_nodes_;
00229 }
00230
00231
00232
00233
00234 ArcIndexType end_arc_index() const {
00235 return kFirstArc + num_arcs_;
00236 }
00237
00238
00239 NodeIndexType max_num_nodes() const { return max_num_nodes_; }
00240
00241
00242
00243 ArcIndexType max_num_arcs() const { return max_num_arcs_; }
00244
00245
00246
00247
00248 NodeIndexType max_end_node_index() const {
00249 return kFirstNode + max_num_nodes_;
00250 }
00251
00252
00253
00254
00255 ArcIndexType max_end_arc_index() const {
00256 return kFirstArc + max_num_arcs_;
00257 }
00258
00259
00260
00261
00262
00263
00264
00265 bool IsNodeValid(NodeIndexType node) const {
00266 return node >= kFirstNode && node < max_num_nodes_;
00267 }
00268
00269
00270
00271
00272
00273
00274 ArcIndexType AddArc(NodeIndexType tail, NodeIndexType head) {
00275 if (num_arcs_ >= max_num_arcs_
00276 || !IsNodeValid(tail) || !IsNodeValid(head)) {
00277 return kNilArc;
00278 }
00279 if (tail + 1 > num_nodes_) {
00280 num_nodes_ = tail + 1;
00281 }
00282 if (head + 1 > num_nodes_) {
00283 num_nodes_ = head + 1;
00284 }
00285 ArcIndexType arc = num_arcs_;
00286 ++num_arcs_;
00287 ThisAsDerived()->RecordArc(arc, tail, head);
00288 return arc;
00289 }
00290
00291
00292
00293 #if !SWIG
00294 template <typename ArcIndexTypeStrictWeakOrderingFunctor>
00295 void GroupForwardArcsByFunctor(
00296 const ArcIndexTypeStrictWeakOrderingFunctor& compare,
00297 PermutationCycleHandler<ArcIndexType>* annotation_handler) {
00298 scoped_array<ArcIndexType>
00299 arc_permutation(new ArcIndexType[end_arc_index()]);
00300
00301
00302 for (ArcIndexType i = 0; i < end_arc_index(); ++i) {
00303
00304 arc_permutation[i] = i;
00305 }
00306 std::sort(&arc_permutation[kFirstArc],
00307 &arc_permutation[end_arc_index()],
00308 compare);
00309
00310
00311
00312 CycleHandlerForAnnotatedArcs cycle_handler(annotation_handler,
00313 ThisAsDerived());
00314 PermutationApplier<ArcIndexType> permutation(&cycle_handler);
00315 permutation.Apply(&arc_permutation[0], kFirstArc, end_arc_index());
00316
00317
00318 ThisAsDerived()->BuildRepresentation();
00319 }
00320
00321 class CycleHandlerForAnnotatedArcs :
00322 public PermutationCycleHandler<ArcIndexType> {
00323 public:
00324 CycleHandlerForAnnotatedArcs(
00325 PermutationCycleHandler<ArcIndexType>* annotation_handler,
00326 DerivedGraph* graph)
00327 : annotation_handler_(annotation_handler),
00328 graph_(graph),
00329 head_temp_(kNilNode),
00330 tail_temp_(kNilNode) { }
00331
00332 virtual void SetTempFromIndex(ArcIndexType source) {
00333 if (annotation_handler_ != NULL) {
00334 annotation_handler_->SetTempFromIndex(source);
00335 }
00336 head_temp_ = graph_->Head(source);
00337 tail_temp_ = graph_->Tail(source);
00338 }
00339
00340 virtual void SetIndexFromIndex(ArcIndexType source,
00341 ArcIndexType destination) const {
00342 if (annotation_handler_ != NULL) {
00343 annotation_handler_->SetIndexFromIndex(source, destination);
00344 }
00345 graph_->SetHead(destination, graph_->Head(source));
00346 graph_->SetTail(destination, graph_->Tail(source));
00347 }
00348
00349 virtual void SetIndexFromTemp(ArcIndexType destination) const {
00350 if (annotation_handler_ != NULL) {
00351 annotation_handler_->SetIndexFromTemp(destination);
00352 }
00353 graph_->SetHead(destination, head_temp_);
00354 graph_->SetTail(destination, tail_temp_);
00355 }
00356
00357
00358
00359
00360
00361 virtual void SetSeen(ArcIndexType* permutation_element) const {
00362 *permutation_element = kNilArc;
00363 }
00364
00365 virtual bool Unseen(ArcIndexType permutation_element) const {
00366 return permutation_element != kNilArc;
00367 }
00368
00369 virtual ~CycleHandlerForAnnotatedArcs() { }
00370
00371 private:
00372 PermutationCycleHandler<ArcIndexType>* annotation_handler_;
00373 DerivedGraph* graph_;
00374 NodeIndexType head_temp_;
00375 NodeIndexType tail_temp_;
00376 };
00377 #endif // SWIG
00378
00379
00380 class NodeIterator {
00381 public:
00382 explicit NodeIterator(const DerivedGraph& graph)
00383 : graph_(graph), head_(graph_.StartNode(kFirstNode)) {}
00384
00385
00386 bool Ok() const { return head_ != kNilNode; }
00387
00388
00389 void Next() { head_ = graph_.NextNode(head_); }
00390
00391
00392 NodeIndexType Index() const { return head_; }
00393
00394 private:
00395
00396 const DerivedGraph& graph_;
00397
00398
00399 NodeIndexType head_;
00400 };
00401
00402
00403 class ArcIterator {
00404 public:
00405 explicit ArcIterator(const DerivedGraph& graph)
00406 : graph_(graph), arc_(graph_.StartArc(kFirstArc)) {}
00407
00408
00409 bool Ok() const { return arc_ != kNilArc; }
00410
00411
00412 void Next() { arc_ = graph_.NextArc(arc_); }
00413
00414
00415 ArcIndexType Index() const { return arc_; }
00416
00417 private:
00418
00419 const DerivedGraph& graph_;
00420
00421
00422 ArcIndexType arc_;
00423 };
00424
00425
00426
00427 ArcIndexType LookUpArc(const NodeIndexType tail,
00428 const NodeIndexType head) const {
00429 for (ArcIndexType arc = FirstOutgoingArc(tail);
00430 arc != kNilArc;
00431 arc = ThisAsDerived()->NextOutgoingArc(arc)) {
00432 if (Head(arc) == head) {
00433 return arc;
00434 }
00435 }
00436 return kNilArc;
00437 }
00438
00439
00440 NodeIndexType Head(const ArcIndexType arc) const {
00441 DCHECK(ThisAsDerived()->CheckArcValidity(arc));
00442 return head_[arc];
00443 }
00444
00445 string NodeDebugString(const NodeIndexType node) const {
00446 if (node == kNilNode) {
00447 return "NilNode";
00448 } else {
00449 return StringPrintf("%lld", static_cast<int64>(node));
00450 }
00451 }
00452
00453 string ArcDebugString(const ArcIndexType arc) const {
00454 if (arc == kNilArc) {
00455 return "NilArc";
00456 } else {
00457 return StringPrintf("%lld", static_cast<int64>(arc));
00458 }
00459 }
00460
00461 protected:
00462 EbertGraphCore()
00463 : max_num_nodes_(0),
00464 max_num_arcs_(0),
00465 num_nodes_(0),
00466 num_arcs_(0),
00467 head_(),
00468 next_adjacent_arc_(),
00469 first_incident_arc_(),
00470 representation_clean_(true) {}
00471
00472 ~EbertGraphCore() {}
00473
00474 void Initialize(NodeIndexType max_num_nodes,
00475 ArcIndexType max_num_arcs) {
00476 if (!Reserve(max_num_nodes, max_num_arcs)) {
00477 LOG(DFATAL) << "Could not reserve memory for "
00478 << static_cast<int64>(max_num_nodes) << " nodes and "
00479 << static_cast<int64>(max_num_arcs) << " arcs.";
00480 }
00481 first_incident_arc_.SetAll(kNilArc);
00482 next_adjacent_arc_.SetAll(kNilArc);
00483 head_.SetAll(kNilNode);
00484 }
00485
00486
00487
00488
00489 NodeIndexType StartNode(NodeIndexType node) const {
00490 return num_nodes_ == 0 ? kNilNode : node;
00491 }
00492
00493
00494
00495 ArcIndexType StartArc(ArcIndexType arc) const {
00496 return num_arcs_ == 0 ? kNilArc : arc;
00497 }
00498
00499
00500 ArcIndexType FirstIncidentArc(const NodeIndexType node) const {
00501 DCHECK(representation_clean_);
00502 DCHECK(IsNodeValid(node));
00503 return first_incident_arc_[node];
00504 }
00505
00506
00507 ArcIndexType NextAdjacentArc(const ArcIndexType arc) const {
00508 DCHECK(representation_clean_);
00509 DCHECK(ThisAsDerived()->CheckArcValidity(arc));
00510 return next_adjacent_arc_[arc];
00511 }
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522 NodeIndexType NextNode(const NodeIndexType node) const {
00523 DCHECK(IsNodeValid(node));
00524 const NodeIndexType next_node = node + 1;
00525 return next_node < num_nodes_ ? next_node : kNilNode;
00526 }
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536 ArcIndexType NextArc(const ArcIndexType arc) const {
00537 DCHECK(ThisAsDerived()->CheckArcValidity(arc));
00538 const ArcIndexType next_arc = arc + 1;
00539 return next_arc < num_arcs_ ? next_arc : kNilArc;
00540 }
00541
00542
00543 ArcIndexType FirstOutgoingArc(const NodeIndexType node) const {
00544 DCHECK(IsNodeValid(node));
00545 return ThisAsDerived()->FindNextOutgoingArc(FirstIncidentArc(node));
00546 }
00547
00548
00549 NodeIndexType max_num_nodes_;
00550
00551
00552 ArcIndexType max_num_arcs_;
00553
00554
00555 NodeIndexType num_nodes_;
00556
00557
00558 ArcIndexType num_arcs_;
00559
00560
00561 ZVector<NodeIndexType> head_;
00562
00563
00564
00565 ZVector<ArcIndexType> next_adjacent_arc_;
00566
00567
00568
00569 ZVector<ArcIndexType> first_incident_arc_;
00570
00571
00572
00573
00574 bool representation_clean_;
00575
00576 private:
00577
00578
00579 inline const DerivedGraph* ThisAsDerived() const {
00580 return static_cast<const DerivedGraph*>(this);
00581 }
00582
00583
00584
00585 inline DerivedGraph* ThisAsDerived() {
00586 return static_cast<DerivedGraph*>(this);
00587 }
00588
00589
00590
00591
00592 void SetHead(const ArcIndexType arc, const NodeIndexType head) {
00593 representation_clean_ = false;
00594 head_.Set(arc, head);
00595 }
00596 };
00597
00598
00599 template<typename NodeIndexType, typename ArcIndexType, typename DerivedGraph>
00600 const NodeIndexType EbertGraphCore<NodeIndexType,
00601 ArcIndexType,
00602 DerivedGraph>::kNilNode = -1;
00603
00604
00605 template<typename NodeIndexType, typename ArcIndexType, typename DerivedGraph>
00606 const ArcIndexType EbertGraphCore<NodeIndexType,
00607 ArcIndexType,
00608 DerivedGraph>::kNilArc =
00609 std::numeric_limits<ArcIndexType>::min();
00610
00611
00612 template<typename NodeIndexType, typename ArcIndexType, typename DerivedGraph>
00613 const NodeIndexType EbertGraphCore<NodeIndexType,
00614 ArcIndexType,
00615 DerivedGraph>::kFirstNode = 0;
00616
00617
00618 template<typename NodeIndexType, typename ArcIndexType, typename DerivedGraph>
00619 const ArcIndexType EbertGraphCore<NodeIndexType,
00620 ArcIndexType,
00621 DerivedGraph>::kFirstArc = 0;
00622
00623
00624 template<typename NodeIndexType, typename ArcIndexType, typename DerivedGraph>
00625 const NodeIndexType EbertGraphCore<NodeIndexType,
00626 ArcIndexType,
00627 DerivedGraph>::kMaxNumNodes =
00628 std::numeric_limits<NodeIndexType>::max();
00629
00630
00631
00632 template<typename NodeIndexType, typename ArcIndexType, typename DerivedGraph>
00633 const ArcIndexType EbertGraphCore<NodeIndexType,
00634 ArcIndexType,
00635 DerivedGraph>::kMaxNumArcs =
00636 std::numeric_limits<ArcIndexType>::max();
00637
00638
00639
00640 template<typename NodeIndexType, typename ArcIndexType> class EbertGraph
00641 : public EbertGraphCore<NodeIndexType, ArcIndexType,
00642 EbertGraph<NodeIndexType, ArcIndexType> > {
00643 typedef EbertGraphCore<NodeIndexType, ArcIndexType,
00644 EbertGraph<NodeIndexType, ArcIndexType> > Base;
00645 friend class EbertGraphCore<NodeIndexType, ArcIndexType,
00646 EbertGraph<NodeIndexType, ArcIndexType> >;
00647
00648 using Base::ArcDebugString;
00649 using Base::FirstIncidentArc;
00650 using Base::Initialize;
00651 using Base::NextAdjacentArc;
00652 using Base::NodeDebugString;
00653
00654 using Base::first_incident_arc_;
00655 using Base::head_;
00656 using Base::max_num_arcs_;
00657 using Base::max_num_nodes_;
00658 using Base::next_adjacent_arc_;
00659 using Base::num_arcs_;
00660 using Base::num_nodes_;
00661 using Base::representation_clean_;
00662
00663 public:
00664 #ifndef SWIG
00665 using Base::kFirstArc;
00666 using Base::kFirstNode;
00667 using Base::kNilArc;
00668 using Base::kNilNode;
00669 using Base::Head;
00670 using Base::IsNodeValid;
00671 #endif // SWIG
00672
00673 typedef NodeIndexType NodeIndex;
00674 typedef ArcIndexType ArcIndex;
00675
00676 EbertGraph() {}
00677
00678 EbertGraph(NodeIndexType max_num_nodes, ArcIndexType max_num_arcs) {
00679 Initialize(max_num_nodes, max_num_arcs);
00680 }
00681
00682 ~EbertGraph() {}
00683
00684
00685
00686 class IncidentArcIterator {
00687 public:
00688 IncidentArcIterator(const EbertGraph& graph, NodeIndexType node)
00689 : graph_(graph),
00690 node_(graph_.StartNode(node)),
00691 arc_(graph_.StartArc(graph_.FirstIncidentArc(node))) {
00692 DCHECK(CheckInvariant());
00693 }
00694
00695
00696
00697 IncidentArcIterator(const EbertGraph& graph,
00698 NodeIndexType node,
00699 ArcIndexType arc)
00700 : graph_(graph),
00701 node_(graph_.StartNode(node)),
00702 arc_(graph_.StartArc(arc)) {
00703 DCHECK(CheckInvariant());
00704 }
00705
00706
00707 void operator=(const IncidentArcIterator& iterator) {
00708 DCHECK(&iterator.graph_ == &graph_);
00709 node_ = iterator.node_;
00710 arc_ = iterator.arc_;
00711 }
00712
00713
00714 bool Ok() const { return arc_ != kNilArc; }
00715
00716
00717 void Next() {
00718 arc_ = graph_.NextAdjacentArc(arc_);
00719 DCHECK(CheckInvariant());
00720 }
00721
00722
00723 ArcIndexType Index() const { return arc_; }
00724
00725 private:
00726
00727
00728 bool CheckInvariant() const {
00729 if (arc_ == kNilArc) {
00730 return true;
00731 }
00732 DCHECK(graph_.IsIncident(arc_, node_));
00733 return true;
00734 }
00735
00736 const EbertGraph& graph_;
00737
00738
00739 NodeIndexType node_;
00740
00741
00742 ArcIndexType arc_;
00743 };
00744
00745
00746
00747
00748
00749
00750
00751 class IncomingArcIterator {
00752 public:
00753 IncomingArcIterator(const EbertGraph& graph, NodeIndexType node)
00754 : graph_(graph),
00755 node_(graph_.StartNode(node)),
00756 arc_(graph_.StartArc(graph_.FirstIncomingArc(node))) {
00757 DCHECK(CheckInvariant());
00758 }
00759
00760
00761
00762 IncomingArcIterator(const EbertGraph& graph,
00763 NodeIndexType node,
00764 ArcIndexType arc)
00765 : graph_(graph),
00766 node_(graph_.StartNode(node)),
00767 arc_(graph_.StartArc(arc)) {
00768 DCHECK(CheckInvariant());
00769 }
00770
00771
00772 void operator=(const IncomingArcIterator& iterator) {
00773 DCHECK(&iterator.graph_ == &graph_);
00774 node_ = iterator.node_;
00775 arc_ = iterator.arc_;
00776 }
00777
00778
00779 bool Ok() const { return arc_ != kNilArc; }
00780
00781
00782 void Next() {
00783 arc_ = graph_.NextIncomingArc(arc_);
00784 DCHECK(CheckInvariant());
00785 }
00786
00787
00788 ArcIndexType Index() const { return arc_; }
00789
00790 private:
00791
00792
00793 bool CheckInvariant() const {
00794 if (arc_ == kNilArc) {
00795 return true;
00796 }
00797 DCHECK(graph_.IsIncoming(arc_, node_));
00798 return true;
00799 }
00800
00801 const EbertGraph& graph_;
00802
00803
00804 NodeIndexType node_;
00805
00806
00807 ArcIndexType arc_;
00808 };
00809
00810
00811 class OutgoingArcIterator {
00812 public:
00813 OutgoingArcIterator(const EbertGraph& graph, NodeIndexType node)
00814 : graph_(graph),
00815 node_(graph_.StartNode(node)),
00816 arc_(graph_.StartArc(graph_.FirstOutgoingArc(node))) {
00817 DCHECK(CheckInvariant());
00818 }
00819
00820
00821
00822 OutgoingArcIterator(const EbertGraph& graph,
00823 NodeIndexType node,
00824 ArcIndexType arc)
00825 : graph_(graph),
00826 node_(graph_.StartNode(node)),
00827 arc_(graph_.StartArc(arc)) {
00828 DCHECK(CheckInvariant());
00829 }
00830
00831
00832 void operator=(const OutgoingArcIterator& iterator) {
00833 DCHECK(&iterator.graph_ == &graph_);
00834 node_ = iterator.node_;
00835 arc_ = iterator.arc_;
00836 }
00837
00838
00839 bool Ok() const { return arc_ != kNilArc; }
00840
00841
00842 void Next() {
00843 arc_ = graph_.NextOutgoingArc(arc_);
00844 DCHECK(CheckInvariant());
00845 }
00846
00847
00848 ArcIndexType Index() const { return arc_; }
00849
00850 private:
00851
00852
00853 bool CheckInvariant() const {
00854 if (arc_ == kNilArc) {
00855 return true;
00856 }
00857 DCHECK(graph_.IsOutgoing(arc_, node_));
00858 return true;
00859 }
00860
00861
00862 const EbertGraph& graph_;
00863
00864
00865 NodeIndexType node_;
00866
00867
00868 ArcIndexType arc_;
00869 };
00870
00871
00872
00873
00874 bool CheckArcBounds(const ArcIndexType arc) const {
00875 return (arc == kNilArc) || (arc >= -max_num_arcs_ && arc < max_num_arcs_);
00876 }
00877
00878
00879
00880
00881
00882 bool CheckArcValidity(const ArcIndexType arc) const {
00883 return (arc != kNilArc) && (arc >= -max_num_arcs_ && arc < max_num_arcs_);
00884 }
00885
00886
00887 NodeIndexType Tail(const ArcIndexType arc) const {
00888 DCHECK(CheckArcValidity(arc));
00889 return head_[Opposite(arc)];
00890 }
00891
00892
00893
00894
00895 NodeIndexType DirectArcTail(const ArcIndexType arc) const {
00896 return Tail(DirectArc(arc));
00897 }
00898
00899
00900
00901
00902 NodeIndexType DirectArcHead(const ArcIndexType arc) const {
00903 return Head(DirectArc(arc));
00904 }
00905
00906
00907 ArcIndexType DirectArc(const ArcIndexType arc) const {
00908 DCHECK(CheckArcValidity(arc));
00909 return std::max(arc, Opposite(arc));
00910 }
00911
00912
00913 ArcIndexType ReverseArc(const ArcIndexType arc) const {
00914 DCHECK(CheckArcValidity(arc));
00915 return std::min(arc, Opposite(arc));
00916 }
00917
00918
00919
00920 ArcIndexType Opposite(const ArcIndexType arc) const {
00921 const ArcIndexType opposite = ~arc;
00922 DCHECK(CheckArcValidity(arc));
00923 DCHECK(CheckArcValidity(opposite));
00924 return opposite;
00925 }
00926
00927
00928 bool IsDirect(const ArcIndexType arc) const {
00929 DCHECK(CheckArcBounds(arc));
00930 return arc != kNilArc && arc >= 0;
00931 }
00932
00933
00934 bool IsReverse(const ArcIndexType arc) const {
00935 DCHECK(CheckArcBounds(arc));
00936 return arc != kNilArc && arc < 0;
00937 }
00938
00939
00940 bool IsIncident(ArcIndexType arc, NodeIndexType node) const {
00941 return IsIncoming(arc, node) || IsOutgoing(arc, node);
00942 }
00943
00944
00945 bool IsIncoming(ArcIndexType arc, NodeIndexType node) const {
00946 return DirectArcHead(arc) == node;
00947 }
00948
00949
00950 bool IsOutgoing(ArcIndexType arc, NodeIndexType node) const {
00951 return DirectArcTail(arc) == node;
00952 }
00953
00954
00955
00956
00957
00958 void BuildRepresentation() {
00959 first_incident_arc_.SetAll(kNilArc);
00960 for (ArcIndexType arc = kFirstArc; arc < max_num_arcs_; ++arc) {
00961 Attach(arc);
00962 }
00963 representation_clean_ = true;
00964 }
00965
00966
00967
00968 string DebugString() const {
00969 DCHECK(representation_clean_);
00970 string result = "Arcs:(node, next arc) :\n";
00971 for (ArcIndexType arc = -num_arcs_; arc < num_arcs_; ++arc) {
00972 result += " " + ArcDebugString(arc) + ":(" + NodeDebugString(head_[arc])
00973 + "," + ArcDebugString(next_adjacent_arc_[arc]) + ")\n";
00974 }
00975 result += "Node:First arc :\n";
00976 for (NodeIndexType node = kFirstNode; node < num_nodes_; ++node) {
00977 result += " " + NodeDebugString(node) + ":"
00978 + ArcDebugString(first_incident_arc_[node]) + "\n";
00979 }
00980 return result;
00981 }
00982
00983 private:
00984
00985
00986
00987
00988
00989
00990
00991 void ReserveNextAdjacentArcAndHeadEntries(ArcIndexType new_max_num_arcs) {
00992 head_.Reserve(-new_max_num_arcs, new_max_num_arcs - 1);
00993 next_adjacent_arc_.Reserve(-new_max_num_arcs, new_max_num_arcs - 1);
00994 for (ArcIndexType arc = -new_max_num_arcs; arc < -max_num_arcs_; ++arc) {
00995 head_.Set(arc, kNilNode);
00996 next_adjacent_arc_.Set(arc, kNilArc);
00997 }
00998 for (ArcIndexType arc = max_num_arcs_; arc < new_max_num_arcs; ++arc) {
00999 head_.Set(arc, kNilNode);
01000 next_adjacent_arc_.Set(arc, kNilArc);
01001 }
01002 }
01003
01004 void ReserveInternal(NodeIndexType new_max_num_nodes,
01005 ArcIndexType new_max_num_arcs) {
01006 }
01007
01008
01009 ArcIndexType NextOutgoingArc(const ArcIndexType arc) const {
01010 DCHECK(CheckArcValidity(arc));
01011 DCHECK(IsDirect(arc));
01012 return FindNextOutgoingArc(NextAdjacentArc(arc));
01013 }
01014
01015
01016 ArcIndexType FirstIncomingArc(const NodeIndexType node) const {
01017 DCHECK_LE(kFirstNode, node);
01018 DCHECK_GE(max_num_nodes_, node);
01019 return FindNextIncomingArc(FirstIncidentArc(node));
01020 }
01021
01022
01023 ArcIndexType NextIncomingArc(const ArcIndexType arc) const {
01024 DCHECK(CheckArcValidity(arc));
01025 DCHECK(IsReverse(arc));
01026 return FindNextIncomingArc(NextAdjacentArc(arc));
01027 }
01028
01029
01030
01031 void RecordArc(ArcIndexType arc, NodeIndexType tail, NodeIndexType head) {
01032 head_.Set(Opposite(arc), tail);
01033 head_.Set(arc, head);
01034 Attach(arc);
01035 }
01036
01037
01038
01039
01040 void SetTail(const ArcIndexType arc, const NodeIndexType tail) {
01041 representation_clean_ = false;
01042 head_.Set(Opposite(arc), tail);
01043 }
01044
01045
01046 void Attach(ArcIndexType arc) {
01047 DCHECK(CheckArcValidity(arc));
01048 const NodeIndexType tail = head_[Opposite(arc)];
01049 DCHECK(IsNodeValid(tail));
01050 next_adjacent_arc_.Set(arc, first_incident_arc_[tail]);
01051 first_incident_arc_.Set(tail, arc);
01052 const NodeIndexType head = head_[arc];
01053 DCHECK(IsNodeValid(head));
01054 next_adjacent_arc_.Set(Opposite(arc), first_incident_arc_[head]);
01055 first_incident_arc_.Set(head, Opposite(arc));
01056 }
01057
01058
01059 ArcIndexType FindNextOutgoingArc(ArcIndexType arc) const {
01060 DCHECK(CheckArcBounds(arc));
01061 while (IsReverse(arc)) {
01062 arc = NextAdjacentArc(arc);
01063 DCHECK(CheckArcBounds(arc));
01064 }
01065 return arc;
01066 }
01067
01068
01069 ArcIndexType FindNextIncomingArc(ArcIndexType arc) const {
01070 DCHECK(CheckArcBounds(arc));
01071 while (IsDirect(arc)) {
01072 arc = NextAdjacentArc(arc);
01073 DCHECK(CheckArcBounds(arc));
01074 }
01075 return arc;
01076 }
01077 };
01078
01079
01080
01081 template<typename NodeIndexType, typename ArcIndexType> class ForwardEbertGraph
01082 : public EbertGraphCore<NodeIndexType, ArcIndexType,
01083 ForwardEbertGraph<NodeIndexType, ArcIndexType> > {
01084 typedef EbertGraphCore<NodeIndexType, ArcIndexType,
01085 ForwardEbertGraph<NodeIndexType, ArcIndexType> > Base;
01086 friend class EbertGraphCore<NodeIndexType, ArcIndexType,
01087 ForwardEbertGraph<NodeIndexType, ArcIndexType> >;
01088
01089 using Base::ArcDebugString;
01090 using Base::Initialize;
01091 using Base::NextAdjacentArc;
01092 using Base::NodeDebugString;
01093
01094 using Base::first_incident_arc_;
01095 using Base::head_;
01096 using Base::max_num_arcs_;
01097 using Base::max_num_nodes_;
01098 using Base::next_adjacent_arc_;
01099 using Base::num_arcs_;
01100 using Base::num_nodes_;
01101 using Base::representation_clean_;
01102
01103 public:
01104 #ifndef SWIG
01105 using Base::kFirstArc;
01106 using Base::kFirstNode;
01107 using Base::kNilArc;
01108 using Base::kNilNode;
01109
01110 using Base::Head;
01111 #endif // SWIG
01112
01113 typedef NodeIndexType NodeIndex;
01114 typedef ArcIndexType ArcIndex;
01115
01116 ForwardEbertGraph() {}
01117
01118 ForwardEbertGraph(NodeIndexType max_num_nodes, ArcIndexType max_num_arcs) {
01119 Initialize(max_num_nodes, max_num_arcs);
01120 }
01121
01122 ~ForwardEbertGraph() {}
01123
01124
01125
01126 class OutgoingArcIterator {
01127 public:
01128 OutgoingArcIterator(const ForwardEbertGraph& graph, NodeIndexType node)
01129 : graph_(graph),
01130 node_(graph_.StartNode(node)),
01131 arc_(graph_.StartArc(graph_.FirstIncidentArc(node))) {}
01132
01133
01134
01135 OutgoingArcIterator(const ForwardEbertGraph& graph,
01136 NodeIndexType node,
01137 ArcIndexType arc)
01138 : graph_(graph),
01139 node_(graph_.StartNode(node)),
01140 arc_(graph_.StartArc(arc)) {}
01141
01142
01143 void operator=(const OutgoingArcIterator& iterator) {
01144 DCHECK(&iterator.graph_ == &graph_);
01145 node_ = iterator.node_;
01146 arc_ = iterator.arc_;
01147 }
01148
01149
01150 bool Ok() const { return arc_ != kNilArc; }
01151
01152
01153 void Next() {
01154 arc_ = graph_.NextAdjacentArc(arc_);
01155 }
01156
01157
01158 ArcIndexType Index() const { return arc_; }
01159
01160 private:
01161
01162 const ForwardEbertGraph& graph_;
01163
01164
01165 NodeIndexType node_;
01166
01167
01168 ArcIndexType arc_;
01169 };
01170
01171
01172
01173
01174 bool CheckArcBounds(const ArcIndexType arc) const {
01175 return (arc == kNilArc) || (arc >= kFirstArc && arc < max_num_arcs_);
01176 }
01177
01178
01179
01180
01181
01182 bool CheckArcValidity(const ArcIndexType arc) const {
01183 return (arc != kNilArc) && (arc >= kFirstArc && arc < max_num_arcs_);
01184 }
01185
01186
01187
01188
01189
01190
01191 bool IsNodeValid(const NodeIndexType node) const {
01192 return node >= kFirstNode && node < max_num_nodes_;
01193 }
01194
01195
01196 bool CheckTailIndexValidity(const ArcIndexType arc) const {
01197 return (tail_ != NULL) && (arc >= kFirstArc) && (arc <= tail_->max_index());
01198 }
01199
01200
01201 NodeIndexType Tail(const ArcIndexType arc) const {
01202 DCHECK(CheckArcValidity(arc));
01203 DCHECK(CheckTailIndexValidity(arc));
01204 return (*tail_)[arc];
01205 }
01206
01207
01208 bool IsIncoming(ArcIndexType arc, NodeIndexType node) const {
01209 return Head(arc) == node;
01210 }
01211
01212
01213
01214
01215
01216 void BuildRepresentation() {
01217 first_incident_arc_.SetAll(kNilArc);
01218 DCHECK(TailArrayComplete());
01219 for (ArcIndexType arc = kFirstArc; arc < max_num_arcs_; ++arc) {
01220 DCHECK(CheckTailIndexValidity(arc));
01221 Attach((*tail_)[arc], arc);
01222 }
01223 representation_clean_ = true;
01224 }
01225
01226 bool BuildTailArray() {
01227
01228
01229
01230 if (tail_ == NULL) {
01231 if (!representation_clean_) {
01232
01233
01234
01235 return false;
01236 }
01237
01238
01239 tail_.reset(new ZVector<NodeIndexType>);
01240 tail_->Reserve(kFirstArc, max_num_arcs_ - 1);
01241 typename Base::NodeIterator node_it(*this);
01242 for (; node_it.Ok(); node_it.Next()) {
01243 NodeIndexType node = node_it.Index();
01244 OutgoingArcIterator arc_it(*this, node);
01245 for (; arc_it.Ok(); arc_it.Next()) {
01246 (*tail_)[arc_it.Index()] = node;
01247 }
01248 }
01249 }
01250 DCHECK(TailArrayComplete());
01251 return true;
01252 }
01253
01254 void ReleaseTailArray() {
01255 tail_.reset(NULL);
01256 }
01257
01258
01259 bool TailArrayComplete() const {
01260 CHECK_NOTNULL(tail_);
01261 for (ArcIndexType arc = kFirstArc; arc < num_arcs_; ++arc) {
01262 CHECK(CheckTailIndexValidity(arc));
01263 CHECK(IsNodeValid((*tail_)[arc]));
01264 }
01265 return true;
01266 }
01267
01268
01269
01270 string DebugString() const {
01271 DCHECK(representation_clean_);
01272 string result = "Arcs:(node, next arc) :\n";
01273 for (ArcIndexType arc = kFirstArc; arc < num_arcs_; ++arc) {
01274 result += " " + ArcDebugString(arc) + ":("
01275 + NodeDebugString(head_[arc])
01276 + "," + ArcDebugString(next_adjacent_arc_[arc]) + ")\n";
01277 }
01278 result += "Node:First arc :\n";
01279 for (NodeIndexType node = kFirstNode; node < num_nodes_; ++node) {
01280 result += " " + NodeDebugString(node) + ":"
01281 + ArcDebugString(first_incident_arc_[node]) + "\n";
01282 }
01283 return result;
01284 }
01285
01286 private:
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308
01309
01310 void ReserveNextAdjacentArcAndHeadEntries(ArcIndexType new_max_num_arcs) {
01311 head_.Reserve(kFirstArc, new_max_num_arcs - 1);
01312 next_adjacent_arc_.Reserve(kFirstArc, new_max_num_arcs - 1);
01313 for (ArcIndexType arc = max_num_arcs_; arc < new_max_num_arcs; ++arc) {
01314 head_.Set(arc, kNilNode);
01315 next_adjacent_arc_.Set(arc, kNilArc);
01316 }
01317 }
01318
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328 void ReserveTailArray(ArcIndexType new_max_num_arcs) {
01329 if (tail_ != NULL) {
01330
01331
01332
01333 if (tail_->Reserve(kFirstArc, new_max_num_arcs - 1)) {
01334 for (ArcIndexType arc = tail_->max_index() + 1;
01335 arc < new_max_num_arcs;
01336 ++arc) {
01337 tail_->Set(arc, kNilNode);
01338 }
01339 }
01340 }
01341 }
01342
01343 void ReserveInternal(NodeIndexType new_max_num_nodes,
01344 ArcIndexType new_max_num_arcs) {
01345 ReserveTailArray(new_max_num_arcs);
01346 }
01347
01348
01349 ArcIndexType NextOutgoingArc(const ArcIndexType arc) const {
01350 DCHECK(CheckArcValidity(arc));
01351 return FindNextOutgoingArc(NextAdjacentArc(arc));
01352 }
01353
01354
01355
01356 void RecordArc(ArcIndexType arc, NodeIndexType tail, NodeIndexType head) {
01357 head_.Set(arc, head);
01358 Attach(tail, arc);
01359 }
01360
01361
01362
01363
01364 void SetTail(const ArcIndexType arc, const NodeIndexType tail) {
01365 DCHECK(CheckTailIndexValidity(arc));
01366 CHECK_NOTNULL(tail_);
01367 representation_clean_ = false;
01368 tail_->Set(arc, tail);
01369 }
01370
01371
01372 void Attach(NodeIndexType tail, ArcIndexType arc) {
01373 DCHECK(CheckArcValidity(arc));
01374 DCHECK(IsNodeValid(tail));
01375 next_adjacent_arc_.Set(arc, first_incident_arc_[tail]);
01376 first_incident_arc_.Set(tail, arc);
01377 const NodeIndexType head = head_[arc];
01378 DCHECK(IsNodeValid(head));
01379
01380
01381 if (tail_ != NULL) {
01382 DCHECK(CheckTailIndexValidity(arc));
01383 tail_->Set(arc, tail);
01384 }
01385 }
01386
01387
01388 ArcIndexType FindNextOutgoingArc(ArcIndexType arc) const {
01389 DCHECK(CheckArcBounds(arc));
01390 return arc;
01391 }
01392
01393 private:
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408
01409 scoped_ptr<ZVector<NodeIndexType> > tail_;
01410 };
01411
01412
01413
01414
01415
01416
01417
01418 template <typename GraphType> struct graph_traits {
01419 static const bool has_reverse_arcs = true;
01420 };
01421
01422 template <typename NodeIndexType, typename ArcIndexType>
01423 struct graph_traits<ForwardEbertGraph<NodeIndexType, ArcIndexType> > {
01424 static const bool has_reverse_arcs = false;
01425 };
01426
01427
01428 template <typename GraphType, bool has_reverse_arcs> struct TailArrayBuilder {
01429 explicit TailArrayBuilder(GraphType* unused_graph) {}
01430
01431 bool BuildTailArray() const {
01432 return true;
01433 }
01434 };
01435
01436
01437
01438
01439 template <typename GraphType> struct TailArrayBuilder<GraphType, false> {
01440 explicit TailArrayBuilder(GraphType* graph) : graph_(graph) {}
01441
01442 bool BuildTailArray() const {
01443 return graph_->BuildTailArray();
01444 }
01445
01446 GraphType* const graph_;
01447 };
01448
01449
01450 template <typename GraphType, bool has_reverse_arcs> struct TailArrayReleaser {
01451 explicit TailArrayReleaser(GraphType* unused_graph) {}
01452
01453 void ReleaseTailArray() const {}
01454 };
01455
01456
01457
01458
01459 template <typename GraphType> struct TailArrayReleaser<GraphType, false> {
01460 explicit TailArrayReleaser(GraphType* graph) : graph_(graph) {}
01461
01462 void ReleaseTailArray() const {
01463 graph_->ReleaseTailArray();
01464 }
01465
01466 GraphType* const graph_;
01467 };
01468
01469 template <typename GraphType> class TailArrayManager {
01470 public:
01471 explicit TailArrayManager(GraphType* g) : graph_(g) {}
01472
01473 void BuildTailArrayFromAdjacencyListsIfForwardGraph() const {
01474 TailArrayBuilder<GraphType, graph_traits<GraphType>::has_reverse_arcs>
01475 tail_array_builder(graph_);
01476 tail_array_builder.BuildTailArray();
01477 }
01478
01479 void ReleaseTailArrayIfForwardGraph() const {
01480 TailArrayReleaser<GraphType, graph_traits<GraphType>::has_reverse_arcs>
01481 tail_array_releaser(graph_);
01482 tail_array_releaser.ReleaseTailArray();
01483 }
01484
01485 private:
01486 GraphType* graph_;
01487 };
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500 template<typename GraphType> bool BuildLineGraph(const GraphType& graph,
01501 GraphType* const line_graph) {
01502 if (line_graph == NULL) {
01503 LOG(DFATAL) << "line_graph must not be NULL";
01504 return false;
01505 }
01506 if (line_graph->num_nodes() != 0) {
01507 LOG(DFATAL) << "line_graph must be empty";
01508 return false;
01509 }
01510 typedef typename GraphType::ArcIterator ArcIterator;
01511 typedef typename GraphType::OutgoingArcIterator OutgoingArcIterator;
01512
01513 typename GraphType::ArcIndex num_arcs = 0;
01514 for (ArcIterator arc_iterator(graph);
01515 arc_iterator.Ok();
01516 arc_iterator.Next()) {
01517 const typename GraphType::ArcIndex arc = arc_iterator.Index();
01518 const typename GraphType::NodeIndex head = graph.Head(arc);
01519 for (OutgoingArcIterator iterator(graph, head);
01520 iterator.Ok();
01521 iterator.Next()) {
01522 ++num_arcs;
01523 }
01524 }
01525 line_graph->Reserve(graph.num_arcs(), num_arcs);
01526 for (ArcIterator arc_iterator(graph);
01527 arc_iterator.Ok();
01528 arc_iterator.Next()) {
01529 const typename GraphType::ArcIndex arc = arc_iterator.Index();
01530 const typename GraphType::NodeIndex head = graph.Head(arc);
01531 for (OutgoingArcIterator iterator(graph, head);
01532 iterator.Ok();
01533 iterator.Next()) {
01534 line_graph->AddArc(arc, iterator.Index());
01535 }
01536 }
01537 return true;
01538 }
01539
01540 }
01541 #endif // OR_TOOLS_GRAPH_EBERT_GRAPH_H_