ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::K2Tree Class Reference

the adjacency matrix of a static directed graph as a \( k^2 \)-tree More...

#include <ADS/succinct/K2Tree.h>

Public types

using Edge = std::pair< std::size_t , std::size_t >
 an edge \( ( u , v ) \), from \( u \) to \( v \)

Public member functions

 K2Tree (void)=default
 the graph with no node
 K2Tree (std::size_t n, const std::vector< Edge > &e, std::size_t k=2)
 the graph of \( n \) nodes and the edges \( e \) (repeated ones counted once)
std::size_t order (void) const
 the number of nodes, \( n \)
std::size_t edges (void) const
 the number of distinct edges
std::vector< std::size_t > neighbors (std::size_t u) const
 the successors of \( u \), in increasing order
std::vector< std::size_t > reverseNeighbors (std::size_t v) const
 the predecessors of \( v \), in increasing order: the walk of neighbors() on the children that cross the column \( v \), the place \( \mathit{base} + j k + \lfloor r / c \rfloor \) in its line 6
bool hasEdge (std::size_t u, std::size_t v) const
 whether \( ( u , v ) \) is an edge
const BitVector & levels (void) const
 \( T \), the marks of the levels but the last
const BitVector & leaves (void) const
 \( L \), the marks of the cells
std::size_t sizeInBits (void) const
 the bits of \( T \), \( L \) and the rank directory of \( T \)
template<class A>
void persist (A &a)
 the members written to, or read from, the archive \( a \) [see Persistence.h]

Static public attributes

static constexpr std::string_view persistName = "K2Tree"
 the name of the structure in a file [see Persistence.h]

Detailed description

the adjacency matrix of a static directed graph as a \( k^2 \)-tree

Member typedef documentation

◆ Edge

using ADS::K2Tree::Edge = std::pair< std::size_t , std::size_t >

an edge \( ( u , v ) \), from \( u \) to \( v \)

Constructor and destructor documentation

◆ K2Tree() [1/2]

ADS::K2Tree::K2Tree ( void )
default

the graph with no node

◆ K2Tree() [2/2]

ADS::K2Tree::K2Tree ( std::size_t n,
const std::vector< Edge > & e,
std::size_t k = 2 )

the graph of \( n \) nodes and the edges \( e \) (repeated ones counted once)

In pseudocode (Brisaboa, Ladra and Navarro, beyond the book), by levels from the edges, each submatrix holding the run of its edges, which a counting sort by child splits among its \( k^2 \) children:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{build}( n , E , k ) \cr 2 & \quad E \gets \text{the distinct edges, sorted};\ N \gets \text{the } \text{least power of } k \text{ not below } \max\{ n , k \} \cr 3 & \quad \mathit{level} \gets ( \text{the whole matrix with } E );\ s \gets N / k;\ T \gets ();\ L \gets () \cr 4 & \quad \textbf{while}\ \mathit{level} \ne ()\ \textbf{do} \cr 5 & \qquad \mathit{next} \gets () \cr 6 & \qquad \textbf{foreach}\ \text{submatrix } x \text{ of } \mathit{level} \text{, in order}\ \textbf{do} \cr 7 & \qquad\quad \text{split the edges of } x \text{ among its } k^2 \text{ children of side } s \text{, by counting} \cr 8 & \qquad\quad \textbf{for}\ j \gets 0\ \textbf{to}\ k^2 - 1 \ \textbf{do} \cr 9 & \qquad\qquad \beta \gets 1 \text{ if the child } j \text{ has an } \text{edge, else } 0 \cr 10 & \qquad\qquad \textbf{if}\ s = 1\ \textbf{then}\ L \gets L \cdot \beta\ \textbf{else}\ T \gets T \cdot \beta \cr 11 & \qquad\qquad \textbf{if}\ s > 1\ \textbf{and}\ \beta = 1 \ \textbf{then}\ \text{append the child } j \text{ to } \mathit{next} \cr 12 & \qquad \mathit{level} \gets \mathit{next};\ s \gets s / k \cr 13 & \quad \textbf{return}\ ( T , L ) \end{array}} \]

Precondition
\( k \ge 2 \) and the nodes of the edges in \( [ 0 , n ) \)
Postcondition
the cells marked are the distinct edges

