Skip to content

Commit 205d3a1

Browse files
committed
perf: optimize dijkstra all_paths by searching form end to start
1 parent 2539c2c commit 205d3a1

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
| [Day 13: Claw Contraption](src/solutions/year2024/day13.rs) | ⭐⭐ | 0.241 | 0.331 |
2727
| [Day 14: Restroom Redoubt](src/solutions/year2024/day14.rs) | ⭐⭐ | 0.172 | 102.252 |
2828
| [Day 15: Warehouse Woes](src/solutions/year2024/day15.rs) | ⭐⭐ | 7.226 | 9.084 |
29-
| [Day 16: Reindeer Maze](src/solutions/year2024/day16.rs) | ⭐⭐ | 6.478 | 22716.831 |
29+
| [Day 16: Reindeer Maze](src/solutions/year2024/day16.rs) | ⭐⭐ | 6.478 | 74.105 |
3030
| [Day 17: Chronospatial Computer](src/solutions/year2024/day17.rs) | - | - | - |
3131
| [Day 18: RAM Run](src/solutions/year2024/day18.rs) | ⭐⭐ | 2.487 | 204.885 |
3232
| [Day 19: Linen Layout](src/solutions/year2024/day19.rs) | ⭐⭐ | 2.923 | 22.751 |

src/utils/graphs/dijkstra.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,18 @@ impl<'a, T> Dijkstra<'a, T> {
145145
T: Hash + Eq + PartialEq + Ord + Debug + Copy,
146146
{
147147
{
148-
visited.push(from);
149-
path.push_back(from);
148+
visited.push(end);
149+
path.push_front(end);
150150

151151
if from == end {
152152
paths.push(path.clone());
153153

154154
return;
155155
}
156156

157-
for p in come_from
158-
.iter()
159-
.filter(|(_, froms)| froms.contains(&from))
160-
.map(|(to, _)| to)
161-
{
157+
for p in come_from.get(&end).unwrap_or(&Vec::new()) {
162158
if !visited.contains(p) {
163-
Self::visit(*p, end, visited.clone(), path.clone(), paths, come_from);
159+
Self::visit(from, *p, visited.clone(), path.clone(), paths, come_from);
164160
}
165161
}
166162
}

0 commit comments

Comments
 (0)