You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// This file is part of the CPP-GL project (https://github.com/SpectraL519/cpp-gl).
3
3
// Licensed under the MIT License. See the LICENSE file in the project root for full license information.
4
4
5
+
/// @file gl/algorithm/pathfinding/dijkstra.hpp
6
+
/// @brief Concrete implementation of Dijkstra's single-source shortest path algorithm and path reconstruction utilities.
7
+
5
8
#pragma once
6
9
7
10
#include"gl/algorithm/core.hpp"
@@ -10,30 +13,117 @@
10
13
#include"gl/constants.hpp"
11
14
#include"gl/types/core.hpp"
12
15
13
-
#include<deque>
14
-
15
16
namespacegl::algorithm {
16
17
18
+
/// @ingroup GL GL-Algorithm
19
+
/// @brief A descriptor structure holding the results of a single-source shortest path execution.
20
+
///
21
+
/// ### Template Parameters
22
+
/// | Parameter | Description | Constraint |
23
+
/// | :-------- | :--- | :--- |
24
+
/// | G | The type of the graph. | Must satisfy the [**c_graph**](gl_concepts.md#gl-traits-c-graph) concept. |
25
+
/// | VertexDistanceType | The numeric type used to represent accumulated path weights/distances. | Must satisfy the [**c_arithmetic**](gl_concepts.md#gl-traits-c-arithmetic) concept. |
/// @brief The predecessor map tracking the optimal path tree.
25
37
predecessors_map<G> predecessors;
38
+
/// @brief The accumulated shortest distances to each vertex from the source.
26
39
std::vector<distance_type> distances;
27
40
};
28
41
42
+
/// @ingroup GL GL-Algorithm
43
+
/// @brief An alias for @ref gl::algorithm::paths_descriptor "paths_descriptor" that automatically deduces the appropriate distance type for the graph.
44
+
/// @tparam G The type of the graph.
29
45
template <traits::c_graph G>
30
46
using paths_descriptor_type = paths_descriptor<G, vertex_distance_type<G>>;
31
47
48
+
/// @ingroup GL GL-Algorithm
49
+
/// @brief Factory function to create an initialized paths descriptor sized for the given graph.
50
+
/// @tparam G The type of the graph.
51
+
/// @param graph The graph to size the descriptor against.
52
+
/// @return A @ref gl::algorithm::paths_descriptor "paths_descriptor" initialized with invalid predecessors and default-constructed distances.
/// 1\. Executes the shortest path calculation from the given `source_id`.
97
+
///
98
+
/// 2\. Reconstructs the exact sequence of vertices from the source to the `target_id` using the @ref gl::algorithm::reconstruct_path "reconstruct_path" function.
99
+
///
100
+
/// 3\. Prints the path to the target vertex using the @ref gl::io::range_formatter "range_formatter" helper.
101
+
///
102
+
/// 4\. Retrievs the total distance to the target vertex from the @ref gl::algorithm::paths_descriptor "paths descriptor" object returned by Dijkstra's algorithm.
103
+
///
104
+
/// > [!INFO] Algorithmic Complexity
105
+
/// >
106
+
/// > The time complexity depends on the underlying representation of `GraphType` and the priority queue overhead:
/// > - *Note:* Iterating over adjacent vertices requires scanning the entire \f$|V|\f$-length matrix row.
112
+
///
113
+
/// ### Template Parameters
114
+
/// | Parameter | Description | Constraint |
115
+
/// | :-------- | :--- | :--- |
116
+
/// | G | The type of the graph being traversed. Must define a valid distance/weight property. | Must satisfy the [**c_graph**](gl_concepts.md#gl-traits-c-graph) concept. |
117
+
/// | PreVisitCallback | Type of the callable executed immediately before a vertex is officially visited. | Must be one of:<br/>- `(id_type) -> void` callable<br/>- An @ref gl::algorithm::empty_callback "empty_callback" |
118
+
/// | PostVisitCallback | Type of the callable executed after all adjacent edges of a vertex are evaluated. | Must be one of:<br/>- `(id_type) -> void` callable<br/>- An @ref gl::algorithm::empty_callback "empty_callback" |
119
+
///
120
+
/// @param graph The graph to evaluate.
121
+
/// @param source_id The starting vertex ID for the shortest path calculation.
122
+
/// @param pre_visit Hook executed immediately before the internal visit logic.
123
+
/// @param post_visit Hook executed after all adjacent edges of the current vertex have been enqueued.
124
+
/// @return A @ref gl::algorithm::paths_descriptor "paths_descriptor" containing the accumulated distances and predecessor map.
125
+
/// @throws std::invalid_argument If an edge with a negative weight is encountered.
/// @brief Reconstructs the sequence of vertices forming a path to a specific target.
208
+
///
209
+
/// This utility walks backward through a predecessor map, starting from the `vertex_id`
210
+
/// until it reaches the root vertex (a vertex that is its own predecessor).
211
+
///
212
+
/// ### Template Parameters
213
+
/// | Parameter | Description | Constraint |
214
+
/// | :-------- | :--- | :--- |
215
+
/// | IdType | The type of the vertex IDs. | Must satisfy the [**c_id_type**](gl_concepts.md#gl-traits-c-id-type) concept. |
216
+
/// | IdRange | The type of the random-access range containing the predecessor map. | Must satisfy the [**c_random_access_range_of**](gl_concepts.md#gl-traits-c-random-access-range-of) concept for `IdType`. |
217
+
///
218
+
/// @param predecessor_map The predecessor map generated by a traversal algorithm (e.g., BFS, DFS, Dijkstra).
219
+
/// @param vertex_id The target vertex ID to reconstruct the path for.
220
+
/// @return A `std::vector` containing the sequence of vertex IDs from the source to the target.
221
+
/// @throws std::invalid_argument If the target `vertex_id` is unreachable (its predecessor is invalid).
/// 5\. Predicate ensuring we only enqueue adjacent vertices that haven't been visited yet, returning a @ref gl::algorithm::decision "decision".
51
51
///
52
52
/// ### Template Parameters
53
-
/// | Parameter | Description |
54
-
/// | :-------- | :--- |
55
-
/// | G | The type of the graph being traversed. |
56
-
/// | InitQueueRangeType | The type of the container providing the initial roots to enqueue. |
57
-
/// | VisitVertexPredicate | Type of the callable deciding if a popped vertex should be processed. |
58
-
/// | VisitCallback | Type of the callable executed when a vertex is officially visited. |
59
-
/// | EnqueueVertexPred | Type of the callable deciding if an adjacent vertex should be pushed to the queue. |
60
-
/// | PreVisitCallback | Type of the callable executed immediately before `VisitCallback`. |
61
-
/// | PostVisitCallback | Type of the callable executed after all adjacent edges are evaluated. |
53
+
/// | Parameter | Description | Constraint |
54
+
/// | :-------- | :--- | :--- |
55
+
/// | G | The type of the graph being traversed. | Must satisfy the [**c_graph**](gl_concepts.md#gl-traits-c-graph) concept. |
56
+
/// | InitQueueRangeType | The type of the container providing the initial roots to enqueue. | Must be a *forward range* of @ref gl::algorithm::search_node "search nodes". |
57
+
/// | VisitVertexPredicate | Type of the callable deciding if a popped vertex should be processed. | Must be one of:<br/>- An `(id_type) -> bool` callable<br/>- An @ref gl::algorithm::empty_callback "empty_callback" |
58
+
/// | VisitCallback | Type of the callable executed when a vertex is officially visited. | Must be one of:<br/>- An `(id_type, id_type) -> bool` callable<br/>- An @ref gl::algorithm::empty_callback "empty_callback" |
59
+
/// | EnqueueVertexPred | Type of the callable deciding if an adjacent vertex should be pushed to the queue. | Must be one of:<br/>- An `(id_type, const edge_type&) -> decision` callable<br/>- An @ref gl::algorithm::empty_callback "empty_callback" |
60
+
/// | PreVisitCallback | Type of the callable executed immediately before `VisitCallback`. | Must be one of:<br/>- An `(id_type) -> void` callable<br/>- An @ref gl::algorithm::empty_callback "empty_callback" |
61
+
/// | PostVisitCallback | Type of the callable executed after all adjacent edges are evaluated. | Must be one of:<br/>- An `(id_type) -> void` callable<br/>- An @ref gl::algorithm::empty_callback "empty_callback" |
62
62
///
63
63
/// @param graph The graph to traverse.
64
64
/// @param initial_queue_content A range of initial @ref gl::algorithm::search_node "search nodes" to seed the BFS queue.
@@ -71,6 +71,7 @@ namespace gl::algorithm {
71
71
/// @param pre_visit Hook executed immediately before the `visit` callback.
72
72
/// @param post_visit Hook executed after all adjacent edges of the current vertex have been evaluated.
73
73
/// @return `true` if the queue was exhausted naturally, `false` if the search was aborted early by a callback or predicate.
0 commit comments