generated from noahgift/rust-new-project-template
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |