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

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. More...

#include "ADS/Contract.h"
#include "ADS/external/ExternalSorting.h"
#include "ADS/external/TwoLevelMemory.h"
#include "ADS/search/BinarySearch.h"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <utility>
#include <vector>
#include "ADS/external/impl/ListRanking.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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 [see TwoLevelMemory.h], following chapter 4, List Ranking, of Ferragina's book.

A list of \( n \) items is the vector \( \mathit{succ} \) of their successors, the items being \( 0 , \ldots , n - 1 \) and the tail \( t \) the only one with \( \mathit{succ}[ t ] = t \).

  • listRank() is the sequential algorithm: the predecessors, then a walk back from the tail, \( O( n ) \) time, but up to one I/O per item on the disk, since the links jump anywhere.
  • listRankPointerJumping() is the parallel algorithm of Wyllie: every item at once adds the rank of its successor to its own and jumps to the successor of its successor, so that the rank of \( i \) is the distance between \( i \) and its current successor; \( \lceil \log_2 n \rceil \) rounds, \( O( n \log n ) \) operations.
  • parallelStep() simulates one parallel step \( A[ a_i ] \gets \mathrm{op}( A[ a_i ] , A[ b_i ] ) \) on the disk by two sorts and three scans: the triples \( \langle a_i , b_i , \cdot \rangle \) sorted by \( b_i \), filled in by a scan of \( A \), sorted back by \( a_i \) and applied by a second scan of \( A \); any parallel algorithm of \( T \) steps on \( n \) processors becomes \( \tilde O( ( n / B ) T ) \) I/Os (Chiang et al.). externalPointerJumping() is the algorithm of Wyllie so simulated, the rank and the successor carried in one record, \( \tilde O( ( n / B ) \log n ) \) I/Os.
  • The divide and conquer of the same chapter removes an independent set \( I \) (no item of \( I \) is the successor of another one of \( I \)), letting the predecessors of its items jump over them, ranks the shorter list and puts the items of \( I \) back, each one adding the rank of its successor. randomIndependentSet() tosses a coin per item and takes the heads whose successor shows tails, \( n / 4 \) items on average; deterministicCoinTossing() is the technique of Cole and Vishkin: the colors start as the distinct indices and each round replaces the color of \( i \) by \( 2 \pi + z \), \( \pi \) the lowest bit where it differs from the color of the successor and \( z \) that bit of \( i \) (the tail compares with its bit 0), so that adjacent colors stay different while \( b \) bits become \( \lceil \log_2 b \rceil + 1 \), down to six colors in \( \log^* n \) rounds, and three by recoloring 5, 4 and 3; independentSetOfColors() takes the local minima, at least about \( n / 4 \) items. listRankIndependentSet() is the recursion in the memory with either set, and externalListRank() is the same recursion on the disk with the random coins, each step a join of records by sorts and scans, \( T( n ) = \tilde O( n / B ) + T( 7 n / 8 ) = \tilde O( n / B ) \) I/Os (the coins are tossed again until \( | I | \ge n / 8 \), which happens after \( O( 1 ) \) attempts on average), the list in memory once it fits.

Compared with the literature: the optimal parallel rankings of Cole and Vishkin and of Anderson and Miller reach \( O( n ) \) operations in \( O( \log n ) \) time by the same independent sets; on the disk the bound \( \Theta( \mathrm{sort}( n ) ) \) of Chiang et al. is optimal, and is what externalListRank() attains, while the deterministic recursion on the disk would repeat the joins of the coins \( \log^* n \) times per level, and is here only in the memory. The records of the disk are atomic items of the model.

The contracts [see Contract.h]: \( \mathit{succ} \) is a list, one tail and every item reached from the only head, an audit; the operations of a parallel step have distinct destinations and the indices are in \( A \), an audit; the colors of adjacent items differ and the independent sets are independent, audits; the files share the memory, which is at least \( 3 B \), and the outputs are empty. The functions are defined in ListRanking.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 4, List Ranking, doi:10.1017/9781009128933
J. C. Wyllie, The complexity of parallel computations, PhD thesis, Cornell University, 1979
R. Cole, U. Vishkin, Deterministic coin tossing with applications to optimal parallel list ranking, Information and Control 70(1), 32-53, 1986, doi:10.1016/S0019-9958(86)80023-7
R. J. Anderson, G. L. Miller, Deterministic parallel list ranking, Algorithmica 6, 859-868, 1991, doi:10.1007/BF01759076
Y.-J. Chiang, M. T. Goodrich, E. F. Grove, R. Tamassia, D. E. Vengroff, J. S. Vitter, External-memory graph algorithms, Proceedings of the 6th ACM-SIAM Symposium on Discrete Algorithms (SODA), 139-149, 1995
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa