XNetwork 1.7.8
Loading...
Searching...
No Matches
rand_cover.hpp
Go to the documentation of this file.
1#pragma once
2
23#include <cassert>
24#include <optional>
25#include <py2cpp/set.hpp>
26#include <random>
27#include <utility>
28#include <vector>
30
31namespace detail {
32
46 template <typename Node, typename Validator>
47 void reverse_delete_cover(py::set<Node>& soln, const std::vector<Node>& added_order,
49 for (auto it = added_order.rbegin(); it != added_order.rend(); ++it) {
50 soln.erase(*it);
51 if (!std::forward<Validator>(is_valid)()) {
52 soln.insert(*it);
53 }
54 }
55 }
56
57} // namespace detail
58
97template <typename Graph, typename WeightMap, typename RNG>
98auto rand_vertex_cover_trial(const Graph& ugraph, const WeightMap& weight,
100 -> std::pair<py::set<typename Graph::node_t>, typename WeightMap::mapped_type>;
101
102// -----------------------------------------------------------------------
103// Convenience overload - single trial with seed
104// -----------------------------------------------------------------------
105
115template <typename Graph, typename WeightMap>
116auto rand_vertex_cover(const Graph& ugraph, const WeightMap& weight,
117 std::optional<unsigned int> seed = std::optional<unsigned int>{0},
120 // using node_t = typename Graph::node_t;
121
122 if (seed.has_value()) {
123 std::mt19937 rng{seed.value()};
124 return rand_vertex_cover_trial(ugraph, weight, coverset, rng);
125 }
126 std::random_device rd;
127 std::mt19937 rng{rd()};
128 return rand_vertex_cover_trial(ugraph, weight, coverset, rng);
129}
130
131// -----------------------------------------------------------------------
132// Multi-threaded: run num_trials independent trials, return best cover
133// -----------------------------------------------------------------------
134
150template <typename Graph, typename WeightMap>
151auto rand_vertex_cover_mt(const Graph& ugraph, const WeightMap& weight,
152 unsigned int num_trials = 64, unsigned int seed = 0,
Read-only map of maps of maps (view into a dict-of-dict-of-dict structure)
Definition coreviews.hpp:109
AdjacencyView(Atlas &d)
Construct an AdjacencyView from an Atlas container.
Definition coreviews.hpp:115
Definition hadlock.hpp:40
void reverse_delete_cover(py::set< Node > &soln, const std::vector< Node > &added_order, Validator &&is_valid)
Reverse-delete post-processing step.
Definition rand_cover.hpp:47
auto rand_vertex_cover_trial(const Graph &ugraph, const WeightMap &weight, const py::set< typename Graph::node_t > &coverset, RNG &rng) -> std::pair< py::set< typename Graph::node_t >, typename WeightMap::mapped_type >
Single trial of Pitt's randomized vertex cover algorithm.
auto rand_vertex_cover(const Graph &ugraph, const WeightMap &weight, std::optional< unsigned int > seed=std::optional< unsigned int >{0}, const py::set< typename Graph::node_t > &coverset={}) -> std::pair< py::set< typename Graph::node_t >, typename WeightMap::mapped_type >
Single-trial randomized vertex cover (seeded convenience wrapper).
Definition rand_cover.hpp:116
auto rand_vertex_cover_mt(const Graph &ugraph, const WeightMap &weight, unsigned int num_trials=64, unsigned int seed=0, const py::set< typename Graph::node_t > &coverset={}) -> std::pair< py::set< typename Graph::node_t >, typename WeightMap::mapped_type >
Multi-threaded randomized vertex cover.