Struct graph_algorithms::list_of_algorithms::bellmanford::bellmanford::Graph
source · [−]Fields
edges: Vec<(usize, usize, i32)>representation using edge list
vertices: usizetotal no of vertices
Implementations
sourceimpl Graph
impl Graph
pub fn new(vertices: usize) -> Self
sourcepub fn bellman_ford(&self, src: usize) -> Vec<i32>
pub fn bellman_ford(&self, src: usize) -> Vec<i32>
Bellman-Ford algorithm Bellman ford algorithm is used to find the shortest node from one node to all other nodes in a weighted graph
Arguments
- vector(s,d,w) - A vector representing the source, destination and weight
- start - The index of the vertex to start the Bellman Ford algorithm from.
Returns
- vector(s,d,w) -Returns the vector of the shortest distance from source to destination after Bellman Ford is run.
Example
let = vec![
vec![0,1,-1], // Node 0 has edges to node 1 and weight -1
vec![0,2,4], // Node 0 has edges to node 2 and weight 4
vec![1,2,3], // Node 0 has edges to node 2 and weight 4
vec![3,2,5], // Node 0 has edges to node 2 and weight 4
vec![3,1,1], // Node 0 has edges to node 2 and weight 4
vec![1,3,2], // Node 0 has edges to node 2 and weight 4
vec![1,4,2], // Node 0 has edges to node 2 and weight 4
vec![4,3,-3], // Node 0 has edges to node 2 and weight 4
];
let start_vertex = 0;
let visited = bfs(&adj_list, start_vertex);
assert_eq!(visited, vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);Auto Trait Implementations
impl RefUnwindSafe for Graph
impl Send for Graph
impl Sync for Graph
impl Unpin for Graph
impl UnwindSafe for Graph
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more