256using namespace Aleph;
260 for (
int i = 1; i <
argc; ++i)
261 if (std::strcmp(
argv[i], flag) == 0)
277 cout <<
"\n" << string(60,
'=') <<
endl;
278 cout <<
"Demo 1: Project Selection (Max Closure Problem)" <<
endl;
281 cout <<
"\nScenario: A tech company is planning next year's projects." <<
endl;
282 cout <<
"\nAvailable projects:" <<
endl;
283 cout <<
" ID Name Profit Prerequisites" <<
endl;
284 cout <<
" -- ---- ------ -------------" <<
endl;
285 cout <<
" 0 Mobile App +$100K -" <<
endl;
286 cout <<
" 1 Backend API +$80K -" <<
endl;
287 cout <<
" 2 Cloud Migration -$50K (infrastructure)" <<
endl;
288 cout <<
" 3 ML Feature +$120K 2 (needs cloud)" <<
endl;
289 cout <<
" 4 Security Audit -$30K (compliance)" <<
endl;
290 cout <<
" 5 GDPR Compliance +$60K 4 (needs audit)" <<
endl;
293 {0, 100000, {},
"Mobile App"},
294 {1, 80000, {},
"Backend API"},
295 {2, -50000, {},
"Cloud Migration"},
296 {3, 120000, {2},
"ML Feature"},
297 {4, -30000, {},
"Security Audit"},
298 {5, 60000, {4},
"GDPR Compliance"}
301 cout <<
"\n--- Solving with Max-Flow Min-Cut ---" <<
endl;
305 cout <<
"\n*** Optimal Selection ***" <<
endl;
307 << result.max_profit <<
endl;
309 cout <<
"\nSelected projects:" <<
endl;
310 for (
size_t id : result.selected)
313 cout <<
" [" <<
id <<
"] " << p.name
314 <<
" (" << (p.profit >= 0 ?
"+" :
"") <<
"$" << p.profit <<
")" <<
endl;
318 cout <<
" Total revenue: $" << result.total_revenue <<
endl;
319 cout <<
" Total costs: $" << result.total_cost <<
endl;
320 cout <<
" Net profit: $" << result.max_profit <<
endl;
322 cout <<
"\nNote: The ML Feature (+$120K) is selected despite requiring" <<
endl;
323 cout <<
"Cloud Migration (-$50K) because net profit is +$70K." <<
endl;
335 cout <<
"\n" << string(60,
'=') <<
endl;
336 cout <<
"Demo 2: Image Segmentation (Graph Cuts)" <<
endl;
339 cout <<
"\nScenario: Segment a 4x4 grayscale image." <<
endl;
340 cout <<
"Dark pixels (low intensity) -> Background (label 0)" <<
endl;
341 cout <<
"Bright pixels (high intensity) -> Foreground (label 1)" <<
endl;
352 cout <<
"\nOriginal image (intensities):" <<
endl;
367 for (
int i = 0; i < 4; ++i)
368 for (
int j = 0; j < 4; ++j)
379 cout <<
"\n--- Computing Optimal Segmentation ---" <<
endl;
383 cout <<
"\nSegmentation result (0=background, 1=foreground):" <<
endl;
384 for (
const auto&
row : result.labels)
387 for (
int label :
row)
388 cout <<
setw(4) << (label ?
"FG" :
"BG");
392 cout <<
"\nTotal energy: " << result.energy <<
endl;
393 cout <<
"(Lower energy = better segmentation)" <<
endl;
395 cout <<
"\nVisualization (# = foreground, . = background):" <<
endl;
396 for (
const auto&
row : result.labels)
399 for (
int label :
row)
400 cout << (label ?
" # " :
" . ");
404 cout <<
"\nNote: The algorithm correctly separates the bright right half" <<
endl;
405 cout <<
"(foreground) from the dark left half (background)." <<
endl;
416 cout <<
"\n" << string(60,
'=') <<
endl;
417 cout <<
"Demo 3: Baseball Elimination" <<
endl;
420 cout <<
"\nScenario: Late-season standings in a 4-team division." <<
endl;
421 cout <<
"\nCurrent standings:" <<
endl;
422 cout <<
" Team Wins Losses Remaining" <<
endl;
423 cout <<
" ---- ---- ------ ---------" <<
endl;
426 cout <<
" Blue Jays 78 78 6" <<
endl;
433 yankees.against = {0, 1, 2, 1};
437 redsox.against = {1, 0, 0, 2};
445 orioles.against = {1, 2, 4, 0};
448 cout <<
"\nRemaining games matrix:" <<
endl;
450 for (
size_t i = 0; i <
teams.
size(); ++i)
452 cout <<
" " <<
setw(4) << left <<
teams[i].name.substr(0, 3) <<
": ";
453 for (
int g :
teams[i].against)
458 cout <<
"\n--- Checking Each Team's Elimination Status ---" <<
endl;
460 for (
size_t i = 0; i <
teams.
size(); ++i)
465 if (result.eliminated)
468 cout <<
" Max possible wins: " << result.max_possible_wins <<
endl;
469 if (!result.certificate.empty())
471 cout <<
" Certificate (teams blocking): ";
472 for (
size_t t : result.certificate)
480 cout <<
" Max possible wins: " << result.max_possible_wins <<
endl;
484 cout <<
"\nNote: A team is eliminated if the sum of wins among a subset" <<
endl;
485 cout <<
"of other teams, plus their remaining games among themselves," <<
endl;
486 cout <<
"makes it impossible to finish in first place." <<
endl;
499 cout <<
"\n" << string(60,
'=') <<
endl;
500 cout <<
"Demo 4: Survey Design" <<
endl;
503 cout <<
"\nScenario: Design a customer feedback survey." <<
endl;
505 cout <<
" Q0: Product quality (needs 2-4 responses)" <<
endl;
506 cout <<
" Q1: Customer service (needs 2-3 responses)" <<
endl;
507 cout <<
" Q2: Pricing feedback (needs 1-3 responses)" <<
endl;
510 cout <<
" R0: Can answer Q0, Q1 (answers 1-2 questions)" <<
endl;
511 cout <<
" R1: Can answer Q0, Q2 (answers 1-2 questions)" <<
endl;
512 cout <<
" R2: Can answer Q1, Q2 (answers 1-2 questions)" <<
endl;
513 cout <<
" R3: Can answer Q0, Q1, Q2 (answers 2-3 questions)" <<
endl;
528 cout <<
"\n--- Finding Feasible Assignment ---" <<
endl;
534 cout <<
"\n*** Feasible Assignment Found! ***" <<
endl;
538 for (
const auto& [r, q] : result.assignments)
544 cout <<
" Respondent " << r <<
" answers: ";
546 cout <<
"Q" << q <<
" ";
551 cout <<
"\nQuestion coverage:" <<
endl;
555 for (
const auto& [r,
qid] : result.assignments)
558 cout <<
" Q" << q <<
": " <<
count <<
" responses "
559 <<
"(need " <<
questions[q].min_responses <<
"-"
565 cout <<
"\nNo feasible assignment exists!" <<
endl;
566 cout <<
"The constraints are too restrictive." <<
endl;
572 cout <<
"=== Network Flow Applications ===" <<
endl;
573 cout <<
"Real-world problems solved with max-flow/min-cut\n" <<
endl;
577 cout <<
"Usage: " <<
argv[0] <<
" [--project-selection] [--image-segmentation] "
578 "[--baseball-elimination] [--survey-design] [--help]\n";
579 cout <<
"\nIf no flags are given, all demos are executed.\n";
602 cout <<
"\n" << string(60,
'=') <<
endl;
607These problems share a common structure:
6091. **Project Selection** (Max Closure)
610 - Model: Projects as nodes, dependencies as infinite edges
611 - Solution: Min s-t cut separates selected from rejected
6132. **Image Segmentation** (Graph Cuts)
614 - Model: Pixels as nodes, neighbor edges with smoothness cost
615 - Solution: Min cut optimally labels foreground/background
6173. **Baseball Elimination**
618 - Model: Game outcomes as flow through team vertices
619 - Solution: If max-flow < total games, team is eliminated
6214. **Survey Design** (Feasibility)
622 - Model: Bipartite matching with lower bounds
623 - Solution: Flow satisfies constraints if feasible
626 Many combinatorial problems reduce to max-flow/min-cut,
627 yielding polynomial-time algorithms for NP-hard looking problems!
size_t size() const noexcept
Count the number of elements of the list.
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.
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Network flow applications.
void demo_baseball_elimination()
Demo 3: Baseball Elimination.
void demo_survey_design()
Demo 4: Survey Design.
void demo_image_segmentation()
Demo 2: Image Segmentation.
static bool has_flag(int argc, char *argv[], const char *flag)
void demo_project_selection()
Demo 1: Project Selection Problem.
Team information for baseball elimination.