|
ADS
Algorithms and Data Structures
|
The parallel visits of a StaticGraph, beyond the book, on the threads of an executor. More...
Files | |
| file | ADS/parallel/ParallelGraph.h |
| The parallel visits of a StaticGraph [see StaticGraph.h], beyond the book, on the threads of an executor [see Executor.h]: the distances of a visit in breadth and the connected components, with the same results as the sequential visits. | |
Functions | |
| template<Executor E, class W, ArcLayout AL> | |
| std::vector< int > | ADS::bfsDistances (E &ex, const StaticGraph< W, AL > &g, int s, const StaticGraph< W, AL > *gt=nullptr) |
| the distances from \( s \) by a visit in breadth that optimizes its direction, \( -1 \) for the nodes not reached; gt the transpose of \( g \) if \( g \) is oriented (nullptr: the visit goes from the top down only) | |
| template<Executor E, class W, ArcLayout AL> | |
| std::vector< int > | ADS::connectedComponents (E &ex, const StaticGraph< W, AL > &g) |
| the least node of the component of each node, an oriented graph taken as unoriented | |
The parallel visits of a StaticGraph, beyond the book, on the threads of an executor.
The definitions, the invariants and the costs of the area are in Theory of parallelism.
|
nodiscard |
the distances from \( s \) by a visit in breadth that optimizes its direction, \( -1 \) for the nodes not reached; gt the transpose of \( g \) if \( g \) is oriented (nullptr: the visit goes from the top down only)
In pseudocode (the visit of Beamer, Asanovic and Patterson, beyond the book), \( \mathrm{in}( v ) \) the arcs into \( v \), of \( g \) if unoriented and of gt if oriented:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{bfsDistances}( \mathit{ex} , G , s ) \cr 2 & \quad \textbf{foreach}\ v \in V\ \textbf{do}\ d[ v ] \gets -1 \cr 3 & \quad d[ s ] \gets 0;\ F \gets ( s );\ \ell \gets 0;\ m_u \gets m; \ m_f \gets | \mathrm{adj}( s ) | \cr 4 & \quad \textbf{loop} \cr 5 & \qquad \textbf{if}\ \mathrm{in} \text{ is known}\ \textbf{and}\ m_f > m_u / 15\ \textbf{then}\quad \triangleright\ \text{from the bottom } \text{up} \cr 6 & \qquad\quad \textbf{repeat} \cr 7 & \qquad\qquad F' \gets \emptyset \cr 8 & \qquad\qquad \textbf{foreach}\ v\ \textbf{with}\ d[ v ] = -1 \ \textbf{do in parallel} \cr 9 & \qquad\qquad\quad \textbf{if}\ \text{some } u \in \mathrm{in}( v ) \text{ is in } F \text{ (the first found)}\ \textbf{then}\ d[ v ] \gets \ell + 1;\ F' \gets F' \cup \{ v \} \cr 10 & \qquad\qquad \ell \gets \ell + 1;\ F \gets F' \cr 11 & \qquad\quad \textbf{until}\ F = \emptyset\ \textbf{or}\ ( |F| \text{ shrank}\ \textbf{and}\ |F| \le n / 18 ) \cr 12 & \qquad\quad \textbf{if}\ F = \emptyset\ \textbf{then return}\ d \cr 13 & \qquad\quad m_f \gets 1;\ \textbf{continue} \cr 14 & \qquad m_u \gets m_u - m_f;\ F' \gets ();\ m_f \gets 0\quad \triangleright\ \text{from the top down} \cr 15 & \qquad \textbf{foreach}\ u \in F , v \in G.\mathrm{adj}( u ) \ \textbf{do in parallel} \cr 16 & \qquad\quad \textbf{if}\ \mathrm{compareAndSwap}( d[ v ] , -1 , \ell + 1 )\ \textbf{then}\ \text{append } v \text{ to } F';\ m_f \gets m_f + | \mathrm{adj}( v ) | \cr 17 & \qquad F \gets F';\ \ell \gets \ell + 1 \cr 18 & \qquad \textbf{if}\ F = \emptyset\ \textbf{then return}\ d \end{array}} \]
|
nodiscard |
the least node of the component of each node, an oriented graph taken as unoriented
In pseudocode (the sampling of Afforest of Sutton, Ben-Nun and Barak, beyond the book), on a concurrent MFSET whose roots are the least nodes of their sets:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{connectedComponents}( \mathit{ex} , G ) \cr 2 & \quad M \gets \text{an MFSET of the nodes, each alone} \cr 3 & \quad \textbf{for}\ r \gets 1\ \textbf{to}\ 2\ \textbf{do} \cr 4 & \qquad \textbf{foreach}\ v \in V\ \textbf{do in parallel} \ \textbf{if}\ v \text{ has an } r \text{-th arc } ( v , u ) \ \textbf{then}\ \mathrm{merge}( M , v , u ) \cr 5 & \qquad \textbf{foreach}\ v \in V\ \textbf{do in parallel}\ \text{make } \text{the root of } v \text{ its parent} \cr 6 & \quad c \gets \textbf{nil} \cr 7 & \quad \textbf{if}\ G \text{ is unoriented}\ \textbf{then}\ c \gets \text{the root most frequent among 1024 random nodes} \cr 8 & \quad \textbf{foreach}\ v \in V\ \textbf{with}\ \text{parent} \text{ of } v \ne c\ \textbf{do in parallel} \cr 9 & \qquad \textbf{foreach}\ \text{arc } ( v , u ) \text{ after the } \text{first two}\ \textbf{do}\ \mathrm{merge}( M , v , u ) \cr 10 & \quad \textbf{return}\ \text{the root } \mathrm{find}( M , v ) \text{ of each node } v \end{array}} \]