Algorithm Theory

Problem Statement

Given a directed graph \(G=(V,E)\) where each edge \(e\in E\) has a cost \(c_e\) and a time \(t_e\), the goal is to find a cycle \(C\) that minimises the ratio

\[r(C) = \frac{\sum_{e\in C} c_e}{\sum_{e\in C} t_e}.\]

The optimal value \(r^*\) is the minimum of \(r(C)\) over all directed cycles in the graph.

Vectorised Bellman–Ford

Negative cycle queries are answered using a Bellman–Ford relaxation scheme implemented with NumPy arrays. For \(n\) vertices and \(m\) edges the method performs \(O(nm)\) relaxations but operates on whole arrays at a time, yielding significant speedups over a pure Python implementation.

Complexity

Combining the binary search with the relaxation phase yields a total running time of \(O(nm \log((\lambda_{\text{hi}}-\lambda_{\text{lo}})/\text{tol}))\). When the exact Stern–Brocot mode is used the search depth is bounded by the size of the optimal numerator and denominator, providing a polynomial guarantee for integer-weighted inputs.