Skip to content

Commit

Permalink
adding dijkstra
Browse files Browse the repository at this point in the history
  • Loading branch information
noahgift committed Jul 11, 2023
1 parent 5f30e43 commit df6f4c0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lisbon-shortest-path/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "lisbon-shortest-path"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
petgraph = "0.6.3"
13 changes: 13 additions & 0 deletions lisbon-shortest-path/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
format:
cargo fmt --quiet

lint:
cargo clippy --quiet

test:
cargo test --quiet

run:
cargo run

all: format lint test run
33 changes: 33 additions & 0 deletions lisbon-shortest-path/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use petgraph::algo::dijkstra;
use petgraph::prelude::*;

fn main() {
let mut graph = Graph::<&str, u32, Undirected>::new_undirected();

let belem_tower = graph.add_node("Belem Tower");
let monastery = graph.add_node("Jerónimos Monastery");
let lx_factory = graph.add_node("LX Factory");
let commerce_square = graph.add_node("Commerce Square");
let lisbon_cathedral = graph.add_node("Lisbon Cathedral");

graph.extend_with_edges([
(belem_tower, monastery, 1), // The distance from Belem Tower to Jerónimos Monastery is 1 km
(belem_tower, lx_factory, 3), // The distance from Belem Tower to LX Factory is 3 km
(belem_tower, commerce_square, 7), // The distance from Belem Tower to Commerce Square is 7 km
(monastery, lx_factory, 3), // The distance from Jerónimos Monastery to LX Factory is 3 km
(monastery, commerce_square, 6), // The distance from Jerónimos Monastery to Commerce Square is 6 km
(lx_factory, commerce_square, 5), // The distance from LX Factory to Commerce Square is 5 km
(commerce_square, lisbon_cathedral, 1), // The distance from Commerce Square to Lisbon Cathedral is 1 km
]);

let node_map = dijkstra(&graph, belem_tower, Some(lisbon_cathedral), |e| *e.weight());

if let Some(distance) = node_map.get(&lisbon_cathedral) {
println!(
"The shortest distance from Belem Tower to Lisbon Cathedral is {} km",
distance
);
} else {
println!("No route found from Belem Tower to Lisbon Cathedral.");
}
}

0 comments on commit df6f4c0

Please sign in to comment.