Member function documentation

◆ edges()

std::size_t ADS::K2Tree::edges ( void ) const
nodiscard

the number of distinct edges

◆ hasEdge()

bool ADS::K2Tree::hasEdge ( std::size_t u,
std::size_t v ) const
nodiscard

whether \( ( u , v ) \) is an edge

In pseudocode (Brisaboa, Ladra and Navarro, beyond the book), one path down, in \( O( h ) \) time:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{hasEdge}( u , v ) \cr 2 & \quad s \gets N;\ \mathit{base} \gets 0 \cr 3 & \quad \textbf{loop} \cr 4 & \qquad c \gets s / k;\ p \gets \mathit{base} + \lfloor u / c \rfloor k + \lfloor v / c \rfloor \cr 5 & \qquad \textbf{if}\ \mathit{mark}( p ) = 0 \ \textbf{then return false} \cr 6 & \qquad \textbf{if}\ c = 1\ \textbf{then return true} \cr 7 & \qquad \mathit{base} \gets \mathrm{rank}_1( p + 1 ) \, k^2;\ u \gets u \bmod c;\ v \gets v \bmod c;\ s \gets c \end{array}} \]

Precondition
\( u , v < \mathrm{order}() \)

◆ leaves()

const BitVector & ADS::K2Tree::leaves ( void ) const
nodiscard

\( L \), the marks of the cells

◆ levels()

const BitVector & ADS::K2Tree::levels ( void ) const
nodiscard

\( T \), the marks of the levels but the last

◆ neighbors()

std::vector< std::size_t > ADS::K2Tree::neighbors ( std::size_t u) const
nodiscard

the successors of \( u \), in increasing order

In pseudocode (Brisaboa, Ladra and Navarro, beyond the book), with \( \mathit{mark}( p ) \) the bit \( p \) of \( T \) followed by \( L \), and the children of the node marked at \( p \) from the place \( \mathrm{rank}_1( p + 1 ) \, k^2 \), the root's from 0:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{neighbors}( u ) \cr 2 & \quad \mathit{out} \gets ();\ \mathrm{walk}( N , u , 0 , 0 ); \ \textbf{return}\ \mathit{out} \cr 3 & \textbf{procedure}\ \mathrm{walk}( s , r , o , \mathit{base} ) \cr 4 & \quad c \gets s / k\quad \triangleright\ \text{the side of a child} \cr 5 & \quad \textbf{for}\ j \gets 0\ \textbf{to}\ k - 1\ \textbf{do}\quad \triangleright\ \text{the children that cross the row } r \cr 6 & \qquad p \gets \mathit{base} + \lfloor r / c \rfloor k + j \cr 7 & \qquad \textbf{if}\ \mathit{mark}( p ) = 1\ \textbf{then} \cr 8 & \qquad\quad \textbf{if}\ c = 1\ \textbf{then}\ \text{append } o + j \text{ to } \mathit{out} \cr 9 & \qquad\quad \textbf{else}\ \mathrm{walk}( c , r \bmod c , o + j c , \mathrm{rank}_1( p + 1 ) \, k^2 ) \end{array}} \]

Precondition
\( u < \mathrm{order}() \)

◆ order()

std::size_t ADS::K2Tree::order ( void ) const
nodiscard

the number of nodes, \( n \)

◆ persist()

template<class A>
void ADS::K2Tree::persist ( A & a)

the members written to, or read from, the archive \( a \) [see Persistence.h]

◆ reverseNeighbors()

std::vector< std::size_t > ADS::K2Tree::reverseNeighbors ( std::size_t v) const
nodiscard

the predecessors of \( v \), in increasing order: the walk of neighbors() on the children that cross the column \( v \), the place \( \mathit{base} + j k + \lfloor r / c \rfloor \) in its line 6

Precondition
\( v < \mathrm{order}() \)

◆ sizeInBits()

std::size_t ADS::K2Tree::sizeInBits ( void ) const
nodiscard

the bits of \( T \), \( L \) and the rank directory of \( T \)

Member data documentation

◆ persistName

std::string_view ADS::K2Tree::persistName = "K2Tree"
staticconstexpr

the name of the structure in a file [see Persistence.h]


The documentation for this class was generated from the following file: