Longest/shortest path algorithms
Algorithms to facilitate longest (and shortest) path algorithms. Path cost is calculated by either summing or multiplying edge weights.
Floyd-Warshall
Floyd-Warshall algorithm has the ability to calculate longest path (ie. most profitable trade) calculations, albeit at the expensive cost of $O(n^3)$.
For multiplication based weights, we make use of the fact that product maximisation is equivalent to maximisation of log of weights, as per: $x*y = 2^{log2(x) + log2(y)}$.
For longest paths, weights have been multiplied by $-1$ and hence reused in shortest path algorithm.
NOTE: Floyd-Warshall can detect negative path cycles (ie. infinite arbitrage opportunities), which cause the latest price update to be ignored. Potential TBD - remove offending edge to remove negative cycles...
Sample usage of Floyd-Warshall calculator. All prices are in $10{12}$, including self references, eg. cost of BNB -> BNB = $10{12}$
use *;
use FloydWarshallCalculator;
let in_graph = &;
let res_out = calc_best_paths;
let as_nodes = res_out.unwrap.into_iter.;
let res_ref = res_out.as_ref.unwrap;
// multi-hop path path
assert_eq!;
// 1 hop path, based on input ProviderPair
assert_eq!;
// path to self, note cost is still in scale 10^12
assert_eq!;
Utility within a pallet
Best-path serves as a best trade finding mechanism for best-path-pallet.