Skip to content

Commit

Permalink
perf: optimize dijkstra all_paths by searching form end to start
Browse files Browse the repository at this point in the history
  • Loading branch information
manhunto committed Dec 23, 2024
1 parent 2539c2c commit 205d3a1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
| [Day 13: Claw Contraption](src/solutions/year2024/day13.rs) | ⭐⭐ | 0.241 | 0.331 |
| [Day 14: Restroom Redoubt](src/solutions/year2024/day14.rs) | ⭐⭐ | 0.172 | 102.252 |
| [Day 15: Warehouse Woes](src/solutions/year2024/day15.rs) | ⭐⭐ | 7.226 | 9.084 |
| [Day 16: Reindeer Maze](src/solutions/year2024/day16.rs) | ⭐⭐ | 6.478 | 22716.831 |
| [Day 16: Reindeer Maze](src/solutions/year2024/day16.rs) | ⭐⭐ | 6.478 | 74.105 |
| [Day 17: Chronospatial Computer](src/solutions/year2024/day17.rs) | - | - | - |
| [Day 18: RAM Run](src/solutions/year2024/day18.rs) | ⭐⭐ | 2.487 | 204.885 |
| [Day 19: Linen Layout](src/solutions/year2024/day19.rs) | ⭐⭐ | 2.923 | 22.751 |
Expand Down
12 changes: 4 additions & 8 deletions src/utils/graphs/dijkstra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,18 @@ impl<'a, T> Dijkstra<'a, T> {
T: Hash + Eq + PartialEq + Ord + Debug + Copy,
{
{
visited.push(from);
path.push_back(from);
visited.push(end);
path.push_front(end);

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

return;
}

for p in come_from
.iter()
.filter(|(_, froms)| froms.contains(&from))
.map(|(to, _)| to)
{
for p in come_from.get(&end).unwrap_or(&Vec::new()) {
if !visited.contains(p) {
Self::visit(*p, end, visited.clone(), path.clone(), paths, come_from);
Self::visit(from, *p, visited.clone(), path.clone(), paths, come_from);
}
}
}
Expand Down

0 comments on commit 205d3a1

Please sign in to comment.