ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ListRanking.h

The ranking of a list, the distance of each item from the tail, in the memory and in the two-level memory, following chapter 4, List Ranking, of Ferragina's book. More...

Files

file  ADS/external/ListRanking.h
 The ranking of a list, the distance of each item from the tail, in the memory and in the two-level memory [see TwoLevelMemory.h], following chapter 4, List Ranking, of Ferragina's book.

Functions

bool ADS::isList (const std::vector< std::size_t > &succ)
 whether \( \mathit{succ} \) is a list: one tail \( t \) with \( \mathit{succ}[ t ] = t \), and every item reached from the only item that is no successor
std::vector< std::size_t > ADS::listRank (const std::vector< std::size_t > &succ)
 the distances from the tail, by the predecessors and a walk back
std::vector< std::size_t > ADS::listRankPointerJumping (const std::vector< std::size_t > &succ, std::size_t *rounds=nullptr)
 the distances from the tail, by pointer jumping; the rounds go to *rounds, if given
template<class R>
std::vector< bool > ADS::randomIndependentSet (const std::vector< std::size_t > &succ, R &g)
 the heads whose successor shows tails, a coin per item drawn by g
std::vector< int > ADS::deterministicCoinTossing (const std::vector< std::size_t > &succ, std::size_t *rounds=nullptr)
 three colors in \( \{ 0 , 1 , 2 \} \), by deterministic coin tossing; the rounds down to six colors go to *rounds, if given
std::vector< bool > ADS::independentSetOfColors (const std::vector< std::size_t > &succ, const std::vector< int > &color)
 the items whose color is smaller than those of their neighbors, the tail excluded and not counted as a neighbor, so that a list of two items or more gives at least one
template<class R>
std::vector< std::size_t > ADS::listRankIndependentSet (const std::vector< std::size_t > &succ, R &g, bool deterministic=false, std::size_t *levels=nullptr)
 the distances from the tail, by removing independent sets, random ones drawn by g or, if deterministic, the local minima of the three colors; the levels of the recursion go to *levels, if given
template<class T, class Op>
void ADS::parallelStep (const DiskFile< T > &A, const DiskFile< std::pair< std::size_t, std::size_t > > &ops, Op op, DiskFile< T > &out)
 \( \mathit{out}[ i ] = \mathrm{op}( A[ i ] , A[ b ] ) \) for each pair \( ( i , b ) \) of ops, \( \mathit{out}[ i ] = A[ i ] \) for the \( i \) of no pair, the values of \( A \) read before any change; two sorts and three scans
void ADS::externalPointerJumping (const DiskFile< std::size_t > &succ, DiskFile< std::size_t > &rank, std::size_t *rounds=nullptr)
 the ranks of the list succ into rank, by pointer jumping, each round a parallelStep(); the rounds go to *rounds, if given
template<class R>
void ADS::externalListRank (const DiskFile< std::size_t > &succ, DiskFile< std::size_t > &rank, R &g, std::size_t *levels=nullptr)
 the ranks of the list succ into rank, by removing random independent sets drawn by g; the levels of the recursion go to *levels, if given

Detailed description

The ranking of a list, the distance of each item from the tail, in the memory and in the two-level memory, following chapter 4, List Ranking, of Ferragina's book.

The definitions, the invariants and the costs of the area are in Theory of external memory.

Function documentation

◆ deterministicCoinTossing()

std::vector< int > ADS::deterministicCoinTossing ( const std::vector< std::size_t > & succ,
std::size_t * rounds = nullptr )
inlinenodiscard

three colors in \( \{ 0 , 1 , 2 \} \), by deterministic coin tossing; the rounds down to six colors go to *rounds, if given

