bellman_ford_shortest_paths misses negative cycles when integral distances overflow
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 48/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- cpp
Hướng nghiên cứu
Start with the reproducer and expected versus actual behavior in this issue, then read the bellman_ford_shortest_paths implementation and its tests. Check how the final negative-cycle check handles integral distances that have saturated at the minimum value. Done means the reproducer reports a negative cycle for both weight magnitudes without undefined signed overflow.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Before filing
- I searched the existing issues and did not find a duplicate.
- I have reproduced the bug against the
developbranch or the latest Boost release. - I have a minimal reproducer (or I will paste my full failing code below).
Boost version
develop at 08d01bd5, and the latest Boost release on Compiler Explorer.
Compiler family
- GCC / g++
- Clang / clang++
- MSVC (Visual Studio)
- Intel / oneAPI
- Other (specify below)
Exact compiler version: Apple clang 21.0.0
Standard library
- libstdc++
- libc++
- MSVC STL
- Other (specify below)
Operating system
- Linux
- macOS
- Windows
- Other (specify below)
Kind of component affected
- Graph data structure (e.g.
adjacency_list,adjacency_matrix) - Algorithm (e.g.
dijkstra_shortest_paths,breadth_first_search) - Property map
- I/O or file-format reader (e.g.
read_graphml,read_graphviz) - Visitor / event hooks
- Other (specify below)
Exact name of the affected component: bellman_ford_shortest_paths
Steps to reproduce
Compiler Explorer: https://godbolt.org/z/rvTxrY1cE
Bellman-Ford on the negative 2-cycle 0 → 1 → 0 with int weights, once with w = -1 and once with w = -1e9. It runs once with closed_plus and once with the saturating combine proposed in #677.
Reproducer
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/bellman_ford_shortest_paths.hpp>
#include <cstddef>
#include <functional>
#include <iostream>
#include <limits>
#include <vector>
using graph_t = boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, boost::no_property, boost::property< boost::edge_weight_t, int > >;
// Saturating addition as proposed in boostorg/graph#677.
struct saturating_plus
{
int operator()(int a, int b) const
{
auto const max_int = (std::numeric_limits< int >::max)();
auto const lowest_int = (std::numeric_limits< int >::lowest)();
if (a == max_int || b == max_int)
{
return max_int;
}
if (b > 0 && a > max_int - b)
{
return max_int;
}
if (b < 0 && a < lowest_int - b)
{
return lowest_int;
}
return a + b;
}
};
// Runs Bellman-Ford on the negative 2-cycle 0 -> 1 -> 0 and prints the verdict.
template < class Combine > void run(char const* label, int weight, Combine combine)
{
graph_t g(2);
boost::add_edge(0, 1, weight, g);
boost::add_edge(1, 0, weight, g);
std::vector< int > distance(2, (std::numeric_limits< int >::max)());
distance[0] = 0;
std::vector< std::size_t > parent = { 0, 1 };
auto index_map = boost::get(boost::vertex_index, g);
auto distance_map = boost::make_iterator_property_map(distance.begin(), index_map);
auto parent_map = boost::make_iterator_property_map(parent.begin(), index_map);
auto weight_map = boost::get(boost::edge_weight, g);
bool const no_negative_cycle = boost::bellman_ford_shortest_paths(g, 2, weight_map, parent_map, distance_map, combine, std::less< int >(), boost::default_bellman_visitor());
std::cout << label << ", w = " << weight << ": " << (no_negative_cycle ? "cycle MISSED" : "cycle detected") << ", dist = [" << distance[0] << ", " << distance[1] << "]\n";
}
int main()
{
run("closed_plus ", -1, boost::closed_plus< int >());
run("closed_plus ", -1000000000, boost::closed_plus< int >());
run("saturating_plus", -1, saturating_plus());
run("saturating_plus", -1000000000, saturating_plus());
}
Expected behavior
bellman_ford_shortest_paths returns false for both weights, since both graphs contain a negative cycle.
Actual behavior
With w = -1e9 the cycle is missed with either combine:
runtime error: signed integer overflow: -2000000000 + -1000000000 cannot be represented in type 'int'
closed_plus , w = -1: cycle detected, dist = [-4, -3]
closed_plus , w = -1000000000: cycle MISSED, dist = [-2000000000, -1000000000]
saturating_plus, w = -1: cycle detected, dist = [-4, -3]
saturating_plus, w = -1000000000: cycle MISSED, dist = [-2147483648, -2147483648]
With closed_plus the sum overflows, which is UB, and the result is garbage. With saturation both distances pin at INT_MIN, and the final check compare(combine(d[u], w), d[v]) becomes INT_MIN < INT_MIN, which is false. So the algorithm reports no negative cycle.
A possible fix: for integral distances, treat a reached vertex whose distance equals lowest as a negative cycle. The trade-off is that a legitimate shortest path of exactly INT_MIN would be misreported.
Are you willing to help?
- I'd like to submit a fix as a pull request.
- I can help diagnose or test a candidate fix.
- I'm only reporting the issue.
- Ngôn ngữ chính
- C++
- Star
- 402
- Fork
- 251
- Merge trung bình
- 1 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 38
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của boostorg/graph
-
Good first issues: start contributing to Boost.Graph hereCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởbeginner friendly
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
boostorg/graph#593 · 63 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
api data structure
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
Maintainer thường phản hồi trong vòng 1 ngày
-
CMake < 3.10 failsĐang mởinfra priority: high
Độ khó 3/5 Nửa ngày Mức phù hợp với người mới 45/100
boostorg/graph#688 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Trivial Minimum Cycle Ratio FailingCó thể làm lại được @Becheler đã nhận 46 ngày trước và không có pull request nào đang mở. Đang mởalgorithm
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
boostorg/graph#599 · 3 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của boostorg/graph
Issue tương tự
-
agent:WSL code-review linux pri:medium refactoring
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
kind/enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 82/100
psi29a/d2decomped#691 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
CachyOS/packageinstaller#144 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
(S1 - Need confirmation)
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
CleverRaven/Cataclysm-DDA#89715 ·
Maintainer thường phản hồi trong vòng 1 ngày