-
Notifications
You must be signed in to change notification settings - Fork 266
/
Dijkstra.rs
88 lines (72 loc) · 2.5 KB
/
Dijkstra.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
const INF: i32 = i32::MAX;
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
struct Edge {
target: usize,
weight: i32,
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
struct Graph {
nodes: usize,
edges: Vec<Vec<Edge>>,
}
impl Graph {
// Constructor to create a new graph with a given number of nodes.
fn new(nodes: usize) -> Self {
Graph {
nodes,
edges: vec![vec![]; nodes],
}
}
// Add an edge to the graph from 'from' node to 'to' node with a given weight.
fn add_edge(&mut self, from: usize, to: usize, weight: i32) {
self.edges[from].push(Edge { target: to, weight });
}
// Find the shortest path from a specified starting node using Dijkstra's algorithm.
fn shortest_path(&self, start: usize) -> HashMap<usize, i32> {
let mut distance: HashMap<usize, i32> = (0..self.nodes).map(|i| (i, INF)).collect();
let mut visited: HashSet<usize> = HashSet::new();
// Set the distance to the starting node to 0 and initialize a priority queue.
distance.insert(start, 0);
let mut min_heap: BinaryHeap<(i32, usize)> = BinaryHeap::new();
min_heap.push((0, start));
while let Some((_dist, node)) = min_heap.pop() {
if visited.contains(&node) {
continue;
}
visited.insert(node);
// Explore neighboring nodes and update distances if a shorter path is found.
for edge in &self.edges[node] {
let new_dist = distance[&node] + edge.weight;
if new_dist < distance[&edge.target] {
distance.insert(edge.target, new_dist);
min_heap.push((new_dist, edge.target));
}
}
}
distance
}
}
fn main() {
// Create a new graph.
let mut graph = Graph::new(5);
// Add edges to the graph.
graph.add_edge(0, 1, 1);
graph.add_edge(0, 2, 4);
graph.add_edge(1, 2, 2);
graph.add_edge(1, 3, 5);
graph.add_edge(2, 3, 1);
graph.add_edge(3, 4, 3);
let start_node = 0;
// Find the shortest distances from the starting node.
let shortest_distances = graph.shortest_path(start_node);
// Print the results.
for (node, distance) in shortest_distances.iter() {
println!(
"Shortest distance from node {} to node {}: {}",
start_node, node, distance
);
}
}