34 const std::set<mrpt::graphs::TNodeID>& unconnected_nodeIDs,
40 using std::exception::what;
49 ASSERTMSG_(set_nodeIDs,
"\nSet of nodes pointer is invalid\n");
55 set_nodeIDs->insert(m_unconnected_nodeID);
128 typename MAPS_IMPLEMENTATION::template map<TNodeID, std::set<TNodeID>>;
130 typename MAPS_IMPLEMENTATION::template map<TNodeID, TPairNodeIDs>;
132 typename MAPS_IMPLEMENTATION::template map<TNodeID, TDistance>;
134 typename MAPS_IMPLEMENTATION::template map<TNodeID, TPrevious>;
139 std::map<TNodeID, TDistance>
163 std::function<void(const graph_t& graph, size_t visitedCount)>;
217 "Cannot find the source node_ID=%lu in the graph",
218 static_cast<unsigned long>(source_node_ID));
226 size_t visitedCount = 0;
241 double min_d = std::numeric_limits<double>::max();
256 if (itDist->second.dist < min_d)
259 min_d = itDist->second.dist;
269 std::set<TNodeID> nodeIDs_unconnected;
272 for (
auto n_it = graph.nodes.begin(); n_it != graph.nodes.end();
276 bool have_traversed =
false;
280 if (n_it->first == d_it->first)
282 have_traversed =
true;
289 nodeIDs_unconnected.insert(n_it->first);
293 std::string err_str =
"Graph is not fully connected!";
295 nodeIDs_unconnected, err_str);
306 if (functor_on_progress) functor_on_progress(graph, visitedCount);
309 const std::set<TNodeID>& neighborsOfU =
311 for (
unsigned long i : neighborsOfU)
313 if (i == u)
continue;
317 typename graph_t::const_iterator edge_ui;
318 bool edge_ui_reverse =
false;
319 bool edge_ui_found =
false;
322 double edge_ui_weight;
323 if (!functor_edge_weight)
327 edge_ui = graph.edges.find(std::make_pair(u, i));
328 if (edge_ui == graph.edges.end())
330 edge_ui = graph.edges.find(std::make_pair(i, u));
331 edge_ui_reverse =
true;
333 ASSERT_(edge_ui != graph.edges.end());
334 edge_ui_weight = functor_edge_weight(
335 graph, edge_ui->first.first, edge_ui->first.second,
337 edge_ui_found =
true;
340 if ((min_d + edge_ui_weight) <
m_distances[i].dist)
351 edge_ui = graph.edges.find(std::make_pair(u, i));
352 if (edge_ui == graph.edges.end())
354 edge_ui = graph.edges.find(std::make_pair(i, u));
355 edge_ui_reverse =
true;
357 ASSERT_(edge_ui != graph.edges.end());
360 if (!edge_ui_reverse)
366 }
while (visitedCount < nNodes);
380 typename id2dist_map_t::const_iterator it =
m_distances.find(
id);
383 "Node was not found in the graph when running Dijkstra");
384 return it->second.dist;
427 typename id2pairIDs_map_t::const_iterator it =
m_prev_arc.find(nod);
429 out_path.push_front(it->second);
460 const TNodeID id = itArcs->first;
461 const TNodeID id_from = itArcs->second.first;
462 const TNodeID id_to = itArcs->second.second;
464 std::list<TreeEdgeInfo>& edges =
466 TreeEdgeInfo newEdge(
id);
467 newEdge.reverse = (
id == id_from);
473 "Edge %u->%u is in Dijkstra paths but not in original " 475 static_cast<unsigned int>(id_from),
476 static_cast<unsigned int>(id_to)));
477 newEdge.data = &itEdgeData->second;
478 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...
TDistance(const double D)
#define THROW_EXCEPTION(msg)
std::string std::string format(std::string_view fmt, ARGS &&... args)
typename graph_t::edge_t edge_t
The type of edge data in graph_t.
std::map< TNodeID, TDistance > m_distances_non_visited
Abstract graph and tree data structures, plus generic graph algorithms.
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...
const TDistance & operator=(const double D)
TNodeID getRootNodeID() const
Return the node ID of the tree root, as passed in the constructor.
id2dist_map_t m_distances
All the distances.
typename MAPS_IMPLEMENTATION::template map< TNodeID, TPairNodeIDs > id2pairIDs_map_t
#define ASSERT_(f)
Defines an assertion mechanism.
std::set< mrpt::graphs::TNodeID > m_unconnected_nodeIDs
const TNodeID m_source_node_ID
std::list< TPairNodeIDs > edge_list_t
A list of edges used to describe a path on the graph.
typename MAPS_IMPLEMENTATION::template map< TNodeID, TDistance > id2dist_map_t
id2pairIDs_map_t m_prev_arc
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
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...
std::function< void(const graph_t &graph, size_t visitedCount)> functor_on_progress_t
TNodeID root
The root of the tree.
~NotConnectedGraph() noexcept override=default
Traits for using a std::map<> (sparse representation)
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
NotConnectedGraph(const std::set< mrpt::graphs::TNodeID > &unconnected_nodeIDs, std::string err)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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, TPrevious > id2id_map_t
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 ...
uint64_t TNodeID
A generic numeric type for unique IDs of nodes or entities.
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...
const TYPE_GRAPH & m_cached_graph
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Auxiliary struct for backward paths.
void clear()
Empty all edge data and set "root" to INVALID_NODEID.
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...