14 #ifndef OR_TOOLS_GRAPH_EBERT_GRAPH_H_ 15 #define OR_TOOLS_GRAPH_EBERT_GRAPH_H_ 177 #include "absl/strings/str_cat.h" 178 #include "ortools/base/integral_types.h" 179 #include "ortools/base/logging.h" 180 #include "ortools/base/macros.h" 181 #include "ortools/util/permutation.h" 182 #include "ortools/util/zvector.h" 187 template <
typename NodeIndexType,
typename ArcIndexType>
189 template <
typename NodeIndexType,
typename ArcIndexType>
191 template <
typename NodeIndexType,
typename ArcIndexType>
212 template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
286 const NodeIndexType head)
const {
288 arc = ThisAsDerived()->NextOutgoingArc(tail, arc)) {
289 if (
Head(arc) == head) {
297 NodeIndexType
Head(
const ArcIndexType arc)
const {
298 DCHECK(ThisAsDerived()->CheckArcValidity(arc));
306 return absl::StrCat(static_cast<int64>(node));
314 return absl::StrCat(static_cast<int64>(arc));
329 void Next() { head_ = graph_.NextNode(head_); }
332 NodeIndexType
Index()
const {
return head_; }
336 const DerivedGraph& graph_;
352 void Next() { arc_ = graph_.NextArc(arc_); }
355 ArcIndexType
Index()
const {
return arc_; }
359 const DerivedGraph& graph_;
372 DCHECK(CheckInvariant());
382 DCHECK(CheckInvariant());
387 DCHECK(&iterator.graph_ == &graph_);
388 node_ = iterator.node_;
389 arc_ = iterator.arc_;
397 arc_ = graph_.NextOutgoingArc(node_, arc_);
398 DCHECK(CheckInvariant());
402 ArcIndexType
Index()
const {
return arc_; }
407 bool CheckInvariant()
const {
411 DCHECK(graph_.IsOutgoing(arc_, node_));
416 const DerivedGraph& graph_;
458 NodeIndexType
NextNode(
const NodeIndexType node)
const {
460 const NodeIndexType next_node = node + 1;
472 ArcIndexType
NextArc(
const ArcIndexType arc)
const {
473 DCHECK(ThisAsDerived()->CheckArcValidity(arc));
474 const ArcIndexType next_arc = arc + 1;
481 return ThisAsDerived()->FindNextOutgoingArc(
482 ThisAsDerived()->FirstOutgoingOrOppositeIncomingArc(node));
507 inline const DerivedGraph* ThisAsDerived()
const {
508 return static_cast<const DerivedGraph*>(
this);
513 inline DerivedGraph* ThisAsDerived() {
514 return static_cast<DerivedGraph*>(
this);
518 template <
typename NodeIndexType,
typename ArcIndexType>
522 const ZVector<NodeIndexType>& head)
526 return head_[a] < head_[b];
530 const ZVector<NodeIndexType>& head_;
533 template <
typename NodeIndexType,
typename ArcIndexType>
534 class ForwardStaticGraph
535 :
public StarGraphBase<NodeIndexType, ArcIndexType,
536 ForwardStaticGraph<NodeIndexType, ArcIndexType> > {
538 ForwardStaticGraph<NodeIndexType, ArcIndexType> >
571 :
public ArrayIndexCycleHandler<NodeIndexType, ArcIndexType> {
572 typedef ArrayIndexCycleHandler<NodeIndexType, ArcIndexType> Base;
576 PermutationCycleHandler<ArcIndexType>* annotation_handler,
578 : ArrayIndexCycleHandler<NodeIndexType, ArcIndexType>(&data[
kFirstArc]),
579 annotation_handler_(annotation_handler) {}
582 Base::SetTempFromIndex(source);
583 annotation_handler_->SetTempFromIndex(source);
587 ArcIndexType destination)
const override {
588 Base::SetIndexFromIndex(source, destination);
589 annotation_handler_->SetIndexFromIndex(source, destination);
593 Base::SetIndexFromTemp(destination);
594 annotation_handler_->SetIndexFromTemp(destination);
598 PermutationCycleHandler<ArcIndexType>* annotation_handler_;
623 const bool sort_arcs_by_head,
624 std::vector<std::pair<NodeIndexType, NodeIndexType> >* client_input_arcs,
628 operations_research::PermutationCycleHandler<ArcIndexType>*
const 629 client_cycle_handler) {
635 std::vector<std::pair<NodeIndexType, NodeIndexType> >& input_arcs =
652 num_nodes_, static_cast<NodeIndexType>(input_arcs[arc].first + 1));
654 num_nodes_, static_cast<NodeIndexType>(input_arcs[arc].second + 1));
657 for (NodeIndexType node = 0; node <
num_nodes; ++node) {
664 std::unique_ptr<ArcIndexType[]> arc_permutation;
665 if (client_cycle_handler !=
nullptr) {
667 for (ArcIndexType input_arc = 0; input_arc <
num_arcs; ++input_arc) {
668 NodeIndexType tail = input_arcs[input_arc].first;
669 NodeIndexType head = input_arcs[input_arc].second;
674 arc_permutation[
kFirstArc + arc] = input_arc;
682 for (ArcIndexType input_arc = 0; input_arc <
num_arcs; ++input_arc) {
683 NodeIndexType tail = input_arcs[input_arc].first;
686 input_arcs[input_arc].first = static_cast<NodeIndexType>(arc);
688 for (ArcIndexType input_arc = 0; input_arc <
num_arcs; ++input_arc) {
690 static_cast<ArcIndexType>(input_arcs[input_arc].first);
691 NodeIndexType head = input_arcs[input_arc].second;
697 for (ArcIndexType input_arc = 0; input_arc <
num_arcs; ++input_arc) {
698 NodeIndexType tail = input_arcs[input_arc].first;
699 NodeIndexType head = input_arcs[input_arc].second;
715 if (sort_arcs_by_head) {
717 if (client_cycle_handler !=
nullptr) {
718 for (NodeIndexType node = 0; node <
num_nodes; ++node) {
721 &arc_permutation[
begin], &arc_permutation[
end],
727 for (NodeIndexType node = 0; node <
num_nodes; ++node) {
735 ArcIndexType end_index = (
end > 0 ?
end - 1 :
end);
736 ArcIndexType end_offset = (
end > 0 ? 1 : 0);
737 std::sort(&
head_[begin_index] + begin_offset,
738 &
head_[end_index] + end_offset);
743 if (client_cycle_handler !=
nullptr &&
num_arcs > 0) {
750 PermutationApplier<ArcIndexType> permutation(&handler_for_constructor);
756 NodeIndexType
Tail(
const ArcIndexType arc)
const {
759 return (*tail_)[arc];
763 bool IsIncoming(ArcIndexType arc, NodeIndexType node)
const {
764 return Head(arc) == node;
784 return ((tail_ !=
nullptr) && (arc >=
kFirstArc) &&
785 (arc <= tail_->max_index()));
789 ArcIndexType arc)
const {
803 std::string result =
"Arcs:(node) :\n";
808 result +=
"Node:First arc :\n";
820 if (tail_ ==
nullptr) {
821 if (!RepresentationClean()) {
829 tail_.reset(
new ZVector<NodeIndexType>);
831 typename Base::NodeIterator node_it(*
this);
832 for (; node_it.Ok(); node_it.Next()) {
833 NodeIndexType node = node_it.Index();
834 typename Base::OutgoingArcIterator arc_it(*
this, node);
835 for (; arc_it.Ok(); arc_it.Next()) {
836 (*tail_)[arc_it.Index()] = node;
857 bool IsDirect()
const {
return true; }
858 bool RepresentationClean()
const {
return true; }
859 bool IsOutgoing(
const NodeIndexType node,
860 const ArcIndexType unused_arc)
const {
865 ArcIndexType FirstOutgoingOrOppositeIncomingArc(NodeIndexType node)
const {
866 DCHECK(RepresentationClean());
873 ArcIndexType FindNextOutgoingArc(ArcIndexType arc)
const {
893 std::unique_ptr<ZVector<NodeIndexType> > tail_;
897 template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
902 template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
905 std::numeric_limits<ArcIndexType>::min();
908 template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
913 template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
918 template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
921 std::numeric_limits<NodeIndexType>::max();
925 template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
928 std::numeric_limits<ArcIndexType>::max();
947 template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
949 :
public StarGraphBase<NodeIndexType, ArcIndexType, DerivedGraph> {
978 bool Reserve(NodeIndexType new_max_num_nodes, ArcIndexType new_max_num_arcs) {
979 if (new_max_num_nodes < 0 || new_max_num_nodes >
kMaxNumNodes) {
982 if (new_max_num_arcs < 0 || new_max_num_arcs >
kMaxNumArcs) {
990 ThisAsDerived()->ReserveInternal(new_max_num_nodes, new_max_num_arcs);
1001 ArcIndexType
AddArc(NodeIndexType tail, NodeIndexType head) {
1014 ThisAsDerived()->RecordArc(arc, tail, head);
1021 template <
typename ArcIndexTypeStrictWeakOrderingFunctor>
1023 const ArcIndexTypeStrictWeakOrderingFunctor& compare,
1024 PermutationCycleHandler<ArcIndexType>* annotation_handler) {
1025 std::unique_ptr<ArcIndexType[]> arc_permutation(
1031 arc_permutation[i] = i;
1038 CycleHandlerForAnnotatedArcs cycle_handler(annotation_handler,
1040 PermutationApplier<ArcIndexType> permutation(&cycle_handler);
1044 ThisAsDerived()->BuildRepresentation();
1048 :
public PermutationCycleHandler<ArcIndexType> {
1051 PermutationCycleHandler<ArcIndexType>* annotation_handler,
1052 DerivedGraph* graph)
1053 : annotation_handler_(annotation_handler),
1059 if (annotation_handler_ !=
nullptr) {
1060 annotation_handler_->SetTempFromIndex(source);
1062 head_temp_ = graph_->Head(source);
1063 tail_temp_ = graph_->Tail(source);
1067 ArcIndexType destination)
const override {
1068 if (annotation_handler_ !=
nullptr) {
1069 annotation_handler_->SetIndexFromIndex(source, destination);
1071 graph_->SetHead(destination, graph_->Head(source));
1072 graph_->SetTail(destination, graph_->Tail(source));
1076 if (annotation_handler_ !=
nullptr) {
1077 annotation_handler_->SetIndexFromTemp(destination);
1079 graph_->SetHead(destination, head_temp_);
1080 graph_->SetTail(destination, tail_temp_);
1087 void SetSeen(ArcIndexType* permutation_element)
const override {
1088 *permutation_element =
kNilArc;
1091 bool Unseen(ArcIndexType permutation_element)
const override {
1092 return permutation_element !=
kNilArc;
1098 PermutationCycleHandler<ArcIndexType>* annotation_handler_;
1099 DerivedGraph* graph_;
1100 NodeIndexType head_temp_;
1101 NodeIndexType tail_temp_;
1114 LOG(DFATAL) <<
"Could not reserve memory for " 1124 const NodeIndexType node)
const {
1133 DCHECK(ThisAsDerived()->CheckArcValidity(arc));
1139 const ArcIndexType arc)
const {
1140 DCHECK(ThisAsDerived()->CheckArcValidity(arc));
1141 DCHECK(ThisAsDerived()->IsDirect(arc));
1157 inline const DerivedGraph* ThisAsDerived()
const {
1158 return static_cast<const DerivedGraph*>(
this);
1163 inline DerivedGraph* ThisAsDerived() {
1164 return static_cast<DerivedGraph*>(
this);
1177 void SetHead(
const ArcIndexType arc,
const NodeIndexType head) {
1179 head_.Set(arc, head);
1185 template <
typename NodeIndexType,
typename ArcIndexType>
1187 :
public EbertGraphBase<NodeIndexType, ArcIndexType,
1188 EbertGraph<NodeIndexType, ArcIndexType> > {
1190 EbertGraph<NodeIndexType, ArcIndexType> >
1245 DCHECK(CheckInvariant());
1251 NodeIndexType node, ArcIndexType arc)
1255 DCHECK(CheckInvariant());
1260 DCHECK(&iterator.graph_ == &graph_);
1261 node_ = iterator.node_;
1262 arc_ = iterator.arc_;
1271 DCHECK(CheckInvariant());
1275 ArcIndexType
Index()
const {
return arc_; }
1280 bool CheckInvariant()
const {
1291 NodeIndexType node_;
1303 arc_(graph_.
StartArc(graph_.FirstIncomingArc(node))) {
1304 DCHECK(CheckInvariant());
1315 DCHECK(CheckInvariant());
1320 DCHECK(&iterator.graph_ == &graph_);
1321 node_ = iterator.node_;
1322 arc_ = iterator.arc_;
1330 arc_ = graph_.NextIncomingArc(arc_);
1331 DCHECK(CheckInvariant());
1342 bool CheckInvariant()
const {
1353 NodeIndexType node_;
1376 NodeIndexType
Tail(
const ArcIndexType arc)
const {
1398 return std::max(arc,
Opposite(arc));
1404 return std::min(arc,
Opposite(arc));
1410 const ArcIndexType opposite = ~arc;
1419 return arc !=
kNilArc && arc >= 0;
1425 return arc !=
kNilArc && arc < 0;
1430 NodeIndexType node)
const {
1431 return Tail(arc) == node;
1460 std::string result =
"Arcs:(node, next arc) :\n";
1465 result +=
"Node:First arc :\n";
1481 void ReserveInternal(NodeIndexType new_max_num_nodes,
1482 ArcIndexType new_max_num_arcs) {
1483 head_.Reserve(-new_max_num_arcs, new_max_num_arcs - 1);
1485 for (ArcIndexType arc = -new_max_num_arcs; arc < -
max_num_arcs_; ++arc) {
1489 for (ArcIndexType arc =
max_num_arcs_; arc < new_max_num_arcs; ++arc) {
1496 ArcIndexType FirstIncomingArc(
const NodeIndexType node)
const {
1503 ArcIndexType NextIncomingArc(
const ArcIndexType arc)
const {
1511 void RecordArc(ArcIndexType arc, NodeIndexType tail, NodeIndexType head) {
1513 head_.Set(arc, head);
1520 void SetTail(
const ArcIndexType arc,
const NodeIndexType tail) {
1526 void Attach(ArcIndexType arc) {
1532 const NodeIndexType head =
head_[arc];
1539 ArcIndexType FindNextOutgoingArc(ArcIndexType arc)
const {
1549 ArcIndexType FindNextIncomingArc(ArcIndexType arc)
const {
1561 template <
typename NodeIndexType,
typename ArcIndexType>
1562 class ForwardEbertGraph
1563 :
public EbertGraphBase<NodeIndexType, ArcIndexType,
1564 ForwardEbertGraph<NodeIndexType, ArcIndexType> > {
1566 ForwardEbertGraph<NodeIndexType, ArcIndexType> >
1626 return (tail_ !=
nullptr) && (arc >=
kFirstArc) &&
1627 (arc <= tail_->max_index());
1631 NodeIndexType
Tail(
const ArcIndexType arc)
const {
1634 return (*tail_)[arc];
1639 return IsDirect(arc) &&
Head(arc) == node;
1651 Attach((*tail_)[arc], arc);
1660 if (tail_ ==
nullptr) {
1669 tail_.reset(
new ZVector<NodeIndexType>);
1671 typename Base::NodeIterator node_it(*
this);
1672 for (; node_it.Ok(); node_it.Next()) {
1673 NodeIndexType node = node_it.Index();
1674 typename Base::OutgoingArcIterator arc_it(*
this, node);
1675 for (; arc_it.Ok(); arc_it.Next()) {
1676 (*tail_)[arc_it.Index()] = node;
1700 std::string result =
"Arcs:(node, next arc) :\n";
1705 result +=
"Node:First arc :\n";
1722 void ReserveTailArray(ArcIndexType new_max_num_arcs) {
1723 if (tail_ !=
nullptr) {
1727 if (tail_->Reserve(
kFirstArc, new_max_num_arcs - 1)) {
1728 for (ArcIndexType arc = tail_->max_index() + 1; arc < new_max_num_arcs;
1759 void ReserveInternal(NodeIndexType new_max_num_nodes,
1760 ArcIndexType new_max_num_arcs) {
1763 for (ArcIndexType arc =
max_num_arcs_; arc < new_max_num_arcs; ++arc) {
1767 ReserveTailArray(new_max_num_arcs);
1772 void RecordArc(ArcIndexType arc, NodeIndexType tail, NodeIndexType head) {
1773 head_.Set(arc, head);
1780 void SetTail(
const ArcIndexType arc,
const NodeIndexType tail) {
1784 tail_->Set(arc, tail);
1788 void Attach(NodeIndexType tail, ArcIndexType arc) {
1793 const NodeIndexType head =
head_[arc];
1797 if (tail_ !=
nullptr) {
1799 tail_->Set(arc, tail);
1804 ArcIndexType FindNextOutgoingArc(ArcIndexType arc)
const {
1813 bool IsOutgoing(
const ArcIndex unused_arc,
1821 bool IsDirect(
const ArcIndex unused_arc)
const {
return true; }
1838 std::unique_ptr<ZVector<NodeIndexType> > tail_;
1847 template <
typename GraphType>
1853 template <
typename NodeIndexType,
typename ArcIndexType>
1859 template <
typename NodeIndexType,
typename ArcIndexType>
1865 namespace or_internal {
1871 template <
typename GraphType,
bool has_reverse_arcs>
1872 struct TailArrayBuilder {
1873 explicit TailArrayBuilder(GraphType* unused_graph) {}
1875 bool BuildTailArray()
const {
return true; }
1881 template <
typename GraphType>
1882 struct TailArrayBuilder<GraphType,
false> {
1883 explicit TailArrayBuilder(GraphType* graph) : graph_(graph) {}
1885 bool BuildTailArray()
const {
return graph_->BuildTailArray(); }
1887 GraphType*
const graph_;
1894 template <
typename GraphType,
bool has_reverse_arcs>
1895 struct TailArrayReleaser {
1896 explicit TailArrayReleaser(GraphType* unused_graph) {}
1898 void ReleaseTailArray()
const {}
1904 template <
typename GraphType>
1905 struct TailArrayReleaser<GraphType,
false> {
1906 explicit TailArrayReleaser(GraphType* graph) : graph_(graph) {}
1908 void ReleaseTailArray()
const { graph_->ReleaseTailArray(); }
1910 GraphType*
const graph_;
1915 template <
typename GraphType>
1921 or_internal::TailArrayBuilder<GraphType,
1923 tail_array_builder(graph_);
1924 return tail_array_builder.BuildTailArray();
1928 or_internal::TailArrayReleaser<GraphType,
1930 tail_array_releaser(graph_);
1931 tail_array_releaser.ReleaseTailArray();
1938 template <
typename GraphType>
1946 return ((graph_.Tail(a) < graph_.Tail(b)) ||
1947 ((graph_.Tail(a) == graph_.Tail(b)) &&
1948 (graph_.Head(a) < graph_.Head(b))));
1952 const GraphType& graph_;
1955 namespace or_internal {
1962 template <
typename GraphType,
bool is_dynamic>
1963 class GraphBuilderFromArcs {
1968 : num_arcs_(0), sort_arcs_(sort_arcs) {
1969 Reserve(max_num_nodes, max_num_arcs);
1974 DCHECK_LT(num_arcs_, max_num_arcs_);
1975 DCHECK_LT(tail, GraphType::kFirstNode + max_num_nodes_);
1976 DCHECK_LT(head, GraphType::kFirstNode + max_num_nodes_);
1977 if (num_arcs_ < max_num_arcs_ &&
1978 tail < GraphType::kFirstNode + max_num_nodes_ &&
1979 head < GraphType::kFirstNode + max_num_nodes_) {
1981 arcs_.push_back(std::make_pair(tail, head));
1986 return GraphType::kNilArc;
1991 GraphType*
Graph(PermutationCycleHandler<typename GraphType::ArcIndex>*
1992 client_cycle_handler) {
1993 GraphType* graph =
new GraphType(max_num_nodes_, num_arcs_, sort_arcs_,
1994 &arcs_, client_cycle_handler);
2002 max_num_nodes_ = new_max_num_nodes;
2003 max_num_arcs_ = new_max_num_arcs;
2004 arcs_.reserve(new_max_num_arcs);
2013 std::pair<typename GraphType::NodeIndex, typename GraphType::NodeIndex> >
2016 const bool sort_arcs_;
2022 template <
typename GraphType>
2023 class GraphBuilderFromArcs<GraphType, true> {
2028 : graph_(new GraphType(max_num_nodes, max_num_arcs)),
2029 sort_arcs_(sort_arcs) {}
2033 return graph_->Reserve(new_max_num_nodes, new_max_num_arcs);
2039 return graph_->AddArc(tail, head);
2042 GraphType*
Graph(PermutationCycleHandler<typename GraphType::ArcIndex>*
2043 client_cycle_handler) {
2045 TailArrayManager<GraphType> tail_array_manager(graph_);
2046 tail_array_manager.BuildTailArrayFromAdjacencyListsIfForwardGraph();
2047 ArcFunctorOrderingByTailAndHead<GraphType> arc_ordering(*graph_);
2048 graph_->GroupForwardArcsByFunctor(arc_ordering, client_cycle_handler);
2049 tail_array_manager.ReleaseTailArrayIfForwardGraph();
2051 GraphType* result = graph_;
2057 GraphType*
const graph_;
2058 const bool sort_arcs_;
2063 template <
typename GraphType>
2065 :
public or_internal::GraphBuilderFromArcs<
2066 GraphType, graph_traits<GraphType>::is_dynamic> {
2071 : or_internal::GraphBuilderFromArcs<GraphType,
2073 num_nodes, num_arcs, sort_arcs) {}
2087 template <
typename GraphType>
2089 if (line_graph ==
nullptr) {
2090 LOG(DFATAL) <<
"line_graph must not be NULL";
2093 if (line_graph->num_nodes() != 0) {
2094 LOG(DFATAL) <<
"line_graph must be empty";
2097 typedef typename GraphType::ArcIterator ArcIterator;
2098 typedef typename GraphType::OutgoingArcIterator OutgoingArcIterator;
2101 for (ArcIterator arc_iterator(graph); arc_iterator.Ok();
2102 arc_iterator.Next()) {
2105 for (OutgoingArcIterator iterator(graph, head); iterator.Ok();
2110 line_graph->Reserve(graph.num_arcs(), num_arcs);
2111 for (ArcIterator arc_iterator(graph); arc_iterator.Ok();
2112 arc_iterator.Next()) {
2115 for (OutgoingArcIterator iterator(graph, head); iterator.Ok();
2117 line_graph->AddArc(arc, iterator.Index());
2124 #endif // OR_TOOLS_GRAPH_EBERT_GRAPH_H_ NodeIndexType Tail(const ArcIndexType arc) const
Returns the tail or start-node of arc.
AnnotatedGraphBuildManager(typename GraphType::NodeIndex num_nodes, typename GraphType::ArcIndex num_arcs, bool sort_arcs)
bool IsDirect(const ArcIndexType arc) const
Returns true if the arc is direct.
IncomingArcIterator(const EbertGraph &graph, NodeIndexType node)
ZVector< ArcIndexType > first_incident_arc_
Array of arc indices.
ArcIndexType NextOutgoingArc(const NodeIndexType node, ArcIndexType arc) const
static const ArcIndexType kFirstArc
The index of the first arc in the graph.
ArcIndexType NextOutgoingArc(const NodeIndexType unused_node, const ArcIndexType arc) const
Returns the outgoing arc following the argument in the adjacency list.
ArcIndexType FirstOutgoingArc(const NodeIndexType node) const
Returns the first outgoing arc for node.
ArcIndexType Index() const
Returns the index of the arc currently pointed to by the iterator.
ArcIndexType max_end_arc_index() const
Returns one more than the largest valid index of a direct arc.
ArcIndexType ReverseArc(const ArcIndexType arc) const
Returns the arc in reverse direction.
ZVector< ArcIndex > ArcIndexArray
OutgoingArcIterator(const DerivedGraph &graph, NodeIndexType node)
bool Ok() const
Returns true unless all the outgoing arcs have been traversed.
ArcIndexType Index() const
Returns the index of the arc currently pointed to by the iterator.
bool IsIncoming(ArcIndexType arc, NodeIndexType node) const
Returns true if arc is incoming to node.
std::string DebugString() const
Returns a debug std::string containing all the information contained in the data structure in raw for...
std::string NodeDebugString(const NodeIndexType node) const
void Next()
Advances the current incoming arc index.
A template for the base class that holds the functionality that exists in common between the EbertGra...
ForwardEbertGraph< NodeIndex, ArcIndex > ForwardStarGraph
ArcIndexType Opposite(const ArcIndexType arc) const
Returns the opposite arc, i.e the direct arc is the arc is in reverse direction, and the reverse arc ...
static const bool has_reverse_arcs
void operator=(const IncomingArcIterator &iterator)
Can only assign from an iterator on the same graph.
bool operator()(ArcIndexType a, ArcIndexType b) const
NodeIndexType Tail(const ArcIndexType arc) const
Returns the tail or start-node of arc.
ListGraph Graph
Defining the simplest Graph interface as Graph for convenience.
OutgoingOrOppositeIncomingArcIterator(const EbertGraph &graph, NodeIndexType node)
void Next()
Advances the current adjacent arc index.
ZVector< NodeIndexType > head_
Array of node indices. head_[i] contains the head node of arc i.
static const ArcIndexType kMaxNumArcs
The maximum possible number of arcs in the graph.
ArcIndexType max_num_arcs() const
Returns the maximum possible number of original arcs in the graph.
void Next()
Advances the current outgoing arc index.
bool Ok() const
Returns true unless all the incoming arcs have been traversed.
NodeIndexType max_num_nodes_
The maximum number of nodes that the graph can hold.
static const NodeIndexType kFirstNode
The index of the first node in the graph.
ArcIndexType LookUpArc(const NodeIndexType tail, const NodeIndexType head) const
Returns the first arc going from tail to head, if it exists, or kNilArc if such an arc does not exist...
static const NodeIndexType kMaxNumNodes
The maximum possible number of nodes in the graph.
PermutationIndexComparisonByArcHead(const ZVector< NodeIndexType > &head)
void operator=(const OutgoingArcIterator &iterator)
Can only assign from an iterator on the same graph.
static const NodeIndexType kNilNode
The index of the 'nil' node in the graph.
static const ArcIndexType kNilArc
The index of the 'nil' arc in the graph.
NodeIndexType max_end_node_index() const
Returns one more than the largest valid index of a node.
CycleHandlerForAnnotatedArcs(PermutationCycleHandler< ArcIndexType > *annotation_handler, NodeIndexType *data)
ArcIndexType num_arcs_
The current number of arcs held by the graph.
ForwardStaticGraph< NodeIndex, ArcIndex > ForwardStarStaticGraph
bool TailArrayComplete() const
To be used in a DCHECK().
EbertGraph(NodeIndexType max_num_nodes, ArcIndexType max_num_arcs)
ZVector< ArcIndexType > next_adjacent_arc_
Array of next indices.
void SetIndexFromTemp(ArcIndexType destination) const override
OutgoingArcIterator(const DerivedGraph &graph, NodeIndexType node, ArcIndexType arc)
This constructor takes an arc as extra argument and makes the iterator start at arc.
ForwardStaticGraph(const NodeIndexType num_nodes, const ArcIndexType num_arcs, const bool sort_arcs_by_head, std::vector< std::pair< NodeIndexType, NodeIndexType > > *client_input_arcs, operations_research::PermutationCycleHandler< ArcIndexType > *const client_cycle_handler)
Constructor for use by GraphBuilderFromArcs instances and direct clients that want to materialize a g...
Iterator class for traversing all the nodes in the graph.
ForwardEbertGraph(NodeIndexType max_num_nodes, ArcIndexType max_num_arcs)
Iterator class for traversing the outgoing arcs associated to a given node.
bool Ok() const
Returns true unless all the nodes have been traversed.
std::string DebugString() const
Returns a debug std::string containing all the information contained in the data structure in raw for...
ArcIterator(const DerivedGraph &graph)
NodeIterator(const DerivedGraph &graph)
bool Ok() const
Returns true unless all the arcs have been traversed.
bool IsReverse(const ArcIndexType arc) const
Returns true if the arc is in the reverse direction.
bool Reserve(NodeIndexType new_max_num_nodes, ArcIndexType new_max_num_arcs)
Reserves memory needed for max_num_nodes nodes and max_num_arcs arcs.
Iterator class for traversing the incoming arcs associated to a given node.
ArcIndexType FirstOutgoingOrOppositeIncomingArc(const NodeIndexType node) const
Returns the first arc in node's incidence list.
ArcFunctorOrderingByTailAndHead(const GraphType &graph)
bool BuildTailArrayFromAdjacencyListsIfForwardGraph() const
NodeIndexType NextNode(const NodeIndexType node) const
Returns the node following the argument in the graph.
bool CheckArcValidity(const ArcIndexType arc) const
Utility function to check that an arc index is within the bounds AND different from kNilArc.
void SetTempFromIndex(ArcIndexType source) override
ArcIndexType NextArc(const ArcIndexType arc) const
Returns the arc following the argument in the graph.
void SetIndexFromIndex(ArcIndexType source, ArcIndexType destination) const override
Iterator class for traversing the arcs in the graph.
ArcIndexType end_arc_index() const
Returns one more than the largest index of an extant direct arc.
static const ArcIndexType kNilArc
The index of the 'nil' arc in the graph.
bool TailArrayComplete() const
To be used in a DCHECK().
bool IsNodeValid(NodeIndexType node) const
Utility function to check that a node index is within the bounds AND different from kNilNode.
NodeIndexType max_num_nodes() const
Returns the maximum possible number of nodes in the graph.
IncomingArcIterator(const EbertGraph &graph, NodeIndexType node, ArcIndexType arc)
This constructor takes an arc as extra argument and makes the iterator start at arc.
bool BuildLineGraph(const GraphType &graph, GraphType *const line_graph)
Builds a directed line graph for 'graph' (see "directed line graph" in http://en.wikipedia....
ZVector< FlowQuantity > QuantityArray
ArcIndexType max_num_arcs_
The maximum number of arcs that the graph can hold.
ZVector< NodeIndex > NodeIndexArray
bool operator()(typename GraphType::ArcIndex a, typename GraphType::ArcIndex b) const
ArcIndexType num_arcs() const
Returns the number of original arcs in the graph (The ones with positive indices.)
static const ArcIndexType kFirstArc
The index of the first arc in the graph.
TailArrayManager(GraphType *g)
bool CheckTailIndexValidity(const ArcIndexType arc) const
Returns true if arc is a valid index into the (*tail_) array.
static const NodeIndexType kFirstNode
The index of the first node in the graph.
false
This is useful for wrapping iterators of a class that support many different iterations.
Traits for EbertGraphBase types, for use in testing and clients that work with both forward-only and ...
Iterator class for traversing the arcs incident to a given node in the graph.
NodeIndexType Head(const ArcIndexType arc) const
Returns the head or end-node of arc.
NodeIndexType DirectArcHead(const ArcIndexType arc) const
Returns the head or end-node of arc if it is positive (i.e.
std::string DebugString() const
Returns a debug std::string containing all the information contained in the data structure in raw for...
bool CheckArcValidity(const ArcIndexType arc) const
Utility function to check that an arc index is within the bounds AND different from kNilArc.
void SetIndexFromTemp(ArcIndexType destination) const override
void operator=(const OutgoingOrOppositeIncomingArcIterator &iterator)
Can only assign from an iterator on the same graph.
bool CheckArcValidity(const ArcIndexType arc) const
Utility function to check that an arc index is within the bounds AND different from kNilArc.
bool CheckArcBounds(const ArcIndexType arc) const
Utility function to check that an arc index is within the bounds.
void Next()
Advances the current node index.
ArcIndexType NextAdjacentArc(const ArcIndexType arc) const
Returns the next arc following the passed argument in its adjacency list.
ZVector< NodeIndexType > head_
Array of node indices. head_[i] contains the head node of arc i.
void GroupForwardArcsByFunctor(const ArcIndexTypeStrictWeakOrderingFunctor &compare, PermutationCycleHandler< ArcIndexType > *annotation_handler)
bool IsOutgoing(ArcIndexType arc, NodeIndexType node) const
Returns true if arc is outgoing from node.
NodeIndexType DirectArcTail(const ArcIndexType arc) const
Returns the tail or start-node of arc if it is positive (i.e.
bool IsOutgoingOrOppositeIncoming(ArcIndexType arc, NodeIndexType node) const
Returns true if arc is incident to node.
NodeIndexType end_node_index() const
Returns one more than the largest index of an extant node, meaning a node that is mentioned as the he...
bool CheckArcBounds(const ArcIndexType arc) const
Utility function to check that an arc index is within the bounds.
CycleHandlerForAnnotatedArcs(PermutationCycleHandler< ArcIndexType > *annotation_handler, DerivedGraph *graph)
void BuildRepresentation()
Recreates the next_adjacent_arc_ and first_incident_arc_ variables from the array head_ in O(n + m) t...
void SetIndexFromIndex(ArcIndexType source, ArcIndexType destination) const override
void SetTempFromIndex(ArcIndexType source) override
~CycleHandlerForAnnotatedArcs() override
NodeIndexType StartNode(NodeIndexType node) const
Returns kNilNode if the graph has no nodes or node if it has at least one node.
ArcIndexType max_num_arcs_
The maximum number of arcs that the graph can hold.
bool IsIncoming(ArcIndexType arc, NodeIndexType node) const
Returns true if arc is incoming to node.
ZVector< ArcIndexType > first_incident_arc_
Array of arc indices.
NodeIndexType num_nodes_
The maximum index of the node currently held by the graph.
ArcIndexType DirectArc(const ArcIndexType arc) const
Returns the arc in normal/direct direction.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...
NodeIndexType num_nodes_
The maximum index of the node currently held by the graph.
ArcIndexType Index() const
Returns the index of the arc currently pointed to by the iterator.
ArcIndexType AddArc(NodeIndexType tail, NodeIndexType head)
Adds an arc to the graph and returns its index.
void Next()
Advances the current arc index.
OutgoingOrOppositeIncomingArcIterator(const EbertGraph &graph, NodeIndexType node, ArcIndexType arc)
This constructor takes an arc as extra argument and makes the iterator start at arc.
bool representation_clean_
Flag to indicate that BuildRepresentation() needs to be called before the adjacency lists are examine...
static const bool is_dynamic
ArcIndexType StartArc(ArcIndexType arc) const
Returns kNilArc if the graph has no arcs arc if it has at least one arc.
ZVector< CostValue > CostArray
void SetSeen(ArcIndexType *permutation_element) const override
Since we are free to destroy the permutation array we use the kNilArc value to mark entries in the ar...
ArcIndexType num_arcs_
The current number of arcs held by the graph.
void ReleaseTailArrayIfForwardGraph() const
static const NodeIndexType kNilNode
The index of the 'nil' node in the graph.
A forward-star-only graph representation for greater efficiency in those algorithms that don't need r...
bool Ok() const
Returns true unless all the adjancent arcs have been traversed.
bool Unseen(ArcIndexType permutation_element) const override
bool IsNodeValid(NodeIndexType node) const
Utility function to check that a node index is within the bounds AND different from kNilNode.
bool CheckArcBounds(const ArcIndexType arc) const
Utility function to check that an arc index is within the bounds.
void Initialize(NodeIndexType max_num_nodes, ArcIndexType max_num_arcs)
NodeIndexType Tail(const ArcIndexType arc) const
Returns the tail or start-node of arc.
void BuildRepresentation()
Recreates the next_adjacent_arc_ and first_incident_arc_ variables from the arrays head_ and tail_ in...
int32 NodeIndex
Standard instantiation of ForwardEbertGraph (named 'ForwardStarGraph') of EbertGraph (named 'StarGrap...
NodeIndexType num_nodes() const
Returns the number of nodes in the graph.
EbertGraph< NodeIndex, ArcIndex > StarGraph
NodeIndexType Index() const
Returns the index of the node currently pointed to by the iterator.
bool CheckTailIndexValidity(const ArcIndexType arc) const
Returns true if arc is a valid index into the (*tail_) array.
bool IsIncoming(ArcIndexType arc, NodeIndexType node) const
Returns true if arc is incoming to node.
NodeIndexType max_num_nodes_
The maximum number of nodes that the graph can hold.
std::string ArcDebugString(const ArcIndexType arc) const
ArcIndexType Index() const
Returns the index of the arc currently pointed to by the iterator.