10#ifndef PBAT_GRAPH_ADJACENCY_H
11#define PBAT_GRAPH_ADJACENCY_H
32template <
class TWeight = Scalar,
class TIndex = Index>
40template <
class TWeightedEdge>
44 decltype(std::declval<TWeightedEdge>().value())>;
46 std::remove_cvref_t<decltype(std::declval<TWeightedEdge>().row())>;
66 class TWeightedEdgeIterator,
67 class TWeightedEdge =
typename std::iterator_traits<TWeightedEdgeIterator>::value_type,
71 TWeightedEdgeIterator begin,
72 TWeightedEdgeIterator end,
73 TIndex m = TIndex(-1),
74 TIndex n = TIndex(-1)) -> Eigen::SparseMatrix<TScalar, Eigen::RowMajor, TIndex>
81 [](TWeightedEdge
const& lhs, TWeightedEdge
const& rhs) {
82 return lhs.row() < rhs.row();
92 [](TWeightedEdge
const& lhs, TWeightedEdge
const& rhs) {
93 return lhs.col() < rhs.col();
98 Eigen::SparseMatrix<TScalar, Eigen::RowMajor, TIndex> G(m, n);
99 G.setFromTriplets(begin, end);
114 class TScalar =
typename TDerivedA::Scalar,
115 std::integral TIndex =
typename TDerivedA::StorageIndex>
118 using IndexVectorType = Eigen::Map<Eigen::Vector<TIndex, Eigen::Dynamic>>;
119 return Eigen::Map<IndexVectorType const>(A.outerIndexPtr(), A.outerSize() + 1);
133 class TScalar =
typename TDerivedA::Scalar,
134 std::integral TIndex =
typename TDerivedA::StorageIndex>
137 using IndexVectorType = Eigen::Map<Eigen::Vector<TIndex, Eigen::Dynamic>>;
138 return Eigen::Map<IndexVectorType const>(A.innerIndexPtr(), A.nonZeros());
152 class TScalar =
typename TDerivedA::Scalar,
153 std::integral TIndex =
typename TDerivedA::StorageIndex>
156 using WeightVectorType = Eigen::Map<Eigen::Vector<TScalar, Eigen::Dynamic>>;
157 return Eigen::Map<WeightVectorType const>(A.valuePtr(), A.nonZeros());
171template <
class TDerivedP, std::
integral TIndex =
typename TDerivedP::Scalar>
173 -> std::tuple<Eigen::Vector<TIndex, Eigen::Dynamic>, Eigen::Vector<TIndex, Eigen::Dynamic>>
176 n = p.maxCoeff() + TIndex(1);
179 auto adj =
common::ArgSort(p.size(), [&](
auto i,
auto j) { return p(i) < p(j); });
180 return std::make_tuple(ptr, adj);
194 class TScalar =
typename TDerivedA::Scalar,
195 std::integral TIndex =
typename TDerivedA::StorageIndex>
198 return std::make_tuple(
214 class TScalar =
typename TDerivedA::Scalar,
215 std::integral TIndex =
typename TDerivedA::StorageIndex>
218 return std::make_tuple(
232template <
class TIndex = Index>
235 auto n =
static_cast<TIndex
>(lil.size());
237 using IndexVectorType = Eigen::Vector<TIndex, Eigen::Dynamic>;
238 IndexVectorType ptr(n + TIndex(1));
240 for (
auto l = 0; l < n; ++l)
242 auto lStl =
static_cast<std::size_t
>(l);
243 auto nVerticesInPartition =
static_cast<TIndex
>(lil[lStl].size());
244 ptr(l + 1) = ptr(l) + nVerticesInPartition;
245 nEdges += nVerticesInPartition;
247 IndexVectorType adj(nEdges);
248 for (
auto l = 0; l < n; ++l)
251 auto end = ptr(l + 1);
252 auto lStl =
static_cast<std::size_t
>(l);
255 return std::make_tuple(ptr, adj);
273 class TIndex =
typename TDerivedPtr::Scalar,
275void ForEachEdge(Eigen::DenseBase<TDerivedPtr>& ptr, Eigen::DenseBase<TDerivedAdj>& adj, Func&& f)
277 auto nVertices = ptr.size() - 1;
278 for (TIndex u = 0; u < nVertices; ++u)
280 auto uBegin = ptr(u);
281 auto uEnd = ptr(u + 1);
282 for (
auto k = uBegin; k < uEnd; ++k)
305 class TIndex =
typename TDerivedPtr::Scalar,
308 Eigen::DenseBase<TDerivedPtr>& ptr,
309 Eigen::DenseBase<TDerivedAdj>& adj,
310 Func fShouldDeleteEdge)
312 auto nPartitions = ptr.size() - 1;
313 using IndexVectorType = Eigen::Vector<TIndex, Eigen::Dynamic>;
314 IndexVectorType sizes(nPartitions);
318 IndexVectorType adjTmp(adj.size());
319 IndexVectorType ptrTmp = ptr;
320 for (
auto p = 0, kk = 0; p < nPartitions; ++p)
322 auto kBegin = ptr(p);
323 auto kEnd = ptr(p + TIndex(1));
324 sizes(p) = kEnd - kBegin;
325 for (
auto k = kBegin; k < kEnd; ++k)
327 if (fShouldDeleteEdge(p, adj(k)))
338 ptrTmp(p + TIndex(1)) = ptrTmp(p) + sizes(p);
341 adj = adjTmp.head(nEdges);
Eigen adaptors for ranges.
auto Counts(auto begin, auto end, TIndex ncounts) -> Eigen::Vector< TIndex, Eigen::Dynamic >
Counts the number of occurrences of each integer in a contiguous range.
Definition Indexing.h:57
auto CumSum(R &&sizes) -> Eigen::Vector< TIndex, Eigen::Dynamic >
Cumulative sum of a range of integers.
Definition Indexing.h:34
auto ArgSort(TIndex n, FLess less) -> Eigen::Vector< TIndex, Eigen::Dynamic >
Computes the indices that would sort an array.
Definition ArgSort.h:32
auto ToEigen(R &&r) -> Eigen::Map< Eigen::Vector< std::ranges::range_value_t< R >, Eigen::Dynamic > const >
Map a range of scalars to an eigen vector of such scalars.
Definition Eigen.h:28
Definition Adjacency.h:24
Eigen::Triplet< TWeight, TIndex > WeightedEdge
Weighted edge (wrapper around Eigen triplet type)
Definition Adjacency.h:33
void ForEachEdge(Eigen::DenseBase< TDerivedPtr > &ptr, Eigen::DenseBase< TDerivedAdj > &adj, Func &&f)
Edge iteration over the adjacency list in compressed sparse format.
Definition Adjacency.h:275
auto AdjacencyMatrixFromEdges(TWeightedEdgeIterator begin, TWeightedEdgeIterator end, TIndex m=TIndex(-1), TIndex n=TIndex(-1)) -> Eigen::SparseMatrix< TScalar, Eigen::RowMajor, TIndex >
Construct adjacency matrix from edge/triplet list.
Definition Adjacency.h:70
auto ListOfListsToAdjacency(std::vector< std::vector< TIndex > > const &lil)
Construct adjacency list in compressed sparse format from an input adjacency list in list of lists fo...
Definition Adjacency.h:233
auto AdjacencyMatrixIndices(Eigen::SparseCompressedBase< TDerivedA > const &A)
Non-owning wrapper around the indices of a compressed sparse matrix.
Definition Adjacency.h:135
auto MatrixToWeightedAdjacency(Eigen::SparseCompressedBase< TDerivedA > const &A)
Construct adjacency list in compressed sparse format from an input adjacency matrix.
Definition Adjacency.h:216
auto MapToAdjacency(Eigen::DenseBase< TDerivedP > const &p, TIndex n=TIndex(-1)) -> std::tuple< Eigen::Vector< TIndex, Eigen::Dynamic >, Eigen::Vector< TIndex, Eigen::Dynamic > >
Construct adjacency list in compressed sparse format from a map p s.t. p(i) is the index of the verte...
Definition Adjacency.h:172
void RemoveEdges(Eigen::DenseBase< TDerivedPtr > &ptr, Eigen::DenseBase< TDerivedAdj > &adj, Func fShouldDeleteEdge)
In-place removal of edges from the adjacency list in compressed sparse format.
Definition Adjacency.h:307
auto AdjacencyMatrixWeights(Eigen::SparseCompressedBase< TDerivedA > const &A)
Non-owning wrapper around the weights of a compressed sparse matrix.
Definition Adjacency.h:154
auto MatrixToAdjacency(Eigen::SparseCompressedBase< TDerivedA > const &A)
Obtain the offset and indices arrays of an input adjacency matrix.
Definition Adjacency.h:196
auto AdjacencyMatrixPrefix(Eigen::SparseCompressedBase< TDerivedA > const &A)
Non-owning wrapper around the offset pointers of a compressed sparse matrix.
Definition Adjacency.h:116
The main namespace of the library.
Definition Aliases.h:15
Traits for WeightedEdge.
Definition Adjacency.h:42
std::remove_cvref_t< decltype(std::declval< TWeightedEdge >().value())> ScalarType
Scalar type of the graph edge weights.
Definition Adjacency.h:43
std::remove_cvref_t< decltype(std::declval< TWeightedEdge >().row())> IndexType
Definition Adjacency.h:45