-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
clumsy
wants to merge
1
commit into
cp-algorithms:main
Choose a base branch
from
clumsy:fix/0_1_bfs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,27 +17,44 @@ In this article we demonstrate how we can use BFS to solve the SSSP (single-sour | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
We can develop the algorithm by closely studying Dijkstra's algorithm and thinking about the consequences that our special graph implies. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The general form of Dijkstra's algorithm is (here a `set` is used for the priority queue): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
```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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
=== "Python" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
```py | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from heapq import heappop, heappush | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
d = [float("inf")] * n | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
d[s] = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
q = [(0, s)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while q: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dv, v = heappop(q) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if dv == d[v]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for u, w in adj[v]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if d[v] + w < d[u]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
d[u] = d[v] + w | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
heappush(q, (d[u], u)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
We can notice that the difference between the distances between the source `s` and two other vertices in the queue differs by at most one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Especially, we know that $d[v] \le d[u] \le d[v] + 1$ for each $u \in Q$. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -53,27 +70,43 @@ This structure is so simple, that we don't need an actual priority queue, i.e. u | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
We can simply use a normal queue, and append new vertices at the beginning if the corresponding edge has weight $0$, i.e. if $d[u] = d[v]$, or at the end if the edge has weight $1$, i.e. if $d[u] = d[v] + 1$. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This way the queue still remains sorted at all time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
```cpp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
=== "C++" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
```cpp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+75
to
93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
=== "Python" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
```py | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
d = [float("inf")] * n | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
d[s] = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
q = deque([s]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while q: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
v = q.popleft() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for u, w in adj[v]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if d[v] + w < d[u]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
d[u] = d[v] + w | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if w == 1: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
q.append(u) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
q.appendleft(u) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
## Dial's algorithm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Probably best to make this consistent with Python then. Also remove the comment about "set" above if this is incorporated.