In pseudocode (the deterministic coin tossing of Cole and Vishkin, beyond the book), \( \oplus \) the exclusive or of the bits:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{deterministicCoinTossing}( \mathit{succ} ) \cr 2 & \quad \textbf{foreach}\ i\ \textbf{do}\ c[ i ] \gets i \cr 3 & \quad \textbf{while}\ \max_i c[ i ] \ge 6\ \textbf{do} \cr 4 & \qquad \textbf{foreach}\ i\ \textbf{do}\quad \triangleright \ \text{in parallel} \cr 5 & \qquad\quad d \gets ( 1 \text{ if } i \text{ is the tail, else } c[ i ] \oplus c[ \mathit{succ}[ i ] ] ) \cr 6 & \qquad\quad \pi \gets \text{the lowest bit set in } d;\ c[ i ] \gets 2 \pi + \text{the bit } \pi \text{ of } c[ i ] \cr 7 & \quad \textbf{foreach}\ v \in ( 5 , 4 , 3 )\ \textbf{do} \cr 8 & \qquad \textbf{foreach}\ i\ \textbf{with}\ c[ i ] = v\ \textbf{do} \cr 9 & \qquad\quad c[ i ] \gets \text{the least color in } \{ 0 , 1 , 2 \} \text{ of neither neighbor of } i \cr 10 & \quad \textbf{return}\ c \end{array}} \]

Precondition
\( \mathit{succ} \) is a list (an audit)
Postcondition
adjacent items have different colors (an audit)

◆ externalListRank()

template<class R>
void ADS::externalListRank ( const DiskFile< std::size_t > & succ,
DiskFile< std::size_t > & rank,
R & g,
std::size_t * levels = nullptr )

the ranks of the list succ into rank, by removing random independent sets drawn by g; the levels of the recursion go to *levels, if given

In pseudocode (chapter 4, List Ranking, of Ferragina's book, beyond the book), each item a record \( \langle \mathit{id} , s , r \rangle \) and the records sorted by \( \mathit{id} \); a join pairs each record with the record of its successor, by two sorts and two scans:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{rankRecords}( L ) \cr 2 & \quad \textbf{if}\ 4 |L| \le M - B\ \textbf{then return}\ \text{the } \text{records of } L \text{ ranked in memory} \cr 3 & \quad \textbf{repeat} \cr 4 & \qquad \text{toss a coin per record};\ I \gets \text{the heads } \text{whose successor shows tails, by a join} \cr 5 & \quad \textbf{until}\ 8 |I| \ge |L| \cr 6 & \quad L' \gets \text{the records not in } I \text{, those whose } \text{successor } y \text{ is in } I \text{ with} \cr 7 & \qquad r \gets r + y.r \text{ and } s \gets y.s \text{, by a join} \cr 8 & \quad R' \gets \mathrm{rankRecords}( L' ) \cr 9 & \quad \textbf{foreach}\ x \in I\ \textbf{do}\ x.r \gets x.r + R'[ x.s ].r\quad \triangleright\ \text{by a join} \cr 10 & \quad \textbf{return}\ R' \text{ and } I \text{ merged by } \mathit{id} \end{array}} \]

Precondition
\( \mathit{succ} \) is a list (an audit), \( M \ge 3 B \), rank empty, the files on the same memory
Postcondition
the ranks of listRank() (an audit)

◆ externalPointerJumping()

void ADS::externalPointerJumping ( const DiskFile< std::size_t > & succ,
DiskFile< std::size_t > & rank,
std::size_t * rounds = nullptr )
inline

the ranks of the list succ into rank, by pointer jumping, each round a parallelStep(); the rounds go to *rounds, if given

