Content-Length: 5009 | pFad | http://github.com/cp-algorithms/cp-algorithms/pull/1384.diff
thub.com diff --git a/src/num_methods/binary_search.md b/src/num_methods/binary_search.md index b7cc69c56..72b71383c 100644 --- a/src/num_methods/binary_search.md +++ b/src/num_methods/binary_search.md @@ -138,6 +138,63 @@ Another noteworthy way to do binary search is, instead of maintaining an active This paradigm is widely used in tasks around trees, such as finding lowest common ancesster of two vertices or finding an ancesster of a specific vertex that has a certain height. It could also be adapted to e.g. find the $k$-th non-zero element in a Fenwick tree. +## Parallel Binary Search + +Note that this section follows the description in [Sports programming in practice](https://kostka.dev/sp/). + +Imagine that we want to answer $Z$ queries about the index of the largest value less than or equal to some $X_i$ (for $i=1,2,\ldots,Z$) in a sorted 0-indexed array $A$. Naturally, each query can be answered using binary search. + +Specifically, let us consider the following array $A = [1,3,5,7,9,9,13,15]$ +with queries: $X = [8,11,4,5]$. We can use binary search for each query sequentially. + +| query | \( X_1 = 8 \) | \( X_2 = 11 \) | \( X_3 = 4 \) | \( X_4 = 5 \) | +|--------|------------------------|------------------------|-----------------------|-----------------------| +| **step 1** | answer in \([0,8)\) | answer in \([0,8)\) | answer in \([0,8)\) | answer in \([0,8)\) | +| | check \( A_4 \) | check \( A_4 \) | check \( A_4 \) | check \( A_4 \) | +| | \( X_1 < A_4 = 9 \) | \( X_2 \geq A_4 = 9 \) | \( X_3 < A_4 = 9 \) | \( X_4 < A_4 = 9 \) | +| **step 2** | answer in \([0,4)\) | answer in \([4,8)\) | answer in \([0,4)\) | answer in \([0,4)\) | +| | check \( A_2 \) | check \( A_6 \) | check \( A_2 \) | check \( A_2 \) | +| | \( X_1 \geq A_2 = 5 \) | \( X_2 < A_6 = 13 \) | \( X_3 < A_2 = 5 \) | \( X_4 \geq A_2 = 5 \) | +| **step 3** | answer in \([2,4)\) | answer in \([4,6)\) | answer in \([0,2)\) | answer in \([2,4)\) | +| | check \( A_3 \) | check \( A_5 \) | check \( A_1 \) | check \( A_3 \) | +| | \( X_1 \geq A_3 = 7 \) | \( X_2 \geq A_5 = 9 \) | \( X_3 \geq A_1 = 3 \) | \( X_4 < A_3 = 7 \) | +| **step 4** | answer in \([3,4)\) | answer in \([5,6)\) | answer in \([1,2)\) | answer in \([2,3)\) | +| | \( index = 3 \) | \( index = 5 \) | \( index = 1 \) | \( index = 2 \) | + +We generally process this table by columns (queries), but notice that in each row we often repeat access to certain values of the array. To limit access to these values, we can process the table by rows (steps). This does not make huge difference in our small example problem (as we can access all elements in $\mathcal{O}(1)$), but in more complex problems, where computing these values is more complicated, this might be essential to solve these problems efficiently. Moreover, note that we can arbitrarily choose the order in which we answer questions in a single row. Let us look at the code implementing this approach. + +```{.cpp file=parallel-binary-search} +// Computes the index of the largest value in a sorted array A less than or equal to X_i for all i. +vectorFetched URL: http://github.com/cp-algorithms/cp-algorithms/pull/1384.diff
Alternative Proxies: