Gazebo Math

API Reference

8.0.0
gz::math::graph Namespace Reference

Classes

class  DirectedEdge
 A directed edge represents a connection between two vertices. The connection is unidirectional, it's only possible to traverse the edge in one direction (from the tail to the head). More...
 
class  Edge
 Generic edge class. An edge has two ends and some constraint between them. For example, a directed edge only allows traversing the edge in one direction. More...
 
struct  EdgeInitializer
 Used in the Graph constructors for uniform initialization. More...
 
class  Graph
 A generic graph class. Both vertices and edges can store user information. A vertex could be created passing a custom Id if needed, otherwise it will be choosen internally. The vertices also have a name that could be reused among other vertices if needed. This class supports the use of different edge types (e.g. directed or undirected edges). More...
 
class  UndirectedEdge
 An undirected edge represents a connection between two vertices. The connection is bidirectional, it's possible to traverse the edge in both directions. More...
 
class  Vertex
 A vertex of a graph. It stores user information, an optional name, and keeps an internal unique Id. This class does not enforce to choose a unique name. More...
 

Typedefs

using CostInfo = std::pair< double, VertexId >
 Used in Dijkstra. For a given source vertex, this pair represents the cost (first element) to reach a destination vertex (second element).
 
template<typename V , typename E >
using DirectedGraph = Graph< V, E, DirectedEdge< E > >
 
using EdgeId = uint64_t
 The unique Id for an edge.
 
using EdgeId_S = std::set< EdgeId >
 
template<typename EdgeType >
using EdgeRef_M = std::map< EdgeId, std::reference_wrapper< const EdgeType > >
 
template<typename V , typename E >
using UndirectedGraph = Graph< V, E, UndirectedEdge< E > >
 
using VertexId = uint64_t
 The unique Id of each vertex.
 
using VertexId_P = std::pair< VertexId, VertexId >
 
template<typename V >
using VertexRef_M = std::map< VertexId, std::reference_wrapper< const Vertex< V > > >
 

Functions

template<typename V , typename E , typename EdgeType >
std::vector< VertexIdBreadthFirstSort (const Graph< V, E, EdgeType > &_graph, const VertexId &_from)
 Breadth first sort (BFS). Starting from the vertex == _from, it traverses the graph exploring the neighbors first, before moving to the next level neighbors.
 
template<typename V , typename E >
std::vector< UndirectedGraph< V, E > > ConnectedComponents (const UndirectedGraph< V, E > &_graph)
 Calculate the connected components of an undirected graph. A connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices in the supergraph.
 
template<typename V , typename E , typename EdgeType >
std::vector< VertexIdDepthFirstSort (const Graph< V, E, EdgeType > &_graph, const VertexId &_from)
 Depth first sort (DFS). Starting from the vertex == _from, it visits the graph as far as possible along each branch before backtracking.
 
template<typename V , typename E , typename EdgeType >
std::map< VertexId, CostInfoDijkstra (const Graph< V, E, EdgeType > &_graph, const VertexId &_from, const VertexId &_to=kNullId)
 Dijkstra algorithm. Find the shortest path between the vertices in a graph. If only a graph and a source vertex is provided, the algorithm will find shortest paths from the source vertex to all other vertices in the graph. If an additional destination vertex is provided, the algorithm will stop when the shortest path is found between the source and destination vertex.
 
template<typename E , typename EdgeType >
EdgeTypeNullEdge ()
 An invalid edge.
 
template<typename V >
Vertex< V > & NullVertex ()
 An invalid vertex.
 
template<typename VV , typename EE >
std::ostreamoperator<< (std::ostream &_out, const Graph< VV, EE, DirectedEdge< EE > > &_g)
 Partial template specification for directed edges.
 
template<typename VV , typename EE >
std::ostreamoperator<< (std::ostream &_out, const Graph< VV, EE, UndirectedEdge< EE > > &_g)
 Partial template specification for undirected edges.
 
template<typename V , typename E >
UndirectedGraph< V, EToUndirectedGraph (const DirectedGraph< V, E > &_graph)
 Copy a DirectedGraph to an UndirectedGraph with the same vertices and edges.
 

Variables

constexpr VertexId kNullId = MAX_UI64
 Represents an invalid Id.
 

Typedef Documentation

◆ CostInfo

Used in Dijkstra. For a given source vertex, this pair represents the cost (first element) to reach a destination vertex (second element).

◆ DirectedGraph

◆ EdgeId

The unique Id for an edge.

◆ EdgeId_S

◆ EdgeRef_M

◆ UndirectedGraph

◆ VertexId

The unique Id of each vertex.

