# template class mrpt::graphs::CDijkstra

The Dijkstra algorithm for finding the shortest path between a given source node in a (weighted) directed graph and all other nodes in the form of a tree.

The constructor takes as input the graph (the set of directed edges) computes all the needed data, then successive calls to *getShortestPathTo* return the paths efficiently from the root.

The entire generated tree can be also retrieved with *getTreeGraph*.

Input graphs are represented by instances of (or classes derived from) mrpt::graphs::CDirectedGraph, and node’s IDs are uint64_t values, although the type mrpt::graphs::TNodeID is also provided for clarity in the code.

The second template argument MAPS_IMPLEMENTATION allows choosing between a sparse std::map<> representation (using * mrpt::containers::map_traits_stdmap) for several intermediary and final results, and an alternative (using mrpt::containers::map_traits_map_as_vector as argument) dense implementation which is much faster, but can be only used if the TNodeID’s start in 0 or a low value.

See <a href=”http://www.mrpt.org/Example:Dijkstra_optimal_path_search_in_graphs” this page for a complete example.

#include <mrpt/graphs/dijkstra.h> template < class TYPE_GRAPH, class MAPS_IMPLEMENTATION = mrpt::containers::map_traits_stdmap > class CDijkstra { public: // typedefs typedef TYPE_GRAPH graph_t; typedef typename graph_t::edge_t edge_t; typedef std::list<TPairNodeIDs> edge_list_t; typedef std::function<double(const graph_t&graph, const TNodeID id_from, const TNodeID id_to, const edge_t&edge)> functor_edge_weight_t; typedef std::function<void(const graph_t&graph, size_t visitedCount)> functor_on_progress_t; typedef CDirectedTree<const edge_t*> tree_graph_t; // structs struct TDistance; struct TPrevious; // construction 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() ); // methods double getNodeDistanceToRoot(const TNodeID id) const; const std::set<TNodeID>& getListOfAllNodes() const; TNodeID getRootNodeID() const; const list_all_neighbors_t& getCachedAdjacencyMatrix() const; void getShortestPathTo(const TNodeID target_node_ID, edge_list_t& out_path) const; void getTreeGraph(tree_graph_t& out_tree) const; tree_graph_t getTreeGraph() const; };

## Typedefs

typedef TYPE_GRAPH graph_t

The type of the graph, typically a mrpt::graphs::CDirectedGraph<> or any other derived class.

typedef typename graph_t::edge_t edge_t

The type of edge data in graph_t.

typedef std::list<TPairNodeIDs> edge_list_t

A list of edges used to describe a path on the graph.

typedef CDirectedTree<const edge_t*> tree_graph_t

Type for graph returned by *getTreeGraph:* a graph like the original input graph, but with edge data being pointers to the original data (to save copy time & memory)

## Construction

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 root node ID.

The graph is given by the set of directed edges, stored in a mrpt::graphs::CDirectedGraph class.

If a function *functor_edge_weight* is provided, it will be used to compute the weight of edges. Otherwise, all edges weight the unity.

After construction, call *getShortestPathTo* to get the shortest path to a node or *getTreeGraph* for the tree representation.

Parameters:

std::exception |
If the source nodeID is not found in the graph |

See also:

## Methods

double getNodeDistanceToRoot(const TNodeID id) const

Return the distance from the root node to any other node using the Dijkstra-generated tree.

Parameters:

std::exception |
On unknown node ID |

const std::set<TNodeID>& getListOfAllNodes() const

Return the set of all known node IDs (actually, a const ref to the internal set object).

TNodeID getRootNodeID() const

Return the node ID of the tree root, as passed in the constructor.

const list_all_neighbors_t& getCachedAdjacencyMatrix() const

Return the adjacency matrix of the input graph, which is cached at construction so if needed later just use this copy to avoid recomputing it.

See also:

mrpt::graphs::CDirectedGraph::getAdjacencyMatrix

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.

The reconstructed path contains a list of arcs (all of them exist in the graph with the given direction), such as the the first edge starts at the origin passed in the constructor, and the last one contains the given target.

An empty list of edges is returned when target equals the source node.

See also:

void getTreeGraph(tree_graph_t& out_tree) const

Returns a tree representation of the graph, as determined by the Dijkstra shortest paths from the root node.

Note that the annotations on each edge in the tree are “const pointers” to the original graph edge data, so it’s mandatory for the original input graph not to be deleted as long as this tree is used.

See also:

tree_graph_t getTreeGraph() const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.