123#ifndef OR_TOOLS_GRAPH_MAX_FLOW_H_
124#define OR_TOOLS_GRAPH_MAX_FLOW_H_
131#include "absl/strings/string_view.h"
134#include "ortools/graph/flow_problem.pb.h"
142template <
typename Graph>
238 std::vector<NodeIndex> arc_tail_;
239 std::vector<NodeIndex> arc_head_;
240 std::vector<FlowQuantity> arc_capacity_;
241 std::vector<ArcIndex> arc_permutation_;
242 std::vector<FlowQuantity> arc_flow_;
247 typedef ::util::ReverseArcStaticGraph<NodeIndex, ArcIndex>
Graph;
248 std::unique_ptr<Graph> underlying_graph_;
249 std::unique_ptr<GenericMaxFlow<Graph>> underlying_max_flow_;
266template <
typename Element,
typename IntegerPriority>
288 void Push(Element element, IntegerPriority priority);
296 Element PopBack(std::vector<std::pair<Element, IntegerPriority>>* queue);
301 std::vector<std::pair<Element, IntegerPriority>> even_queue_;
302 std::vector<std::pair<Element, IntegerPriority>> odd_queue_;
323template <
typename Graph>
556 template <
bool reverse>
693template <
typename Element,
typename IntegerPriority>
696 return even_queue_.empty() && odd_queue_.empty();
699template <
typename Element,
typename IntegerPriority>
705template <
typename Element,
typename IntegerPriority>
707 Element element, IntegerPriority priority) {
709 DCHECK(even_queue_.empty() || priority >= even_queue_.back().second - 1);
710 DCHECK(odd_queue_.empty() || priority >= odd_queue_.back().second - 1);
716 DCHECK(odd_queue_.empty() || priority >= odd_queue_.back().second);
717 odd_queue_.push_back(std::make_pair(element, priority));
719 DCHECK(even_queue_.empty() || priority >= even_queue_.back().second);
720 even_queue_.push_back(std::make_pair(element, priority));
724template <
typename Element,
typename IntegerPriority>
727 if (even_queue_.empty())
return PopBack(&odd_queue_);
728 if (odd_queue_.empty())
return PopBack(&even_queue_);
729 if (odd_queue_.back().second > even_queue_.back().second) {
730 return PopBack(&odd_queue_);
732 return PopBack(&even_queue_);
736template <
typename Element,
typename IntegerPriority>
738 std::vector<std::pair<Element, IntegerPriority>>* queue) {
739 DCHECK(!queue->empty());
740 Element element = queue->back().first;
void GetSourceSideMinCut(std::vector< NodeIndex > *result)
void SetUseTwoPhaseAlgorithm(bool value)
GenericMaxFlow & operator=(const GenericMaxFlow &)=delete
void PushFlowExcessBackToSource()
void SetArcCapacity(ArcIndex arc, FlowQuantity new_capacity)
Sets the capacity for arc to new_capacity.
void SetCapacityAndClearFlow(ArcIndex arc, FlowQuantity capacity)
Sets the capacity of arc to 'capacity' and clears the flow on arc.
void SetUseGlobalUpdate(bool value)
void Refine()
Performs optimization step.
ZVector< ArcIndex > ArcIndexArray
void PushFlow(FlowQuantity flow, ArcIndex arc)
bool Solve()
Returns true if a maximum flow was solved.
Graph::OutgoingArcIterator OutgoingArcIterator
void InitializePreflow()
Initializes the preflow to a state that enables to run Refine.
const Graph * graph_
A pointer to the graph passed as argument.
bool SaturateOutgoingArcsFromSource()
ZVector< NodeHeight > NodeHeightArray
bool use_two_phase_algorithm_
bool IsEmptyActiveNodeContainer()
Check the emptiness of the container.
void SetCheckInput(bool value)
StatsGroup stats_
Statistics about this class.
void InitializeActiveNodeContainer()
Initializes the container active_nodes_.
NodeIndex source_
The index of the source node in graph_.
QuantityArray residual_arc_capacity_
void PushActiveNode(const NodeIndex &node)
Push element to the active node container.
Graph::IncomingArcIterator IncomingArcIterator
GenericMaxFlow(const GenericMaxFlow &)=delete
This type is neither copyable nor movable.
ArcIndexArray first_admissible_arc_
An array representing the first admissible arc for each node in graph_.
void Discharge(NodeIndex node)
bool IsActive(NodeIndex node) const
virtual ~GenericMaxFlow()
NodeHeightArray node_potential_
FlowModelProto CreateFlowModel()
Returns the protocol buffer representation of the current problem.
QuantityArray node_excess_
An array representing the excess for each node in graph_.
bool CheckInputConsistency() const
bool AugmentingPathExists() const
Status status_
The status of the problem.
PriorityQueueWithRestrictedPush< NodeIndex, NodeHeight > active_node_by_height_
NodeIndex sink_
The index of the sink node in graph_.
Graph::OutgoingOrOppositeIncomingArcIterator OutgoingOrOppositeIncomingArcIterator
const Graph * graph() const
Returns the graph associated to the current object.
NodeIndex GetSinkNodeIndex() const
Returns the index of the node corresponding to the sink of the network.
FlowQuantity GetOptimalFlow() const
Returns the total flow found by the algorithm.
void RefineWithGlobalUpdate()
void SetArcFlow(ArcIndex arc, FlowQuantity new_flow)
Sets the flow for arc.
bool CheckRelabelPrecondition(NodeIndex node) const
FlowQuantity Capacity(ArcIndex arc) const
Graph::NodeIndex NodeIndex
bool process_node_by_height_
void Relabel(NodeIndex node)
bool IsArcDirect(ArcIndex arc) const
std::vector< bool > node_in_bfs_queue_
bool use_global_update_
Whether or not to use GlobalUpdate().
ArcIndex Opposite(ArcIndex arc) const
std::vector< NodeIndex > bfs_queue_
NodeIndex Tail(ArcIndex arc) const
static const FlowQuantity kMaxFlowQuantity
Maximum manageable flow.
std::string DebugString(absl::string_view context, ArcIndex arc) const
void ProcessNodeByHeight(bool value)
bool IsArcValid(ArcIndex arc) const
NodeIndex GetAndRemoveFirstActiveNode()
Get the first element from the active node container.
NodeIndex Head(ArcIndex arc) const
Handy member functions to make the code more compact.
std::vector< NodeIndex > active_nodes_
void GetSinkSideMinCut(std::vector< NodeIndex > *result)
void SetCheckResult(bool value)
void ComputeReachableNodes(NodeIndex start, std::vector< NodeIndex > *result)
GenericMaxFlow(const Graph *graph, NodeIndex source, NodeIndex sink)
bool IsAdmissible(ArcIndex arc) const
Returns true if arc is admissible.
NodeIndex GetSourceNodeIndex() const
Returns the index of the node corresponding to the source of the network.
FlowQuantity Flow(ArcIndex arc) const
MaxFlow(const StarGraph *graph, NodeIndex source, NodeIndex target)
bool IsEmpty() const
Is the queue empty?
void Push(Element element, IntegerPriority priority)
PriorityQueueWithRestrictedPush & operator=(const PriorityQueueWithRestrictedPush &)=delete
PriorityQueueWithRestrictedPush(const PriorityQueueWithRestrictedPush &)=delete
This type is neither copyable nor movable.
PriorityQueueWithRestrictedPush()
void Clear()
Clears the queue.
SimpleMaxFlow & operator=(const SimpleMaxFlow &)=delete
ArcIndex NumArcs() const
Returns the current number of arcs in the graph.
NodeIndex NumNodes() const
FlowModelProto CreateFlowModelProto(NodeIndex source, NodeIndex sink) const
Creates the protocol buffer representation of the current problem.
SimpleMaxFlow(const SimpleMaxFlow &)=delete
This type is neither copyable nor movable.
FlowQuantity OptimalFlow() const
void SetArcCapacity(ArcIndex arc, FlowQuantity capacity)
ArcIndex AddArcWithCapacity(NodeIndex tail, NodeIndex head, FlowQuantity capacity)
void GetSinkSideMinCut(std::vector< NodeIndex > *result)
FlowQuantity Capacity(ArcIndex arc) const
@ BAD_RESULT
This should not happen. There was an error in our code (i.e. file a bug).
@ BAD_INPUT
The input is inconsistent (bad tail/head/capacity values).
Status Solve(NodeIndex source, NodeIndex sink)
NodeIndex Head(ArcIndex arc) const
void GetSourceSideMinCut(std::vector< NodeIndex > *result)
NodeIndex Tail(ArcIndex arc) const
FlowQuantity Flow(ArcIndex arc) const
Base class to print a nice summary of a group of statistics.
void Set(int64_t index, T value)
Sets to value the content of the array at index.
NodeIndexType Tail(ArcIndexType arc) const
NodeIndexType Head(ArcIndexType arc) const
GurobiMPCallbackContext * context
In SWIG mode, we don't want anything besides these top-level includes.