◆ VertexId_P

◆ VertexRef_M

Function Documentation

◆ BreadthFirstSort()

std::vector< VertexId > BreadthFirstSort ( const Graph< V, E, EdgeType > &  _graph,
const VertexId _from 
)

Breadth first sort (BFS). Starting from the vertex == _from, it traverses the graph exploring the neighbors first, before moving to the next level neighbors.

Parameters
[in]_graphA graph.
[in]_fromThe starting vertex.
Returns
The vector of vertices Ids traversed in a breadth first manner.

Referenced by ConnectedComponents().

◆ ConnectedComponents()

template<typename V , typename E >
std::vector< UndirectedGraph< V, E > > ConnectedComponents ( const UndirectedGraph< V, E > &  _graph)

Calculate the connected components of an undirected graph. A connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices in the supergraph.

See also
https://en.wikipedia.org/wiki/Connected_component_(graph_theory)
Parameters
[in]_graphA graph.
Returns
A vector of graphs. Each element of the graph is a component (subgraph) of the original graph.

References BreadthFirstSort().

◆ DepthFirstSort()

std::vector< VertexId > DepthFirstSort ( const Graph< V, E, EdgeType > &  _graph,
const VertexId _from 
)

Depth first sort (DFS). Starting from the vertex == _from, it visits the graph as far as possible along each branch before backtracking.

Parameters
[in]_graphA graph.
[in]_fromThe starting vertex.
Returns
The vector of vertices Ids visited in a depth first manner.

◆ Dijkstra()

std::map< VertexId, CostInfo > Dijkstra ( const Graph< V, E, EdgeType > &  _graph,
const VertexId _from,
const VertexId _to = kNullId 
)

Dijkstra algorithm. Find the shortest path between the vertices in a graph. If only a graph and a source vertex is provided, the algorithm will find shortest paths from the source vertex to all other vertices in the graph. If an additional destination vertex is provided, the algorithm will stop when the shortest path is found between the source and destination vertex.

Parameters
[in]_graphA graph.
[in]_fromThe starting vertex.
[in]_toOptional destination vertex.
Returns
A map where the keys are the destination vertices. For each destination, the value is another pair, where the key is the shortest cost from the origin vertex. The value is the previous neighbor Id in the shortest path. Note: In the case of providing a destination vertex, only the entry in the map with key = _to should be used. The rest of the map may contain incomplete information. If you want all shortest paths to all other vertices, please remove the destination vertex. If the source or destination vertex don't exist, the function will return an empty map.

E.g.: Given the following undirected graph, g, with five vertices:

         (6)                |
      0-------1             |
      |      /|\            |
      |     / | \‍(5)        |
      | (2)/  |  \          |
      |   /   |   2         |
   (1)|  / (2)|  /          |
      | /     | /(5)        |
      |/      |/            |
      3-------4             |
         (1)                |

This is the resut of Dijkstra(g, 0):

================================
================================
| 0 | 0 | 0 |
| 1 | 3 | 3 |
| 2 | 7 | 4 |
| 3 | 1 | 0 |
| 4 | 2 | 3 |
================================

This is the result of Dijkstra(g, 0, 3):

================================
================================
| 0 | 0 | 0 |
| 1 |ignore| ignore |
| 2 |ignore| ignore |
| 3 | 1 | 0 |
| 4 |ignore| ignore |
================================

References std::endl(), kNullId, std::make_pair(), and gz::math::MAX_D.

◆ NullEdge()

template<typename E , typename EdgeType >
EdgeType & NullEdge ( )

An invalid edge.

References kNullId.

◆ NullVertex()

template<typename V >
Vertex< V > & NullVertex ( )

An invalid vertex.

References kNullId.

◆ operator<<() [1/2]

template<typename VV , typename EE >
std::ostream & operator<< ( std::ostream _out,
const Graph< VV, EE, DirectedEdge< EE > > &  _g 
)

Partial template specification for directed edges.

References std::endl().

◆ operator<<() [2/2]

template<typename VV , typename EE >
std::ostream & operator<< ( std::ostream _out,
const Graph< VV, EE, UndirectedEdge< EE > > &  _g 
)

Partial template specification for undirected edges.

References std::endl().

◆ ToUndirectedGraph()

template<typename V , typename E >
UndirectedGraph< V, E > ToUndirectedGraph ( const DirectedGraph< V, E > &  _graph)

Copy a DirectedGraph to an UndirectedGraph with the same vertices and edges.

Parameters
[in]_graphA directed graph.
Returns
An undirected graph with the same vertices and edges as the original graph.

References vector< T >::push_back().

Variable Documentation

◆ kNullId