fn djikstra(&self, source: usize, target: usize) -> Option<(Vec<usize>, Vec<f32>)> {
        let mut dists: HashMap<usize, f32> = HashMap::new();
        let mut heap = BinaryHeap::new();

        dists.insert(source, 0.0);
        heap.push(State { cost: 0.0, position: source });

        while let Some(State { cost, position }) = heap.pop() {
            if position == target {
                let mut predecessors = Vec::new();
                let mut distances = Vec::new();
                for (key, val) in dists.iter_mut() {
                    predecessors.push(*key);
                    distances.push(*val);
                }
                return Some((predecessors, distances));
            }
            if cost > dists[&position] { continue; }

            for edge in &self.edges {
                if edge.source == position as i64 {
                    let next = State { cost: cost + edge.weight, position: edge.target as usize };
                    if !dists.contains_key(&next.position) {
                        dists.insert(next.position, next.cost);
                        heap.push(next);
                    } else if next.cost < dists[&next.position] {
                        dists.insert(next.position, next.cost);
                        heap.push(next);
                    }
                }
            }
        }

        None
    }
/// Runs the Djikstra algorithm on the graph. Returns a tuple, containing a vector of
/// predecessors and a vector of distances to the source node.
Runs the Djikstra algorithm on the graph. Returns a tuple, containing a vector of
predecessors and a vector of distances to the source node.
Runs the Djikstra algorithm on the graph . Returns a tuple containing a vector of predecessors and a vector of distances to the source node .
fn djikstra(&self, source: usize, target: usize) -> Option<(Vec<usize>, Vec<f32>)> {
        let mut dists: HashMap<usize, f32> = HashMap::new();
        let mut heap = BinaryHeap::new();
        dists.insert(source, 0.0);
        heap.push(State { cost: 0.0, position: source });
        while let Some(State { cost, position }) = heap.pop() {
            if position == target {
                let mut predecessors = Vec::new();
                let mut distances = Vec::new();
                for (key, val) in dists.iter_mut() {
                    predecessors.push(*key);
                    distances.push(*val);
                }
                return Some((predecessors, distances));
            }
            if cost > dists[&position] { continue; }
            for edge in &self.edges {
                if edge.source == position as i64 {
                    let next = State { cost: cost + edge.weight, position: edge.target as usize };
                    if !dists.contains_key(&next.position) {
                        dists.insert(next.position, next.cost);
                        heap.push(next);
                    } else if next.cost < dists[&next.position] {
                        dists.insert(next.position, next.cost);
                        heap.push(next);
                    }
                }
            }
        }
        None
    }
fn djikstra ( & self , source : usize , target : usize ) -> Option < ( Vec < usize > , Vec < f32 > ) > { let mut dists : HashMap < usize , f32 > = HashMap :: new ( ) ; let mut heap = BinaryHeap :: new ( ) ; dists . insert ( source , 0.0 ) ; heap . push ( State { cost : 0.0 , position : source } ) ; while let Some ( State { cost , position } ) = heap . pop ( ) { if position == target { let mut predecessors = Vec :: new ( ) ; let mut distances = Vec :: new ( ) ; for ( key , val ) in dists . iter_mut ( ) { predecessors . push ( * key ) ; distances . push ( * val ) ; } return Some ( ( predecessors , distances ) ) ; } if cost > dists [ & position ] { continue ; } for edge in & self . edges { if edge . source == position as i64 { let next = State { cost : cost + edge . weight , position : edge . target as usize } ; if ! dists . contains_key ( & next . position ) { dists . insert ( next . position , next . cost ) ; heap . push ( next ) ; } else if next . cost < dists [ & next . position ] { dists . insert ( next . position , next . cost ) ; heap . push ( next ) ; } } } } None }
Runs the Djikstra algorithm on the graph.
Runs the Djikstra algorithm on the graph .
&self
source
usize
target
usize
&self
source
usize
target
usize