Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: python impl for 0-1 BFS#1380

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
clumsy wants to merge1 commit intocp-algorithms:main
base:main
Choose a base branch
Loading
fromclumsy:fix/0_1_bfs

Conversation

clumsy
Copy link
Contributor

Adding python implementation, fixing a minor grammatical error.

Recreating#1336 against the new base (main).

Comment on lines +20 to 40
=== "C++"
```cpp
d.assign(n, INF);
d[s] = 0;
set<pair<int, int>> q;
q.insert({0, s});
while (!q.empty()) {
int v = q.begin()->second;
q.erase(q.begin());

for (auto edge : adj[v]) {
int u = edge.first;
int w = edge.second;

if (d[v] + w < d[u]) {
q.erase({d[u], u});
d[u] = d[v] + w;
q.insert({d[u], u});
}
}
}
Copy link
Member

@adamant-pwnadamant-pwnOct 24, 2024
edited
Loading

Choose a reason for hiding this comment

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

Suggested change
=== "C++"
```cpp
d.assign(n, INF);
d[s] = 0;
set<pair<int, int>> q;
q.insert({0, s});
while (!q.empty()) {
int v = q.begin()->second;
q.erase(q.begin());
for (auto edge : adj[v]) {
int u = edge.first;
int w = edge.second;
if (d[v] + w < d[u]) {
q.erase({d[u], u});
d[u] = d[v] + w;
q.insert({d[u], u});
}
}
}
=== "C++"
```cpp
d.assign(n, INF);
d[s] = 0;
priority_queue<pair<int, int>> q = {{0, s}};
while (!empty(q)) {
int [dv, v] = q.top();
q.pop();
if (-dv == d[v]) {
for (auto [u, w] : adj[v]) {
if (d[v] + w < d[u]) {
d[u] = d[v] + w;
q.insert({-d[u], u});
}
}
}
}

Probably best to make this consistent with Python then. Also remove the comment about "set" above if this is incorporated.

Comment on lines +75 to 93
vector<int> d(n, INF);
d[s] = 0;
deque<int> q;
q.push_front(s);
while (!q.empty()) {
int v = q.front();
q.pop_front();
for (auto edge : adj[v]) {
int u = edge.first;
int w = edge.second;
if (d[v] + w < d[u]) {
d[u] = d[v] + w;
if (w == 1)
q.push_back(u);
else
q.push_front(u);
}
}
}
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
vector<int> d(n, INF);
d[s] = 0;
deque<int> q;
q.push_front(s);
while (!q.empty()) {
int v = q.front();
q.pop_front();
for (auto edge : adj[v]) {
int u = edge.first;
int w = edge.second;
if (d[v] + w < d[u]) {
d[u] = d[v] + w;
if (w == 1)
q.push_back(u);
else
q.push_front(u);
}
}
}
vector<int> d(n, INF);
d[s] = 0;
deque<int> q = {s};
while (!empty(q)) {
int v = q.front();
q.pop_front();
for (auto [u, w] : adj[v]) {
if (d[v] + w < d[u]) {
d[u] = d[v] + w;
if (w == 1)
q.push_back(u);
else
q.push_front(u);
}
}
}

@adamant-pwn
Copy link
Member

I think it's best to also rewrite C++ code with a newer standard to match the Python style.

But could you please test both implementations on one of the example problems in the article?

clumsy reacted with thumbs up emoji

@mhayter
Copy link
Contributor

Updates?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@adamant-pwnadamant-pwnadamant-pwn left review comments

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@clumsy@adamant-pwn@mhayter

[8]ページ先頭

©2009-2025 Movatter.jp