forked from pgRouting/pgrouting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbdDijkstra.hpp
More file actions
151 lines (111 loc) · 4.82 KB
/
Copy pathbdDijkstra.hpp
File metadata and controls
151 lines (111 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*PGR-GNU*****************************************************************
File: bdDijkstra.hpp
Copyright (c) 2015 pgRouting developers
Mail: project@pgrouting.org
Copyright (c) 2016 Celia Virginia Vergara Castillo
vicky_vergara@hotmail.com
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
#ifndef INCLUDE_BDDIJKSTRA_BDDIJKSTRA_HPP_
#define INCLUDE_BDDIJKSTRA_BDDIJKSTRA_HPP_
#pragma once
#include <string>
#include <queue>
#include <utility>
#include <vector>
#include <limits>
#include <functional>
#include "cpp_common/bidirectional.hpp"
#include "cpp_common/path.hpp"
namespace pgrouting {
namespace bidirectional {
template < typename G >
class Pgr_bdDijkstra : public Pgr_bidirectional<G> {
typedef typename Pgr_bidirectional<G>::V V;
typedef typename Pgr_bidirectional<G>::E E;
typedef typename Pgr_bidirectional<G>::Cost_Vertex_pair Cost_Vertex_pair;
using Pgr_bidirectional<G>::graph;
using Pgr_bidirectional<G>::m_log;
using Pgr_bidirectional<G>::v_source;
using Pgr_bidirectional<G>::v_target;
using Pgr_bidirectional<G>::backward_predecessor;
using Pgr_bidirectional<G>::backward_queue;
using Pgr_bidirectional<G>::backward_finished;
using Pgr_bidirectional<G>::backward_cost;
using Pgr_bidirectional<G>::backward_edge;
using Pgr_bidirectional<G>::forward_predecessor;
using Pgr_bidirectional<G>::forward_queue;
using Pgr_bidirectional<G>::forward_finished;
using Pgr_bidirectional<G>::forward_cost;
using Pgr_bidirectional<G>::forward_edge;
using Pgr_bidirectional<G>::bidirectional;
public:
explicit Pgr_bdDijkstra(G &pgraph):
Pgr_bidirectional<G>(pgraph) {
m_log << "pgr_bdDijkstra constructor\n";
}
~Pgr_bdDijkstra() = default;
Pgr_bdDijkstra(const Pgr_bdDijkstra&) = default;
Pgr_bdDijkstra& operator=(const Pgr_bdDijkstra&) = default;
Pgr_bdDijkstra(Pgr_bdDijkstra&&) = default;
Pgr_bdDijkstra& operator=(Pgr_bdDijkstra&&) = default;
Path pgr_bdDijkstra(V start_vertex, V end_vertex, bool only_cost) {
m_log << "pgr_bdDijkstra\n";
v_source = start_vertex;
v_target = end_vertex;
return bidirectional(only_cost);
}
using Pgr_bidirectional<G>::log;
using Pgr_bidirectional<G>::clean_log;
private:
void explore_forward(const Cost_Vertex_pair &node) {
typename G::EO_i out, out_end;
auto current_cost = node.first;
auto current_node = node.second;
for (boost::tie(out, out_end) = out_edges(current_node, graph.graph);
out != out_end; ++out) {
auto edge_cost = graph[*out].cost;
auto next_node = graph.adjacent(current_node, *out);
if (forward_finished[next_node]) continue;
if (edge_cost + current_cost < forward_cost[next_node]) {
forward_cost[next_node] = edge_cost + current_cost;
forward_predecessor[next_node] = current_node;
forward_edge[next_node] = graph[*out].id;
forward_queue.push({forward_cost[next_node], next_node});
}
}
forward_finished[current_node] = true;
}
void explore_backward(const Cost_Vertex_pair &node) {
typename G::EI_i in, in_end;
auto current_cost = node.first;
auto current_node = node.second;
for (boost::tie(in, in_end) = in_edges(current_node, graph.graph);
in != in_end; ++in) {
auto edge_cost = graph[*in].cost;
auto next_node = graph.adjacent(current_node, *in);
if (backward_finished[next_node]) continue;
if (edge_cost + current_cost < backward_cost[next_node]) {
backward_cost[next_node] = edge_cost + current_cost;
backward_predecessor[next_node] = current_node;
backward_edge[next_node] = graph[*in].id;
backward_queue.push({backward_cost[next_node], next_node});
}
}
backward_finished[current_node] = true;
}
};
} // namespace bidirectional
} // namespace pgrouting
#endif // INCLUDE_BDDIJKSTRA_BDDIJKSTRA_HPP_