## Welcome the Bellman Ford implementation in Rust+WASM
npm install wasm-bellman-fordThe Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers. The algorithm was first proposed by Alfonso Shimbel 1955, but is instead named after Richard Bellman and Lester Ford, Jr., who published it in 1958 and 1956, respectively. Edward F. Moore also published the same algorithm in 1957, and for this reason it is also sometimes called the Bellman–Ford–Moore algorithm.
wikipedia
Since we can use Bellman-Ford as an efficient way to detect negative cycles in a fully connected graph, we can use this to quickly find market inefficencies and arbitrage oppertunities. If a cycle is negative in a graph of currency conversions. eg. USD -> JPY, JPY -> EUR then EUR -> USD. Then we can conclude that the currencies are not in price equilibrium.
Check out RESOURCES.md for a Python example of the code and more resouces about the Bellman-Ford algo.
Clone the repo and navigate to the web dirc, and run the web app. If you need to recompile the wasm then follow the wasm-pack instructions below.
``bash`
git clone https://github.com/drbh/wasm-bellman-ford.git
cd web
npm install
npm run start
Open your browser and navigate to:
``
http://localhost:8080/
In the example file index.html just loads in index.js which is the file that interacts with the wasm library. Here is the full code in index.js and you can see how we pass a JSON payload into the wasm function.
`javascript
import * as wasm from "wasm-bellman-ford";
var data = JSON.stringify(
{
"graph": [
[1.00, 2.00, 4.00],
[0.50, 1.00, 2.00],
[0.25, 0.50, 1.00]
]
})
var bool_response = wasm.bellman_ford_neg_cycle(data);
console.log(bool_response)
`
Here we see an example function that reads in a JSON formatted string, wich bellman_ford_neg_cycle will parse.`rust
fn main() {
let data = r#"
{
"graph": [
[1.00, 2.00, 4.00],
[0.50, 1.00, 2.00],
[0.25, 0.50, 1.00]
]
}"#;
let result = bellman_ford_neg_cycle(data);
println!("{:?}", result);
}
`
Following this template: rustwasm/game-of-life
.[📚 Read this template tutorial! 📚][template-docs]
This template is designed for compiling Rust libraries into WebAssembly and
publishing the resulting package to NPM.
Be sure to check out [other wasm-pack tutorials online][tutorials] for otherwasm-pack
templates and usages of .
[tutorials]: https://rustwasm.github.io/docs/wasm-pack/tutorials/index.html
[template-docs]: https://rustwasm.github.io/docs/wasm-pack/tutorials/npm-browser-packages/index.html
Learn more about cargo generate here.
``
cargo generate --git https://github.com/rustwasm/wasm-pack-template.git --name my-project
cd my-project
``
wasm-pack build
``
wasm-pack test --headless --firefox
``
wasm-pack publish
* wasm-bindgen for communicating
between WebAssembly and JavaScript.
* console_error_panic_hook
for logging panic messages to the developer console.
* wee_alloc`, an allocator optimized
for small code size.