From 4302a7b2f7cd6cbc3d1c4cd8c1738ed66c62fc32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Mon, 5 May 2025 19:05:02 +0200 Subject: [PATCH] Fix Dijkstra's Algorithm by exiting early at target vertex --- src/common/WeightedEdgeGraph.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/common/WeightedEdgeGraph.cpp b/src/common/WeightedEdgeGraph.cpp index 3da63e5..b837674 100644 --- a/src/common/WeightedEdgeGraph.cpp +++ b/src/common/WeightedEdgeGraph.cpp @@ -62,9 +62,13 @@ int WeightedEdgeGraph::dijkstra(const int source, const int target) const distances[neighbor] = newDistance; queue.push(neighbor); } + if (neighbor == target) + { + return distances[target]; + } incidence = vertexEdgeIncidences_[incidence].next; } } - return distances[target]; + return -1; }