fn bellman(&self, source: usize) -> Option<(Vec<usize>, Vec<f32>)> {
        let nodes_count = self.nodes.len();
        let max_length = self.edges.len();
        println!(" ˪— Starting from {}, having {} nodes.", source, nodes_count);
        let mut pred = (0..nodes_count).collect::<Vec<_>>();
        let mut dist = std::iter::repeat(std::f32::MAX).take(nodes_count).collect::<Vec<_>>();
        dist[source] = 0.0;
        let mut count = 0;

        let mut improvement = true;
        while improvement {
            improvement = false;
            for edge in &self.edges {
                let source_dist = dist[edge.source as usize];
                let target_dist = dist[edge.target as usize];

                if source_dist != std::f32::MAX && source_dist + edge.weight < target_dist {
                    dist[edge.target as usize] = source_dist + edge.weight;
                    pred[edge.target as usize] = edge.source as usize;
                    improvement = true;
                }

                // This would be needed for undirected edges, as we'd have to follow every
                // edge both ways in that case.
                // if target_dist != std::f32::MAX && target_dist + edge.weight < source_dist {
                //     dist[edge.source as usize] = target_dist + edge.weight;
                //     pred[edge.source as usize] = edge.target as usize;
                //     improvement = true;
                // }
            }
            count = count + 1;

            // Make sure this doesn't run forever.
            if count > max_length {
                improvement = false;
            }
        }
        println!(" ˪— Bellman iterations: {}", count);

        Some((pred, dist))
    }
/// Runs the Bellman Ford algorithm on the graph. Returns a tuple, containing a vector of
/// predecessors and a vector of distances to the source node.
Runs the Bellman Ford algorithm on the graph. Returns a tuple, containing a vector of
predecessors and a vector of distances to the source node.
Runs the Bellman Ford algorithm on the graph . Returns a tuple containing a vector of predecessors and a vector of distances to the source node .
fn bellman(&self, source: usize) -> Option<(Vec<usize>, Vec<f32>)> {
        let nodes_count = self.nodes.len();
        let max_length = self.edges.len();
        println!(" ˪— Starting from {}, having {} nodes.", source, nodes_count);
        let mut pred = (0..nodes_count).collect::<Vec<_>>();
        let mut dist = std::iter::repeat(std::f32::MAX).take(nodes_count).collect::<Vec<_>>();
        dist[source] = 0.0;
        let mut count = 0;
        let mut improvement = true;
        while improvement {
            improvement = false;
            for edge in &self.edges {
                let source_dist = dist[edge.source as usize];
                let target_dist = dist[edge.target as usize];
                if source_dist != std::f32::MAX && source_dist + edge.weight < target_dist {
                    dist[edge.target as usize] = source_dist + edge.weight;
                    pred[edge.target as usize] = edge.source as usize;
                    improvement = true;
                }
                // This would be needed for undirected edges, as we'd have to follow every
                // edge both ways in that case.
                // if target_dist != std::f32::MAX && target_dist + edge.weight < source_dist {
                //     dist[edge.source as usize] = target_dist + edge.weight;
                //     pred[edge.source as usize] = edge.target as usize;
                //     improvement = true;
                // }
            }
            count = count + 1;
            // Make sure this doesn't run forever.
            if count > max_length {
                improvement = false;
            }
        }
        println!(" ˪— Bellman iterations: {}", count);
        Some((pred, dist))
    }
fn bellman ( & self , source : usize ) -> Option < ( Vec < usize > , Vec < f32 > ) > { let nodes_count = self . nodes . len ( ) ; let max_length = self . edges . len ( ) ; println ! ( " s rce, n es_count); let mut pred = ( 0 .. nodes_count ) . collect :: < Vec < _ > > ( ) ; let mut dist = std :: iter :: repeat ( std :: f32 :: MAX ) . take ( nodes_count ) . collect :: < Vec < _ > > ( ) ; dist [ source ] = 0.0 ; let mut count = 0 ; let mut improvement = true ; while improvement { improvement = false ; for edge in & self . edges { let source_dist = dist [ edge . source as usize ] ; let target_dist = dist [ edge . target as usize ] ; if source_dist != std :: f32 :: MAX && source_dist + edge . weight < target_dist { dist [ edge . target as usize ] = source_dist + edge . weight ; pred [ edge . target as usize ] = edge . source as usize ; improvement = true ; } // This would be needed for undirected edges, as we'd have to follow every // edge both ways in that case. // if target_dist != std::f32::MAX && target_dist + edge.weight < source_dist { //     dist[edge.source as usize] = target_dist + edge.weight; //     pred[edge.source as usize] = edge.target as usize; //     improvement = true; // } } count = count + 1 ; // Make sure this doesn't run forever. if count > max_length { improvement = false ; } } println ! ( " c nt); Some ( ( pred , dist ) ) }
Runs the Bellman Ford algorithm on the graph.
Runs the Bellman Ford algorithm on the graph .
&self
source
usize
&self
source
usize