Skip to content

Commit a990b25

Browse files
authored
Merge branch 'main' into main
2 parents fc498bc + 4685762 commit a990b25

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/graph/finding-negative-cycle-in-graph.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,50 +30,47 @@ Do $N$ iterations of Bellman-Ford algorithm. If there were no changes on the las
3030
struct Edge {
3131
int a, b, cost;
3232
};
33-
34-
int n, m;
33+
34+
int n;
3535
vector<Edge> edges;
3636
const int INF = 1000000000;
37-
37+
3838
void solve() {
39-
vector<int> d(n, INF);
39+
vector<int> d(n, 0);
4040
vector<int> p(n, -1);
4141
int x;
42-
43-
d[0] = 0;
44-
42+
4543
for (int i = 0; i < n; ++i) {
4644
x = -1;
4745
for (Edge e : edges) {
48-
if (d[e.a] < INF && d[e.a] + e.cost < d[e.b]) {
46+
if (d[e.a] + e.cost < d[e.b]) {
4947
d[e.b] = max(-INF, d[e.a] + e.cost);
5048
p[e.b] = e.a;
5149
x = e.b;
5250
}
5351
}
5452
}
55-
53+
5654
if (x == -1) {
5755
cout << "No negative cycle found.";
5856
} else {
5957
for (int i = 0; i < n; ++i)
6058
x = p[x];
61-
59+
6260
vector<int> cycle;
6361
for (int v = x;; v = p[v]) {
6462
cycle.push_back(v);
6563
if (v == x && cycle.size() > 1)
6664
break;
6765
}
6866
reverse(cycle.begin(), cycle.end());
69-
67+
7068
cout << "Negative cycle: ";
7169
for (int v : cycle)
7270
cout << v << ' ';
7371
cout << endl;
7472
}
7573
}
76-
7774
```
7875

7976
## Using Floyd-Warshall algorithm

src/sequences/k-th.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ T order_statistics (std::vector<T> a, unsigned n, unsigned k)
6868
6969
## Notes
7070
* The randomized algorithm above is named [quickselect](https://en.wikipedia.org/wiki/Quickselect). You should do random shuffle on $A$ before calling it or use a random element as a barrier for it to run properly. There are also deterministic algorithms that solve the specified problem in linear time, such as [median of medians](https://en.wikipedia.org/wiki/Median_of_medians).
71-
* A deterministic linear solution is implemented in C++ standard library as [std::nth_element](https://en.cppreference.com/w/cpp/algorithm/nth_element).
71+
* [std::nth_element](https://en.cppreference.com/w/cpp/algorithm/nth_element) solves this in C++ but gcc's implementation runs in worst case $O(n \log n )$ time.
7272
* Finding $K$ smallest elements can be reduced to finding $K$-th element with a linear overhead, as they're exactly the elements that are smaller than $K$-th.
7373
7474
## Practice Problems

0 commit comments

Comments
 (0)