14#ifndef OR_TOOLS_GRAPH_EBERT_GRAPH_H_
15#define OR_TOOLS_GRAPH_EBERT_GRAPH_H_
178#include "absl/strings/str_cat.h"
186template <
typename NodeIndexType,
typename ArcIndexType>
188template <
typename NodeIndexType,
typename ArcIndexType>
189class ForwardEbertGraph;
190template <
typename NodeIndexType,
typename ArcIndexType>
191class ForwardStaticGraph;
211template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
285 const NodeIndexType
head)
const {
287 arc = ThisAsDerived()->NextOutgoingArc(
tail,
arc)) {
296 NodeIndexType
Head(
const ArcIndexType
arc)
const {
297 DCHECK(ThisAsDerived()->CheckArcValidity(
arc));
305 return absl::StrCat(
static_cast<int64_t
>(node));
313 return absl::StrCat(
static_cast<int64_t
>(
arc));
328 void Next() { head_ = graph_.NextNode(head_); }
331 NodeIndexType
Index()
const {
return head_; }
335 const DerivedGraph& graph_;
351 void Next() { arc_ = graph_.NextArc(arc_); }
354 ArcIndexType
Index()
const {
return arc_; }
358 const DerivedGraph& graph_;
371 DCHECK(CheckInvariant());
381 DCHECK(CheckInvariant());
386 DCHECK(&iterator.graph_ == &graph_);
387 node_ = iterator.node_;
388 arc_ = iterator.arc_;
396 arc_ = graph_.NextOutgoingArc(node_, arc_);
397 DCHECK(CheckInvariant());
401 ArcIndexType
Index()
const {
return arc_; }
406 bool CheckInvariant()
const {
410 DCHECK(graph_.IsOutgoing(arc_, node_));
415 const DerivedGraph& graph_;
457 NodeIndexType
NextNode(
const NodeIndexType node)
const {
459 const NodeIndexType next_node = node + 1;
472 DCHECK(ThisAsDerived()->CheckArcValidity(
arc));
473 const ArcIndexType next_arc =
arc + 1;
480 return ThisAsDerived()->FindNextOutgoingArc(
481 ThisAsDerived()->FirstOutgoingOrOppositeIncomingArc(node));
506 inline const DerivedGraph* ThisAsDerived()
const {
507 return static_cast<const DerivedGraph*
>(
this);
512 inline DerivedGraph* ThisAsDerived() {
513 return static_cast<DerivedGraph*
>(
this);
517template <
typename NodeIndexType,
typename ArcIndexType>
525 return head_[
a] < head_[
b];
532template <
typename NodeIndexType,
typename ArcIndexType>
535 ForwardStaticGraph<NodeIndexType, ArcIndexType> > {
578 annotation_handler_(annotation_handler) {}
591 ArcIndexType destination)
const override {
625 const bool sort_arcs_by_head,
626 std::vector<std::pair<NodeIndexType, NodeIndexType> >* client_input_arcs,
631 client_cycle_handler) {
637 std::vector<std::pair<NodeIndexType, NodeIndexType> >& input_arcs =
654 num_nodes_,
static_cast<NodeIndexType
>(input_arcs[
arc].first + 1));
656 num_nodes_,
static_cast<NodeIndexType
>(input_arcs[
arc].second + 1));
659 for (NodeIndexType node = 0; node <
num_nodes; ++node) {
666 std::unique_ptr<ArcIndexType[]> arc_permutation;
667 if (client_cycle_handler !=
nullptr) {
669 for (ArcIndexType input_arc = 0; input_arc <
num_arcs; ++input_arc) {
670 NodeIndexType
tail = input_arcs[input_arc].first;
671 NodeIndexType
head = input_arcs[input_arc].second;
684 for (ArcIndexType input_arc = 0; input_arc <
num_arcs; ++input_arc) {
685 NodeIndexType
tail = input_arcs[input_arc].first;
688 input_arcs[input_arc].first =
static_cast<NodeIndexType
>(
arc);
690 for (ArcIndexType input_arc = 0; input_arc <
num_arcs; ++input_arc) {
692 static_cast<ArcIndexType
>(input_arcs[input_arc].first);
693 NodeIndexType
head = input_arcs[input_arc].second;
699 for (ArcIndexType input_arc = 0; input_arc <
num_arcs; ++input_arc) {
700 NodeIndexType
tail = input_arcs[input_arc].first;
701 NodeIndexType
head = input_arcs[input_arc].second;
717 if (sort_arcs_by_head) {
719 if (client_cycle_handler !=
nullptr) {
720 for (NodeIndexType node = 0; node <
num_nodes; ++node) {
723 &arc_permutation[begin], &arc_permutation[
end],
729 for (NodeIndexType node = 0; node <
num_nodes; ++node) {
735 ArcIndexType begin_index = (begin <
num_arcs ? begin : begin - 1);
736 ArcIndexType begin_offset = (begin <
num_arcs ? 0 : 1);
737 ArcIndexType end_index = (
end > 0 ?
end - 1 :
end);
738 ArcIndexType end_offset = (
end > 0 ? 1 : 0);
739 std::sort(&
head_[begin_index] + begin_offset,
740 &
head_[end_index] + end_offset);
745 if (client_cycle_handler !=
nullptr &&
num_arcs > 0) {
758 NodeIndexType
Tail(
const ArcIndexType
arc)
const {
761 return (*tail_)[
arc];
791 ArcIndexType
arc)
const {
805 std::string result =
"Arcs:(node) :\n";
810 result +=
"Node:First arc :\n";
822 if (tail_ ==
nullptr) {
823 if (!RepresentationClean()) {
833 typename Base::NodeIterator node_it(*
this);
834 for (; node_it.Ok(); node_it.Next()) {
835 NodeIndexType node = node_it.Index();
836 typename Base::OutgoingArcIterator arc_it(*
this, node);
837 for (; arc_it.Ok(); arc_it.Next()) {
838 (*tail_)[arc_it.Index()] = node;
859 bool IsDirect()
const {
return true; }
860 bool RepresentationClean()
const {
return true; }
861 bool IsOutgoing(
const NodeIndexType node,
862 const ArcIndexType unused_arc)
const {
867 ArcIndexType FirstOutgoingOrOppositeIncomingArc(NodeIndexType node)
const {
868 DCHECK(RepresentationClean());
875 ArcIndexType FindNextOutgoingArc(ArcIndexType
arc)
const {
895 std::unique_ptr<ZVector<NodeIndexType> > tail_;
899template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
904template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
907 std::numeric_limits<ArcIndexType>::min();
910template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
915template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
920template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
923 std::numeric_limits<NodeIndexType>::max();
927template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
930 std::numeric_limits<ArcIndexType>::max();
949template <
typename NodeIndexType,
typename ArcIndexType,
typename DerivedGraph>
951 :
public StarGraphBase<NodeIndexType, ArcIndexType, DerivedGraph> {
953 friend class StarGraphBase<NodeIndexType, ArcIndexType, DerivedGraph>;
980 bool Reserve(NodeIndexType new_max_num_nodes, ArcIndexType new_max_num_arcs) {
981 if (new_max_num_nodes < 0 || new_max_num_nodes >
kMaxNumNodes) {
984 if (new_max_num_arcs < 0 || new_max_num_arcs >
kMaxNumArcs) {
992 ThisAsDerived()->ReserveInternal(new_max_num_nodes, new_max_num_arcs);
1023 template <
typename ArcIndexTypeStrictWeakOrderingFunctor>
1025 const ArcIndexTypeStrictWeakOrderingFunctor& compare,
1027 std::unique_ptr<ArcIndexType[]> arc_permutation(
1033 arc_permutation[i] = i;
1046 ThisAsDerived()->BuildRepresentation();
1054 DerivedGraph*
graph)
1055 : annotation_handler_(annotation_handler),
1066 if (annotation_handler_ !=
nullptr) {
1069 head_temp_ = graph_->Head(source);
1070 tail_temp_ = graph_->Tail(source);
1074 ArcIndexType destination)
const override {
1075 if (annotation_handler_ !=
nullptr) {
1078 graph_->SetHead(destination, graph_->Head(source));
1079 graph_->SetTail(destination, graph_->Tail(source));
1083 if (annotation_handler_ !=
nullptr) {
1086 graph_->SetHead(destination, head_temp_);
1087 graph_->SetTail(destination, tail_temp_);
1094 void SetSeen(ArcIndexType* permutation_element)
const override {
1095 *permutation_element =
kNilArc;
1098 bool Unseen(ArcIndexType permutation_element)
const override {
1099 return permutation_element !=
kNilArc;
1106 DerivedGraph* graph_;
1107 NodeIndexType head_temp_;
1108 NodeIndexType tail_temp_;
1119 LOG(DFATAL) <<
"Could not reserve memory for "
1129 const NodeIndexType node)
const {
1138 DCHECK(ThisAsDerived()->CheckArcValidity(
arc));
1144 const ArcIndexType
arc)
const {
1145 DCHECK(ThisAsDerived()->CheckArcValidity(
arc));
1146 DCHECK(ThisAsDerived()->IsDirect(
arc));
1162 inline const DerivedGraph* ThisAsDerived()
const {
1163 return static_cast<const DerivedGraph*
>(
this);
1168 inline DerivedGraph* ThisAsDerived() {
1169 return static_cast<DerivedGraph*
>(
this);
1182 void SetHead(
const ArcIndexType
arc,
const NodeIndexType
head) {
1190template <
typename NodeIndexType,
typename ArcIndexType>
1193 EbertGraph<NodeIndexType, ArcIndexType> > {
1250 DCHECK(CheckInvariant());
1256 NodeIndexType node, ArcIndexType
arc)
1260 DCHECK(CheckInvariant());
1265 DCHECK(&iterator.graph_ == &graph_);
1266 node_ = iterator.node_;
1267 arc_ = iterator.arc_;
1276 DCHECK(CheckInvariant());
1280 ArcIndexType
Index()
const {
return arc_; }
1285 bool CheckInvariant()
const {
1296 NodeIndexType node_;
1308 arc_(graph_.
StartArc(graph_.FirstIncomingArc(node))) {
1309 DCHECK(CheckInvariant());
1320 DCHECK(CheckInvariant());
1325 DCHECK(&iterator.graph_ == &graph_);
1326 node_ = iterator.node_;
1327 arc_ = iterator.arc_;
1335 arc_ = graph_.NextIncomingArc(arc_);
1336 DCHECK(CheckInvariant());
1347 bool CheckInvariant()
const {
1358 NodeIndexType node_;
1381 NodeIndexType
Tail(
const ArcIndexType
arc)
const {
1415 const ArcIndexType opposite = ~arc;
1435 NodeIndexType node)
const {
1465 std::string result =
"Arcs:(node, next arc) :\n";
1470 result +=
"Node:First arc :\n";
1486 void ReserveInternal(NodeIndexType new_max_num_nodes,
1487 ArcIndexType new_max_num_arcs) {
1488 head_.
Reserve(-new_max_num_arcs, new_max_num_arcs - 1);
1501 ArcIndexType FirstIncomingArc(
const NodeIndexType node)
const {
1508 ArcIndexType NextIncomingArc(
const ArcIndexType
arc)
const {
1516 void RecordArc(ArcIndexType
arc, NodeIndexType
tail, NodeIndexType
head) {
1525 void SetTail(
const ArcIndexType
arc,
const NodeIndexType
tail) {
1531 void Attach(ArcIndexType
arc) {
1544 ArcIndexType FindNextOutgoingArc(ArcIndexType
arc)
const {
1554 ArcIndexType FindNextIncomingArc(ArcIndexType
arc)
const {
1566template <
typename NodeIndexType,
typename ArcIndexType>
1569 ForwardEbertGraph<NodeIndexType, ArcIndexType> > {
1636 NodeIndexType
Tail(
const ArcIndexType
arc)
const {
1639 return (*tail_)[
arc];
1656 Attach((*tail_)[
arc],
arc);
1665 if (tail_ ==
nullptr) {
1676 typename Base::NodeIterator node_it(*
this);
1677 for (; node_it.Ok(); node_it.Next()) {
1678 NodeIndexType node = node_it.Index();
1679 typename Base::OutgoingArcIterator arc_it(*
this, node);
1680 for (; arc_it.Ok(); arc_it.Next()) {
1681 (*tail_)[arc_it.Index()] = node;
1705 std::string result =
"Arcs:(node, next arc) :\n";
1710 result +=
"Node:First arc :\n";
1727 void ReserveTailArray(ArcIndexType new_max_num_arcs) {
1728 if (tail_ !=
nullptr) {
1732 if (tail_->Reserve(
kFirstArc, new_max_num_arcs - 1)) {
1733 for (ArcIndexType
arc = tail_->max_index() + 1;
arc < new_max_num_arcs;
1764 void ReserveInternal(NodeIndexType new_max_num_nodes,
1765 ArcIndexType new_max_num_arcs) {
1772 ReserveTailArray(new_max_num_arcs);
1777 void RecordArc(ArcIndexType
arc, NodeIndexType
tail, NodeIndexType
head) {
1785 void SetTail(
const ArcIndexType
arc,
const NodeIndexType
tail) {
1793 void Attach(NodeIndexType
tail, ArcIndexType
arc) {
1802 if (tail_ !=
nullptr) {
1809 ArcIndexType FindNextOutgoingArc(ArcIndexType
arc)
const {
1818 bool IsOutgoing(
const ArcIndex unused_arc,
1826 bool IsDirect(
const ArcIndex unused_arc)
const {
return true; }
1843 std::unique_ptr<ZVector<NodeIndexType> > tail_;
1852template <
typename GraphType>
1858template <
typename NodeIndexType,
typename ArcIndexType>
1864template <
typename NodeIndexType,
typename ArcIndexType>
1870namespace or_internal {
1876template <
typename GraphType,
bool has_reverse_arcs>
1886template <
typename GraphType>
1899template <
typename GraphType,
bool has_reverse_arcs>
1909template <
typename GraphType>
1920template <
typename GraphType>
1928 tail_array_builder(graph_);
1929 return tail_array_builder.BuildTailArray();
1935 tail_array_releaser(graph_);
1936 tail_array_releaser.ReleaseTailArray();
1943template <
typename GraphType>
1950 typename GraphType::ArcIndex
b)
const {
1951 return ((graph_.Tail(
a) < graph_.Tail(
b)) ||
1952 ((graph_.Tail(
a) == graph_.Tail(
b)) &&
1953 (graph_.Head(
a) < graph_.Head(
b))));
1957 const GraphType& graph_;
1960namespace or_internal {
1967template <
typename GraphType,
bool is_dynamic>
1971 typename GraphType::ArcIndex max_num_arcs,
1973 : num_arcs_(0), sort_arcs_(sort_arcs) {
1974 Reserve(max_num_nodes, max_num_arcs);
1977 typename GraphType::ArcIndex
AddArc(
typename GraphType::NodeIndex
tail,
1978 typename GraphType::NodeIndex
head) {
1979 DCHECK_LT(num_arcs_, max_num_arcs_);
1980 DCHECK_LT(
tail, GraphType::kFirstNode + max_num_nodes_);
1981 DCHECK_LT(
head, GraphType::kFirstNode + max_num_nodes_);
1982 if (num_arcs_ < max_num_arcs_ &&
1983 tail < GraphType::kFirstNode + max_num_nodes_ &&
1984 head < GraphType::kFirstNode + max_num_nodes_) {
1985 typename GraphType::ArcIndex result = GraphType::kFirstArc + num_arcs_;
1986 arcs_.push_back(std::make_pair(
tail,
head));
1991 return GraphType::kNilArc;
1997 client_cycle_handler) {
1998 GraphType*
graph =
new GraphType(max_num_nodes_, num_arcs_, sort_arcs_,
1999 &arcs_, client_cycle_handler);
2005 bool Reserve(
typename GraphType::NodeIndex new_max_num_nodes,
2006 typename GraphType::ArcIndex new_max_num_arcs) {
2007 max_num_nodes_ = new_max_num_nodes;
2008 max_num_arcs_ = new_max_num_arcs;
2009 arcs_.reserve(new_max_num_arcs);
2013 typename GraphType::NodeIndex max_num_nodes_;
2014 typename GraphType::ArcIndex max_num_arcs_;
2015 typename GraphType::ArcIndex num_arcs_;
2018 std::pair<typename GraphType::NodeIndex, typename GraphType::NodeIndex> >
2021 const bool sort_arcs_;
2027template <
typename GraphType>
2031 typename GraphType::ArcIndex max_num_arcs,
2033 : graph_(new GraphType(max_num_nodes, max_num_arcs)),
2034 sort_arcs_(sort_arcs) {}
2036 bool Reserve(
const typename GraphType::NodeIndex new_max_num_nodes,
2037 const typename GraphType::ArcIndex new_max_num_arcs) {
2038 return graph_->Reserve(new_max_num_nodes, new_max_num_arcs);
2042 const typename GraphType::NodeIndex
tail,
2043 const typename GraphType::NodeIndex
head) {
2048 client_cycle_handler) {
2053 graph_->GroupForwardArcsByFunctor(arc_ordering, client_cycle_handler);
2056 GraphType* result = graph_;
2062 GraphType*
const graph_;
2063 const bool sort_arcs_;
2068template <
typename GraphType>
2071 GraphType, graph_traits<GraphType>::is_dynamic> {
2074 typename GraphType::ArcIndex num_arcs,
2078 num_nodes, num_arcs, sort_arcs) {}
2092template <
typename GraphType>
2094 if (line_graph ==
nullptr) {
2095 LOG(DFATAL) <<
"line_graph must not be NULL";
2098 if (line_graph->num_nodes() != 0) {
2099 LOG(DFATAL) <<
"line_graph must be empty";
2102 typedef typename GraphType::ArcIterator ArcIterator;
2103 typedef typename GraphType::OutgoingArcIterator OutgoingArcIterator;
2105 typename GraphType::ArcIndex num_arcs = 0;
2106 for (ArcIterator arc_iterator(
graph); arc_iterator.Ok();
2107 arc_iterator.Next()) {
2108 const typename GraphType::ArcIndex
arc = arc_iterator.Index();
2109 const typename GraphType::NodeIndex
head =
graph.Head(
arc);
2110 for (OutgoingArcIterator iterator(
graph,
head); iterator.Ok();
2115 line_graph->Reserve(
graph.num_arcs(), num_arcs);
2116 for (ArcIterator arc_iterator(
graph); arc_iterator.Ok();
2117 arc_iterator.Next()) {
2118 const typename GraphType::ArcIndex
arc = arc_iterator.Index();
2119 const typename GraphType::NodeIndex
head =
graph.Head(
arc);
2120 for (OutgoingArcIterator iterator(
graph,
head); iterator.Ok();
2122 line_graph->AddArc(
arc, iterator.Index());
AnnotatedGraphBuildManager(typename GraphType::NodeIndex num_nodes, typename GraphType::ArcIndex num_arcs, bool sort_arcs)
ArcFunctorOrderingByTailAndHead(const GraphType &graph)
bool operator()(typename GraphType::ArcIndex a, typename GraphType::ArcIndex b) const
void SetTempFromIndex(ArcIndexType source) override
void SetIndexFromTemp(ArcIndexType destination) const override
void SetIndexFromIndex(ArcIndexType source, ArcIndexType destination) const override
CycleHandlerForAnnotatedArcs & operator=(const CycleHandlerForAnnotatedArcs &)=delete
CycleHandlerForAnnotatedArcs(PermutationCycleHandler< ArcIndexType > *annotation_handler, DerivedGraph *graph)
void SetSeen(ArcIndexType *permutation_element) const override
CycleHandlerForAnnotatedArcs(const CycleHandlerForAnnotatedArcs &)=delete
This type is neither copyable nor movable.
void SetTempFromIndex(ArcIndexType source) override
void SetIndexFromTemp(ArcIndexType destination) const override
Sets a data element from the temporary.
void SetIndexFromIndex(ArcIndexType source, ArcIndexType destination) const override
Moves a data element one step along its cycle.
bool Unseen(ArcIndexType permutation_element) const override
~CycleHandlerForAnnotatedArcs() override
static const ArcIndexType kMaxNumArcs
ZVector< NodeIndexType > head_
Array of node indices. head_[i] contains the head node of arc i.
ZVector< ArcIndexType > next_adjacent_arc_
bool Reserve(NodeIndexType new_max_num_nodes, ArcIndexType new_max_num_arcs)
static const NodeIndexType kFirstNode
The index of the first node in the graph.
static const NodeIndexType kNilNode
The index of the 'nil' node in the graph.
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.
NodeIndexType max_num_nodes_
The maximum number of nodes that the graph can hold.
ArcIndexType end_arc_index() const
ArcIndexType FirstOutgoingOrOppositeIncomingArc(const NodeIndexType node) const
Returns the first arc in node's incidence list.
static const ArcIndexType kNilArc
The index of the 'nil' arc in the graph.
ArcIndexType num_arcs_
The current number of arcs held by the graph.
void GroupForwardArcsByFunctor(const ArcIndexTypeStrictWeakOrderingFunctor &compare, PermutationCycleHandler< ArcIndexType > *annotation_handler)
bool representation_clean_
ArcIndexType NextAdjacentArc(const ArcIndexType arc) const
Returns the next arc following the passed argument in its adjacency list.
ArcIndexType AddArc(NodeIndexType tail, NodeIndexType head)
ArcIndexType max_num_arcs_
The maximum number of arcs that the graph can hold.
bool IsNodeValid(NodeIndexType node) const
NodeIndexType num_nodes_
The maximum index of the node currently held by the graph.
static const NodeIndexType kMaxNumNodes
The maximum possible node index in the graph.
void Initialize(NodeIndexType max_num_nodes, ArcIndexType max_num_arcs)
ZVector< ArcIndexType > first_incident_arc_
Iterator class for traversing the incoming arcs associated to a given node.
void operator=(const IncomingArcIterator &iterator)
Can only assign from an iterator on the same graph.
IncomingArcIterator(const EbertGraph &graph, NodeIndexType node, ArcIndexType arc)
bool Ok() const
Returns true unless all the incoming arcs have been traversed.
ArcIndexType Index() const
Returns the index of the arc currently pointed to by the iterator.
IncomingArcIterator(const EbertGraph &graph, NodeIndexType node)
void Next()
Advances the current incoming arc index.
OutgoingOrOppositeIncomingArcIterator(const EbertGraph &graph, NodeIndexType node)
ArcIndexType Index() const
Returns the index of the arc currently pointed to by the iterator.
bool Ok() const
Returns true unless all the adjancent arcs have been traversed.
OutgoingOrOppositeIncomingArcIterator(const EbertGraph &graph, NodeIndexType node, ArcIndexType arc)
void Next()
Advances the current adjacent arc index.
void operator=(const OutgoingOrOppositeIncomingArcIterator &iterator)
Can only assign from an iterator on the same graph.
ArcIndexType Opposite(const ArcIndexType arc) const
NodeIndexType DirectArcHead(const ArcIndexType arc) const
bool IsOutgoingOrOppositeIncoming(ArcIndexType arc, NodeIndexType node) const
Returns true if arc is incident to node.
EbertGraph(NodeIndexType max_num_nodes, ArcIndexType max_num_arcs)
bool IsReverse(const ArcIndexType arc) const
Returns true if the arc is in the reverse direction.
bool CheckArcBounds(const ArcIndexType arc) const
NodeIndexType Tail(const ArcIndexType arc) const
Returns the tail or start-node of arc.
bool CheckArcValidity(const ArcIndexType arc) const
void BuildRepresentation()
ArcIndexType DirectArc(const ArcIndexType arc) const
Returns the arc in normal/direct direction.
NodeIndexType DirectArcTail(const ArcIndexType arc) const
bool IsDirect(const ArcIndexType arc) const
Returns true if the arc is direct.
bool IsIncoming(ArcIndexType arc, NodeIndexType node) const
Returns true if arc is incoming to node.
ArcIndexType ReverseArc(const ArcIndexType arc) const
Returns the arc in reverse direction.
bool IsOutgoing(ArcIndexType arc, NodeIndexType node) const
Returns true if arc is outgoing from node.
std::string DebugString() const
std::string DebugString() const
void BuildRepresentation()
bool IsIncoming(ArcIndexType arc, NodeIndexType node) const
Returns true if arc is incoming to node.
bool CheckArcValidity(const ArcIndexType arc) const
ForwardEbertGraph(NodeIndexType max_num_nodes, ArcIndexType max_num_arcs)
NodeIndexType Tail(const ArcIndexType arc) const
Returns the tail or start-node of arc.
bool CheckTailIndexValidity(const ArcIndexType arc) const
Returns true if arc is a valid index into the (*tail_) array.
bool TailArrayComplete() const
To be used in a DCHECK().
bool CheckArcBounds(const ArcIndexType arc) const
void SetTempFromIndex(ArcIndexType source) override
CycleHandlerForAnnotatedArcs(PermutationCycleHandler< ArcIndexType > *annotation_handler, NodeIndexType *data)
CycleHandlerForAnnotatedArcs & operator=(const CycleHandlerForAnnotatedArcs &)=delete
CycleHandlerForAnnotatedArcs(const CycleHandlerForAnnotatedArcs &)=delete
This type is neither copyable nor movable.
void SetIndexFromTemp(ArcIndexType destination) const override
Sets a data element from the temporary.
void SetIndexFromIndex(ArcIndexType source, ArcIndexType destination) const override
Moves a data element one step along its cycle.
bool CheckArcValidity(const ArcIndexType arc) const
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)
bool CheckArcBounds(const ArcIndexType arc) const
bool CheckTailIndexValidity(const ArcIndexType arc) const
Returns true if arc is a valid index into the (*tail_) array.
bool TailArrayComplete() const
To be used in a DCHECK().
std::string DebugString() const
ArcIndexType NextOutgoingArc(const NodeIndexType node, ArcIndexType arc) const
NodeIndexType Tail(const ArcIndexType arc) const
Returns the tail or start-node of arc.
bool IsIncoming(ArcIndexType arc, NodeIndexType node) const
Returns true if arc is incoming to node.
void Apply(IndexType permutation[], int permutation_start, int permutation_end)
virtual void SetIndexFromTemp(IndexType destination) const =0
Sets a data element from the temporary.
virtual void SetTempFromIndex(IndexType source)=0
virtual void SetIndexFromIndex(IndexType source, IndexType destination) const =0
Moves a data element one step along its cycle.
bool operator()(ArcIndexType a, ArcIndexType b) const
PermutationIndexComparisonByArcHead(const ZVector< NodeIndexType > &head)
Iterator class for traversing the arcs in the graph.
void Next()
Advances the current arc index.
ArcIterator(const DerivedGraph &graph)
ArcIndexType Index() const
Returns the index of the arc currently pointed to by the iterator.
bool Ok() const
Returns true unless all the arcs have been traversed.
Iterator class for traversing all the nodes in the graph.
bool Ok() const
Returns true unless all the nodes have been traversed.
NodeIndexType Index() const
Returns the index of the node currently pointed to by the iterator.
void Next()
Advances the current node index.
NodeIterator(const DerivedGraph &graph)
Iterator class for traversing the outgoing arcs associated to a given node.
OutgoingArcIterator(const DerivedGraph &graph, NodeIndexType node)
void operator=(const OutgoingArcIterator &iterator)
Can only assign from an iterator on the same graph.
void Next()
Advances the current outgoing arc index.
OutgoingArcIterator(const DerivedGraph &graph, NodeIndexType node, ArcIndexType arc)
ArcIndexType Index() const
Returns the index of the arc currently pointed to by the iterator.
bool Ok() const
Returns true unless all the outgoing arcs have been traversed.
NodeIndexType end_node_index() const
static const ArcIndexType kMaxNumArcs
ZVector< NodeIndexType > head_
Array of node indices. head_[i] contains the head node of arc i.
static const NodeIndexType kFirstNode
The index of the first node in the graph.
static const NodeIndexType kNilNode
The index of the 'nil' node in the graph.
ArcIndexType NextArc(const ArcIndexType arc) const
std::string ArcDebugString(const ArcIndexType arc) const
static const ArcIndexType kFirstArc
The index of the first arc in the graph.
NodeIndexType max_num_nodes() const
Returns the maximum possible number of nodes in the graph.
NodeIndexType max_num_nodes_
The maximum number of nodes that the graph can hold.
std::string NodeDebugString(const NodeIndexType node) const
ArcIndexType end_arc_index() const
NodeIndexType max_end_node_index() const
static const ArcIndexType kNilArc
The index of the 'nil' arc in the graph.
ArcIndexType num_arcs_
The current number of arcs held by the graph.
NodeIndexType StartNode(NodeIndexType node) const
ArcIndexType max_end_arc_index() const
NodeIndexType NextNode(const NodeIndexType node) const
ArcIndexType max_num_arcs() const
ArcIndexType max_num_arcs_
The maximum number of arcs that the graph can hold.
ArcIndexType StartArc(ArcIndexType arc) const
NodeIndexType num_nodes() const
Returns the number of nodes in the graph.
NodeIndexType Head(const ArcIndexType arc) const
Returns the head or end-node of arc.
ArcIndexType num_arcs() const
ArcIndexType LookUpArc(const NodeIndexType tail, const NodeIndexType head) const
bool IsNodeValid(NodeIndexType node) const
NodeIndexType num_nodes_
The maximum index of the node currently held by the graph.
ArcIndexType FirstOutgoingArc(const NodeIndexType node) const
Returns the first outgoing arc for node.
static const NodeIndexType kMaxNumNodes
The maximum possible node index in the graph.
ZVector< ArcIndexType > first_incident_arc_
bool BuildTailArrayFromAdjacencyListsIfForwardGraph() const
void ReleaseTailArrayIfForwardGraph() const
TailArrayManager(GraphType *g)
bool Reserve(int64_t new_min_index, int64_t new_max_index)
void Set(int64_t index, T value)
Sets to value the content of the array at index.
int64_t max_index() const
void SetAll(T value)
Sets all the elements in the array to value.
GraphType * Graph(PermutationCycleHandler< typename GraphType::ArcIndex > *client_cycle_handler)
bool Reserve(const typename GraphType::NodeIndex new_max_num_nodes, const typename GraphType::ArcIndex new_max_num_arcs)
GraphBuilderFromArcs(typename GraphType::NodeIndex max_num_nodes, typename GraphType::ArcIndex max_num_arcs, bool sort_arcs)
GraphType::ArcIndex AddArc(const typename GraphType::NodeIndex tail, const typename GraphType::NodeIndex head)
GraphType * Graph(PermutationCycleHandler< typename GraphType::ArcIndex > *client_cycle_handler)
Builds the graph from the given arcs.
GraphBuilderFromArcs(typename GraphType::NodeIndex max_num_nodes, typename GraphType::ArcIndex max_num_arcs, bool sort_arcs)
GraphType::ArcIndex AddArc(typename GraphType::NodeIndex tail, typename GraphType::NodeIndex head)
In SWIG mode, we don't want anything besides these top-level includes.
ZVector< NodeIndex > NodeIndexArray
ForwardEbertGraph< NodeIndex, ArcIndex > ForwardStarGraph
ZVector< FlowQuantity > QuantityArray
ZVector< ArcIndex > ArcIndexArray
ForwardStaticGraph< NodeIndex, ArcIndex > ForwardStarStaticGraph
bool BuildLineGraph(const GraphType &graph, GraphType *const line_graph)
EbertGraph< NodeIndex, ArcIndex > StarGraph
ZVector< CostValue > CostArray
std::optional< int64_t > end
static constexpr bool has_reverse_arcs
static constexpr bool is_dynamic
TailArrayBuilder(GraphType *graph)
bool BuildTailArray() const
TailArrayBuilder(GraphType *unused_graph)
bool BuildTailArray() const
TailArrayReleaser(GraphType *graph)
void ReleaseTailArray() const
void ReleaseTailArray() const
TailArrayReleaser(GraphType *unused_graph)