|
| | Graph ()=default |
| | Creates an empty graph.
|
| | Graph (const std::vector< std::array< Vertex, 2 > > &edges) |
| | Creates a graph from a list of undirected edges.
|
| void | addVertex (const Vertex &vertex) |
| | Adds a vertex if it is not already present.
|
| void | addEdge (const Vertex &u, const Vertex &v) |
| | Adds an undirected edge and its endpoints.
|
| bool | containsVertex (const Vertex &vertex) const |
| | Tests whether a vertex is present.
|
| bool | containsEdge (const Vertex &u, const Vertex &v) const |
| | Tests whether an undirected edge is present.
|
| int | degree (const Vertex &vertex) const |
| | Returns the degree of a vertex.
|
| int | maxDegree () const |
| | Returns the largest vertex degree.
|
| int | vertexCount () const |
| | Returns the number of vertices.
|
| int | edgeCount () const |
| | Returns the number of undirected edges.
|
| void | removeEdge (const Vertex &u, const Vertex &v) |
| | Removes an undirected edge if it is present.
|
| void | removeVertex (const Vertex &vertex) |
| | Removes a vertex and every incident edge.
|
| void | clear () |
| | Removes every vertex and edge.
|
| auto | vertices () const |
| | Returns a lazy view over the vertices.
|
| auto | edges () const |
| | Returns a lazy view over the undirected edges.
|
| const NeighborSet & | neighbors (const Vertex &vertex) const |
| | Returns the neighbors of a vertex.
|
| NeighborSet | closedNeighbors (const Vertex &vertex) const |
| | Returns a vertex together with all of its neighbors.
|
| std::vector< Vertex > | bfs (const Vertex &vertex, int maxVertices=0) const |
| | Traverses a connected component in breadth-first order.
|
| std::vector< std::vector< Vertex > > | components () const |
| | Returns the graph's connected components.
|
| std::vector< std::vector< Vertex > > | biconnectedComponents () const |
| | Returns the vertex-biconnected blocks of the graph.
|
| std::vector< std::vector< Vertex > > | cliqueCover () const |
| | Computes a vertex clique cover using the DSATUR heuristic.
|
| std::vector< Vertex > | independentSet () const |
| | Computes a maximal independent set greedily from low-degree vertices.
|
| template<class WeightFunction> |
| Graph | spanningTree (WeightFunction weight) const |
| | Computes a minimum spanning forest using Prim's algorithm.
|
| template<class WeightFunction> |
| std::vector< Vertex > | shortestPath (const Vertex &source, const Vertex &target, WeightFunction weight) const |
| | Computes a shortest path between two vertices using Dijkstra's algorithm.
|
| template<class WeightFunction, class LowerBoundFunction> |
| std::vector< Vertex > | shortestPath (const Vertex &source, const Vertex &target, WeightFunction weight, LowerBoundFunction lowerBound) const |
| | Computes a shortest path between two vertices using the A* algorithm.
|
| iterator | begin () |
| | Returns an iterator to the first vertex.
|
| iterator | end () |
| | Returns the end iterator.
|
| const_iterator | begin () const |
| | Returns a const iterator to the first vertex.
|
| const_iterator | end () const |
| | Returns the const end iterator.
|
| const_iterator | cbegin () const |
| | Returns a const iterator to the first vertex.
|
| const_iterator | cend () const |
| | Returns the const end iterator.
|
template<class Vertex>
class pgl::Graph< Vertex >
Undirected simple graph stored as adjacency sets.
Undirected simple graph with hashable vertices.
Adding an edge also adds both of its endpoints. Self-loops are ignored and repeated edges are coalesced. Vertex and traversal order are unspecified because the graph uses unordered containers.
- Template Parameters
-
| Vertex | Hashable, equality-comparable vertex type. |
template<class Vertex>
template<class WeightFunction>
| std::vector< Vertex > pgl::Graph< Vertex >::shortestPath |
( |
const Vertex & | source, |
|
|
const Vertex & | target, |
|
|
WeightFunction | weight ) const |
|
inlinenodiscard |
Computes a shortest path between two vertices using Dijkstra's algorithm.
The returned path starts at source, ends at target and lists every vertex along the way; the path from a vertex to itself is that vertex alone. An empty result means that no path exists, either because the two vertices lie in different connected components or because one of them is absent. Ties between equally long paths are broken by the graph's unspecified iteration order.
The frontier is a lazy binary heap (std::priority_queue): a vertex is pushed once per incident edge relaxed and discarded when popped if it has already been settled. The search stops as soon as target is settled. Complexity is $O(m \log m)$ weight comparisons and $O(m)$ calls to weight for a graph with $m$ edges.
- Template Parameters
-
| WeightFunction | Callable taking two vertices and returning a copyable edge weight ordered by < and added by +; the weight type is chosen by the callable. |
- Parameters
-
| source | First vertex of the path. |
| target | Last vertex of the path. |
| weight | Edge weight function. It must be symmetric and must not return a negative weight; neither is checked, and the returned path is unspecified when either fails. |
- Returns
- The vertices of a shortest path from
source to target, or an empty vector if there is none.
template<class Vertex>
template<class WeightFunction, class LowerBoundFunction>
| std::vector< Vertex > pgl::Graph< Vertex >::shortestPath |
( |
const Vertex & | source, |
|
|
const Vertex & | target, |
|
|
WeightFunction | weight, |
|
|
LowerBoundFunction | lowerBound ) const |
|
inlinenodiscard |
Computes a shortest path between two vertices using the A* algorithm.
This overload has the same result and endpoint behavior as the Dijkstra overload, but uses lowerBound to prioritize vertices that appear closer to target. The lower bound need not be consistent: a vertex is reopened whenever a shorter path to it is found.
The frontier is a lazy binary heap (std::priority_queue) ordered by the sum of the path length so far and the estimated remaining distance. With a consistent lower bound, complexity is $O(m \log m)$ weight comparisons and $O(m)$ calls to weight and lowerBound for a graph with $m$ edges. An inconsistent lower bound can cause vertices to be reopened.
- Template Parameters
-
| WeightFunction | Callable taking two vertices and returning a copyable edge weight ordered by < and added by +; the weight type is chosen by the callable. |
| LowerBoundFunction | Callable taking two vertices and returning a lower bound in the same weight type. |
- Parameters
-
| source | First vertex of the path. |
| target | Last vertex of the path. |
| weight | Edge weight function. It must be symmetric and must not return a negative weight; neither is checked, and the returned path is unspecified when either fails. |
| lowerBound | Estimate of the distance between two vertices. For every vertex reached by the search, lowerBound(vertex, target) must be nonnegative, must not exceed the shortest distance to target, and must be zero when vertex == target. These requirements are not checked. |
- Returns
- The vertices of a shortest path from
source to target, or an empty vector if there is none.
template<class Vertex>
template<class WeightFunction>
Computes a minimum spanning forest using Prim's algorithm.
The tree of a connected graph is a minimum spanning tree. A disconnected graph yields one minimum spanning tree per connected component, so the result always contains every vertex of this graph, isolated ones included. Ties between equally heavy edges are broken by the graph's unspecified iteration order.
The frontier is a lazy binary heap (std::priority_queue): an edge is pushed when one endpoint enters the tree and discarded when popped if its other endpoint has since been reached. Complexity is $O(m \log m)$ weight comparisons and $O(m)$ calls to weight for a graph with $m$ edges.
- Template Parameters
-
| WeightFunction | Callable taking two vertices and returning a copyable, less-than-comparable edge weight; the weight type is chosen by the callable. |
- Parameters
-
| weight | Edge weight function. It must be symmetric, otherwise which of the two values is used for an edge is unspecified. |
- Returns
- A minimum spanning forest of this graph.