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
Copy file name to clipboardExpand all lines: src/graph/01_bfs.md
+71-38Lines changed: 71 additions & 38 deletions
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
17
17
We can develop the algorithm by closely studying Dijkstra's algorithm and thinking about the consequences that our special graph implies.
18
18
The general form of Dijkstra's algorithm is (here a`set` is used for the priority queue):
19
19
20
-
```cpp
21
-
d.assign(n, INF);
22
-
d[s] =0;
23
-
set<pair<int,int>> q;
24
-
q.insert({0, s});
25
-
while (!q.empty()) {
26
-
int v = q.begin()->second;
27
-
q.erase(q.begin());
28
-
29
-
for (auto edge : adj[v]) {
30
-
int u = edge.first;
31
-
int w = edge.second;
32
-
33
-
if (d[v] + w < d[u]) {
34
-
q.erase({d[u], u});
35
-
d[u] = d[v] + w;
36
-
q.insert({d[u], u});
20
+
=== "C++"
21
+
```cpp
22
+
d.assign(n, INF);
23
+
d[s] = 0;
24
+
set<pair<int, int>> q;
25
+
q.insert({0, s});
26
+
while (!q.empty()) {
27
+
int v = q.begin()->second;
28
+
q.erase(q.begin());
29
+
30
+
for (auto edge : adj[v]) {
31
+
int u = edge.first;
32
+
int w = edge.second;
33
+
34
+
if (d[v] + w < d[u]) {
35
+
q.erase({d[u], u});
36
+
d[u] = d[v] + w;
37
+
q.insert({d[u], u});
38
+
}
37
39
}
38
40
}
39
-
}
40
-
```
41
+
```
42
+
=== "Python"
43
+
```py
44
+
from heapq import heappop, heappush
45
+
46
+
47
+
d = [float("inf")] * n
48
+
d[s] = 0
49
+
q = [(0, s)]
50
+
while q:
51
+
dv, v = heappop(q)
52
+
if dv == d[v]:
53
+
for u, w in adj[v]:
54
+
if d[v] + w < d[u]:
55
+
d[u] = d[v] + w
56
+
heappush(q, (d[u], u))
57
+
```
41
58
42
59
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.
43
60
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
53
70
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$.
54
71
This way the queue still remains sorted at all time.