|
Template Numerical Library version\ main:4e6e2c1
|
Graph class represents a mathematical graph using an adjacency matrix. More...
#include <TNL/Graphs/Graph.h>
Public Types | |
| using | AdjacencyMatrixType = AdjacencyMatrix |
| Type of the adjacency matrix. | |
| using | AdjacencyMatrixView = typename AdjacencyMatrixType::ViewType |
| Type of the adjacency matrix view. | |
| using | ConstAdjacencyMatrixView = typename AdjacencyMatrixType::ConstViewType |
| Type of constant view of the adjacency matrix. | |
| using | ConstVertexView = typename VertexView::ConstVertexView |
| Type of constant graph nodes view. | |
| using | ConstViewType = GraphView< std::add_const_t< Value >, Device, Index, Orientation, ConstAdjacencyMatrixView > |
| Type of constant view of the adjacency matrix. | |
| using | DeviceType = Device |
| Type of device where the graph will be operating. | |
| using | GraphOrientation = Orientation |
| Type of the graph - directed or undirected. | |
| using | IndexType = Index |
| Type for indexing of the graph nodes. | |
| template<typename Value_ = Value, typename Device_ = Device, typename Index_ = Index, typename Orientation_ = Orientation, template< typename, typename, typename > class Segments_ = Segments, typename AdjacencyMatrix_ = AdjacencyMatrix> | |
| using | Self = Graph< Value_, Device_, Index_, Orientation_, Segments_, AdjacencyMatrix_ > |
| Helper type for getting self type or its modifications. | |
| using | ValueType = std::remove_cv_t< Value > |
| Type for weights of the graph edges. | |
| using | VertexView = GraphVertexView< AdjacencyMatrixView, Orientation > |
| Type of the graph nodes view. | |
| using | ViewType = GraphView< Value, Device, Index, Orientation, AdjacencyMatrixView > |
| Type of constant view of the adjacency matrix. | |
| Public Types inherited from TNL::Graphs::GraphBase< Value, Device, Index, DirectedGraph, TNL::Matrices::SparseMatrix< Value, Device, Index, TNL::Matrices::GeneralMatrix, TNL::Algorithms::Segments::CSR >::ViewType > | |
| using | AdjacencyMatrixView |
| Type of the adjacency matrix view. | |
| using | ConstAdjacencyMatrixView |
| Type of constant view of the adjacency matrix. | |
| using | ConstVertexView |
| using | DeviceType |
| Type of device where the graph will be operating. | |
| using | GraphOrientation |
| Type of the graph - directed or undirected. | |
| using | IndexType |
| Type for indexing of the graph nodes. | |
| using | ValueType |
| Type for weights of the graph edges. | |
| using | VertexView |
Public Member Functions | |
| Graph ()=default | |
| Default constructor. | |
| Graph (AdjacencyMatrixType &&matrix) | |
| Constructor with adjacency matrix. | |
| Graph (const AdjacencyMatrixType &matrix) | |
| Constructor with adjacency matrix. | |
| Graph (const Graph &)=default | |
| Copy constructor. | |
| template<typename OtherGraph> | |
| Graph (const OtherGraph &&other) | |
| Templated move constructor from another graph type. | |
| template<typename OtherGraph> | |
| Graph (const OtherGraph &other) | |
| Templated copy constructor from another graph type. | |
| template<typename T = AdjacencyMatrixType, typename C = std::enable_if_t< Matrices::is_dense_matrix_v< T > >> | |
| Graph (const std::initializer_list< std::initializer_list< ValueType > > &data, Matrices::MatrixElementsEncoding encoding=isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricMixed) | |
| Constructor with number of nodes and edges given as nested initializer list (for dense adjacency matrix). | |
| Graph (Graph &&)=default | |
| Move constructor. | |
| Graph (IndexType nodesCount) | |
| Constructor with number of nodes. | |
| Graph (IndexType vertexCount, const std::initializer_list< std::tuple< IndexType, IndexType, ValueType > > &data, Matrices::MatrixElementsEncoding encoding=isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricMixed) | |
| Constructor with number of nodes and edges given as initializer list. | |
| template<typename MapIndex, typename MapValue> | |
| Graph (IndexType vertexCount, const std::map< std::pair< MapIndex, MapIndex >, MapValue > &map, Matrices::MatrixElementsEncoding encoding=isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricMixed) | |
| Constructor with number of nodes and edges given as a map. | |
| ~Graph ()=default | |
| Destructor. | |
| AdjacencyMatrixType & | getAdjacencyMatrix () |
| Returns the modifiable adjacency matrix of the graph. | |
| const AdjacencyMatrixType & | getAdjacencyMatrix () const |
| Returns the modifiable adjacency matrix of the graph. | |
| ConstViewType | getConstView () const |
| Returns the constant view of the graph. | |
| ViewType | getView () |
| Returns the modifiable view of the graph. | |
| Graph & | operator= (const Graph &other) |
| Copy-assignment operator. | |
| template<typename OtherGraph, std::enable_if_t< isGraph< OtherGraph >(std::declval< OtherGraph >()) >> | |
| Graph & | operator= (const OtherGraph &other) |
| Graph & | operator= (Graph &&other) noexcept |
| Move-assignment operator. | |
| void | reset () |
| Resets the graph to zero vertices and edges. | |
| void | setAdjacencyMatrix (AdjacencyMatrixType &&matrix) |
| Sets the adjacency matrix of the graph. | |
| void | setAdjacencyMatrix (const AdjacencyMatrixType &matrix) |
| Sets the adjacency matrix of the graph. | |
| template<typename Matrix_> | |
| void | setAdjacencyMatrix (const Matrix_ &matrix) |
| Sets the adjacency matrix of the graph. | |
| template<typename T = AdjacencyMatrixType, typename C = std::enable_if_t< Matrices::is_dense_matrix_v< T > >> | |
| void | setDenseEdges (const std::initializer_list< std::initializer_list< ValueType > > &data, Matrices::MatrixElementsEncoding encoding=isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricLower) |
| Sets the edges of the graph from a nested initializer list (for dense adjacency matrix). | |
| template<typename Vector> | |
| void | setEdgeCounts (const Vector &edgeCounts) |
| Sets the edge counts (capacities) for all vertices in the graph. | |
| void | setEdges (const std::initializer_list< std::tuple< IndexType, IndexType, ValueType > > &data, Matrices::MatrixElementsEncoding encoding=isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricMixed) |
| Sets the edges of the graph from an initializer list. | |
| template<typename MapIndex, typename MapValue> | |
| void | setEdges (const std::map< std::pair< MapIndex, MapIndex >, MapValue > &map, Matrices::MatrixElementsEncoding encoding=isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricMixed) |
| Sets the edges of the graph from a map. | |
| template<typename Vector> | |
| void | setVertexCapacities (const Vector &nodeCapacities) |
| Sets the capacities of the graph nodes. | |
| void | setVertexCount (IndexType nodesCount) |
| Sets the number of nodes in the graph. | |
| Public Member Functions inherited from TNL::Graphs::GraphBase< Value, Device, Index, DirectedGraph, TNL::Matrices::SparseMatrix< Value, Device, Index, TNL::Matrices::GeneralMatrix, TNL::Algorithms::Segments::CSR >::ViewType > | |
| GraphBase ()=default | |
| Default constructor. | |
| ~GraphBase ()=default | |
| Destructor. | |
| __cuda_callable__ const AdjacencyMatrixView & | getAdjacencyMatrixView () const |
| Returns the constant view of adjacency matrix of the graph. | |
| __cuda_callable__ IndexType | getEdgeCount () const |
| Returns the number of edges in the graph. | |
| __cuda_callable__ ValueType | getEdgeWeight (IndexType vertexIdx, IndexType edgeIdx) const |
| __cuda_callable__ ConstVertexView | getVertex (IndexType vertexIdx) const |
| __cuda_callable__ IndexType | getVertexCount () const |
| Returns the number of nodes in the graph. | |
| __cuda_callable__ IndexType | getVertexDegree (IndexType nodeIdx) const |
| GraphBase & | operator= (const GraphBase &)=delete |
| Copy-assignment operator. | |
| bool | operator== (const GraphBase &other) const |
| Comparisons operator. | |
| __cuda_callable__ void | setEdgeWeight (IndexType vertexIdx, IndexType edgeIdx, const ValueType &value) |
Static Public Member Functions | |
| static constexpr bool | isDirected () |
| Checks if the graph is directed. | |
| static constexpr bool | isUndirected () |
| Checks if the graph is undirected. | |
| Static Public Member Functions inherited from TNL::Graphs::GraphBase< Value, Device, Index, DirectedGraph, TNL::Matrices::SparseMatrix< Value, Device, Index, TNL::Matrices::GeneralMatrix, TNL::Algorithms::Segments::CSR >::ViewType > | |
| static std::string | getSerializationType () |
| Returns the type of serialization used for the graph. | |
| static constexpr bool | isDirected () |
| Checks if the graph is directed. | |
| static constexpr bool | isUndirected () |
| Checks if the graph is undirected. | |
Protected Types | |
| using | Base = GraphBase< Value, Device, Index, Orientation, typename AdjacencyMatrix::ViewType > |
Protected Attributes | |
| AdjacencyMatrixType | adjacencyMatrix |
| Protected Attributes inherited from TNL::Graphs::GraphBase< Value, Device, Index, DirectedGraph, TNL::Matrices::SparseMatrix< Value, Device, Index, TNL::Matrices::GeneralMatrix, TNL::Algorithms::Segments::CSR >::ViewType > | |
| AdjacencyMatrixView | adjacencyMatrixView |
Graph class represents a mathematical graph using an adjacency matrix.
By default, the adjacency matrix is stored using a sparse matrix representation. This behavior can be changed by specifying adifferent matrix type as the last template parameter.
When a sparse matrix is used, all matrix elements that are not explicitly stored are interpreted as zero. In the context of graphs, however, it is important to distinguish between the absence of an edge and the presence of an edge with zero weight. To preserve this distinction, edges with zero weight are represented by explicitly stored matrix entries with zero value.
For unweighted graphs, use bool as the Value type. In this case, the sparse adjacency matrix is binary, which only stores the positions of the edges without any associated weights.
If a dense matrix is used as the adjacency matrix, it represents a complete graph, meaning that all possible edges between nodes are present.
| Value | is type for weights of the graph edges. |
| Device | is type of device where the graph will be operating. |
| Index | is type for indexing of the graph nodes. |
| Orientation | is type of the graph - directed or undirected. |
| Segments | is template for segments type used to store adjacency matrix. |
| AdjacencyMatrix | is type of matrix used to store the adjacency matrix of the graph. |
| TNL::Graphs::Graph< Value, Device, Index, Orientation, Segments, AdjacencyMatrix >::Graph | ( | IndexType | vertexCount, |
| const std::initializer_list< std::tuple< IndexType, IndexType, ValueType > > & | data, | ||
| Matrices::MatrixElementsEncoding | encoding = isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricMixed ) |
Constructor with number of nodes and edges given as initializer list.
| vertexCount | is the number of nodes in the graph. |
| data | is the initializer list of tuples (source node, target node, edge weight). |
| encoding | is the encoding for symmetric matrices (used only for undirected graphs). |
If the graph is undirected, the adjacency matrix can be symmetric. In this case, the parameter encoding specifies whether the lower or upper part of the matrix is provided. If the graph is undirected and the adjacency matrix type is not symmetric, the constructor will create a symmetric adjacency matrix by adding both (source, target) and (target, source) entries for each edge.
| TNL::Graphs::Graph< Value, Device, Index, Orientation, Segments, AdjacencyMatrix >::Graph | ( | const std::initializer_list< std::initializer_list< ValueType > > & | data, |
| Matrices::MatrixElementsEncoding | encoding = isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricMixed ) |
Constructor with number of nodes and edges given as nested initializer list (for dense adjacency matrix).
This constructor is only available when the adjacency matrix is a dense matrix type. The edges are specified as a nested initializer list where each inner list represents all edge weights from a single vertex. The number of vertices is inferred from the size of the outer list.
| data | is the nested initializer list where data[i] contains all edge weights from vertex i. Use 0.0 for non-existent edges. |
| encoding | is the encoding for symmetric matrices (used only for undirected graphs). |
For dense matrices, the graph represents a complete graph where all possible edges are present. Zero-weighted edges does not indicate the absence of an edge in the logical graph structure.
| TNL::Graphs::Graph< Value, Device, Index, Orientation, Segments, AdjacencyMatrix >::Graph | ( | IndexType | vertexCount, |
| const std::map< std::pair< MapIndex, MapIndex >, MapValue > & | map, | ||
| Matrices::MatrixElementsEncoding | encoding = isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricMixed ) |
Constructor with number of nodes and edges given as a map.
| vertexCount | is the number of nodes in the graph. |
| map | is the map with keys as (source node, target node) pairs and values as edge weights. |
| encoding | is the encoding for symmetric matrices (used only for undirected graphs). |
If the graph is undirected, the adjacency matrix can be symmetric. In this case, the parameter encoding specifies whether the lower or upper part of the matrix is provided. If the graph is undirected and the adjacency matrix type is not symmetric, the constructor will create a symmetric adjacency matrix by adding both (source, target) and (target, source) entries for each edge.
| void TNL::Graphs::Graph< Value, Device, Index, Orientation, Segments, AdjacencyMatrix >::reset | ( | ) |
Resets the graph to zero vertices and edges.
This method resets the adjacency matrix to zero dimensions, effectively removing all vertices and edges from the graph.
| void TNL::Graphs::Graph< Value, Device, Index, Orientation, Segments, AdjacencyMatrix >::setAdjacencyMatrix | ( | const Matrix_ & | matrix | ) |
Sets the adjacency matrix of the graph.
| Matrix_ | is type of the input adjacency matrix. |
| matrix | is the input adjacency matrix. |
The method makes a copy of the provided adjacency matrix and so the type if the input matrix can be different from the type of the adjacency matrix of the graph.
| void TNL::Graphs::Graph< Value, Device, Index, Orientation, Segments, AdjacencyMatrix >::setDenseEdges | ( | const std::initializer_list< std::initializer_list< ValueType > > & | data, |
| Matrices::MatrixElementsEncoding | encoding = isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricLower ) |
Sets the edges of the graph from a nested initializer list (for dense adjacency matrix).
This method is only available when the adjacency matrix is a dense matrix type. The edges are specified as a nested initializer list where each inner list represents all edge weights from a single vertex.
The edge values are given as nested lists data: { { weight_0_0, weight_0_1, weight_0_2, ... }, // edges from vertex 0 { weight_1_0, weight_1_1, weight_1_2, ... }, // edges from vertex 1 ... }.
| data | is a nested initializer list where data[i][j] is the weight of edge from vertex i to vertex j. Use 0.0 for non-existent edges. |
| encoding | defines encoding for symmetric matrices (used only for undirected graphs). |
See TNL::Matrices::DenseMatrix::setElements for details on how the encoding parameter works.
| void TNL::Graphs::Graph< Value, Device, Index, Orientation, Segments, AdjacencyMatrix >::setEdgeCounts | ( | const Vector & | edgeCounts | ) |
Sets the edge counts (capacities) for all vertices in the graph.
| Vector | is type of the vector holding the edge counts. |
| edgeCounts | is the vector holding the number of edges for each vertex. |
This method sets the row capacities of the adjacency matrix to the provided edge counts. For undirected graphs with symmetric adjacency matrix, only capacities for edges in one direction should be provided.
| void TNL::Graphs::Graph< Value, Device, Index, Orientation, Segments, AdjacencyMatrix >::setEdges | ( | const std::initializer_list< std::tuple< IndexType, IndexType, ValueType > > & | data, |
| Matrices::MatrixElementsEncoding | encoding = isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricMixed ) |
Sets the edges of the graph from an initializer list.
The edge values are given as a list data of triples: { { source1, target1, weight1 }, { source2, target2, weight2 }, ... }.
| data | is an initializer list of tuples representing edges (source, target, weight). |
| encoding | defines encoding for symmetric matrices (used only for undirected graphs). |
See TNL::Matrices::SparseMatrix::setElements for details on how the encoding parameter works.
| void TNL::Graphs::Graph< Value, Device, Index, Orientation, Segments, AdjacencyMatrix >::setEdges | ( | const std::map< std::pair< MapIndex, MapIndex >, MapValue > & | map, |
| Matrices::MatrixElementsEncoding | encoding = isDirected() ? Matrices::MatrixElementsEncoding::Complete :Matrices::MatrixElementsEncoding::SymmetricMixed ) |
Sets the edges of the graph from a map.
| MapIndex | is type for indexing of the nodes in the map. |
| MapValue | is type for weights of the edges in the map. |
| map | is the map with keys as (source node, target node) pairs and values as edge weights. |
| encoding | defines encoding for symmetric matrices (used only for undirected graphs). |
See TNL::Matrices::SparseMatrix::setElements for details on how the encoding parameter works.
| void TNL::Graphs::Graph< Value, Device, Index, Orientation, Segments, AdjacencyMatrix >::setVertexCapacities | ( | const Vector & | nodeCapacities | ) |
Sets the capacities of the graph nodes.
| nodeCapacities | is the vector holding the node capacities. |
The method sets the row capacities of the adjacency matrix to the provided node capacities. It is not necessary to call this method if the adjacency matrix is dense. If the adjacency matrix is sparse and symmetric, capacity only for edges in one direction (e.g., lower part) should be provided.