Skip to main content

Module bellman_ford

Module bellman_ford 

Source
Expand description

Bellman-Ford algorithm with SPFA optimization for simulation-driven routing.

Runs actual pool simulations (get_amount_out()) during edge relaxation to find optimal A-to-B routes that account for slippage, fees, and pool mechanics at the given trade size.

Key features:

  • Gas-aware relaxation: When token prices and gas price are available, relaxation compares net amounts (gross output minus cumulative gas cost in token terms) instead of gross output alone. Falls back to gross comparison when data is unavailable.
  • Subgraph extraction: BFS prunes the graph to nodes reachable within max_hops
  • SPFA (Shortest Path Faster Algorithm) queuing: Only re-relaxes edges from nodes whose amount improved
  • Forbid revisits: Skips edges that would revisit a token or pool already in the path

Structsยง

BellmanFordAlgorithm