Expand description
Bellman-Ford Algorithm
Computes shortest paths from a source vertex to all other vertices in a weighted graph. Unlike Dijkstra’s algorithm, it can handle graphs with negative edge weights and can detect negative cycles.
Time complexity: O(VE)
§Examples
use advanced_algorithms::graph::{Graph, bellman_ford};
let mut graph = Graph::new(4);
graph.add_edge(0, 1, 1.0);
graph.add_edge(1, 2, -1.0); // Negative weight OK
graph.add_edge(2, 3, 1.0);
let result = bellman_ford::shortest_path(&graph, 0).unwrap();
assert_eq!(result.distance[3], 1.0);Structs§
- Shortest
Path Result - Result of Bellman-Ford algorithm
Functions§
- find_
negative_ cycle - Finds a negative cycle in the graph if one exists
- has_
negative_ cycle - Detects if the graph contains any negative cycle
- shortest_
path - Computes shortest paths using Bellman-Ford algorithm