ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
StaticGraph.h File Reference

A static GRAPH for the engineered algorithms, beyond the book [see Graph.h]: the compressed sparse rows of ArrayGraph [see ArrayGraph.h] (the format CSR of the sparse matrices), with the arcs of each node sorted by head, which makes the reads of the visits that follow them go forward in memory. More...

#include "ADS/Contract.h"
#include "ADS/Portable.h"
#include "ADS/external/MappedFile.h"
#include "ADS/graph/Graph.h"
#include <algorithm>
#include <compare>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <optional>
#include <ranges>
#include <span>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
#include "ADS/graph/impl/StaticGraph.tpp"

Go to the source code of this file.

Classes

struct  ADS::WeightedArc< W >
 an arc of a weighted StaticGraph, its head with its weight More...
class  ADS::StaticGraph< W, L >
 a static graph in compressed sparse rows, the arcs of each node sorted by head, their heads and their weights in two arrays or, with ArcLayout::Interleaved, in one array of pairs More...

Namespaces

namespace  ADS

Concepts

concept  ADS::CsrArrays
 a graph whose compressed rows are plain arrays, offsets(), heads() and, if weighted, weightData(), which a library that takes the compressed rows (METIS, KaHIP, GraphBLAS) reads without a copy: a StaticGraph of the separate layout, or an ArrayGraph

Enumerations

enum class  ADS::ArcLayout { ADS::ArcLayout::Separate , ADS::ArcLayout::Interleaved }
 the layout of the arcs of a weighted StaticGraph More...

Functions

template<class W, ArcLayout L>
std::vector< int > ADS::bfsOrder (const StaticGraph< W, L > &g, int r=0)
 the order of a visit in breadth from \( r \), then from the least node not reached, and so on: the name p[ u ] of each node u in the relabelled graph, a permutation of the nodes

Detailed description

A static GRAPH for the engineered algorithms, beyond the book [see Graph.h]: the compressed sparse rows of ArrayGraph [see ArrayGraph.h] (the format CSR of the sparse matrices), with the arcs of each node sorted by head, which makes the reads of the visits that follow them go forward in memory.

The graph is three plain arrays, as a structure of arrays: the offsets of the rows, the heads of the arcs and, if the graph is weighted, their weights in the same places, so that a visit that needs only the heads reads none of the weights, and the offsets and the heads are the arrays xadj and adjncy of METIS and the arrays Ap and Aj of the compressed rows of GraphBLAS, with the weights as adjwgt and Ax, which those libraries take without a copy. With \( a = m \) arcs if oriented and \( a = 2 m \) if not, the arcs of u are the places

\[ \mathit{off}( u ) \leq k < \mathit{off}( u + 1 ) , \qquad \mathit{off}( 0 ) = 0 , \quad \mathit{off}( n ) = a , \]

so that the graph takes \( 4 ( n + 1 ) + ( 4 + s ) a \) bytes, s the size of a weight (0 if unweighted), with no padding between a head and a weight wider than it. The graph is built from a list of edges in \( \Theta( n + a ) \) time by two passes of counting sort, by head and then, stably, by tail, and an unoriented edge [ u , v ] gives the two arcs ( u , v ) and ( v , u ). There may be parallel arcs.

An arc is still read as a whole, a head with its weight: out( u ) is the row of u as a range of random access of WeightedArc values, made from the two arrays as it is read, and arcData() is the cursor at the first arc, which takes the indexing and the arithmetic of a pointer; on an unweighted graph they are a std::span and a pointer of the heads.

The layout of a weighted graph is the parameter \( L \): the two arrays above (ArcLayout::Separate, the default), or one array of the pairs WeightedArc (ArcLayout::Interleaved), in which a head and its weight share a line of the cache, at the price of the padding of a weight wider than a head, so that \( 4 ( n + 1 ) + s' a \) bytes are taken, \( s' \) the size of a pair. The algorithms that read each head with its weight (Dijkstra, the stepping algorithms) miss the cache less often on the pairs when the weights are integers, those that read only the heads (the visits) on the two arrays. Both layouts give the same interface: out( u ) is a range of random access of WeightedArc, a std::span of the pairs if interleaved, arcData() its cursor, and arcHeads() and arcWeights() read the heads and the weights in place by the indexing of an array; only the separate layout has heads() and weightData(), the plain arrays that the concept CsrArrays requires.

transpose() gives the graph with the arcs reversed, which the visits from the bottom up and the strongly connected components of a reversed graph need, and relabel() gives the same graph with the nodes renamed, e.g., by bfsOrder(), the order of a visit in breadth from a node and then from each node not reached (the ordering of Cuthill and McKee without its sort by degree), which puts close together the nodes that the algorithms visit close in time; both take \( \Theta( n + a ) \). StaticGraph models the concept Graph, so that the algorithms of the book also apply to it.

The contracts [see Contract.h]: the graph has n >= 0 nodes and fewer than \( 2^{31} \) arcs, its edges join two distinct nodes; the operations on a node need a node, relabel() a permutation of the nodes; the invariant is that the offsets do not decrease and end at a, and that the heads are nodes, in increasing order for each tail. The operations are defined in StaticGraph.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 9.4, https://algoritmicamente.it/materiali/libro/capitoli/cap09.pdf
Y. Saad, Iterative Methods for Sparse Linear Systems, 2nd ed., SIAM, 2003, section 3.4, doi:10.1137/1.9780898718003
E. Cuthill, J. McKee, Reducing the bandwidth of sparse symmetric matrices, Proceedings of the 24th ACM National Conference, 157-172, 1969, doi:10.1145/800195.805928
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa