Skip to content

fix: boykov_kolmogorov_max_flow color map with several edges to the sink - #656

Merged
Becheler merged 2 commits into
boostorg:developfrom
BayKeremm:fix-bk-color-map
Oct 28, 2026
Merged

Becheler merged 2 commits into
boostorg:developfrom
BayKeremm:fix-bk-color-map

Conversation

@BayKeremm

@BayKeremm BayKeremm commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

AI disclosure: I used an AI assistant (Claude) to help write the tests, trace the cause and draft this text. I have checked the analysis and the fix myself and take responsibility for them.

Motivation

Fixes #655

When a vertex has more than one edge to the sink, the color map of boykov_kolmogorov_max_flow is not a minimum cut, although the header says it is ("Use this if you are interested in the minimum cut, as the color map provides that info"). The flow returned is correct; a vertex the source still reaches is left gray. The issue has the details and a reproducer.

What this PR does

Two commits

  1. test: adds test_color_map_is_min_cut() to test/boykov_kolmogorov_max_flow_test.cpp, using the issue's two small graphs and no random ones. The first has two parallel edges to the sink; in the second, an edge out of the sink is followed by one into it. The test checks the flow, that vertex 1 is black, and that the cut the color map describes costs exactly the flow. On develop the flow is right but the other two checks fail in both graphs: 4 failures, so CI goes red.
  2. fix: adds one condition in augment_direct_paths(): the loop over the sink's edges no longer moves a vertex that the loop over the source's edges has already put in the source tree. That vertex is active, so grow() finds the path through its remaining edges to the sink. Before, the vertex was moved to the sink tree, left gray once those edges were saturated, and never reached again from the source, which is not an active vertex. CI goes green.

The existing tests missed this because they compare flows, which were always right. invariant_four checks that no residual edge crosses from one tree to another, but it skips the source, and the missing edge runs from the source to a gray vertex.

Tested with g++ 16.2.1 and clang 23.1.3 (libstdc++), at -std=c++14, 17, 20 and 23, -Wall -Wextra: no new warnings, and boykov_kolmogorov_max_flow_test, max_flow_test and max_flow_algorithms_bundled_properties_and_named_params pass. Separately, the color map with this fix was checked against brute force on 3,000 random networks of up to 10 vertices (every cut enumerated; parallel, reciprocal and zero-capacity edges, many ties) and against Edmonds-Karp on 320 larger ones. With the fix it was always a minimum cut, and on integer capacities exactly the smallest one.

Before submitting

  • I searched for an existing PR or issue covering the same change.
  • This PR targets the develop branch.
  • My contribution is licensed under the Boost Software License 1.0.

Kind of change

  • Reformat (layout only, no token added or removed)
  • Refactor (no observable behavior change)
  • Behavior change (observable to users of the library)
  • No code change (documentation, build, CI, or tooling)

Change category

  • Bug fix
  • New algorithm or API addition
  • Deprecation or removal
  • Performance
  • Tests
  • Documentation
  • Build, CI, or tooling
  • Other (specify below)

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Boost dependency footprint vs develop (auto-generated).
PR run 40300387296 vs develop run 39949899700 (f954df869f).

Header-inclusion weights (graph files pulling each direct dependency in):

No header-inclusion-weight changes.

Transitive Boost modules: 47 → 47 (0)

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Compiler-warning counts vs develop (auto-generated).
PR run 40300387063 vs develop run 40490763514 (f954df869f).

Job Baseline After Delta
macos (clang, 14) 256 256 0
macos (clang, 17) 255 255 0
macos (clang, 20) 255 255 0
ubuntu (clang-19, 14) 256 256 0
ubuntu (clang-19, 17) 255 255 0
ubuntu (clang-19, 20) 255 255 0
ubuntu (clang-19, 23) 255 255 0
ubuntu (gcc-14, 14) 352 352 0
ubuntu (gcc-14, 17) 348 348 0
ubuntu (gcc-14, 20) 348 348 0
ubuntu (gcc-14, 23) 348 348 0
windows_msvc_14_3 (msvc-14.3) 961 961 0

@codecov

codecov Bot commented Oct 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…sink

The color map should describe a minimum cut: the black vertices are the
source side, and the edges from them to the others carry exactly the
flow. Two small graphs in which vertex 1 has two edges to the sink break
it: the flow is right, but vertex 1 is left gray although the source
still reaches it. The random graph of test_adjacency_list_vecS breaks it
too, so that test now checks the cut as well. This fails until the fix
that follows.

Refs boostorg#655
augment_direct_paths() looked up only a vertex's first edge to the sink
when it put the vertex in the source tree, then moved every vertex with
residual capacity left to the sink into the sink tree, whatever its tree.
A vertex with a second edge to the sink was moved, left gray once that
edge was saturated, and never reached again from the source, which is
not an active vertex. The flow was still maximal, but the color map was
not a minimum cut.

A vertex already in the source tree now stays there: it is active, so
grow() finds its remaining edges to the sink.

Fixes boostorg#655
@Becheler Becheler added algorithm type of issue related to algorithms fix labels Oct 28, 2026

@Becheler Becheler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much @BayKeremm for this fix ! 🚀

@Becheler
Becheler merged commit 2d3a278 into boostorg:develop Oct 28, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

algorithm type of issue related to algorithms fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

boykov_kolmogorov_max_flow: color map is not a minimum cut when a vertex has two edges to the sink

2 participants