9 #ifndef MRPT_DIJKSTRA_H 10 #define MRPT_DIJKSTRA_H 37 const std::set<mrpt::utils::TNodeID>& unconnected_nodeIDs,
43 using std::exception::what;
50 std::set<mrpt::utils::TNodeID>* set_nodeIDs)
const 52 ASSERTMSG_(set_nodeIDs,
"\nSet of nodes pointer is invalid\n");
60 set_nodeIDs->insert(*it);
99 template <
class TYPE_GRAPH,
132 typename MAPS_IMPLEMENTATION::template map<TNodeID, std::set<TNodeID>>
134 typedef typename MAPS_IMPLEMENTATION::template map<TNodeID, TPairNodeIDs>
136 typedef typename MAPS_IMPLEMENTATION::template map<TNodeID, TDistance>
138 typedef typename MAPS_IMPLEMENTATION::template map<TNodeID, TPrevious>
144 std::map<TNodeID, TDistance>
168 std::function<void(const graph_t& graph, size_t visitedCount)>;
222 "Cannot find the source node_ID=%lu in the graph",
223 static_cast<unsigned long>(source_node_ID));
231 size_t visitedCount = 0;
246 double min_d = std::numeric_limits<double>::max();
262 if (itDist->second.dist < min_d)
265 min_d = itDist->second.dist;
275 std::set<TNodeID> nodeIDs_unconnected;
280 n_it != graph.nodes.end(); ++n_it)
283 bool have_traversed =
false;
288 if (n_it->first == d_it->first)
290 have_traversed =
true;
297 nodeIDs_unconnected.insert(n_it->first);
304 nodeIDs_unconnected, err_str);
315 if (functor_on_progress) functor_on_progress(graph, visitedCount);
318 const std::set<TNodeID>& neighborsOfU =
321 itNei != neighborsOfU.end(); ++itNei)
324 if (i == u)
continue;
329 bool edge_ui_reverse =
false;
330 bool edge_ui_found =
false;
333 double edge_ui_weight;
334 if (!functor_edge_weight)
338 edge_ui = graph.edges.find(std::make_pair(u, i));
339 if (edge_ui == graph.edges.end())
341 edge_ui = graph.edges.find(std::make_pair(i, u));
342 edge_ui_reverse =
true;
344 ASSERT_(edge_ui != graph.edges.end());
345 edge_ui_weight = functor_edge_weight(
346 graph, edge_ui->first.first, edge_ui->first.second,
348 edge_ui_found =
true;
351 if ((min_d + edge_ui_weight) <
m_distances[i].dist)
362 edge_ui = graph.edges.find(std::make_pair(u, i));
363 if (edge_ui == graph.edges.end())
365 edge_ui = graph.edges.find(std::make_pair(i, u));
366 edge_ui_reverse =
true;
368 ASSERT_(edge_ui != graph.edges.end());
371 if (!edge_ui_reverse)
377 }
while (visitedCount < nNodes);
394 "Node was not found in the graph when running Dijkstra");
395 return it->second.dist;
441 out_path.push_front(it->second);
473 const TNodeID id = itArcs->first;
474 const TNodeID id_from = itArcs->second.first;
475 const TNodeID id_to = itArcs->second.second;
477 std::list<TreeEdgeInfo>& edges =
479 TreeEdgeInfo newEdge(
id);
480 newEdge.reverse = (
id == id_from);
486 "Edge %u->%u is in Dijkstra paths but not in original " 488 static_cast<unsigned int>(id_from),
489 static_cast<unsigned int>(id_to)))
490 newEdge.data = &itEdgeData->second;
491 edges.push_back(newEdge);
Auxiliary struct for topological distances from root node.
std::function< double(const graph_t &graph, const TNodeID id_from, const TNodeID id_to, const edge_t &edge)> functor_edge_weight_t
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.
A special kind of graph in the form of a tree with directed edges and optional edge annotations of te...
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
TDistance(const double D)
TYPE_GRAPH graph_t
The type of the graph, typically a mrpt::graphs::CDirectedGraph<> or any other derived class...
#define THROW_EXCEPTION(msg)
std::map< TNodeID, TDistance > m_distances_non_visited
#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.
MAPS_IMPLEMENTATION::template map< TNodeID, TDistance > id2dist_map_t
id2dist_map_t m_distances
All the distances.
const TNodeID m_source_node_ID
MAPS_IMPLEMENTATION::template map< TNodeID, TPrevious > id2id_map_t
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...
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::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
id2pairIDs_map_t m_prev_arc
CDijkstra(const graph_t &graph, const TNodeID source_node_ID, functor_edge_weight_t functor_edge_weight=functor_edge_weight_t(), functor_on_progress_t functor_on_progress=functor_on_progress_t())
Constructor which takes the input graph and executes the entire Dijkstra algorithm from the given roo...
uint64_t TNodeID
The type for node IDs in graphs of different types.
std::function< void(const graph_t &graph, size_t visitedCount)> functor_on_progress_t
TNodeID root
The root of the tree.
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 ...
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)
MAPS_IMPLEMENTATION::template map< TNodeID, TPairNodeIDs > id2pairIDs_map_t
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...
std::set< TNodeID > m_lstNode_IDs
The Dijkstra algorithm for finding the shortest path between a given source node in a (weighted) dire...
Traits for using a std::map<> (sparse representation)
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