Skip to content

Commit acd7bab

Browse files
docs: add unit tests for all GRAPH_GUIDE.md examples and format code
Co-authored-by: samueltardieu <44656+samueltardieu@users.noreply.github.com>
1 parent 3926b9a commit acd7bab

File tree

4 files changed

+385
-16
lines changed

4 files changed

+385
-16
lines changed

examples/graph-adjacency-list.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ fn main() {
1919
.collect();
2020

2121
// Define the successor function
22-
let successors = |node: &&str| -> Vec<(&str, u32)> {
23-
graph.get(node).cloned().unwrap_or_default()
24-
};
22+
let successors =
23+
|node: &&str| -> Vec<(&str, u32)> { graph.get(node).cloned().unwrap_or_default() };
2524

2625
// Find shortest path from A to E using Dijkstra's algorithm
2726
let result = dijkstra(&"A", successors, |&node| node == "E");
@@ -40,7 +39,7 @@ fn main() {
4039

4140
// Find another path: A to D
4241
let result2 = dijkstra(&"A", successors, |&node| node == "D");
43-
42+
4443
match result2 {
4544
Some((path, cost)) => {
4645
println!("\nShortest path from A to D:");

examples/graph-adjacency-matrix.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ fn main() {
99
// Represent nodes as indices (0=A, 1=B, 2=C, 3=D, 4=E)
1010
// None means no edge, Some(weight) means edge with that weight
1111
let adjacency_matrix: Vec<Vec<Option<u32>>> = vec![
12-
vec![None, Some(4), Some(2), None, None], // Node 0 (A)
13-
vec![None, None, Some(1), Some(5), None], // Node 1 (B)
14-
vec![None, None, None, Some(8), Some(10)], // Node 2 (C)
15-
vec![None, None, None, None, Some(2)], // Node 3 (D)
16-
vec![None, None, None, None, None], // Node 4 (E)
12+
vec![None, Some(4), Some(2), None, None], // Node 0 (A)
13+
vec![None, None, Some(1), Some(5), None], // Node 1 (B)
14+
vec![None, None, None, Some(8), Some(10)], // Node 2 (C)
15+
vec![None, None, None, None, Some(2)], // Node 3 (D)
16+
vec![None, None, None, None, None], // Node 4 (E)
1717
];
1818

1919
let num_nodes = adjacency_matrix.len();
@@ -40,7 +40,7 @@ fn main() {
4040
match result {
4141
Some((path, cost)) => {
4242
let path_names: Vec<char> = path.iter().map(|&i| NODE_NAMES[i]).collect();
43-
43+
4444
println!("Shortest path from A to E using A*:");
4545
println!(" Path (indices): {path:?}");
4646
println!(" Path (names): {path_names:?}");
@@ -54,11 +54,11 @@ fn main() {
5454

5555
// Example 2: Find path from B (1) to E (4)
5656
let result2 = astar(&1, successors, heuristic, |&node| node == 4);
57-
57+
5858
match result2 {
5959
Some((path, cost)) => {
6060
let path_names: Vec<char> = path.iter().map(|&i| NODE_NAMES[i]).collect();
61-
61+
6262
println!("\nShortest path from B to E:");
6363
println!(" Path (indices): {path:?}");
6464
println!(" Path (names): {path_names:?}");

examples/graph-unweighted-bfs.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ fn main() {
1919
.collect();
2020

2121
// Successor function returns just neighbors (no costs)
22-
let successors = |node: &&str| -> Vec<&str> {
23-
graph.get(node).cloned().unwrap_or_default()
24-
};
22+
let successors = |node: &&str| -> Vec<&str> { graph.get(node).cloned().unwrap_or_default() };
2523

2624
// Find shortest path from A to F using BFS
2725
let result = bfs(&"A", successors, |&node| node == "F");
@@ -38,7 +36,7 @@ fn main() {
3836

3937
// Example 2: Find path from A to E
4038
let result2 = bfs(&"A", successors, |&node| node == "E");
41-
39+
4240
match result2 {
4341
Some(path) => {
4442
println!("\nShortest path from A to E: {path:?}");

0 commit comments

Comments
 (0)