Skip to content
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

Conversation

Talha-Taki002
Copy link
Contributor

#3624

First draft.

  • [done] I have tested my code.
  • [done] I have added my solution according to the steps here.
  • [done] I have followed the code conventions mentioned here.
    • I understand that if it is clear that I have not attempted to follow these conventions, my PR will be closed.
    • If changes are requested, I will re-request a review after addressing them.

@dmiao623
Copy link
Contributor

Put all code blocks in <LanguageSection> blocks, write the tutorial for each section at the bare minimum, and use focus problems.

@Talha-Taki002
Copy link
Contributor Author

Put all code blocks in <LanguageSection> blocks, write the tutorial for each section at the bare minimum, and use focus problems.

I guess, I did put all code blocks in <LanguageSection> blocks. I didn't get what you said. Could you please, explain a little more

underscore to hypend in MODULE.ID
@dmiao623
Copy link
Contributor

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.

@Talha-Taki002
Copy link
Contributor Author

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?

@bqi343
Copy link
Member

bqi343 commented Jun 29, 2023

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[ ]
changed name to an existing one
title="Tutorial on Parallel Binary Search"
url="https://codeforces.com/blog/entry/45578"
starred
>
Copy link
Member

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**.
Copy link
Member

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 $\mathcal{O}(\log MaxAns)$ times.

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.
Copy link
Member

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?

Copy link
Member

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

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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

@stale
Copy link

stale bot commented Jul 9, 2023

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!

@stale stale bot added stale and removed stale labels Jul 9, 2023
@envyaims
Copy link
Member

Please put 2.1 of this book in resources 🙏
https://kostka.dev/sp/spbook.pdf

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:
Copy link
Member

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

Comment on lines +45 to +74
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)
Copy link
Member

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()}$
Copy link
Member

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()}$
Copy link
Member

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is event[i]

Comment on lines +130 to +159
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);
}
Copy link
Member

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?

Copy link
Member

@jessechoe10 jessechoe10 Jul 13, 2023

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.

Comment on lines +187 to +188
// [0....m-1] are the possible answers. 'm' is equivalent of -1 for the
// answer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// [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
Copy link
Member

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.

@Talha-Taki002
Copy link
Contributor Author

Talha-Taki002 commented Jul 13, 2023

Yeah, the module is pretty vague. I am planning to improve this. Might change the whole structure of it

@SansPapyrus683
Copy link
Contributor

maybe don't submit half-baked stuff as ready? maybe mark this pr as a draft and undo that when you're done

@Talha-Taki002 Talha-Taki002 marked this pull request as draft July 16, 2023 16:23
@stale
Copy link

stale bot commented Jul 23, 2023

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!

@stale stale bot added the stale label Jul 23, 2023
@stale
Copy link

stale bot commented Jul 30, 2023

Changes requested have not been made. Free free to create a new PR.

@stale stale bot closed this Jul 30, 2023
@Muaath5 Muaath5 mentioned this pull request Aug 7, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants