Repository navigation
fix(graph): restore reverse residual capacity in BFS and DFS maximum flow - #2788
Miladkhoshdel wants to merge 3 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to The change appears ready to merge after normal checks. Pre-merge checks |
|
saliou19
left a comment
There was a problem hiding this comment.
Approved in CodeRabbit Change Stack
Closes #2787
Problem
maximum_flow_bfsandmaximum_flow_dfscan return a flow smaller than the true maximum when reaching the optimal result requires rerouting an earlier augmenting path.Both functions subtract the amount of flow sent from each edge’s forward capacity, but they never increase the corresponding reverse residual capacity. As a result, subsequent searches cannot cancel earlier flow and choose a better route.
For example, the BFS regression network supports two paths, each carrying one unit:
0 → 1 → 4 → 50 → 2 → 3 → 5However, BFS first selects
0 → 1 → 3 → 5. Reaching the maximum flow then requires traversing the reverse residual edge3 → 1to reroute that flow. Without this edge, the function stops and returns1instead of2.The DFS implementation has the same defect, reproduced with a different vertex ordering of the same network.
Changes
After sending flow along an edge
u → v, the residual graph now reduces capacity onu → vand increases capacity onv → uby the same amount.Validation
1before the fix and now correctly return2.python3 -m unittest tests.test_graph -q.git diff --checkpasses.Summary by CodeRabbit