In pseudocode (Wyllie's algorithm simulated on the disk, beyond the book), each item a record \( \langle s , r \rangle \) on the disk:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{externalPointerJumping}( \mathit{succ} , \mathit{rank} ) \cr 2 & \quad N \gets \text{the records } \langle \mathit{succ}[ i ] , ( 0 \text{ if } i \text{ is the tail, else } 1 ) \rangle \text{, by a } \text{scan} \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \mathit{ops} \gets \text{the pairs } ( i , N[ i ].s ) \text{ with } N[ i ].s \text{ not the tail, by a scan} \cr 5 & \qquad \textbf{if}\ \mathit{ops} = \emptyset\ \textbf{then} \cr 6 & \qquad\quad \mathit{rank} \gets \text{the components } r \text{ of } N \text{, by a scan};\ \textbf{return} \cr 7 & \qquad N \gets \mathrm{parallelStep}( N , \mathit{ops} , ( x , y ) \mapsto \langle y.s , x.r + y.r \rangle ) \end{array}} \]

Precondition
\( \mathit{succ} \) is a list (an audit), \( M \ge 3 B \), rank empty, the files on the same memory
Postcondition
the ranks of listRank() (an audit)

◆ independentSetOfColors()

std::vector< bool > ADS::independentSetOfColors ( const std::vector< std::size_t > & succ,
const std::vector< int > & color )
inlinenodiscard

the items whose color is smaller than those of their neighbors, the tail excluded and not counted as a neighbor, so that a list of two items or more gives at least one

In pseudocode (chapter 4, List Ranking, of Ferragina's book, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{independentSetOfColors}( \mathit{succ} , c ) \cr 2 & \quad \textbf{return}\ \{ i : i \text{ is not the tail, } c[ i ] < c[ \mathit{pred}[ i ] ] \text{ unless } i \text{ is the } \text{head,} \cr 3 & \qquad c[ i ] < c[ \mathit{succ}[ i ] ] \text{ unless } \mathit{succ}[ i ] \text{ is the tail} \} \end{array}} \]

Precondition
adjacent items have different colors
Postcondition
an independent set (an audit)

◆ isList()

bool ADS::isList ( const std::vector< std::size_t > & succ)
inlinenodiscard

whether \( \mathit{succ} \) is a list: one tail \( t \) with \( \mathit{succ}[ t ] = t \), and every item reached from the only item that is no successor

◆ listRank()

std::vector< std::size_t > ADS::listRank ( const std::vector< std::size_t > & succ)
inlinenodiscard

the distances from the tail, by the predecessors and a walk back

In pseudocode (the sequential algorithm of chapter 4, List Ranking, of Ferragina's book, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{listRank}( \mathit{succ} ) \cr 2 & \quad \textbf{foreach}\ i\ \textbf{with}\ \mathit{succ}[ i ] \ne i \ \textbf{do}\ \mathit{pred}[ \mathit{succ}[ i ] ] \gets i \cr 3 & \quad x \gets \text{the tail};\ r[ x ] \gets 0 \cr 4 & \quad \textbf{while}\ x \text{ has a predecessor}\ \textbf{do} \cr 5 & \qquad r[ \mathit{pred}[ x ] ] \gets r[ x ] + 1;\ x \gets \mathit{pred}[ x ] \cr 6 & \quad \textbf{return}\ r \end{array}} \]

Precondition
\( \mathit{succ} \) is a list (an audit)

◆ listRankIndependentSet()

template<class R>
std::vector< std::size_t > ADS::listRankIndependentSet ( const std::vector< std::size_t > & succ,
R & g,
bool deterministic = false,
std::size_t * levels = nullptr )

the distances from the tail, by removing independent sets, random ones drawn by g or, if deterministic, the local minima of the three colors; the levels of the recursion go to *levels, if given

In pseudocode (chapter 4, List Ranking, of Ferragina's book, beyond the book), the recursion unrolled into a loop over the levels:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{listRankIndependentSet}( \mathit{succ} ) \cr 2 & \quad \textbf{foreach}\ i\ \textbf{do}\ s[ i ] \gets \mathit{succ}[ i ];\ r[ i ] \gets ( 0 \text{ if } i \text{ is the tail, else } 1 ) \cr 3 & \quad L \gets \text{all the items};\ \ell \gets 0 \cr 4 & \quad \textbf{while}\ |L| > 1\ \textbf{do} \cr 5 & \qquad I_\ell \gets \text{an independent set of the list } L \text{, random with } |I_\ell| \ge |L| / 8 \text{ or of the colors} \cr 6 & \qquad \textbf{foreach}\ x \in L \setminus I_\ell\ \textbf{with} \ s[ x ] \in I_\ell\ \textbf{do} \cr 7 & \qquad\quad y \gets s[ x ];\ r[ x ] \gets r[ x ] + r[ y ];\ s[ x ] \gets s[ y ] \cr 8 & \qquad L \gets L \setminus I_\ell;\ \ell \gets \ell + 1 \cr 9 & \quad \textbf{foreach}\ j \in ( \ell - 1 , \ldots , 1 , 0 ) \ \textbf{do} \cr 10 & \qquad \textbf{foreach}\ x \in I_j\ \textbf{do}\ r[ x ] \gets r[ x ] + r[ s[ x ] ] \cr 11 & \quad \textbf{return}\ r \end{array}} \]

Precondition
\( \mathit{succ} \) is a list (an audit)
Postcondition
the ranks of listRank() (an audit)

◆ listRankPointerJumping()

std::vector< std::size_t > ADS::listRankPointerJumping ( const std::vector< std::size_t > & succ,
std::size_t * rounds = nullptr )
inlinenodiscard

the distances from the tail, by pointer jumping; the rounds go to *rounds, if given

In pseudocode (Wyllie's algorithm, beyond the book), each round synchronous, its reads before its writes:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{listRankPointerJumping}( \mathit{succ} ) \cr 2 & \quad \textbf{foreach}\ i\ \textbf{do}\ s[ i ] \gets \mathit{succ}[ i ];\ r[ i ] \gets ( 0 \text{ if } i \text{ is the tail, else } 1 ) \cr 3 & \quad \textbf{repeat} \cr 4 & \qquad \textbf{foreach}\ i\ \textbf{with}\ s[ i ] \ne s[ s[ i ] ] \ \textbf{do}\quad \triangleright\ \text{in parallel} \cr 5 & \qquad\quad r[ i ] \gets r[ i ] + r[ s[ i ] ];\ s[ i ] \gets s[ s[ i ] ] \cr 6 & \quad \textbf{until}\ \text{no item jumped} \cr 7 & \quad \textbf{return}\ r \end{array}} \]

Precondition
\( \mathit{succ} \) is a list (an audit)

◆ parallelStep()

template<class T, class Op>
void ADS::parallelStep ( const DiskFile< T > & A,
const DiskFile< std::pair< std::size_t, std::size_t > > & ops,
Op op,
DiskFile< T > & out )

\( \mathit{out}[ i ] = \mathrm{op}( A[ i ] , A[ b ] ) \) for each pair \( ( i , b ) \) of ops, \( \mathit{out}[ i ] = A[ i ] \) for the \( i \) of no pair, the values of \( A \) read before any change; two sorts and three scans

In pseudocode (chapter 4, List Ranking, of Ferragina's book, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{parallelStep}( A , \mathit{ops} , \mathrm{op} , \mathit{out} ) \cr 2 & \quad P \gets \text{the triples } \langle a , b , \cdot \rangle \text{ of the pairs } ( a , b ) \text{ of } \mathit{ops} \cr 3 & \quad \text{sort } P \text{ by } b \cr 4 & \quad \text{fill the third component of each triple with } A[ b ] \text{, by a coordinated scan of } A \cr 5 & \quad \text{sort } P \text{ by } a \cr 6 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ |A| - 1\ \textbf{do} \quad \triangleright\ \text{a coordinated scan of } A \text{ and } P \cr 7 & \qquad \textbf{if}\ P \text{ has the triple } \langle i , b , v \rangle\ \textbf{then}\ \text{append } \mathrm{op}( A[ i ] , v ) \text{ to } \mathit{out} \cr 8 & \qquad \textbf{else}\ \text{append } A[ i ] \text{ to } \mathit{out} \end{array}} \]

Precondition
the first components of ops distinct (an audit), all indices below the size of \( A \), out empty, the files on the same memory

◆ randomIndependentSet()

template<class R>
std::vector< bool > ADS::randomIndependentSet ( const std::vector< std::size_t > & succ,
R & g )

the heads whose successor shows tails, a coin per item drawn by g

In pseudocode (chapter 4, List Ranking, of Ferragina's book, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{randomIndependentSet}( \mathit{succ} ) \cr 2 & \quad \textbf{foreach}\ i\ \textbf{do}\ c[ i ] \gets \text{a random } \text{bit} \cr 3 & \quad \textbf{return}\ \{ i : \mathit{succ}[ i ] \ne i ,\ c[ i ] = 1 ,\ c[ \mathit{succ}[ i ] ] = 0 \} \end{array}} \]

Precondition
\( \mathit{succ} \) is a list (an audit)
Postcondition
an independent set, without the tail (an audit)