use heapless_graphs::algorithms::bellman_ford;
use heapless_graphs::containers::maps::staticdict::Dictionary;
use heapless_graphs::containers::maps::MapTrait;
use heapless_graphs::edgelist::edge_list::EdgeList;
use heapless_graphs::edges::EdgeValueStruct;
fn main() {
println!("Bellman-Ford Algorithm Example");
println!("==============================");
let edge_data = EdgeValueStruct([
(0usize, 1usize, 5i32), (1, 2, 3), (2, 0, -2), ]);
let graph = EdgeList::<8, _, _>::new(edge_data);
let distance_map = Dictionary::<usize, Option<i32>, 16>::new();
println!("Running Bellman-Ford from source node 0...");
match bellman_ford(&graph, 0, distance_map) {
Ok(result) => {
println!("Algorithm completed successfully!");
println!("\nShortest distances from node 0:");
for node in 0..3 {
if let Some(dist_opt) = result.get(&node) {
match dist_opt {
Some(dist) => println!(" Node {}: distance = {}", node, dist),
None => println!(" Node {}: unreachable", node),
}
}
}
}
Err(e) => {
println!("Algorithm failed: {:?}", e);
}
}
println!("\n--- Testing Negative Cycle Detection ---");
let negative_cycle_data = EdgeValueStruct([
(0usize, 1usize, -1i32), (1, 0, -1), ]);
let neg_graph = EdgeList::<8, _, _>::new(negative_cycle_data);
let distance_map2 = Dictionary::<usize, Option<i32>, 16>::new();
match bellman_ford(&neg_graph, 0, distance_map2) {
Ok(_) => println!("No negative cycle detected"),
Err(e) => println!("Negative cycle detected: {:?}", e),
}
}