Module bellman_ford

Module bellman_ford 

Source
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§

ShortestPathResult
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