use crate::util::DirectedWeightedGraph;

pub fn dijkstra(g: &mut DirectedWeightedGraph) -> () {
    let mut x: Vec<usize> = vec![0];
    g.distance[0] = 0;

    while x.len() < g.nodes.len() {
        let mut min_node = std::usize::MAX;
        let mut min_distance = std::usize::MAX;

        for &i in x.iter() {
            let v = &g.nodes[i];
            for &edge in v.iter() {
                let w = edge.0;
                let distance = edge.1;
                if x.contains(&w) {
                    continue;
                }

                let current_edge_distance = g.distance[i] + distance;
                if current_edge_distance < min_distance {
                    min_distance = current_edge_distance;
                    min_node = w;
                }
            }
        }

        if min_node == std::usize::MAX {
            panic!("Did not find a min_node");
        }

        g.distance[min_node] = min_distance;
        x.push(min_node);
    }
}
