9#include <unordered_map>
17 std::unordered_map<T, std::vector<std::pair<T, double>>> adj;
18 std::unordered_map<T, double> nodes;
28 explicit hill_climbing(std::unordered_map<T, std::vector<std::pair<T, double>>> v = {},
29 std::unordered_map<T, double> nodes = {}) {
53 if (adj.find(u) != adj.end()) {
54 for (std::pair<T, double>& x : adj[u]) {
70 if (nodes.find(u) != nodes.end() && nodes.find(v) != nodes.end()) {
71 adj[u].push_back(std::make_pair(v, nodes[v]));
73 throw std::logic_error(
"One of the two nodes that passed to the "
74 "function do not exist in the graph");
76 }
catch (std::logic_error& e) {
77 std::cerr << e.what() <<
'\n';
92 std::unordered_map<T, bool> visited;
95 visited[start] =
true;
97 int64_t size = q.size();
98 for (int64_t i = 0; i < size; i++) {
99 T current = q.front();
100 if (current == end) {
104 double minim = std::numeric_limits<double>::max();
106 for (std::pair<T, double>& x : adj[current]) {
107 if (visited.find(x.first) == visited.end()) {
108 visited[x.first] =
true;
109 minim = std::min(minim, x.second);
113 if (minim != INT_MAX && neigh != current) {
114 if (current == start || minim <= nodes[current]) {
hill_climbing(std::unordered_map< T, std::vector< std::pair< T, double > > > v={}, std::unordered_map< T, double > nodes={})
hill_climbing constructor
Definition hill_climbing.h:28
bool has_edge(T u, T v)
has_edge function
Definition hill_climbing.h:52
bool search(T start, T end)
search function
Definition hill_climbing.h:88
void add_edge(T u, T v)
add_edge function
Definition hill_climbing.h:68
void insert_node(T u, double val)
insert_node function
Definition hill_climbing.h:43