87template <
typename Flow_Type>
150template <
class Net,
class GetDemand,
class GetLower,
template <
class>
class Maxflow =
Dinic_Maximum_Flow>
162 if constexpr (std::is_floating_point_v<Flow_Type>)
163 return std::abs(a - b) <= std::max(std::abs(a), std::abs(b)) * 1e-9;
174 original_nodes.
append(it.get_curr());
186 Arc *arc = it.get_curr();
189 original_arcs.
append(Arc_Bounds{arc, arc->cap, lb});
190 result.
flow[arc] = lb;
203 for (
Node *v : original_nodes)
207 [[maybe_unused]]
size_t terms = 1;
210 Arc *arc = ait.get_curr();
216 if constexpr (std::is_floating_point_v<Flow_Type>)
222 if constexpr (std::is_floating_point_v<Flow_Type>)
223 if (std::abs(d) <= std::numeric_limits<Flow_Type>::epsilon() *
Flow_Type(terms + 2) * scale)
234 if (not bounded or not same(need, give))
245 Node *super_source =
nullptr;
246 Node *super_sink =
nullptr;
249 for (
auto it = original_arcs.
get_it(); it.has_curr(); it.next_ne())
251 const Arc_Bounds &b = it.get_curr();
255 if (super_sink !=
nullptr)
258 super_sink =
nullptr;
260 if (super_source !=
nullptr)
263 super_source =
nullptr;
274 for (
Node *v : original_nodes)
284 if (it.get_curr() != super_source)
285 sources.
append(it.get_curr());
287 if (it.get_curr() != super_sink)
288 sinks.
append(it.get_curr());
289 for (
Node *v : sources)
291 for (
Node *v : sinks)
294 for (
auto it = original_arcs.get_it(); it.has_curr(); it.next_ne())
296 const Arc_Bounds &b = it.get_curr();
297 b.arc->cap = b.cap - b.lower;
301 const Flow_Type got = Maxflow<Net>()(net);
302 result.excess_flow = got;
303 result.feasible = same(got, need);
305 for (
auto it = original_arcs.get_it(); it.has_curr(); it.next_ne())
307 const Arc_Bounds &b = it.get_curr();
308 result.flow[b.arc] = b.arc->flow + b.lower;
337template <
typename Value_Type>
345 Project(
size_t id_, Value_Type profit_,
const std::vector<size_t> &prereqs = {},
346 const std::string &name_ =
"")
354template <
typename Value_Type>
396template <
typename Value_Type>
404 if (projects.empty())
408 const Value_Type INF = std::numeric_limits<Value_Type>::max() / 2;
415 std::vector<Node *> project_nodes(projects.size());
416 for (
size_t i = 0; i < projects.size(); ++i)
420 Value_Type sum_positive{0};
422 for (
size_t i = 0; i < projects.size(); ++i)
424 Value_Type p = projects[i].profit;
428 Value_Type source_cap = (p > 0) ? p : Value_Type{0};
429 net.
insert_arc(source, project_nodes[i], source_cap);
435 Value_Type sink_cap = (p < 0) ? -p : Value_Type{0};
436 net.
insert_arc(project_nodes[i], sink, sink_cap);
439 for (
size_t prereq : projects[i].prerequisites)
440 if (prereq < projects.size())
441 net.
insert_arc(project_nodes[i], project_nodes[prereq], INF);
464 auto arc = it.get_curr();
473 residual = arc->cap - arc->flow;
475 residual = arc->flow;
477 if (residual > Value_Type{0})
486 for (
size_t i = 0; i < projects.size(); ++i)
487 if (reachable.
contains(project_nodes[i]))
490 if (projects[i].profit > 0)
514 Team(
const std::string &n =
"",
int w = 0,
int l = 0,
int r = 0)
570 if (team_idx >= teams.size())
573 const Team &x = teams[team_idx];
577 for (
size_t i = 0; i < teams.size(); ++i)
591 std::vector<Node *> team_nodes(teams.size(),
nullptr);
592 for (
size_t i = 0; i < teams.size(); ++i)
605 for (
size_t i = 0; i < teams.size(); ++i)
610 for (
size_t j = i + 1; j < teams.size(); ++j)
615 int games = teams[i].against[j];
618 constexpr int INF = 1000000;
619 total_games += games;
623 net.
insert_arc(game_node, team_nodes[i], INF);
624 net.
insert_arc(game_node, team_nodes[j], INF);
630 if (total_games == 0)
636 for (
Node *p : team_nodes)
660 const auto arc = it.get_curr();
666 if (
const int residual =
667 (net.
get_src_node(arc) == u) ? (arc->cap - arc->flow) : arc->flow;
676 for (
size_t i = 0; i < teams.size(); ++i)
677 if (i != team_idx
and team_nodes[i] !=
nullptr and reachable.
contains(team_nodes[i]))
734template <
typename Value_Type>
736 const std::vector<std::vector<std::array<Value_Type, 2>>> &data_cost,
737 Value_Type smoothness)
755 std::vector<std::vector<Node *>> pixels(
rows, std::vector<Node *>(
cols));
756 for (
size_t i = 0; i <
rows; ++i)
757 for (
size_t j = 0; j <
cols; ++j)
762 for (
size_t i = 0; i <
rows; ++i)
763 for (
size_t j = 0; j <
cols; ++j)
767 net.
insert_arc(source, pixels[i][j], data_cost[i][j][0]);
771 net.
insert_arc(pixels[i][j], sink, data_cost[i][j][1]);
775 for (
size_t i = 0; i <
rows; ++i)
776 for (
size_t j = 0; j <
cols; ++j)
781 net.
insert_arc(pixels[i][j], pixels[i][j + 1], smoothness);
782 net.
insert_arc(pixels[i][j + 1], pixels[i][j], smoothness);
788 net.
insert_arc(pixels[i][j], pixels[i + 1][j], smoothness);
789 net.
insert_arc(pixels[i + 1][j], pixels[i][j], smoothness);
806 foreground.
insert(source);
815 auto arc = it.get_curr();
821 Value_Type residual = (net.
get_src_node(arc) == u) ? (arc->cap - arc->flow) : arc->flow;
823 if (residual > Value_Type{0})
832 for (
size_t i = 0; i <
rows; ++i)
833 for (
size_t j = 0; j <
cols; ++j)
891 const std::vector<SurveyRespondent> &respondents)
899 if (questions.empty() or respondents.empty())
903 for (
const auto &q : questions)
904 if (q.min_responses < 0 or q.min_responses > q.max_responses)
906 for (
const auto &
r : respondents)
907 if (
r.min_questions < 0 or
r.min_questions >
r.max_questions)
919 std::vector<Node *> question_nodes(questions.size());
920 for (
size_t i = 0; i < questions.size(); ++i)
923 Arc *a = net.
insert_arc(question_nodes[i], sink, questions[i].max_responses);
924 lower.
insert(a, questions[i].min_responses);
928 std::vector<std::vector<std::pair<size_t, Arc *>>> answers(respondents.size());
929 for (
size_t i = 0; i < respondents.size(); ++i)
933 lower.
insert(a, respondents[i].min_questions);
934 most += respondents[i].max_questions;
936 for (
size_t q : respondents[i].eligible_questions)
937 if (q < questions.size())
938 answers[i].push_back({q, net.
insert_arc(
r, question_nodes[q], 1)});
944 [](
Node *) {
return 0; },
948 return p ==
nullptr ? 0 : p->second;
951 result.
feasible = circulation.feasible;
953 for (
size_t r = 0;
r < respondents.size(); ++
r)
954 for (
const auto &[q, a] : answers[
r])
955 if (circulation.flow.find(a) > 0)
WeightedDigraph::Node Node
Iterator over the arcs of a node.
bool has_curr() const noexcept
Return true if the iterator is on an arc.
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
T & front()
Return a modifiable reference to the oldest item in the queue.
bool is_empty() const noexcept
Return true if this is empty.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
bool contains(const Key &key) const
Checks if a key exists in the set.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Filtered iterator on the nodes of a graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_connected_node(Arc *arc, Node *node) const noexcept
Return the adjacent node to node through arc.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Main namespace for Aleph-w library functions.
BaseballEliminationResult check_baseball_elimination(const std::vector< Team > &teams, size_t team_idx)
Check if a team is mathematically eliminated from winning.
Net::Flow_Type dinic_maximum_flow(Net &net)
Compute maximum flow using Dinic's algorithm.
and
Check uniqueness with explicit hash + equality functors.
ProjectSelectionResult< Value_Type > solve_project_selection(const std::vector< Project< Value_Type > > &projects)
Solve project selection problem using max-flow.
SegmentationResult segment_image(size_t rows, size_t cols, const std::vector< std::vector< std::array< Value_Type, 2 > > > &data_cost, Value_Type smoothness)
Segment image using graph cuts.
SurveyDesignResult design_survey(const std::vector< SurveyQuestion > &questions, const std::vector< SurveyRespondent > &respondents)
Design survey assignment using network flow.
CirculationResult< typename Net::Flow_Type > solve_circulation(Net &net, GetDemand get_demand, GetLower get_lower)
Solve a circulation problem with demands.
Net::Flow_Type min_cut(Net &net, DynSetTree< typename Net::Node * > &vs, DynSetTree< typename Net::Node * > &vt, DynList< typename Net::Arc * > &cuts, DynList< typename Net::Arc * > &cutt)
Compute max flow and the corresponding minimum cut.
Filtered iterator on all the arcs of a graph.
Result of baseball elimination check.
std::vector< size_t > certificate
Teams that form elimination certificate.
int max_possible_wins
Maximum wins team can achieve.
bool eliminated
Is the team mathematically eliminated?
Result of a circulation problem.
bool feasible
Is there a feasible circulation?
Flow_Type excess_flow
Flow needed to satisfy demands.
DynMapTree< void *, Flow_Type > flow
Flow on each edge (arc pointer -> flow)
Functor wrapper for Dinic's algorithm.
Arc of a flow network implemented with adjacency lists.
Flow network implemented with adjacency lists.
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
const DynSetTree< Node * > & get_sink_nodes() const noexcept
Return the set of sink nodes.
const DynSetTree< Node * > & get_src_nodes() const noexcept
Return the set of source nodes.
void make_super_source()
Convert a multi-source network into a single super-source network.
void make_super_sink()
Convert a multi-sink network into a single super-sink network.
Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &flow, const typename Arc::Arc_Type &arc_info=Arc_Type())
Insert a capacitated arc with an initial flow.
void remove_node(Node *p) override
Remove node p and all its arcs from the network.
typename Arc::Flow_Type Flow_Type
Capacity/flow numeric type.
size_t get_in_degree(Node *p) const noexcept
Return the in-degree of p (number of incoming arcs).
Result of project selection.
Value_Type max_profit
Maximum achievable profit.
std::vector< size_t > selected
IDs of selected projects.
Value_Type total_cost
Sum of negative profits (costs)
Value_Type total_revenue
Sum of positive profits.
Project with profit and dependencies.
Value_Type profit
Profit (positive) or cost (negative)
size_t id
Unique project ID.
std::string name
Optional name.
std::vector< size_t > prerequisites
IDs of prerequisite projects.
Project(size_t id_, Value_Type profit_, const std::vector< size_t > &prereqs={}, const std::string &name_="")
Result of binary image segmentation.
std::vector< std::vector< int > > labels
0 or 1 for each pixel
double energy
Total energy of segmentation.
std::vector< std::pair< size_t, size_t > > assignments
(respondent, question)
Survey question with constraints.
int max_responses
Maximum responses accepted.
int min_responses
Minimum number of responses needed.
Survey respondent with constraints.
std::vector< size_t > eligible_questions
Questions this respondent can answer.
int min_questions
Minimum questions to answer.
int max_questions
Maximum questions can answer.
Team information for baseball elimination.
std::vector< int > against
Games remaining against each team.
Team(const std::string &n="", int w=0, int l=0, int r=0)
int remaining
Games remaining.
Dynamic queue implementation based on linked lists.
Dynamic set implementations based on balanced binary search trees.
Advanced maximum flow algorithms.
Network flow graph structures.
Maximum flow minimum cost network algorithms.