256using namespace Aleph;
258static bool has_flag(
int argc,
char* argv[],
const char* flag)
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;
279 cout << string(60,
'=') << 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;
292 vector<Project<double>> projects = {
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;
306 cout <<
"Maximum profit: $" << fixed << setprecision(0)
307 << result.max_profit << endl;
309 cout <<
"\nSelected projects:" << endl;
310 for (
size_t id : result.selected)
312 const auto& p = projects[id];
313 cout <<
" [" <<
id <<
"] " << p.name
314 <<
" (" << (p.profit >= 0 ?
"+" :
"") <<
"$" << p.profit <<
")" << endl;
317 cout <<
"\nAnalysis:" << 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;
337 cout << string(60,
'=') << 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;
345 vector<vector<int>> image = {
352 cout <<
"\nOriginal image (intensities):" << endl;
353 for (
const auto&
row : image)
357 cout << setw(4) << val;
364 vector<vector<array<double, 2>>> data_cost(4,
365 vector<array<double, 2>>(4));
367 for (
int i = 0; i < 4; ++i)
368 for (
int j = 0; j < 4; ++j)
370 double intensity = image[i][j];
371 data_cost[i][j][0] = intensity;
372 data_cost[i][j][1] = 255 - intensity;
375 double smoothness = 50.0;
377 cout <<
"\nSmooth penalty for label changes: " << smoothness << endl;
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;
418 cout << string(60,
'=') << 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;
424 cout <<
" Yankees 83 71 8" << endl;
425 cout <<
" Red Sox 80 79 3" << endl;
426 cout <<
" Blue Jays 78 78 6" << endl;
427 cout <<
" Orioles 75 84 3" << endl;
432 Team yankees(
"Yankees", 83, 71, 8);
433 yankees.
against = {0, 1, 2, 1};
434 teams.push_back(yankees);
436 Team redsox(
"Red Sox", 80, 79, 3);
438 teams.push_back(redsox);
440 Team bluejays(
"Blue Jays", 78, 78, 6);
441 bluejays.
against = {2, 0, 0, 4};
442 teams.push_back(bluejays);
444 Team orioles(
"Orioles", 75, 84, 3);
445 orioles.
against = {1, 2, 4, 0};
446 teams.push_back(orioles);
448 cout <<
"\nRemaining games matrix:" << endl;
449 cout <<
" NYY BOS TOR BAL" << 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)
454 cout << setw(4) << g;
458 cout <<
"\n--- Checking Each Team's Elimination Status ---" << endl;
460 for (
size_t i = 0; i < teams.size(); ++i)
464 cout <<
"\n" << teams[i].name <<
": ";
465 if (result.eliminated)
467 cout <<
"ELIMINATED!" << endl;
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)
473 cout << teams[t].name <<
" ";
479 cout <<
"Can still win!" << endl;
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;
501 cout << string(60,
'=') << endl;
503 cout <<
"\nScenario: Design a customer feedback survey." << endl;
504 cout <<
"\nQuestions:" << 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;
509 cout <<
"\nRespondents:" << 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;
515 vector<SurveyQuestion> questions = {
521 vector<SurveyRespondent> respondents = {
528 cout <<
"\n--- Finding Feasible Assignment ---" << endl;
534 cout <<
"\n*** Feasible Assignment Found! ***" << endl;
537 map<size_t, vector<size_t>> resp_questions;
538 for (
const auto& [
r, q] : result.assignments)
539 resp_questions[
r].push_back(q);
541 cout <<
"\nAssignments:" << endl;
542 for (
const auto& [
r, qs] : resp_questions)
544 cout <<
" Respondent " <<
r <<
" answers: ";
546 cout <<
"Q" << q <<
" ";
551 cout <<
"\nQuestion coverage:" << endl;
552 for (
size_t q = 0; q < questions.size(); ++q)
555 for (
const auto& [
r, qid] : result.assignments)
556 if (qid == q)
count++;
558 cout <<
" Q" << q <<
": " <<
count <<
" responses "
559 <<
"(need " << questions[q].min_responses <<
"-"
560 << questions[q].max_responses <<
")" << endl;
565 cout <<
"\nNo feasible assignment exists!" << endl;
566 cout <<
"The constraints are too restrictive." << endl;
570int main(
int argc,
char* argv[])
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";
583 const bool any_specific =
584 has_flag(argc, argv,
"--project-selection") ||
585 has_flag(argc, argv,
"--image-segmentation") ||
586 has_flag(argc, argv,
"--baseball-elimination") ||
587 has_flag(argc, argv,
"--survey-design");
589 if (!any_specific ||
has_flag(argc, argv,
"--project-selection"))
592 if (!any_specific ||
has_flag(argc, argv,
"--image-segmentation"))
595 if (!any_specific ||
has_flag(argc, argv,
"--baseball-elimination"))
598 if (!any_specific ||
has_flag(argc, argv,
"--survey-design"))
602 cout <<
"\n" << string(60,
'=') << endl;
603 cout <<
"Summary" << endl;
604 cout << 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!
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.
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.
std::vector< int > against
Games remaining against each team.