1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
// Copyright (C) 2019-2025 Garth N. Wells
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#include <cassert>
#include <concepts>
#include <cstdint>
#include <numeric>
#include <optional>
#include <span>
#include <sstream>
#include <utility>
#include <vector>
namespace dolfinx::graph
{
/// @brief This class provides a static adjacency list data structure.
///
/// It is commonly used to store directed graphs. For each node in the
/// contiguous list of nodes [0, 1, 2, ..., n) it stores the connected
/// nodes. The representation is strictly local, i.e. it is not parallel
/// aware.
///
/// The link (edge) type is template parameter, which allows link data
/// to be stored, e.g. a pair with the target node index and the link
/// weight.
///
/// Node data can also be stored.
///
/// @tparam LinkData_t Graph link (edge) type.
/// @tparam NodeData_t Data type for graph node data.
template <typename LinkData, typename NodeData = std::nullptr_t>
class AdjacencyList
{
public:
/// @brief Adjacency list link (edge) type
using link_type = LinkData;
/// @brief Adjacency list node data type
using node_data_type = NodeData;
/// @brief Construct trivial adjacency list where each of the n nodes
/// is connected to itself.
/// @param[in] n Number of nodes.
explicit AdjacencyList(const std::int32_t n) : _array(n), _offsets(n + 1)
{
std::iota(_array.begin(), _array.end(), 0);
std::iota(_offsets.begin(), _offsets.end(), 0);
}
/// @brief Construct adjacency list from arrays of link (edge) data
/// and offsets.
/// @param[in] data Adjacency lost data array.
/// @param[in] offsets Offsets into `data` for each node, where
/// `offsets[i]` is the first index in `data` for node `i`. The last
/// index in `offsets` is the equal to the length of `data`. array for
/// node `i`.
template <typename U, typename V>
requires std::is_convertible_v<std::remove_cvref_t<U>,
std::vector<LinkData>>
and std::is_convertible_v<std::remove_cvref_t<V>,
std::vector<std::int32_t>>
AdjacencyList(U&& data, V&& offsets)
: _array(std::forward<U>(data)), _offsets(std::forward<V>(offsets))
{
_array.reserve(_offsets.back());
assert(_offsets.back() == (std::int32_t)_array.size());
}
/// @brief Construct adjacency list from arrays of link (edge) data,
/// offsets, and node data.
/// @param[in] data Adjacency lost data array.
/// @param[in] offsets Offsets into `data` for each node, where
/// `offsets[i]` is the first index in `data` for node `i`. The last
/// index in `offsets` is the equal to the length of `data`.
/// @param[in] node_data Node data array where `node_data[i]` is the
/// data attached to node `i`.
template <typename U, typename V, typename W>
requires std::is_convertible_v<std::remove_cvref_t<U>,
std::vector<LinkData>>
and std::is_convertible_v<std::remove_cvref_t<V>,
std::vector<std::int32_t>>
and std::is_convertible_v<std::remove_cvref_t<W>,
std::vector<NodeData>>
AdjacencyList(U&& data, V&& offsets, W&& node_data)
: _array(std::forward<U>(data)), _offsets(std::forward<V>(offsets)),
_node_data(std::forward<W>(node_data))
{
assert(_node_data.has_value()
and _node_data->size() == _offsets.size() - 1);
_array.reserve(_offsets.back());
assert(_offsets.back() == (std::int32_t)_array.size());
}
/// Set all connections for all entities (T is a '2D' container, e.g.
/// a `std::vector<<std::vector<std::size_t>>`,
/// `std::vector<<std::set<std::size_t>>`, etc).
/// @param[in] data Adjacency list data, where `std::next(data, i)`
/// points to the container of links (edges) for node `i`.
template <typename X>
explicit AdjacencyList(const std::vector<X>& data)
{
// Initialize offsets and compute total size
_offsets.reserve(data.size() + 1);
_offsets.push_back(0);
for (auto& row : data)
_offsets.push_back(_offsets.back() + row.size());
_array.reserve(_offsets.back());
for (auto& e : data)
_array.insert(_array.end(), e.begin(), e.end());
}
/// Copy constructor
AdjacencyList(const AdjacencyList& list) = default;
/// Move constructor
AdjacencyList(AdjacencyList&& list) = default;
/// Destructor
~AdjacencyList() = default;
/// Assignment operator
AdjacencyList& operator=(const AdjacencyList& list) = default;
/// Move assignment operator
AdjacencyList& operator=(AdjacencyList&& list) = default;
/// Equality operator
/// @return True is the adjacency lists are equal
bool operator==(const AdjacencyList& list) const
{
return this->_array == list._array and this->_offsets == list._offsets;
}
/// @brief Get the number of nodes.
/// @return The number of nodes in the adjacency list
std::int32_t num_nodes() const { return _offsets.size() - 1; }
/// @brief Number of connections for given node.
/// @param[in] node Node index.
/// @return The number of outgoing links (edges) from the node.
int num_links(std::size_t node) const
{
assert((node + 1) < _offsets.size());
return _offsets[node + 1] - _offsets[node];
}
/// @brief Get the links (edges) for given node.
/// @param[in] node Node index.
/// @return Array of outgoing links for the node. The length will be
/// `AdjacencyList::num_links(node)`.
std::span<LinkData> links(std::size_t node)
{
return std::span<LinkData>(_array.data() + _offsets[node],
_offsets[node + 1] - _offsets[node]);
}
/// @brief Get the links (edges) for given node (const version).
/// @param[in] node Node index.
/// @return Array of outgoing links for the node. The length will be
/// `AdjacencyList:num_links(node)`.
std::span<const LinkData> links(std::size_t node) const
{
return std::span<const LinkData>(_array.data() + _offsets[node],
_offsets[node + 1] - _offsets[node]);
}
/// Return contiguous array of links for all nodes (const version).
const std::vector<LinkData>& array() const { return _array; }
/// Return contiguous array of links for all nodes.
std::vector<LinkData>& array() { return _array; }
/// Offset for each node in array() (const version).
const std::vector<std::int32_t>& offsets() const { return _offsets; }
/// Offset for each node in array().
std::vector<std::int32_t>& offsets() { return _offsets; }
/// Return node data (if present), where `node_data()[i]` is the data
/// for node `i` (const version).
const std::optional<std::vector<NodeData>>& node_data() const
{
return _node_data;
}
/// Return node data (if present), where `node_data()[i]` is the data for node
/// `i`.
std::optional<std::vector<NodeData>>& node_data() { return _node_data; }
/// @brief Informal string representation (pretty-print).
/// @return String representation of the adjacency list.
std::string str() const
{
std::stringstream s;
s << "<AdjacencyList> with " + std::to_string(this->num_nodes()) + " nodes"
<< std::endl;
for (std::size_t e = 0; e < _offsets.size() - 1; ++e)
{
s << " " << e << ": [";
for (auto link : this->links(e))
s << link << " ";
s << "]" << '\n';
}
return s.str();
}
private:
// Connections (links/edges) for all entities stored as a contiguous
// array
std::vector<LinkData> _array;
// Position of first connection for each entity (using local index)
std::vector<std::int32_t> _offsets;
// Node data, where _node_data[i] is the data associated with node `i`
std::optional<std::vector<NodeData>> _node_data = std::nullopt;
};
/// @private Deduction
template <typename T, typename U>
AdjacencyList(T, U) -> AdjacencyList<typename T::value_type, std::nullptr_t>;
/// @private Deduction
template <typename T, typename U, typename W>
AdjacencyList(T, U, W)
-> AdjacencyList<typename T::value_type, typename W::value_type>;
/// @brief Construct a constant degree (valency) adjacency list.
///
/// A constant degree graph has the same number of links (edges) for
/// every node.
///
/// @param[in] data Adjacency array.
/// @param[in] degree Number of (outgoing) links for each node.
/// @return An adjacency list.
template <typename V = std::nullptr_t, typename U>
requires requires {
typename std::decay_t<U>::value_type;
requires std::convertible_to<
U, std::vector<typename std::decay_t<U>::value_type>>;
}
AdjacencyList<typename std::decay_t<U>::value_type, V>
regular_adjacency_list(U&& data, int degree)
{
if (degree == 0 and !data.empty())
{
throw std::runtime_error("Degree is zero but data is not empty for "
"constant degree AdjacencyList");
}
if (degree > 0 and data.size() % degree != 0)
{
throw std::runtime_error(
"Incompatible data size and degree for constant degree AdjacencyList");
}
std::int32_t num_nodes = degree == 0 ? data.size() : data.size() / degree;
std::vector<std::int32_t> offsets(num_nodes + 1, 0);
for (std::size_t i = 1; i < offsets.size(); ++i)
offsets[i] = offsets[i - 1] + degree;
return AdjacencyList<typename std::decay_t<U>::value_type, V>(
std::forward<U>(data), std::move(offsets));
}
} // namespace dolfinx::graph
|