-
Notifications
You must be signed in to change notification settings - Fork 537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added parallel binary search module #3777
Added parallel binary search module #3777
Conversation
for more information, see https://pre-commit.ci
Put all code blocks in |
I guess, I did put all code blocks in |
underscore to hyphen
underscore to hypend in MODULE.ID
My mistake, I misread the pseudocode section. In that case, it would probably be better to replace it with actual code for each of C++, Java and Python. |
Yeah, I think you are right, but for that reason the deployment failed? |
the module needs to be added to https://github.com/cpinitiative/usaco-guide/blob/master/content/ordering.ts |
Added parallel_binary_search module ID
switched all tag[" "] to tag[ ]
Co-authored-by: Benjamin Qi <[email protected]>
changed name to an existing one
title="Tutorial on Parallel Binary Search" | ||
url="https://codeforces.com/blog/entry/45578" | ||
starred | ||
> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use self-closing tags here
## Pseudocode | ||
Let $\text{event[i]}$ corresponds to some stuff that needed be done at that particular index $i$ ($lo \leq i \leq hi$) | ||
|
||
For all $\mathcal{O}(\log MaxAns)$ levels, we maintain independent data structure $\text{DS[level]}$. Associated with every data structure we have our monotonic function $\text{DS[level].f()}$ and $\text{DS[level].add()}$ for maintaining ongoing **events** at that **level**. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's no need to maintain many independent data structures at the same time. instead, you can have one data structure and reset it
author: Talha Taki | ||
prerequisites: | ||
- binary-search | ||
description: Parallel binary search optimizes query efficiency by splitting queries based on the relevance of specific ranges within the binary search. Rather than repeatedly querying the same range, this technique intelligently divides the search process to enhance overall performance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this description. What do you mean by repeatedly querying the same range?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also is very wordy lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesnt make sense either: by splitting queries based on the relevance of specific ranges within the binary search.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is so vague: this technique intelligently divides the search process to enhance overall performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also is very wordy lol
yeah try writing it in one short sentence instead of like two long sentences
This pull request has been automatically marked as stale because it has not had recent activity. Please address the requested changes and re-request reviews. Thank you for your contribution! |
Please put 2.1 of this book in resources 🙏 |
For all $\mathcal{O}(\log MaxAns)$ levels, we maintain independent data structure $\text{DS[level]}$. Associated with every data structure we have our monotonic function $\text{DS[level].f()}$ and $\text{DS[level].add()}$ for maintaining ongoing **events** at that **level**. | ||
|
||
|
||
A pseudocode for parallel binary search looks something like this: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a brief description of what each of the variables mean, like level
and candidate
and ans
parallel_binary_search(l, r, level, candidate): | ||
m = candidate.size | ||
|
||
if m == 1: | ||
for i = 0 to m-1: | ||
ans[candidate[i]] = l | ||
|
||
candidate.clean() | ||
return | ||
|
||
mid = floor((l + r) / 2) | ||
|
||
while pointer[level] <= mid: | ||
DS[level].add(event[pointer[level]]) | ||
pointer[level] = pointer[level] + 1 | ||
|
||
|
||
for i = 0 to m-1: | ||
if DS[level].f(lo, mid, candidate[i]) == True: | ||
satisfied_candidate.add(candidate[i]) | ||
else: | ||
unsatisfied_candidate.add(candidate[i]) | ||
|
||
candidate.clean() | ||
|
||
parallel_binary_search(l, mid, level + 1, satisfied_candidate) | ||
parallel_binary_search(mid + 1, r, level + 1, unsatisfied_candidate) | ||
|
||
// Calculating answers for the range [lo, hi] where `relevant_candidate` belongs to that range | ||
parallel_binary_search(lo, hi, 0, relevant_candidate) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of pseudocode, could you provide C++ code?
|
||
$\text{DS[level].add()}$ = $\text{DSU[level].Union()}$ | ||
|
||
$\text{DS[level].f()}$ = $\text{DSU[level].Find()}$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does .f() mean here?
|
||
$\text{event[i]}$ = Add $i$th edge. | ||
|
||
$\text{DS[level].add()}$ = $\text{DSU[level].Union()}$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does .add() mean here?
## Solution - New Road Queries | ||
In this problem, the required data structure is [DSU](/gold/dsu) for knowing whether there is a path between two vertexes after adding some edges of the graph. Here: | ||
|
||
$\text{event[i]}$ = Add $i$th edge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is event[i]
void parallel_binary_search(int l, int r, int level, vector<int> &candidate) { | ||
if (l == r) { | ||
for (auto c : candidate) { ans[c] = l; } | ||
candidate.clear(); | ||
return; | ||
} | ||
|
||
int mid = (l + r) / 2; | ||
|
||
// Adding edges upto 'mid' | ||
for (; ptr[level] <= mid; ptr[level]++) { | ||
DSU[level].Union(edge[ptr[level]].first, edge[ptr[level]].second); | ||
} | ||
|
||
vector<int> left_candidate, right_candidate; | ||
for (int c : candidate) { | ||
if (DSU[level].Find(query[c].first) != | ||
DSU[level].Find(query[c].second)) { | ||
right_candidate.push_back(c); | ||
} else { | ||
left_candidate.push_back(c); | ||
} | ||
} | ||
|
||
// Memory complexity reduced to O(q) | ||
candidate.clear(); | ||
|
||
parallel_binary_search(l, mid, level + 1, left_candidate); | ||
parallel_binary_search(mid + 1, r, level + 1, right_candidate); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about you add this where you added the pseudocode?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also you should explicitly explain what this code means here.
// [0....m-1] are the possible answers. 'm' is equivalent of -1 for the | ||
// answer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// [0....m-1] are the possible answers. 'm' is equivalent of -1 for the | |
// answer. | |
// [0....m-1] are the possible answers. |
<LanguageSection> | ||
|
||
<CPPSection> | ||
## Solution - New Road Queries |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think your explanation is pretty vague, and it doesn't really explain how to solve this particular problem.
Yeah, the module is pretty vague. I am planning to improve this. Might change the whole structure of it |
maybe don't submit half-baked stuff as ready? maybe mark this pr as a draft and undo that when you're done |
This pull request has been automatically marked as stale because it has not had recent activity. Please address the requested changes and re-request reviews. Thank you for your contribution! |
Changes requested have not been made. Free free to create a new PR. |
#3624
First draft.