9 #ifndef MRPT_DIJKSTRA_H 10 #define MRPT_DIJKSTRA_H 38 const std::set<mrpt::utils::TNodeID>& unconnected_nodeIDs,
41 m_err(err +
"\n\n") { }
43 using std::exception::what;
54 ASSERTMSG_(set_nodeIDs,
"\nSet of nodes pointer is invalid\n");
62 set_nodeIDs->insert(*it);
102 template<
class TYPE_GRAPH,
class MAPS_IMPLEMENTATION = mrpt::utils::map_traits_stdmap >
129 typedef typename MAPS_IMPLEMENTATION::template map<TNodeID,TDistance>
id2dist_map_t;
130 typedef typename MAPS_IMPLEMENTATION::template map<TNodeID,TPrevious>
id2id_map_t;
171 void (*functor_on_progress)(
const graph_t& graph,
size_t visitedCount) = NULL
197 THROW_EXCEPTION_FMT(
"Cannot find the source node_ID=%lu in the graph",static_cast<unsigned long>(source_node_ID));
205 size_t visitedCount = 0;
218 double min_d = std::numeric_limits<double>::max();
233 if (itDist->second.dist < min_d) {
235 min_d = itDist->second.dist;
244 std::set<TNodeID> nodeIDs_unconnected;
248 n_it = graph.nodes.begin();
249 n_it != graph.nodes.end();
253 bool have_traversed =
false;
258 if (n_it->first == d_it->first) {
259 have_traversed =
true;
264 if (!have_traversed) {
265 nodeIDs_unconnected.insert(n_it->first);
271 nodeIDs_unconnected, err_str);
281 if (functor_on_progress) (*functor_on_progress)(graph, visitedCount);
287 if (i == u)
continue;
291 bool edge_ui_reverse =
false;
292 bool edge_ui_found =
false;
295 double edge_ui_weight;
296 if (!functor_edge_weight)
299 edge_ui = graph.edges.find(std::make_pair(u,i));
300 if (edge_ui==graph.edges.end()) {
301 edge_ui = graph.edges.find(std::make_pair(i,u));
302 edge_ui_reverse =
true;
304 ASSERT_(edge_ui!=graph.edges.end());
305 edge_ui_weight = (*functor_edge_weight)(graph, edge_ui->first.first,edge_ui->first.second, edge_ui->second);
306 edge_ui_found =
true;
309 if ((min_d+edge_ui_weight) <
m_distances[i].dist) {
316 if (!edge_ui_found) {
317 edge_ui = graph.edges.find(std::make_pair(u,i));
318 if (edge_ui==graph.edges.end()) {
319 edge_ui = graph.edges.find(std::make_pair(i,u));
320 edge_ui_reverse =
true;
322 ASSERT_(edge_ui!=graph.edges.end());
325 if (!edge_ui_reverse)
330 }
while (visitedCount < nNodes);
346 return it->second.dist;
387 out_path.push_front(it->second);
415 const TNodeID id = itArcs->first;
416 const TNodeID id_from = itArcs->second.first;
417 const TNodeID id_to = itArcs->second.second;
419 std::list<TreeEdgeInfo> &edges = out_tree.
edges_to_children[
id==id_from ? id_to : id_from];
420 TreeEdgeInfo newEdge(
id);
421 newEdge.reverse = (
id==id_from);
425 "Edge %u->%u is in Dijkstra paths but not in original graph!",static_cast<unsigned int>(id_from),static_cast<unsigned int>(id_to)))
426 newEdge.data = & itEdgeData->second;
427 edges.push_back(newEdge);
MAPS_IMPLEMENTATION::template map< TNodeID, TDistance > id2dist_map_t
Auxiliary struct for topological distances from root node.
const list_all_neighbors_t & getCachedAdjacencyMatrix() const
Return the adjacency matrix of the input graph, which is cached at construction so if needed later ju...
TMapNode2ListEdges edges_to_children
The edges of each node.
< Make available this typedef in this namespace too
TDistance(const double D)
TYPE_GRAPH graph_t
The type of the graph, typically a mrpt::graphs::CDirectedGraph<> or any other derived class...
std::map< TNodeID, TDistance > m_distances_non_visited
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
const Scalar * const_iterator
const TDistance & operator=(const double D)
TNodeID getRootNodeID() const
Return the node ID of the tree root, as passed in the constructor.
CDijkstra(const graph_t &graph, const TNodeID source_node_ID, double(*functor_edge_weight)(const graph_t &graph, const TNodeID id_from, const TNodeID id_to, const edge_t &edge)=NULL, void(*functor_on_progress)(const graph_t &graph, size_t visitedCount)=NULL)
Constructor which takes the input graph and executes the entire Dijkstra algorithm from the given roo...
id2dist_map_t m_distances
All the distances.
MAPS_IMPLEMENTATION::template map< TNodeID, TPairNodeIDs > id2pairIDs_map_t
const TNodeID m_source_node_ID
void getUnconnectedNodeIDs(std::set< mrpt::utils::TNodeID > *set_nodeIDs) const
Fill set with the nodeIDs Dijkstra algorithm could not reach starting from the root node...
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
id2pairIDs_map_t m_prev_arc
uint64_t TNodeID
The type for node IDs in graphs of different types.
TNodeID root
The root of the tree.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
GLsizei const GLchar ** string
MAPS_IMPLEMENTATION::template map< TNodeID, std::set< TNodeID > > list_all_neighbors_t
A std::map (or a similar container according to MAPS_IMPLEMENTATION) with all the neighbors of every ...
MAPS_IMPLEMENTATION::template map< TNodeID, TPrevious > id2id_map_t
const std::set< TNodeID > & getListOfAllNodes() const
Return the set of all known node IDs (actually, a const ref to the internal set object).
list_all_neighbors_t m_allNeighbors
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
NotConnectedGraph(const std::set< mrpt::utils::TNodeID > &unconnected_nodeIDs, std::string err)
std::list< TPairNodeIDs > edge_list_t
A list of edges used to describe a path on the graph.
void getShortestPathTo(const TNodeID target_node_ID, edge_list_t &out_path) const
Returns the shortest path between the source node passed in the constructor and the given target node...
CDirectedTree< const edge_t * > tree_graph_t
Type for graph returned by getTreeGraph: a graph like the original input graph, but with edge data be...
std::set< TNodeID > m_lstNode_IDs
The Dijkstra algorithm for finding the shortest path between a given source node in a (weighted) dire...
const TYPE_GRAPH & m_cached_graph
Auxiliary struct for backward paths.
void clear()
Empty all edge data and set "root" to INVALID_NODEID.
graph_t::edge_t edge_t
The type of edge data in graph_t.
#define ASSERTMSG_(f, __ERROR_MSG)
Custom exception class that passes information in case an unconnected graph is passed to a Dijkstra i...
double getNodeDistanceToRoot(const TNodeID id) const
Return the distance from the root node to any other node using the Dijkstra-generated tree...
void getTreeGraph(tree_graph_t &out_tree) const
Returns a tree representation of the graph, as determined by the Dijkstra shortest paths from the roo...
std::set< mrpt::utils::TNodeID > m_unconnected_nodeIDs