Skip to content

Commit 5278b44

Browse files
committed
KargerMinCutTimeout: recorded discovery_time iteration
1 parent 8d7124d commit 5278b44

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

KargerMinCutTimeout/karger_timeout.h

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#pragma once
22

3+
#include <any> // std::any
4+
#include <array> // std::array
35
#include <iostream> // std::cout, std::endl
46
#include <limits> // std::numeric_limits
57
#include <memory> // std::shared_ptr, std::unique_ptr, std::make_unique
6-
#include <utility> // std::make_pair
78

89
#include "AdjacencyMapGraph.h"
910
#include "full_contraction.h"
@@ -12,19 +13,22 @@
1213

1314
/**
1415
* 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.
1617
* Time: O(n^4 * log(n))
1718
* Space: O(n + m)
1819
*/
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 {
2223
// keep track of the min-cut discovery time
2324
stopwatch::time_point_t discovery_time_stop;
2425

2526
// keeps track of the minimum cut
2627
size_t min_cut = std::numeric_limits<size_t>::max();
2728

29+
// keeps track of the iteration of the minimum cut
30+
size_t discovery_iteration = 0;
31+
2832
bool keep_going = true;
2933

3034
// execute full_contraction k times to hopefully find the minimum cut.
@@ -37,8 +41,10 @@
3741
const size_t cut = contracted_graph->edge_size();
3842

3943
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
4146
min_cut = cut;
47+
discovery_iteration = i;
4248
discovery_time_stop = stopwatch::now();
4349
}
4450

@@ -54,6 +60,6 @@
5460

5561
// std::make_tuple doesn't work, that's probably related to the fact that std::tuple<...>
5662
// 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};
5965
}

KargerMinCutTimeout/main.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <any>
12
#include <iostream> // std::cout, std::endl
23
#include <string> // std::string_literals
34

@@ -33,11 +34,11 @@ int main(int argc, char** argv) {
3334
// the timeout is set to 2 minutes
3435
auto timeout_min = 2min;
3536

36-
const auto [karger_result, karger_duration] =
37+
const auto [result, karger_duration] =
3738
timeout::with_timeout(std::move(timeout_min), stopwatch::decorator<stopwatch::us_t>(karger),
3839
std::ref(graph), k, program_time_start);
3940

40-
const auto& [min_cut, discovery_time] = karger_result;
41+
const auto& [min_cut, discovery_time, discovery_iteration] = result;
4142

4243
// stop the stopwatch
4344
auto program_time_stop = stopwatch::now();
@@ -46,7 +47,10 @@ int main(int argc, char** argv) {
4647
const auto program_time =
4748
stopwatch::duration<stopwatch::us_t>(program_time_start, program_time_stop);
4849

49-
std::cout << "min_cut: "s << min_cut << std::endl;
50+
std::cout << "min_cut: "s << std::any_cast<size_t>(min_cut) << std::endl;
5051
std::cout << "program_time: "s << program_time << std::endl;
51-
std::cout << "discovery_time: "s << discovery_time << std::endl;
52+
std::cout << "discovery_time: "s << std::any_cast<decltype(program_time)>(discovery_time)
53+
<< std::endl;
54+
std::cout << "discovery_iteration: "s << std::any_cast<size_t>(discovery_iteration)
55+
<< std::endl;
5256
}

0 commit comments

Comments
 (0)