Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ReconstructPath.hpp
Go to the documentation of this file.
1
6// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved.
7
8#pragma once
9
10#include <alib/map>
11#include <alib/vector>
12#include <algorithm>
13
14namespace graph::common {
15
17// ---------------------------------------------------------------------------------------------------------------------
18 public:
19 template<typename TNode>
21 const TNode &start,
22 const TNode &goal);
23
24// ---------------------------------------------------------------------------------------------------------------------
25
26 template<typename TNode>
28 const ext::map<TNode, TNode> &p_backward,
29 const TNode &start,
30 const TNode &goal,
31 const TNode &intersection_node);
32
33// ---------------------------------------------------------------------------------------------------------------------
34
35 template<typename TNode, typename TWeight>
36 static
40 const TNode &start,
41 const TNode &goal);
42
43// ---------------------------------------------------------------------------------------------------------------------
44
45 template<typename TNode, typename TWeight>
46 static
49 const ext::map<TNode, TNode> &p_backward,
50 const ext::map<TNode, TWeight> &g_forward,
51 const ext::map<TNode, TWeight> &g_backward,
52 const TNode &start,
53 const TNode &goal,
54 const TNode &intersection_node);
55
56// ---------------------------------------------------------------------------------------------------------------------
57
58 template<typename TNode>
59 static
61 joinPath(ext::vector<TNode> &forward_path, const ext::vector<TNode> &backward_path);
62
63// ---------------------------------------------------------------------------------------------------------------------
64};
65// =====================================================================================================================
66
67template<typename TNode>
69 const TNode &start,
70 const TNode &goal) {
72
73 // Path not found -> return empty vector
74 if (p.find(goal) == p.end()) {
75 return path;
76 }
77
78 path.push_back(goal);
79 TNode current_vertex = goal;
80
81 while (current_vertex != start) {
82 current_vertex = p.at(current_vertex);
83 path.push_back(current_vertex);
84 }
85
86 // Path need to be reverse.
87 std::reverse(path.begin(), path.end());
88
89 return path;
90}
91
92template<typename TNode>
94 const ext::map<TNode, TNode> &p_backward,
95 const TNode &start,
96 const TNode &goal,
97 const TNode &intersection_node) {
99
100 // Path not found -> return empty vector
101 if (p_forward.find(intersection_node) == p_forward.end()
102 || p_backward.find(intersection_node) == p_backward.end()) {
103 return path;
104 }
105
106 // First part of the path. From start to intersectionVertex (include).
107 path.push_back(intersection_node);
108 TNode current_vertex = intersection_node;
109
110 while (current_vertex != start) {
111 current_vertex = p_forward.at(current_vertex);
112 path.push_back(current_vertex);
113 }
114 // This part of the path need to be reverse.
115 std::reverse(path.begin(), path.end());
116
117 // Second part of the path. From intersectionVertex (exclude) to goal.
118 current_vertex = intersection_node;
119 while (current_vertex != goal) {
120 current_vertex = p_backward.at(current_vertex);
121 path.push_back(current_vertex);
122 }
123 // No reverse at the end.
124
125 return path;
126}
127
128// ---------------------------------------------------------------------------------------------------------------------
129
130template<typename TNode, typename TWeight>
134 const TNode &start,
135 const TNode &goal) {
136 if (g.find(goal) == g.end()) {
138 }
139
140 return ext::make_pair(reconstructPath(p, start, goal), g.at(goal));
141}
142
143template<typename TNode, typename TWeight>
146 const ext::map<TNode, TNode> &p_backward,
147 const ext::map<TNode, TWeight> &g_forward,
148 const ext::map<TNode, TWeight> &g_backward,
149 const TNode &start,
150 const TNode &goal,
151 const TNode &intersection_node) {
152 if (g_forward.find(intersection_node) == g_forward.end() || g_backward.find(intersection_node) == g_backward.end()) {
154 }
155
156 return ext::make_pair(reconstructPath(p_forward, p_backward, start, goal, intersection_node),
157 g_forward.at(intersection_node) + g_backward.at(intersection_node));
158}
159
160// ---------------------------------------------------------------------------------------------------------------------
161
162template<typename TNode>
164 const ext::vector<TNode> &backward_path) {
165 // ++ because we want skip the insertion node -> already in forward_path
166 forward_path.insert(forward_path.end(), ++backward_path.rbegin(), backward_path.rend());
167 return forward_path;
168}
169
170// ---------------------------------------------------------------------------------------------------------------------
171
172} // namespace graph::common
173
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
R & at(const T &key, R &defaultValue)
Definition: map.hpp:163
auto end() &
Inherited behavior of end for non-const instance.
Definition: map.hpp:215
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: vector.hpp:125
reverse_iterator insert(const_reverse_iterator pos, const T &value)
Inserts the value on position given by iterator pos.
Definition: vector.hpp:229
auto end() &
Inherited behavior of end for non-const instance.
Definition: vector.hpp:155
Definition: ReconstructPath.hpp:16
static ext::vector< TNode > joinPath(ext::vector< TNode > &forward_path, const ext::vector< TNode > &backward_path)
Definition: ReconstructPath.hpp:163
static ext::pair< ext::vector< TNode >, TWeight > reconstructWeightedPath(const ext::map< TNode, TNode > &p, const ext::map< TNode, TWeight > &g, const TNode &start, const TNode &goal)
Definition: ReconstructPath.hpp:132
static ext::vector< TNode > reconstructPath(const ext::map< TNode, TNode > &p, const TNode &start, const TNode &goal)
Definition: ReconstructPath.hpp:68
constexpr const T & max(const T &a)
Root case of maximum computation. The maximum from one value is the value itself.
Definition: algorithm.hpp:278
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: ReconstructPath.hpp:14
void start(measurements::stealth_string name, measurements::Type type)
Definition: measurements.cpp:14