forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmax_flow.cc
148 lines (129 loc) · 4.78 KB
/
max_flow.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/graph/max_flow.h"
#include <algorithm>
#include <memory>
#include <vector>
#include "ortools/graph/generic_max_flow.h"
#include "ortools/graph/graph.h"
namespace operations_research {
SimpleMaxFlow::SimpleMaxFlow() : num_nodes_(0) {}
SimpleMaxFlow::ArcIndex SimpleMaxFlow::AddArcWithCapacity(
NodeIndex tail, NodeIndex head, FlowQuantity capacity) {
const ArcIndex num_arcs = arc_tail_.size();
num_nodes_ = std::max(num_nodes_, tail + 1);
num_nodes_ = std::max(num_nodes_, head + 1);
arc_tail_.push_back(tail);
arc_head_.push_back(head);
arc_capacity_.push_back(capacity);
return num_arcs;
}
SimpleMaxFlow::NodeIndex SimpleMaxFlow::NumNodes() const { return num_nodes_; }
SimpleMaxFlow::ArcIndex SimpleMaxFlow::NumArcs() const {
return arc_tail_.size();
}
SimpleMaxFlow::NodeIndex SimpleMaxFlow::Tail(ArcIndex arc) const {
return arc_tail_[arc];
}
SimpleMaxFlow::NodeIndex SimpleMaxFlow::Head(ArcIndex arc) const {
return arc_head_[arc];
}
SimpleMaxFlow::FlowQuantity SimpleMaxFlow::Capacity(ArcIndex arc) const {
return arc_capacity_[arc];
}
void SimpleMaxFlow::SetArcCapacity(ArcIndex arc, FlowQuantity capacity) {
arc_capacity_[arc] = capacity;
}
SimpleMaxFlow::Status SimpleMaxFlow::Solve(NodeIndex source, NodeIndex sink) {
const ArcIndex num_arcs = arc_capacity_.size();
arc_flow_.assign(num_arcs, 0);
underlying_max_flow_.reset();
underlying_graph_.reset();
optimal_flow_ = 0;
if (source == sink || source < 0 || sink < 0) {
return BAD_INPUT;
}
if (source >= num_nodes_ || sink >= num_nodes_) {
return OPTIMAL;
}
underlying_graph_ = std::make_unique<Graph>(num_nodes_, num_arcs);
underlying_graph_->AddNode(source);
underlying_graph_->AddNode(sink);
for (int arc = 0; arc < num_arcs; ++arc) {
underlying_graph_->AddArc(arc_tail_[arc], arc_head_[arc]);
}
underlying_graph_->Build(&arc_permutation_);
underlying_max_flow_ = std::make_unique<GenericMaxFlow<Graph>>(
underlying_graph_.get(), source, sink);
for (ArcIndex arc = 0; arc < num_arcs; ++arc) {
ArcIndex permuted_arc =
arc < arc_permutation_.size() ? arc_permutation_[arc] : arc;
underlying_max_flow_->SetArcCapacity(permuted_arc, arc_capacity_[arc]);
}
if (underlying_max_flow_->Solve()) {
optimal_flow_ = underlying_max_flow_->GetOptimalFlow();
for (ArcIndex arc = 0; arc < num_arcs; ++arc) {
ArcIndex permuted_arc =
arc < arc_permutation_.size() ? arc_permutation_[arc] : arc;
arc_flow_[arc] = underlying_max_flow_->Flow(permuted_arc);
}
}
// Translate the GenericMaxFlow::Status. It is different because NOT_SOLVED
// does not make sense in the simple api.
switch (underlying_max_flow_->status()) {
case GenericMaxFlow<Graph>::NOT_SOLVED:
return BAD_RESULT;
case GenericMaxFlow<Graph>::OPTIMAL:
return OPTIMAL;
case GenericMaxFlow<Graph>::INT_OVERFLOW:
return POSSIBLE_OVERFLOW;
case GenericMaxFlow<Graph>::BAD_INPUT:
return BAD_INPUT;
case GenericMaxFlow<Graph>::BAD_RESULT:
return BAD_RESULT;
}
return BAD_RESULT;
}
SimpleMaxFlow::FlowQuantity SimpleMaxFlow::OptimalFlow() const {
return optimal_flow_;
}
SimpleMaxFlow::FlowQuantity SimpleMaxFlow::Flow(ArcIndex arc) const {
return arc_flow_[arc];
}
void SimpleMaxFlow::GetSourceSideMinCut(std::vector<NodeIndex>* result) {
if (underlying_max_flow_ == nullptr) return;
underlying_max_flow_->GetSourceSideMinCut(result);
}
void SimpleMaxFlow::GetSinkSideMinCut(std::vector<NodeIndex>* result) {
if (underlying_max_flow_ == nullptr) return;
underlying_max_flow_->GetSinkSideMinCut(result);
}
FlowModelProto SimpleMaxFlow::CreateFlowModelProto(NodeIndex source,
NodeIndex sink) const {
FlowModelProto model;
model.set_problem_type(FlowModelProto::MAX_FLOW);
for (int n = 0; n < num_nodes_; ++n) {
FlowNodeProto* node = model.add_nodes();
node->set_id(n);
if (n == source) node->set_supply(1);
if (n == sink) node->set_supply(-1);
}
for (int a = 0; a < arc_tail_.size(); ++a) {
FlowArcProto* arc = model.add_arcs();
arc->set_tail(Tail(a));
arc->set_head(Head(a));
arc->set_capacity(Capacity(a));
}
return model;
}
} // namespace operations_research