9 #ifndef MRPT_DIJKSTRA_H
10 #define MRPT_DIJKSTRA_H
35 const std::set<mrpt::graphs::TNodeID>& unconnected_nodeIDs,
41 using std::exception::what;
48 std::set<mrpt::graphs::TNodeID>* set_nodeIDs)
const
50 ASSERTMSG_(set_nodeIDs,
"\nSet of nodes pointer is invalid\n");
58 set_nodeIDs->insert(*it);
131 typename MAPS_IMPLEMENTATION::template map<TNodeID, std::set<TNodeID>>;
133 typename MAPS_IMPLEMENTATION::template map<TNodeID, TPairNodeIDs>;
135 typename MAPS_IMPLEMENTATION::template map<TNodeID, TDistance>;
137 typename MAPS_IMPLEMENTATION::template map<TNodeID, TPrevious>;
142 std::map<TNodeID, TDistance>
166 std::function<
void(
const graph_t& graph,
size_t visitedCount)>;
220 "Cannot find the source node_ID=%lu in the graph",
221 static_cast<unsigned long>(source_node_ID));
229 size_t visitedCount = 0;
244 double min_d = std::numeric_limits<double>::max();
260 if (itDist->second.dist < min_d)
263 min_d = itDist->second.dist;
273 std::set<TNodeID> nodeIDs_unconnected;
278 n_it != graph.nodes.end(); ++n_it)
281 bool have_traversed =
false;
286 if (n_it->first == d_it->first)
288 have_traversed =
true;
295 nodeIDs_unconnected.insert(n_it->first);
302 nodeIDs_unconnected, err_str);
313 if (functor_on_progress) functor_on_progress(graph, visitedCount);
316 const std::set<TNodeID>& neighborsOfU =
319 itNei != neighborsOfU.end(); ++itNei)
322 if (i == u)
continue;
327 bool edge_ui_reverse =
false;
328 bool edge_ui_found =
false;
331 double edge_ui_weight;
332 if (!functor_edge_weight)
336 edge_ui = graph.edges.find(std::make_pair(u, i));
337 if (edge_ui == graph.edges.end())
339 edge_ui = graph.edges.find(std::make_pair(i, u));
340 edge_ui_reverse =
true;
342 ASSERT_(edge_ui != graph.edges.end());
343 edge_ui_weight = functor_edge_weight(
344 graph, edge_ui->first.first, edge_ui->first.second,
346 edge_ui_found =
true;
349 if ((min_d + edge_ui_weight) <
m_distances[i].dist)
360 edge_ui = graph.edges.find(std::make_pair(u, i));
361 if (edge_ui == graph.edges.end())
363 edge_ui = graph.edges.find(std::make_pair(i, u));
364 edge_ui_reverse =
true;
366 ASSERT_(edge_ui != graph.edges.end());
369 if (!edge_ui_reverse)
375 }
while (visitedCount < nNodes);
392 "Node was not found in the graph when running Dijkstra");
393 return it->second.dist;
438 out_path.push_front(it->second);
470 const TNodeID id = itArcs->first;
471 const TNodeID id_from = itArcs->second.first;
472 const TNodeID id_to = itArcs->second.second;
474 std::list<TreeEdgeInfo>& edges =
476 TreeEdgeInfo newEdge(
id);
477 newEdge.reverse = (
id == id_from);
483 "Edge %u->%u is in Dijkstra paths but not in original "
485 static_cast<unsigned int>(id_from),
486 static_cast<unsigned int>(id_to)));
487 newEdge.data = &itEdgeData->second;
488 edges.push_back(newEdge);
The Dijkstra algorithm for finding the shortest path between a given source node in a (weighted) dire...
typename graph_t::edge_t edge_t
The type of edge data in graph_t.
std::map< TNodeID, TDistance > m_distances_non_visited
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...
double getNodeDistanceToRoot(const TNodeID id) const
Return the distance from the root node to any other node using the Dijkstra-generated tree.
TNodeID getRootNodeID() const
Return the node ID of the tree root, as passed in the constructor.
std::function< void(const graph_t &graph, size_t visitedCount)> functor_on_progress_t
std::function< double(const graph_t &graph, const TNodeID id_from, const TNodeID id_to, const edge_t &edge)> functor_edge_weight_t
std::set< TNodeID > m_lstNode_IDs
id2pairIDs_map_t m_prev_arc
TYPE_GRAPH graph_t
The type of the graph, typically a mrpt::graphs::CDirectedGraph<> or any other derived class.
typename MAPS_IMPLEMENTATION::template map< TNodeID, TDistance > id2dist_map_t
id2dist_map_t m_distances
All the distances.
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...
const TYPE_GRAPH & m_cached_graph
typename MAPS_IMPLEMENTATION::template map< TNodeID, TPairNodeIDs > id2pairIDs_map_t
list_all_neighbors_t m_allNeighbors
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...
typename 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 TNodeID m_source_node_ID
typename 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).
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...
A special kind of graph in the form of a tree with directed edges and optional edge annotations of te...
void clear()
Empty all edge data and set "root" to INVALID_NODEID.
TNodeID root
The root of the tree.
TMapNode2ListEdges edges_to_children
The edges of each node.
Custom exception class that passes information in case an unconnected graph is passed to a Dijkstra i...
std::set< mrpt::graphs::TNodeID > m_unconnected_nodeIDs
void getUnconnectedNodeIDs(std::set< mrpt::graphs::TNodeID > *set_nodeIDs) const
Fill set with the nodeIDs Dijkstra algorithm could not reach starting from the root node.
NotConnectedGraph(const std::set< mrpt::graphs::TNodeID > &unconnected_nodeIDs, std::string err)
const Scalar * const_iterator
#define ASSERT_(f)
Defines an assertion mechanism.
#define THROW_EXCEPTION(msg)
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLenum GLsizei GLenum format
GLsizei const GLchar ** string
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Abstract graph and tree data structures, plus generic graph algorithms.
uint64_t TNodeID
A generic numeric type for unique IDs of nodes or entities.
Traits for using a std::map<> (sparse representation)
Auxiliary struct for topological distances from root node.
TDistance(const double D)
const TDistance & operator=(const double D)
Auxiliary struct for backward paths.