Skip to content

Commit b4a5aa0

Browse files
authored
Merge pull request kelvins#202 from BDadmehr0/rust
Dijkstra's algorithm with Rust
2 parents a3e8ad2 + 2532bd1 commit b4a5aa0

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ In order to achieve greater coverage and encourage more people to contribute to
107107
</a>
108108
</td>
109109
<td> <!-- Rust -->
110-
<a href="./CONTRIBUTING.md">
111-
<img align="center" height="25" src="./logos/github.svg" />
110+
<a href="./src/rust/Dijkstra.rs">
111+
<img align="center" height="25" src="./logos/rust.svg" />
112112
</a>
113113
</td>
114114
<td> <!-- Scala -->

src/rust/Dijkstra.rs

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

0 commit comments

Comments
 (0)