|
1 | 1 | #pragma once
|
2 | 2 |
|
| 3 | +#include <any> // std::any |
| 4 | +#include <array> // std::array |
3 | 5 | #include <iostream> // std::cout, std::endl
|
4 | 6 | #include <limits> // std::numeric_limits
|
5 | 7 | #include <memory> // std::shared_ptr, std::unique_ptr, std::make_unique
|
6 |
| -#include <utility> // std::make_pair |
7 | 8 |
|
8 | 9 | #include "AdjacencyMapGraph.h"
|
9 | 10 | #include "full_contraction.h"
|
|
12 | 13 |
|
13 | 14 | /**
|
14 | 15 | * Run Karger's randomized min-cut algorithm k times on the given graph.
|
15 |
| - * Return the a pair containing the min-cut found and the discovery time. |
| 16 | + * Return the an array containing the min-cut found, the discovery time and the discovery iteration. |
16 | 17 | * Time: O(n^4 * log(n))
|
17 | 18 | * Space: O(n + m)
|
18 | 19 | */
|
19 |
| -[[nodiscard]] auto karger(timeout::timeout_signal&& signal, |
20 |
| - const std::shared_ptr<AdjacencyMapGraph>& graph, size_t k, |
21 |
| - const stopwatch::time_point_t program_time_start) noexcept { |
| 20 | +[[nodiscard]] std::array<std::any, 3> karger( |
| 21 | + timeout::timeout_signal&& signal, const std::shared_ptr<AdjacencyMapGraph>& graph, size_t k, |
| 22 | + const stopwatch::time_point_t program_time_start) noexcept { |
22 | 23 | // keep track of the min-cut discovery time
|
23 | 24 | stopwatch::time_point_t discovery_time_stop;
|
24 | 25 |
|
25 | 26 | // keeps track of the minimum cut
|
26 | 27 | size_t min_cut = std::numeric_limits<size_t>::max();
|
27 | 28 |
|
| 29 | + // keeps track of the iteration of the minimum cut |
| 30 | + size_t discovery_iteration = 0; |
| 31 | + |
28 | 32 | bool keep_going = true;
|
29 | 33 |
|
30 | 34 | // execute full_contraction k times to hopefully find the minimum cut.
|
|
37 | 41 | const size_t cut = contracted_graph->edge_size();
|
38 | 42 |
|
39 | 43 | if (cut < min_cut) {
|
40 |
| - // a better cut has been found, so we update min_cut and reset stop_discovery_time |
| 44 | + // a better cut has been found, so we update min_cut, discovery_iteration and reset |
| 45 | + // stop_discovery_time |
41 | 46 | min_cut = cut;
|
| 47 | + discovery_iteration = i; |
42 | 48 | discovery_time_stop = stopwatch::now();
|
43 | 49 | }
|
44 | 50 |
|
|
54 | 60 |
|
55 | 61 | // std::make_tuple doesn't work, that's probably related to the fact that std::tuple<...>
|
56 | 62 | // doesn't have a default constructor but std::packaged_task (used in timeout.h) needs a default
|
57 |
| - // constructor. |
58 |
| - return std::make_pair(min_cut, discovery_time); |
| 63 | + // constructor. Using std::array<std::any, 3> is just an escamotage. |
| 64 | + return {min_cut, discovery_time, discovery_iteration + 1}; |
59 | 65 | }
|
0 commit comments