You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
During the execution of the algorithm, we never evaluate neither $A_L$ nor $A_R$, as $L < M < R$. In the end, $L$ will be the index of the last element that is not greater than $k$ (or $-1$ if there is no such element) and $R$ will be the index of the first element larger than $k$ (or $n$ if there is no such element).
73
73
74
-
**Note.** Calculating`m` as`m = (r + l) / 2` can lead to overflow if`l` and`r` are two positive integers, and this error lived about 9 years in JDK as described in the[blogpost](https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html). Some alternative approaches include e.g. writing`m = l + (r - l) / 2` which always works for positive integer`l` and`r`, but might still overflow if`l` is a negative number. If you use C++20, it offers an alternative solution in the form of`m = midpoint(l, r)` which always works correctly.
74
+
**Note.** Calculating`m` as`m = (r + l) / 2` can lead to overflow if`l` and`r` are two positive integers, and this error lived about 9 years in JDK as described in the[blogpost](https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html). Some alternative approaches include e.g. writing`m = l + (r - l) / 2` which always works for positive integer`l` and`r`, but might still overflow if`l` is a negative number. If you use C++20, it offers an alternative solution in the form of`m =std::midpoint(l, r)` which always works correctly.
75
75
76
76
##Search on arbitrary predicate
77
77
@@ -138,126 +138,56 @@ Another noteworthy way to do binary search is, instead of maintaining an active
138
138
139
139
This paradigm is widely used in tasks around trees, such as finding lowest common ancestor of two vertices or finding an ancestor 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.
140
140
141
-
##Parallel Binary Search
141
+
##Parallel Binary Search
142
142
143
-
[^1] Imaginethatwe want to answer $Z$ queries abouttheindex of the largest value less than or equal to some $X_i$ (for $i=1,2,\ldots,Z$) in some sorted 0-indexed array $A$. Naturally, each query can be answered using binary search.
143
+
<small>Notethatthis section is followingthedescription in[Sports programming in practice](https://kostka.dev/sp/).</small>
144
144
145
-
Specifally, let us consider the following array $A$:
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 some sorted 0-indexed array $A$. Naturally, each query can be answered using binary search.
150
146
147
+
Specifally, let us consider the following array $A =[1,3,5,7,9,9,13,15]$
151
148
with queries: $X =[8,11,4,5]$. We can use binary search for each query sequentially.
||\( index = 3\)|\( index = 5\)|\( index = 1\)|\( index = 2\)|
233
163
234
164
We generally process this table by columns (queries), but notice that in each row we often repeat access to certain values of our array. To limit access to the 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.
235
165
236
-
```cpp
166
+
```{.cpp file=parallel-binary-search}
237
167
// Computes the index of the largest value in table A less than or equal to $X_i$ for all $i$.
238
168
vector<int>ParallelBinarySearch(vector<int>& A, vector<int>& X) {
239
169
int N = A.size();
240
170
int M = X.size();
241
-
vector<int>P(M, -1);
242
-
vector<int>Q(M, N-1);
171
+
vector<int>left(M, -1);
172
+
vector<int>right(M, N-1);
243
173
244
174
for (int step = 1; step <= ceil(log2(N)); ++step) {
245
175
// Map to store indices of queries asking for this value.
246
-
unordered_map<int, vector<int>>important_values;
176
+
unordered_map<int, vector<int>>mid_to_queries;
247
177
248
178
// Calculate mid and populate the important_values map.
249
179
for (int i = 0; i < M; ++i) {
250
-
int mid = (P[i] +Q[i]) / 2;
251
-
important_values[mid].push_back(i);
180
+
int mid = (left[i] +right[i]) / 2;
181
+
mid_to_queries[mid].push_back(i);
252
182
}
253
183
254
184
// Process each value in important_values.
255
-
for (const auto& [mid, queries]:important_values) {
185
+
for (const auto& [mid, queries]:mid_to_queries) {
256
186
for (int query : queries) {
257
187
if (A[mid] > X[query]) {
258
-
Q[query] = mid;
188
+
right[query] = mid;
259
189
} else {
260
-
P[query] = mid;
190
+
left[query] = mid;
261
191
}
262
192
}
263
193
}
@@ -286,7 +216,4 @@ vector<int> ParallelBinarySearch(vector<int>& A, vector<int>& X) {