diff --git a/d0/d3e/classdata__structures_1_1trie.html b/d0/d3e/classdata__structures_1_1trie.html index b157406ce..13dfa04d1 100644 --- a/d0/d3e/classdata__structures_1_1trie.html +++ b/d0/d3e/classdata__structures_1_1trie.html @@ -150,7 +150,7 @@ Static Private Attributes

Detailed Description

Trie implementation for small-case English alphabets a-z

-

Definition at line 24 of file trie_tree.cpp.

+

Definition at line 25 of file trie_tree.cpp.

Member Function Documentation

◆ char_to_int()

@@ -184,19 +184,19 @@ Static Private Attributes
Returns
unsigned integer index
-

Definition at line 37 of file trie_tree.cpp.

-
37 {
-
38 if (ch >= 'A' && ch <= 'Z') {
-
39 return ch - 'A';
-
40 } else if (ch >= 'a' && ch <= 'z') {
-
41 return ch - 'a' + NUM_CHARS;
-
42 }
-
43
-
44 std::cerr << "Invalid character present. Exiting...";
-
45 std::exit(EXIT_FAILURE);
-
46 return 0;
-
47 }
-
static constexpr uint8_t NUM_CHARS
Number of alphabets.
Definition trie_tree.cpp:26
+

Definition at line 38 of file trie_tree.cpp.

+
38 {
+
39 if (ch >= 'A' && ch <= 'Z') {
+
40 return ch - 'A';
+
41 } else if (ch >= 'a' && ch <= 'z') {
+
42 return ch - 'a' + NUM_CHARS;
+
43 }
+
44
+
45 std::cerr << "Invalid character present. Exiting...";
+
46 std::exit(EXIT_FAILURE);
+
47 return 0;
+
48 }
+
static constexpr uint8_t NUM_CHARS
Number of alphabets.
Definition trie_tree.cpp:27
@@ -239,48 +239,48 @@ Static Private Attributes
false if unsuccessful
-

Definition at line 133 of file trie_tree.cpp.

-
133 {
-
134 if (index == str.length()) {
-
135 if (!isEndofWord) {
-
136 return false;
-
137 }
-
138 isEndofWord = false;
-
139 // following lines - possible source of error?
-
140 // for (int i = 0; i < NUM_CHARS; i++)
-
141 // if (!arr[i])
-
142 // return false;
-
143 return true;
-
144 }
-
145 int j = char_to_int(str[index]);
-
146 if (!arr[j]) {
-
147 return false;
-
148 }
-
149 bool var = deleteString(str, index + 1);
-
150 if (var) {
-
151 arr[j].reset();
-
152 if (isEndofWord) {
-
153 return false;
-
154 } else {
-
155 int i = 0;
-
156 for (i = 0; i < NUM_CHARS; i++) {
-
157 if (arr[i]) {
-
158 return false;
-
159 }
-
160 }
-
161 return true;
-
162 }
-
163 }
-
164
-
165 /* should not return here */
-
166 std::cout << __func__ << ":" << __LINE__
-
167 << "Should not reach this line\n";
-
168 return false;
-
169 }
-
std::array< std::shared_ptr< trie >, NUM_CHARS<< 1 > arr
Recursive tree nodes as an array of shared-pointers.
Definition trie_tree.cpp:28
-
bool isEndofWord
identifier if a node is terminal node
Definition trie_tree.cpp:29
-
uint8_t char_to_int(const char &ch) const
Convert a character to integer for indexing.
Definition trie_tree.cpp:37
-
bool deleteString(const std::string &str, int index)
+

Definition at line 134 of file trie_tree.cpp.

+
134 {
+
135 if (index == str.length()) {
+
136 if (!isEndofWord) {
+
137 return false;
+
138 }
+
139 isEndofWord = false;
+
140 // following lines - possible source of error?
+
141 // for (int i = 0; i < NUM_CHARS; i++)
+
142 // if (!arr[i])
+
143 // return false;
+
144 return true;
+
145 }
+
146 int j = char_to_int(str[index]);
+
147 if (!arr[j]) {
+
148 return false;
+
149 }
+
150 bool var = deleteString(str, index + 1);
+
151 if (var) {
+
152 arr[j].reset();
+
153 if (isEndofWord) {
+
154 return false;
+
155 } else {
+
156 int i = 0;
+
157 for (i = 0; i < NUM_CHARS; i++) {
+
158 if (arr[i]) {
+
159 return false;
+
160 }
+
161 }
+
162 return true;
+
163 }
+
164 }
+
165
+
166 /* should not return here */
+
167 std::cout << __func__ << ":" << __LINE__
+
168 << "Should not reach this line\n";
+
169 return false;
+
170 }
+
std::array< std::shared_ptr< trie >, NUM_CHARS<< 1 > arr
Recursive tree nodes as an array of shared-pointers.
Definition trie_tree.cpp:29
+
bool isEndofWord
identifier if a node is terminal node
Definition trie_tree.cpp:30
+
uint8_t char_to_int(const char &ch) const
Convert a character to integer for indexing.
Definition trie_tree.cpp:38
+
bool deleteString(const std::string &str, int index)
@@ -313,30 +313,30 @@ Static Private Attributes -

Definition at line 76 of file trie_tree.cpp.

-
76 {
-
77 std::shared_ptr<trie> root(nullptr);
-
78
-
79 for (const char& ch : str) {
-
80 int j = char_to_int(ch);
-
81 if (root) {
-
82 if (root->arr[j]) {
-
83 root = root->arr[j];
-
84 } else {
-
85 std::shared_ptr<trie> temp(new trie());
-
86 root->arr[j] = temp;
-
87 root = temp;
-
88 }
-
89 } else if (arr[j]) {
-
90 root = arr[j];
-
91 } else {
-
92 std::shared_ptr<trie> temp(new trie());
-
93 arr[j] = temp;
-
94 root = temp;
-
95 }
-
96 }
-
97 root->isEndofWord = true;
-
98 }
+

Definition at line 77 of file trie_tree.cpp.

+
77 {
+
78 std::shared_ptr<trie> root(nullptr);
+
79
+
80 for (const char& ch : str) {
+
81 int j = char_to_int(ch);
+
82 if (root) {
+
83 if (root->arr[j]) {
+
84 root = root->arr[j];
+
85 } else {
+
86 std::shared_ptr<trie> temp(new trie());
+
87 root->arr[j] = temp;
+
88 root = temp;
+
89 }
+
90 } else if (arr[j]) {
+
91 root = arr[j];
+
92 } else {
+
93 std::shared_ptr<trie> temp(new trie());
+
94 arr[j] = temp;
+
95 root = temp;
+
96 }
+
97 }
+
98 root->isEndofWord = true;
+
99 }
trie()=default
Class default constructor.
@@ -383,21 +383,21 @@ Static Private Attributes
false if not found
-

Definition at line 55 of file trie_tree.cpp.

-
56 {
-
57 if (index == str.length()) {
-
58 if (!root->isEndofWord) {
-
59 return false;
-
60 }
-
61 return true;
-
62 }
-
63 int j = char_to_int(str[index]);
-
64 if (!root->arr[j]) {
-
65 return false;
-
66 }
-
67 return search(root->arr[j], str, index + 1);
-
68 }
-
bool search(const std::shared_ptr< trie > &root, const std::string &str, int index)
Definition trie_tree.cpp:55
+

Definition at line 56 of file trie_tree.cpp.

+
57 {
+
58 if (index == str.length()) {
+
59 if (!root->isEndofWord) {
+
60 return false;
+
61 }
+
62 return true;
+
63 }
+
64 int j = char_to_int(str[index]);
+
65 if (!root->arr[j]) {
+
66 return false;
+
67 }
+
68 return search(root->arr[j], str, index + 1);
+
69 }
+
bool search(const std::shared_ptr< trie > &root, const std::string &str, int index)
Definition trie_tree.cpp:56
@@ -438,20 +438,20 @@ Static Private Attributes
false if not found
-

Definition at line 106 of file trie_tree.cpp.

-
106 {
-
107 if (index == str.length()) {
-
108 if (!isEndofWord) {
-
109 return false;
-
110 }
-
111 return true;
-
112 }
-
113 int j = char_to_int(str[index]);
-
114 if (!arr[j]) {
-
115 return false;
-
116 }
-
117 return search(arr[j], str, index + 1);
-
118 }
+

Definition at line 107 of file trie_tree.cpp.

+
107 {
+
108 if (index == str.length()) {
+
109 if (!isEndofWord) {
+
110 return false;
+
111 }
+
112 return true;
+
113 }
+
114 int j = char_to_int(str[index]);
+
115 if (!arr[j]) {
+
116 return false;
+
117 }
+
118 return search(arr[j], str, index + 1);
+
119 }
@@ -478,7 +478,7 @@ Static Private Attributes

Recursive tree nodes as an array of shared-pointers.

-

Definition at line 28 of file trie_tree.cpp.

+

Definition at line 29 of file trie_tree.cpp.

@@ -504,7 +504,7 @@ Static Private Attributes

identifier if a node is terminal node

-

Definition at line 29 of file trie_tree.cpp.

+

Definition at line 30 of file trie_tree.cpp.

@@ -530,7 +530,7 @@ Static Private Attributes

Number of alphabets.

-

Definition at line 26 of file trie_tree.cpp.

+

Definition at line 27 of file trie_tree.cpp.

diff --git a/d1/d51/classrange__queries_1_1heavy__light__decomposition_1_1_tree.html b/d1/d51/classrange__queries_1_1heavy__light__decomposition_1_1_tree.html index 7ffcec4a6..8d1890fc2 100644 --- a/d1/d51/classrange__queries_1_1heavy__light__decomposition_1_1_tree.html +++ b/d1/d51/classrange__queries_1_1heavy__light__decomposition_1_1_tree.html @@ -191,7 +191,7 @@ class range_queries::heavy_light_decomposition::Tree< X >

A Basic

Deleting the default constructor An instance can only be created with the number of nodes Defaults: t_node indexing are zero based t_root is 0 depth of root_node is 0 Supports: lift :- lift a node k units up the tree kth_ancestor :- returns the kth ancestor lca :- returns the least common ancestor

-

Definition at line 78 of file heavy_light_decomposition.cpp.

+

Definition at line 79 of file heavy_light_decomposition.cpp.

Constructor & Destructor Documentation

◆ Tree()

@@ -226,25 +226,25 @@ template<typename X> -

Definition at line 140 of file heavy_light_decomposition.cpp.

-
141 : t_nodes(nodes), t_maxlift(static_cast<int>(floor(log2(nodes))) + 1) {
-
142 /* Initialize and resize all the vectors */
-
143 t_root = 0; /* Default */
-
144 t_adj.resize(t_nodes);
-
145 t_par.assign(t_nodes, std::vector<int>(t_maxlift, -1));
-
146 t_depth.assign(t_nodes, 0);
-
147 t_size.assign(t_nodes, 1);
-
148 t_val.resize(t_nodes);
-
149 }
-
A Basic Tree, which supports binary lifting.
-
std::vector< int > t_depth
a vector to store the depth of a node,
-
std::vector< X > t_val
values of nodes
-
std::vector< std::vector< int > > t_par
a matrix to store every node's 2^kth parent
-
int t_root
the root of the tree
-
std::vector< std::list< int > > t_adj
an adjacency list to stores the tree edges
-
const int t_maxlift
maximum possible height of the tree
-
std::vector< int > t_size
a vector to store the subtree size rooted at node
-
const int t_nodes
number of nodes
+

Definition at line 142 of file heavy_light_decomposition.cpp.

+
143 : t_nodes(nodes), t_maxlift(static_cast<int>(floor(log2(nodes))) + 1) {
+
144 /* Initialize and resize all the vectors */
+
145 t_root = 0; /* Default */
+
146 t_adj.resize(t_nodes);
+ +
148 t_depth.assign(t_nodes, 0);
+
149 t_size.assign(t_nodes, 1);
+
150 t_val.resize(t_nodes);
+
151 }
+
A Basic Tree, which supports binary lifting.
+
std::vector< int > t_depth
a vector to store the depth of a node,
+ +
std::vector< std::vector< int > > t_par
a matrix to store every node's 2^kth parent
+ +
std::vector< std::list< int > > t_adj
an adjacency list to stores the tree edges
+
const int t_maxlift
maximum possible height of the tree
+
std::vector< int > t_size
a vector to store the subtree size rooted at node
+
@@ -288,11 +288,11 @@ template<typename X>
Returns
void
-

Definition at line 157 of file heavy_light_decomposition.cpp.

-
157 {
-
158 t_adj[u].push_back(v);
-
159 t_adj[v].push_back(u);
-
160 }
+

Definition at line 159 of file heavy_light_decomposition.cpp.

+
159 {
+
160 t_adj[u].push_back(v);
+
161 t_adj[v].push_back(u);
+
162 }
@@ -330,8 +330,8 @@ template<typename X>
Returns
void
-

Definition at line 167 of file heavy_light_decomposition.cpp.

-
167{ t_root = new_root; }
+

Definition at line 169 of file heavy_light_decomposition.cpp.

+
169{ t_root = new_root; }
@@ -374,25 +374,25 @@ template<typename X>
Returns
void
-

Definition at line 116 of file heavy_light_decomposition.cpp.

-
116 {
-
117 t_par[u][0] = p;
-
118 if (p != -1) {
-
119 t_depth[u] = 1 + t_depth[p];
-
120 }
-
121 for (int k = 1; k < t_maxlift; k++) {
-
122 if (t_par[u][k - 1] != -1) {
-
123 t_par[u][k] = t_par[t_par[u][k - 1]][k - 1];
-
124 }
-
125 }
-
126
-
127 for (const int &v : t_adj[u]) {
-
128 if (v ^ p) {
-
129 dfs_lca(v, u);
-
130 }
-
131 }
-
132 }
-
void dfs_lca(int u, int p=-1)
Utility function to populate the t_par vector.
+

Definition at line 118 of file heavy_light_decomposition.cpp.

+
118 {
+
119 t_par[u][0] = p;
+
120 if (p != -1) {
+
121 t_depth[u] = 1 + t_depth[p];
+
122 }
+
123 for (int k = 1; k < t_maxlift; k++) {
+
124 if (t_par[u][k - 1] != -1) {
+
125 t_par[u][k] = t_par[t_par[u][k - 1]][k - 1];
+
126 }
+
127 }
+
128
+
129 for (const int &v : t_adj[u]) {
+
130 if (v ^ p) {
+
131 dfs_lca(v, u);
+
132 }
+
133 }
+
134 }
+
void dfs_lca(int u, int p=-1)
Utility function to populate the t_par vector.
@@ -436,16 +436,16 @@ template<typename X>
Returns
void
-

Definition at line 101 of file heavy_light_decomposition.cpp.

-
101 {
-
102 for (const int &v : t_adj[u]) {
-
103 if (v ^ p) {
-
104 dfs_size(v, u);
-
105 t_size[u] += t_size[v];
-
106 }
-
107 }
-
108 }
-
void dfs_size(int u, int p=-1)
Utility function to compute sub-tree sizes.
+

Definition at line 103 of file heavy_light_decomposition.cpp.

+
103 {
+
104 for (const int &v : t_adj[u]) {
+
105 if (v ^ p) {
+
106 dfs_size(v, u);
+
107 t_size[u] += t_size[v];
+
108 }
+
109 }
+
110 }
+
void dfs_size(int u, int p=-1)
Utility function to compute sub-tree sizes.
@@ -477,12 +477,12 @@ template<typename X>

This function must be called after the tree adjacency list and node values are populated The function initializes the required parameters, and populates the segment tree.

Returns
void
-

Definition at line 186 of file heavy_light_decomposition.cpp.

-
186 {
-
187 assert(t_nodes > 0);
-
188 dfs_size(t_root);
-
189 dfs_lca(t_root);
-
190 }
+

Definition at line 188 of file heavy_light_decomposition.cpp.

+
188 {
+
189 assert(t_nodes > 0);
+ + +
192 }
@@ -525,12 +525,12 @@ template<typename X>
Returns
the kth ancestor of node
-

Definition at line 218 of file heavy_light_decomposition.cpp.

-
218 {
-
219 lift(&p, dist);
-
220 return p;
-
221 }
-
void lift(int *const p, int dist)
The function lifts a node, k units up the tree. The lifting is done in place, and the result is store...
+

Definition at line 220 of file heavy_light_decomposition.cpp.

+
220 {
+
221 lift(&p, dist);
+
222 return p;
+
223 }
+
void lift(int *const p, int dist)
The function lifts a node, k units up the tree. The lifting is done in place, and the result is store...
@@ -573,26 +573,26 @@ template<typename X>
Returns
the least common ancestor of node a, and node b
-

Definition at line 229 of file heavy_light_decomposition.cpp.

-
229 {
-
230 assert(a >= 0 and b >= 0 and a < t_nodes and b < t_nodes);
-
231 if (t_depth[a] > t_depth[b]) {
-
232 lift(&a, t_depth[a] - t_depth[b]);
-
233 }
-
234 if (t_depth[b] > t_depth[a]) {
-
235 lift(&b, t_depth[b] - t_depth[a]);
-
236 }
-
237 if (a == b) {
-
238 return a;
-
239 }
-
240 for (int k = t_maxlift - 1; k >= 0; k--) {
-
241 if (t_par[a][k] != t_par[b][k]) {
-
242 a = t_par[a][k];
-
243 b = t_par[b][k];
-
244 }
-
245 }
-
246 return t_par[a][0];
-
247 }
+

Definition at line 231 of file heavy_light_decomposition.cpp.

+
231 {
+
232 assert(a >= 0 and b >= 0 and a < t_nodes and b < t_nodes);
+
233 if (t_depth[a] > t_depth[b]) {
+
234 lift(&a, t_depth[a] - t_depth[b]);
+
235 }
+
236 if (t_depth[b] > t_depth[a]) {
+
237 lift(&b, t_depth[b] - t_depth[a]);
+
238 }
+
239 if (a == b) {
+
240 return a;
+
241 }
+
242 for (int k = t_maxlift - 1; k >= 0; k--) {
+
243 if (t_par[a][k] != t_par[b][k]) {
+
244 a = t_par[a][k];
+
245 b = t_par[b][k];
+
246 }
+
247 }
+
248 return t_par[a][0];
+
249 }
@@ -635,18 +635,18 @@ template<typename X>
Returns
void
-

Definition at line 200 of file heavy_light_decomposition.cpp.

-
200 {
-
201 for (int k = 0; k < t_maxlift; k++) {
-
202 if (*p == -1) {
-
203 return;
-
204 }
-
205 if (dist & 1) {
-
206 *p = t_par[*p][k];
-
207 }
-
208 dist >>= 1;
-
209 }
-
210 }
+

Definition at line 202 of file heavy_light_decomposition.cpp.

+
202 {
+
203 for (int k = 0; k < t_maxlift; k++) {
+
204 if (*p == -1) {
+
205 return;
+
206 }
+
207 if (dist & 1) {
+
208 *p = t_par[*p][k];
+
209 }
+
210 dist >>= 1;
+
211 }
+
212 }
@@ -684,11 +684,11 @@ template<typename X>
Returns
void
-

Definition at line 175 of file heavy_light_decomposition.cpp.

-
175 {
-
176 assert(static_cast<int>(node_val.size()) == t_nodes);
-
177 t_val = node_val;
-
178 }
+

Definition at line 177 of file heavy_light_decomposition.cpp.

+
177 {
+
178 assert(static_cast<int>(node_val.size()) == t_nodes);
+
179 t_val = node_val;
+
180 }
@@ -717,7 +717,7 @@ template<typename T>
-

Definition at line 93 of file heavy_light_decomposition.cpp.

+

Definition at line 95 of file heavy_light_decomposition.cpp.

@@ -746,7 +746,7 @@ template<typename X>

an adjacency list to stores the tree edges

-

Definition at line 83 of file heavy_light_decomposition.cpp.

+

Definition at line 84 of file heavy_light_decomposition.cpp.

@@ -774,7 +774,7 @@ template<typename X>

a vector to store the depth of a node,

-

Definition at line 88 of file heavy_light_decomposition.cpp.

+

Definition at line 89 of file heavy_light_decomposition.cpp.

@@ -802,7 +802,7 @@ template<typename X>

maximum possible height of the tree

-

Definition at line 85 of file heavy_light_decomposition.cpp.

+

Definition at line 86 of file heavy_light_decomposition.cpp.

@@ -830,7 +830,7 @@ template<typename X>

number of nodes

-

Definition at line 84 of file heavy_light_decomposition.cpp.

+

Definition at line 85 of file heavy_light_decomposition.cpp.

@@ -858,7 +858,7 @@ template<typename X>

a matrix to store every node's 2^kth parent

-

Definition at line 87 of file heavy_light_decomposition.cpp.

+

Definition at line 88 of file heavy_light_decomposition.cpp.

@@ -886,7 +886,7 @@ template<typename X>

the root of the tree

-

Definition at line 91 of file heavy_light_decomposition.cpp.

+

Definition at line 92 of file heavy_light_decomposition.cpp.

@@ -914,7 +914,7 @@ template<typename X>

a vector to store the subtree size rooted at node

-

Definition at line 89 of file heavy_light_decomposition.cpp.

+

Definition at line 90 of file heavy_light_decomposition.cpp.

@@ -942,7 +942,7 @@ template<typename X>

values of nodes

-

Definition at line 92 of file heavy_light_decomposition.cpp.

+

Definition at line 93 of file heavy_light_decomposition.cpp.

diff --git a/d1/de9/integral__approximation_8cpp.html b/d1/de9/integral__approximation_8cpp.html index 2caff0a63..c65ba3e4c 100644 --- a/d1/de9/integral__approximation_8cpp.html +++ b/d1/de9/integral__approximation_8cpp.html @@ -259,9 +259,9 @@ Functions
125 math::test_eval(test_5, 0.1781477117815607, .001);
126 std::cout << "Test 5 Passed!" << std::endl;
127}
-
test_1
static void test_1()
Definition heavy_light_decomposition.cpp:505
-
test_2
static void test_2()
Definition heavy_light_decomposition.cpp:549
-
test_3
static void test_3()
Definition heavy_light_decomposition.cpp:592
+
test_1
static void test_1()
Definition heavy_light_decomposition.cpp:511
+
test_2
static void test_2()
Definition heavy_light_decomposition.cpp:555
+
test_3
static void test_3()
Definition heavy_light_decomposition.cpp:599
lru_tests::log
void log(T msg)
A function to print given message on console.
Definition lru_cache.cpp:149
ciphers::elliptic_curve_key_exchange::exp
uint256_t exp(uint256_t number, uint256_t power, const uint256_t &mod)
This function calculates number raised to exponent power under modulo mod using Modular Exponentiatio...
Definition elliptic_curve_key_exchange.cpp:78
math::test_eval
void test_eval(double approx, double expected, double threshold)
Wrapper to evaluate if the approximated value is within .XX% threshold of the exact value.
Definition integral_approximation.cpp:62
diff --git a/d1/de9/integral__approximation_8cpp_source.html b/d1/de9/integral__approximation_8cpp_source.html index bcf57372e..faf1672d4 100644 --- a/d1/de9/integral__approximation_8cpp_source.html +++ b/d1/de9/integral__approximation_8cpp_source.html @@ -209,9 +209,9 @@ $(function(){initNavTree('d1/de9/integral__approximation_8cpp_source.html','../.
136}
test
static void test()
Self-test implementations.
Definition generate_parentheses.cpp:82
-
test_1
static void test_1()
Definition heavy_light_decomposition.cpp:505
-
test_2
static void test_2()
Definition heavy_light_decomposition.cpp:549
-
test_3
static void test_3()
Definition heavy_light_decomposition.cpp:592
+
test_1
static void test_1()
Definition heavy_light_decomposition.cpp:511
+
test_2
static void test_2()
Definition heavy_light_decomposition.cpp:555
+
test_3
static void test_3()
Definition heavy_light_decomposition.cpp:599
main
int main()
Main function.
Definition integral_approximation.cpp:133
lru_tests::log
void log(T msg)
A function to print given message on console.
Definition lru_cache.cpp:149
ciphers::elliptic_curve_key_exchange::exp
uint256_t exp(uint256_t number, uint256_t power, const uint256_t &mod)
This function calculates number raised to exponent power under modulo mod using Modular Exponentiatio...
Definition elliptic_curve_key_exchange.cpp:78
diff --git a/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.map b/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.map index 2a231a6c4..760e9b4ae 100644 --- a/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.map +++ b/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.map @@ -1,9 +1,11 @@ - - - - - - - + + + + + + + + + diff --git a/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.md5 b/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.md5 index f2e10e824..e636ba578 100644 --- a/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.md5 +++ b/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.md5 @@ -1 +1 @@ -10475f54a4456f16d8246f63621d065f \ No newline at end of file +8d9398fac901fe42ddd8eea6074882d8 \ No newline at end of file diff --git a/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.svg b/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.svg index a3e69428a..dd7ba610b 100644 --- a/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.svg +++ b/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl.svg @@ -4,8 +4,8 @@ - + @@ -23,9 +23,9 @@ Node1 - -range_queries/persistent -_seg_tree_lazy_prop.cpp + +range_queries/persistent +_seg_tree_lazy_prop.cpp @@ -33,8 +33,8 @@ Node2 - -iostream + +cstdint @@ -42,8 +42,8 @@ Node1->Node2 - - + + @@ -51,8 +51,8 @@ Node3 - -memory + +iostream @@ -60,8 +60,8 @@ Node1->Node3 - - + + @@ -69,8 +69,8 @@ Node4 - -vector + +memory @@ -78,8 +78,26 @@ Node1->Node4 - - + + + + + + + +Node5 + + +vector + + + + + +Node1->Node5 + + + diff --git a/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl_org.svg b/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl_org.svg index 6efab642b..21a725dbf 100644 --- a/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl_org.svg +++ b/d2/d86/persistent__seg__tree__lazy__prop_8cpp__incl_org.svg @@ -4,17 +4,17 @@ - + range_queries/persistent_seg_tree_lazy_prop.cpp Node1 - -range_queries/persistent -_seg_tree_lazy_prop.cpp + +range_queries/persistent +_seg_tree_lazy_prop.cpp @@ -22,8 +22,8 @@ Node2 - -iostream + +cstdint @@ -31,8 +31,8 @@ Node1->Node2 - - + + @@ -40,8 +40,8 @@ Node3 - -memory + +iostream @@ -49,8 +49,8 @@ Node1->Node3 - - + + @@ -58,8 +58,8 @@ Node4 - -vector + +memory @@ -67,8 +67,26 @@ Node1->Node4 - - + + + + + + + +Node5 + + +vector + + + + + +Node1->Node5 + + + diff --git a/d2/d8a/classrange__queries_1_1heavy__light__decomposition_1_1_h_l_d.html b/d2/d8a/classrange__queries_1_1heavy__light__decomposition_1_1_h_l_d.html index 730d41976..418deb3ce 100644 --- a/d2/d8a/classrange__queries_1_1heavy__light__decomposition_1_1_h_l_d.html +++ b/d2/d8a/classrange__queries_1_1heavy__light__decomposition_1_1_h_l_d.html @@ -190,7 +190,7 @@ class range_queries::heavy_light_decomposition::HLD< X >

The Heavy -

Definition at line 336 of file heavy_light_decomposition.cpp.

+

Definition at line 342 of file heavy_light_decomposition.cpp.

Constructor & Destructor Documentation

◆ HLD()

@@ -225,22 +225,22 @@ template<typename X> -

Definition at line 409 of file heavy_light_decomposition.cpp.

-
435 : Tree<X>(nodes), SG<X>(nodes) {
-
436 /* Initialization and resize vectors */
-
437 label = 0;
-
438 h_label.assign(Tree<X>::t_nodes, -1);
-
439 h_heavychlid.assign(Tree<X>::t_nodes, -1);
-
440 h_parent.resize(Tree<X>::t_nodes);
-
441 iota(h_parent.begin(), h_parent.end(), 0);
-
442 }
-
The Heavy-Light Decomposition class.
-
std::vector< int > h_parent
stores the top of the heavy chain from a node
-
int label
utility member to assign labels in dfs_labels()
-
std::vector< int > h_heavychlid
stores the heavy child of a node
-
std::vector< int > h_label
stores the label of a node
-
SG(int size)
Class parameterized constructor. Resizes the and initilizes the data members.
-
Tree(int nodes)
Class parameterized constructor, resizes the and initializes the data members.
+

Definition at line 441 of file heavy_light_decomposition.cpp.

+
441 : Tree<X>(nodes), SG<X>(nodes) {
+
442 /* Initialization and resize vectors */
+
443 label = 0;
+
444 h_label.assign(Tree<X>::t_nodes, -1);
+
445 h_heavychlid.assign(Tree<X>::t_nodes, -1);
+ +
447 iota(h_parent.begin(), h_parent.end(), 0);
+
448 }
+ +
std::vector< int > h_parent
stores the top of the heavy chain from a node
+
int label
utility member to assign labels in dfs_labels()
+
std::vector< int > h_heavychlid
stores the heavy child of a node
+
std::vector< int > h_label
stores the label of a node
+
SG(int size)
Class parameterized constructor. Resizes the and initilizes the data members.
+
Tree(int nodes)
Class parameterized constructor, resizes the and initializes the data members.
@@ -284,27 +284,27 @@ template<typename X>
Returns
the sum of ndoe values in the simple path from a to b
-

Definition at line 409 of file heavy_light_decomposition.cpp.

-
409 {
-
410 X ret = SG<X>::sret_init;
-
411 if (Tree<X>::t_depth[a] < Tree<X>::t_depth[b]) {
-
412 std::swap(a, b);
-
413 }
-
414 while (Tree<X>::t_depth[a] >= Tree<X>::t_depth[b]) {
-
415 int l = h_label[h_parent[a]];
-
416 int r = h_label[a];
-
417 if (Tree<X>::t_depth[h_parent[a]] < Tree<X>::t_depth[b]) {
-
418 l += Tree<X>::t_depth[b] - Tree<X>::t_depth[h_parent[a]];
-
419 }
-
420 ret = SG<X>::combine(ret, SG<X>::query(l, r));
-
421 a = Tree<X>::t_par[h_parent[a]][0];
-
422 if (a == -1) {
-
423 break;
-
424 }
-
425 }
-
426 return ret;
-
427 }
-
X combine(X lhs, X rhs)
Function that specifies the type of operation involved when segments are combined.
+

Definition at line 415 of file heavy_light_decomposition.cpp.

+
415 {
+ + +
418 std::swap(a, b);
+
419 }
+
420 while (Tree<X>::t_depth[a] >= Tree<X>::t_depth[b]) {
+
421 int l = h_label[h_parent[a]];
+
422 int r = h_label[a];
+ + +
425 }
+ +
427 a = Tree<X>::t_par[h_parent[a]][0];
+
428 if (a == -1) {
+
429 break;
+
430 }
+
431 }
+
432 return ret;
+
433 }
+
X combine(X lhs, X rhs)
Function that specifies the type of operation involved when segments are combined.
@@ -347,21 +347,21 @@ template<typename X>
Returns
void
-

Definition at line 350 of file heavy_light_decomposition.cpp.

-
350 {
-
351 int hc_size = -1, hc_id = -1;
-
352 for (const int &v : Tree<X>::t_adj[u]) {
-
353 if (v ^ p) {
-
354 dfs_hc(v, u);
-
355 if (Tree<X>::t_size[v] > hc_size) {
-
356 hc_size = Tree<X>::t_size[v];
-
357 hc_id = v;
-
358 }
-
359 }
-
360 }
-
361 h_heavychlid[u] = hc_id;
-
362 }
-
void dfs_hc(int u, int p=-1)
Utility function to assign heavy child to each node (-1 for a leaf node)
+

Definition at line 356 of file heavy_light_decomposition.cpp.

+
356 {
+
357 int hc_size = -1, hc_id = -1;
+
358 for (const int &v : Tree<X>::t_adj[u]) {
+
359 if (v ^ p) {
+
360 dfs_hc(v, u);
+
361 if (Tree<X>::t_size[v] > hc_size) {
+ +
363 hc_id = v;
+
364 }
+
365 }
+
366 }
+ +
368 }
+
void dfs_hc(int u, int p=-1)
Utility function to assign heavy child to each node (-1 for a leaf node)
@@ -404,19 +404,19 @@ template<typename X>
Returns
void
-

Definition at line 390 of file heavy_light_decomposition.cpp.

-
390 {
-
391 h_label[u] = label++;
-
392 if (h_heavychlid[u] != -1) {
-
393 dfs_labels(h_heavychlid[u], u);
-
394 }
-
395 for (const int &v : Tree<X>::t_adj[u]) {
-
396 if (v ^ p and v ^ h_heavychlid[u]) {
-
397 dfs_labels(v, u);
-
398 }
-
399 }
-
400 }
-
void dfs_labels(int u, int p=-1)
Utility function to lable the nodes so that heavy chains have a contigous lable.
+

Definition at line 396 of file heavy_light_decomposition.cpp.

+
396 {
+
397 h_label[u] = label++;
+
398 if (h_heavychlid[u] != -1) {
+ +
400 }
+
401 for (const int &v : Tree<X>::t_adj[u]) {
+
402 if (v ^ p and v ^ h_heavychlid[u]) {
+
403 dfs_labels(v, u);
+
404 }
+
405 }
+
406 }
+
void dfs_labels(int u, int p=-1)
Utility function to lable the nodes so that heavy chains have a contigous lable.
@@ -459,19 +459,19 @@ template<typename X>
Returns
void
-

Definition at line 371 of file heavy_light_decomposition.cpp.

-
371 {
-
372 if (h_heavychlid[u] != -1) {
-
373 h_parent[h_heavychlid[u]] = h_parent[u];
-
374 dfs_par(h_heavychlid[u], u);
-
375 }
-
376 for (const int &v : Tree<X>::t_adj[u]) {
-
377 if (v ^ p and v ^ h_heavychlid[u]) {
-
378 dfs_par(v, u);
-
379 }
-
380 }
-
381 }
-
void dfs_par(int u, int p=-1)
Utility function to assign highest parent that can be reached though heavy chains.
+

Definition at line 377 of file heavy_light_decomposition.cpp.

+
377 {
+
378 if (h_heavychlid[u] != -1) {
+ + +
381 }
+
382 for (const int &v : Tree<X>::t_adj[u]) {
+
383 if (v ^ p and v ^ h_heavychlid[u]) {
+
384 dfs_par(v, u);
+
385 }
+
386 }
+
387 }
+
void dfs_par(int u, int p=-1)
Utility function to assign highest parent that can be reached though heavy chains.
@@ -503,26 +503,26 @@ template<typename X>

This function must be called after the tree adjacency list and node values are populated The function initializes the required parametes, and populates the segment tree.

Returns
void
-

Definition at line 450 of file heavy_light_decomposition.cpp.

-
450 {
-
451 Tree<X>::init();
-
452
-
453 // Fill the heavy child, greatest parent, and labels
-
454 label = 0;
-
455 dfs_hc(Tree<X>::t_root);
-
456 dfs_par(Tree<X>::t_root);
-
457 dfs_labels(Tree<X>::t_root);
+

Definition at line 456 of file heavy_light_decomposition.cpp.

+
456 {
+
458
-
459 // Segment Tree Initialization
-
460 for (int i = 0; i < Tree<X>::t_nodes; i++) {
- -
462 }
-
463 for (int i = Tree<X>::t_nodes - 1; i > 0; i--) {
- -
465 SG<X>::combine(SG<X>::s_tree[i << 1], SG<X>::s_tree[i << 1 | 1]);
-
466 }
-
467 }
-
void init()
This function must be called after the tree adjacency list and node values are populated The function...
+
459 // Fill the heavy child, greatest parent, and labels
+
460 label = 0;
+ + + +
464
+
465 // Segment Tree Initialization
+
466 for (int i = 0; i < Tree<X>::t_nodes; i++) {
+ +
468 }
+
469 for (int i = Tree<X>::t_nodes - 1; i > 0; i--) {
+ +
471 SG<X>::s_tree[i << 1 | 1]);
+
472 }
+
473 }
+
void init()
This function must be called after the tree adjacency list and node values are populated The function...
@@ -565,17 +565,17 @@ template<typename X>
Returns
the sum of node values in the simple path from a to b
-

Definition at line 489 of file heavy_light_decomposition.cpp.

-
489 {
-
490 int lc = Tree<X>::lca(a, b);
-
491 X ret = SG<X>::sret_init;
-
492 assert(lc != -1);
-
493 ret += chain_query(a, lc);
-
494 ret += chain_query(b, lc);
-
495 return ret - Tree<X>::t_val[lc];
-
496 }
-
X chain_query(int a, int b)
Utility function to break down a path query into two chain queries.
-
int lca(int a, int b)
The function returns the least common ancestor of two nodes.
+

Definition at line 495 of file heavy_light_decomposition.cpp.

+
495 {
+
496 int lc = Tree<X>::lca(a, b);
+ +
498 assert(lc != -1);
+
499 ret += chain_query(a, lc);
+
500 ret += chain_query(b, lc);
+
501 return ret - Tree<X>::t_val[lc];
+
502 }
+
X chain_query(int a, int b)
Utility function to break down a path query into two chain queries.
+
int lca(int a, int b)
The function returns the least common ancestor of two nodes.
@@ -618,13 +618,13 @@ template<typename X>
Returns
void
-

Definition at line 475 of file heavy_light_decomposition.cpp.

-
475 {
-
476 X diff = val - Tree<X>::t_val[node];
-
477 SG<X>::update(h_label[node], diff);
-
478 Tree<X>::t_val[node] = val;
-
479 }
-
void update(int p, X v)
Update the value at a node.
+

Definition at line 481 of file heavy_light_decomposition.cpp.

+
481 {
+ + + +
485 }
+
void update(int p, X v)
Update the value at a node.
@@ -653,7 +653,7 @@ template<typename X>

stores the heavy child of a node

-

Definition at line 340 of file heavy_light_decomposition.cpp.

+

Definition at line 346 of file heavy_light_decomposition.cpp.

@@ -681,7 +681,7 @@ template<typename X>

stores the label of a node

-

Definition at line 339 of file heavy_light_decomposition.cpp.

+

Definition at line 345 of file heavy_light_decomposition.cpp.

@@ -709,7 +709,7 @@ template<typename X>

stores the top of the heavy chain from a node

-

Definition at line 341 of file heavy_light_decomposition.cpp.

+

Definition at line 347 of file heavy_light_decomposition.cpp.

@@ -737,7 +737,7 @@ template<typename X>

utility member to assign labels in dfs_labels()

-

Definition at line 338 of file heavy_light_decomposition.cpp.

+

Definition at line 344 of file heavy_light_decomposition.cpp.

diff --git a/d2/de9/heavy__light__decomposition_8cpp.html b/d2/de9/heavy__light__decomposition_8cpp.html index 5a6c71b8e..a9265670b 100644 --- a/d2/de9/heavy__light__decomposition_8cpp.html +++ b/d2/de9/heavy__light__decomposition_8cpp.html @@ -197,16 +197,16 @@ Functions

Main function

-

Definition at line 634 of file heavy_light_decomposition.cpp.

-
634 {
-
635 test_1();
-
636 test_2();
-
637 test_3();
-
638 return 0;
-
639}
-
static void test_1()
-
static void test_2()
-
static void test_3()
+

Definition at line 641 of file heavy_light_decomposition.cpp.

+
641 {
+
642 test_1();
+
643 test_2();
+
644 test_3();
+
645 return 0;
+
646}
+
static void test_1()
+
static void test_2()
+
static void test_3()
@@ -234,47 +234,47 @@ Functions

Test implementations

Returns
none
-

Definition at line 505 of file heavy_light_decomposition.cpp.

-
505 {
-
506 std::cout << "Test 1:\n";
-
507
-
508 // Test details
-
509 int n = 5;
-
510 std::vector<int64_t> node_values = {4, 2, 5, 2, 1};
-
511 std::vector<std::vector<int>> edges = {{1, 2}, {1, 3}, {3, 4}, {3, 5}};
-
512 std::vector<std::vector<int>> queries = {
-
513 {2, 1, 4},
-
514 {1, 3, 2},
-
515 {2, 1, 4},
-
516 };
-
517 std::vector<int> expected_result = {11, 8};
-
518 std::vector<int> code_result;
-
519
- -
521 hld.set_node_val(node_values);
-
522 for (int i = 0; i < n - 1; i++) {
-
523 int u = edges[i][0], v = edges[i][1];
-
524 hld.add_edge(u - 1, v - 1);
-
525 }
-
526 hld.init();
-
527 for (const auto &q : queries) {
-
528 int type = q[0];
-
529 if (type == 1) {
-
530 int p = q[1], x = q[2];
-
531 hld.update(p - 1, x);
-
532 } else if (type == 2) {
-
533 int a = q[1], b = q[2];
-
534 code_result.push_back(hld.query(a - 1, b - 1));
-
535 } else {
-
536 continue;
-
537 }
-
538 }
-
539 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
-
540 assert(expected_result[i] == code_result[i]);
-
541 }
-
542 std::cout << "\nTest 1 passed!\n";
-
543}
- +

Definition at line 511 of file heavy_light_decomposition.cpp.

+
511 {
+
512 std::cout << "Test 1:\n";
+
513
+
514 // Test details
+
515 int n = 5;
+
516 std::vector<int64_t> node_values = {4, 2, 5, 2, 1};
+
517 std::vector<std::vector<int>> edges = {{1, 2}, {1, 3}, {3, 4}, {3, 5}};
+
518 std::vector<std::vector<int>> queries = {
+
519 {2, 1, 4},
+
520 {1, 3, 2},
+
521 {2, 1, 4},
+
522 };
+
523 std::vector<int> expected_result = {11, 8};
+
524 std::vector<int> code_result;
+
525
+ +
527 hld.set_node_val(node_values);
+
528 for (int i = 0; i < n - 1; i++) {
+
529 int u = edges[i][0], v = edges[i][1];
+
530 hld.add_edge(u - 1, v - 1);
+
531 }
+
532 hld.init();
+
533 for (const auto &q : queries) {
+
534 int type = q[0];
+
535 if (type == 1) {
+
536 int p = q[1], x = q[2];
+
537 hld.update(p - 1, x);
+
538 } else if (type == 2) {
+
539 int a = q[1], b = q[2];
+
540 code_result.push_back(hld.query(a - 1, b - 1));
+
541 } else {
+
542 continue;
+
543 }
+
544 }
+
545 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
+
546 assert(expected_result[i] == code_result[i]);
+
547 }
+
548 std::cout << "\nTest 1 passed!\n";
+
549}
+
@@ -302,45 +302,46 @@ Functions

Second test implementations

Returns
void
-

Definition at line 549 of file heavy_light_decomposition.cpp.

-
549 {
-
550 std::cout << "Test 2:\n";
-
551
-
552 // Test details (Bamboo)
-
553 int n = 10;
-
554 std::vector<int64_t> node_values = {1, 8, 6, 8, 6, 2, 9, 2, 3, 2};
-
555 std::vector<std::vector<int>> edges = {
-
556 {10, 5}, {6, 2}, {10, 7}, {5, 2}, {3, 9}, {8, 3}, {1, 4}, {6, 4}, {8, 7}};
-
557 std::vector<std::vector<int>> queries = {
-
558 {2, 1, 10}, {2, 1, 6}, {1, 3, 4}, {2, 1, 9}, {1, 5, 3},
-
559 {1, 7, 8}, {2, 1, 4}, {2, 1, 8}, {1, 1, 4}, {1, 2, 7}};
-
560 std::vector<int> expected_result = {27, 11, 45, 9, 34};
-
561 std::vector<int> code_result;
-
562
- -
564 hld.set_node_val(node_values);
-
565 for (int i = 0; i < n - 1; i++) {
-
566 int u = edges[i][0], v = edges[i][1];
-
567 hld.add_edge(u - 1, v - 1);
-
568 }
-
569 hld.init();
-
570 for (const auto &q : queries) {
-
571 int type = q[0];
-
572 if (type == 1) {
-
573 int p = q[1], x = q[2];
-
574 hld.update(p - 1, x);
-
575 } else if (type == 2) {
-
576 int a = q[1], b = q[2];
-
577 code_result.push_back(hld.query(a - 1, b - 1));
-
578 } else {
-
579 continue;
-
580 }
-
581 }
-
582 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
-
583 assert(expected_result[i] == code_result[i]);
-
584 }
-
585 std::cout << "\nTest2 passed!\n";
-
586}
+

Definition at line 555 of file heavy_light_decomposition.cpp.

+
555 {
+
556 std::cout << "Test 2:\n";
+
557
+
558 // Test details (Bamboo)
+
559 int n = 10;
+
560 std::vector<int64_t> node_values = {1, 8, 6, 8, 6, 2, 9, 2, 3, 2};
+
561 std::vector<std::vector<int>> edges = {{10, 5}, {6, 2}, {10, 7},
+
562 {5, 2}, {3, 9}, {8, 3},
+
563 {1, 4}, {6, 4}, {8, 7}};
+
564 std::vector<std::vector<int>> queries = {
+
565 {2, 1, 10}, {2, 1, 6}, {1, 3, 4}, {2, 1, 9}, {1, 5, 3},
+
566 {1, 7, 8}, {2, 1, 4}, {2, 1, 8}, {1, 1, 4}, {1, 2, 7}};
+
567 std::vector<int> expected_result = {27, 11, 45, 9, 34};
+
568 std::vector<int> code_result;
+
569
+ +
571 hld.set_node_val(node_values);
+
572 for (int i = 0; i < n - 1; i++) {
+
573 int u = edges[i][0], v = edges[i][1];
+
574 hld.add_edge(u - 1, v - 1);
+
575 }
+
576 hld.init();
+
577 for (const auto &q : queries) {
+
578 int type = q[0];
+
579 if (type == 1) {
+
580 int p = q[1], x = q[2];
+
581 hld.update(p - 1, x);
+
582 } else if (type == 2) {
+
583 int a = q[1], b = q[2];
+
584 code_result.push_back(hld.query(a - 1, b - 1));
+
585 } else {
+
586 continue;
+
587 }
+
588 }
+
589 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
+
590 assert(expected_result[i] == code_result[i]);
+
591 }
+
592 std::cout << "\nTest2 passed!\n";
+
593}
@@ -368,45 +369,45 @@ Functions

Third test implementations

Returns
void
-

Definition at line 592 of file heavy_light_decomposition.cpp.

-
592 {
-
593 std::cout << "Test 3:\n";
-
594
-
595 // Test details
-
596 int n = 8;
-
597 std::vector<int64_t> node_values = {1, 8, 6, 8, 6, 2, 9, 2};
-
598 std::vector<std::vector<int>> edges = {{1, 2}, {2, 3}, {3, 4}, {1, 5},
-
599 {6, 3}, {7, 5}, {8, 7}};
-
600 std::vector<std::vector<int>> queries = {
-
601 {2, 6, 8}, {2, 3, 6}, {1, 3, 4}, {2, 7, 1}, {1, 5, 3},
-
602 {1, 7, 8}, {2, 6, 4}, {2, 7, 8}, {1, 1, 4}, {1, 2, 7}};
-
603 std::vector<int> expected_result = {34, 8, 16, 14, 10};
-
604 std::vector<int> code_result;
-
605
- -
607 hld.set_node_val(node_values);
-
608 for (int i = 0; i < n - 1; i++) {
-
609 int u = edges[i][0], v = edges[i][1];
-
610 hld.add_edge(u - 1, v - 1);
-
611 }
-
612 hld.init();
-
613 for (const auto &q : queries) {
-
614 int type = q[0];
-
615 if (type == 1) {
-
616 int p = q[1], x = q[2];
-
617 hld.update(p - 1, x);
-
618 } else if (type == 2) {
-
619 int a = q[1], b = q[2];
-
620 code_result.push_back(hld.query(a - 1, b - 1));
-
621 } else {
-
622 continue;
-
623 }
-
624 }
-
625 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
-
626 assert(expected_result[i] == code_result[i]);
-
627 }
-
628 std::cout << "\nTest3 passed!\n";
-
629}
+

Definition at line 599 of file heavy_light_decomposition.cpp.

+
599 {
+
600 std::cout << "Test 3:\n";
+
601
+
602 // Test details
+
603 int n = 8;
+
604 std::vector<int64_t> node_values = {1, 8, 6, 8, 6, 2, 9, 2};
+
605 std::vector<std::vector<int>> edges = {{1, 2}, {2, 3}, {3, 4}, {1, 5},
+
606 {6, 3}, {7, 5}, {8, 7}};
+
607 std::vector<std::vector<int>> queries = {
+
608 {2, 6, 8}, {2, 3, 6}, {1, 3, 4}, {2, 7, 1}, {1, 5, 3},
+
609 {1, 7, 8}, {2, 6, 4}, {2, 7, 8}, {1, 1, 4}, {1, 2, 7}};
+
610 std::vector<int> expected_result = {34, 8, 16, 14, 10};
+
611 std::vector<int> code_result;
+
612
+ +
614 hld.set_node_val(node_values);
+
615 for (int i = 0; i < n - 1; i++) {
+
616 int u = edges[i][0], v = edges[i][1];
+
617 hld.add_edge(u - 1, v - 1);
+
618 }
+
619 hld.init();
+
620 for (const auto &q : queries) {
+
621 int type = q[0];
+
622 if (type == 1) {
+
623 int p = q[1], x = q[2];
+
624 hld.update(p - 1, x);
+
625 } else if (type == 2) {
+
626 int a = q[1], b = q[2];
+
627 code_result.push_back(hld.query(a - 1, b - 1));
+
628 } else {
+
629 continue;
+
630 }
+
631 }
+
632 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
+
633 assert(expected_result[i] == code_result[i]);
+
634 }
+
635 std::cout << "\nTest3 passed!\n";
+
636}
diff --git a/d2/de9/heavy__light__decomposition_8cpp_source.html b/d2/de9/heavy__light__decomposition_8cpp_source.html index 6502ddf5a..5c4cc4ce9 100644 --- a/d2/de9/heavy__light__decomposition_8cpp_source.html +++ b/d2/de9/heavy__light__decomposition_8cpp_source.html @@ -130,488 +130,494 @@ $(function(){initNavTree('d2/de9/heavy__light__decomposition_8cpp_source.html','
53
58namespace range_queries {
63namespace heavy_light_decomposition {
-
-
78template <typename X> class Tree {
-
79 //
-
80
-
81private:
-
82 std::vector<std::list<int>>
-
83 t_adj;
-
84 const int t_nodes,
-
85 t_maxlift;
-
86 std::vector<std::vector<int>>
-
87 t_par;
-
88 std::vector<int> t_depth,
-
89 t_size;
-
90
-
91 int t_root;
-
92 std::vector<X> t_val;
-
93 template <typename T> friend class HLD;
-
94
-
-
101 void dfs_size(int u, int p = -1) {
-
102 for (const int &v : t_adj[u]) {
-
103 if (v ^ p) {
-
104 dfs_size(v, u);
-
105 t_size[u] += t_size[v];
-
106 }
-
107 }
-
108 }
+
78template <typename X>
+
+
79class Tree {
+
80 //
+
81
+
82 private:
+
83 std::vector<std::list<int>>
+ +
85 const int t_nodes,
+ +
87 std::vector<std::vector<int>>
+ +
89 std::vector<int> t_depth,
+ +
91
+
92 int t_root;
+
93 std::vector<X> t_val;
+
94 template <typename T>
+
95 friend class HLD;
+
96
+
+
103 void dfs_size(int u, int p = -1) {
+
104 for (const int &v : t_adj[u]) {
+
105 if (v ^ p) {
+
106 dfs_size(v, u);
+
107 t_size[u] += t_size[v];
+
108 }
+
109 }
+
110 }
-
109
-
-
116 void dfs_lca(int u, int p = -1) {
-
117 t_par[u][0] = p;
-
118 if (p != -1) {
-
119 t_depth[u] = 1 + t_depth[p];
-
120 }
-
121 for (int k = 1; k < t_maxlift; k++) {
-
122 if (t_par[u][k - 1] != -1) {
-
123 t_par[u][k] = t_par[t_par[u][k - 1]][k - 1];
-
124 }
-
125 }
-
126
-
127 for (const int &v : t_adj[u]) {
-
128 if (v ^ p) {
-
129 dfs_lca(v, u);
-
130 }
-
131 }
-
132 }
+
111
+
+
118 void dfs_lca(int u, int p = -1) {
+
119 t_par[u][0] = p;
+
120 if (p != -1) {
+
121 t_depth[u] = 1 + t_depth[p];
+
122 }
+
123 for (int k = 1; k < t_maxlift; k++) {
+
124 if (t_par[u][k - 1] != -1) {
+
125 t_par[u][k] = t_par[t_par[u][k - 1]][k - 1];
+
126 }
+
127 }
+
128
+
129 for (const int &v : t_adj[u]) {
+
130 if (v ^ p) {
+
131 dfs_lca(v, u);
+
132 }
+
133 }
+
134 }
-
133
-
134public:
-
-
140 explicit Tree(int nodes)
-
141 : t_nodes(nodes), t_maxlift(static_cast<int>(floor(log2(nodes))) + 1) {
-
142 /* Initialize and resize all the vectors */
-
143 t_root = 0; /* Default */
-
144 t_adj.resize(t_nodes);
-
145 t_par.assign(t_nodes, std::vector<int>(t_maxlift, -1));
-
146 t_depth.assign(t_nodes, 0);
-
147 t_size.assign(t_nodes, 1);
-
148 t_val.resize(t_nodes);
-
149 }
+
135
+
136 public:
+
+
142 explicit Tree(int nodes)
+
143 : t_nodes(nodes), t_maxlift(static_cast<int>(floor(log2(nodes))) + 1) {
+
144 /* Initialize and resize all the vectors */
+
145 t_root = 0; /* Default */
+
146 t_adj.resize(t_nodes);
+
147 t_par.assign(t_nodes, std::vector<int>(t_maxlift, -1));
+
148 t_depth.assign(t_nodes, 0);
+
149 t_size.assign(t_nodes, 1);
+
150 t_val.resize(t_nodes);
+
151 }
-
150
-
-
157 void add_edge(const int u, const int v) {
-
158 t_adj[u].push_back(v);
-
159 t_adj[v].push_back(u);
-
160 }
+
152
+
+
159 void add_edge(const int u, const int v) {
+
160 t_adj[u].push_back(v);
+
161 t_adj[v].push_back(u);
+
162 }
-
161
-
167 void change_root(int new_root) { t_root = new_root; }
-
168
-
-
175 void set_node_val(const std::vector<X> &node_val) {
-
176 assert(static_cast<int>(node_val.size()) == t_nodes);
-
177 t_val = node_val;
-
178 }
+
163
+
169 void change_root(int new_root) { t_root = new_root; }
+
170
+
+
177 void set_node_val(const std::vector<X> &node_val) {
+
178 assert(static_cast<int>(node_val.size()) == t_nodes);
+
179 t_val = node_val;
+
180 }
-
179
-
-
186 void init() {
-
187 assert(t_nodes > 0);
- - -
190 }
+
181
+
+
188 void init() {
+
189 assert(t_nodes > 0);
+ + +
192 }
-
191
-
-
200 void lift(int *const p, int dist) {
-
201 for (int k = 0; k < t_maxlift; k++) {
-
202 if (*p == -1) {
-
203 return;
-
204 }
-
205 if (dist & 1) {
-
206 *p = t_par[*p][k];
-
207 }
-
208 dist >>= 1;
-
209 }
-
210 }
+
193
+
+
202 void lift(int *const p, int dist) {
+
203 for (int k = 0; k < t_maxlift; k++) {
+
204 if (*p == -1) {
+
205 return;
+
206 }
+
207 if (dist & 1) {
+
208 *p = t_par[*p][k];
+
209 }
+
210 dist >>= 1;
+
211 }
+
212 }
-
211
-
-
218 int kth_ancestor(int p, const int &dist) {
-
219 lift(&p, dist);
-
220 return p;
-
221 }
+
213
+
+
220 int kth_ancestor(int p, const int &dist) {
+
221 lift(&p, dist);
+
222 return p;
+
223 }
-
222
-
-
229 int lca(int a, int b) {
-
230 assert(a >= 0 and b >= 0 and a < t_nodes and b < t_nodes);
-
231 if (t_depth[a] > t_depth[b]) {
-
232 lift(&a, t_depth[a] - t_depth[b]);
-
233 }
-
234 if (t_depth[b] > t_depth[a]) {
-
235 lift(&b, t_depth[b] - t_depth[a]);
-
236 }
-
237 if (a == b) {
-
238 return a;
-
239 }
-
240 for (int k = t_maxlift - 1; k >= 0; k--) {
-
241 if (t_par[a][k] != t_par[b][k]) {
-
242 a = t_par[a][k];
-
243 b = t_par[b][k];
-
244 }
-
245 }
-
246 return t_par[a][0];
-
247 }
+
224
+
+
231 int lca(int a, int b) {
+
232 assert(a >= 0 and b >= 0 and a < t_nodes and b < t_nodes);
+
233 if (t_depth[a] > t_depth[b]) {
+
234 lift(&a, t_depth[a] - t_depth[b]);
+
235 }
+
236 if (t_depth[b] > t_depth[a]) {
+
237 lift(&b, t_depth[b] - t_depth[a]);
+
238 }
+
239 if (a == b) {
+
240 return a;
+
241 }
+
242 for (int k = t_maxlift - 1; k >= 0; k--) {
+
243 if (t_par[a][k] != t_par[b][k]) {
+
244 a = t_par[a][k];
+
245 b = t_par[b][k];
+
246 }
+
247 }
+
248 return t_par[a][0];
+
249 }
-
248};
+
250};
-
249
-
-
254template <typename X> class SG {
-
255private:
-
261
-
262 std::vector<X> s_tree;
-
263 int s_size;
-
264 X sret_init = 0;
-
265 template <typename T> friend class HLD;
-
266
-
274 X combine(X lhs, X rhs) { return lhs + rhs; }
-
275
-
-
282 explicit SG(int size) {
-
283 s_size = size;
-
284 s_tree.assign(2 * s_size, 0ll);
-
285 }
+
251
+
256template <typename X>
+
+
257class SG {
+
258 private:
+
264
+
265 std::vector<X> s_tree;
+
266 int s_size;
+
267 X sret_init = 0;
+
268 template <typename T>
+
269 friend class HLD;
+
270
+
278 X combine(X lhs, X rhs) { return lhs + rhs; }
+
279
+
+
286 explicit SG(int size) {
+
287 s_size = size;
+
288 s_tree.assign(2 * s_size, 0ll);
+
289 }
-
286
-
-
293 void update(int p, X v) {
-
294 for (p += s_size; p > 0; p >>= 1) {
-
295 s_tree[p] += v;
-
296 }
-
297 }
+
290
+
+
297 void update(int p, X v) {
+
298 for (p += s_size; p > 0; p >>= 1) {
+
299 s_tree[p] += v;
+
300 }
+
301 }
-
298
-
-
305 X query(int l, int r) {
-
306 X lhs = sret_init, rhs = sret_init;
-
307 for (l += s_size, r += s_size + 1; l < r; l >>= 1, r >>= 1) {
-
308 if (l & 1) {
-
309 lhs = combine(lhs, s_tree[l++]);
-
310 }
-
311 if (r & 1) {
-
312 rhs = combine(s_tree[--r], rhs);
-
313 }
-
314 }
-
315 return combine(lhs, rhs);
-
316 }
+
302
+
+
309 X query(int l, int r) {
+
310 X lhs = sret_init, rhs = sret_init;
+
311 for (l += s_size, r += s_size + 1; l < r; l >>= 1, r >>= 1) {
+
312 if (l & 1) {
+
313 lhs = combine(lhs, s_tree[l++]);
+
314 }
+
315 if (r & 1) {
+
316 rhs = combine(s_tree[--r], rhs);
+
317 }
+
318 }
+
319 return combine(lhs, rhs);
+
320 }
-
317
-
329 void set_sret_init(X new_sret_init) { sret_init = new_sret_init; }
-
330};
+
321
+
334 void set_sret_init(X new_sret_init) { sret_init = new_sret_init; }
+
335};
-
331
-
-
336template <typename X> class HLD : public Tree<X>, public SG<X> {
-
337private:
-
338 int label;
-
339 std::vector<int> h_label,
- - -
342
-
-
350 void dfs_hc(int u, int p = -1) {
-
351 int hc_size = -1, hc_id = -1;
-
352 for (const int &v : Tree<X>::t_adj[u]) {
-
353 if (v ^ p) {
-
354 dfs_hc(v, u);
-
355 if (Tree<X>::t_size[v] > hc_size) {
-
356 hc_size = Tree<X>::t_size[v];
-
357 hc_id = v;
-
358 }
-
359 }
-
360 }
-
361 h_heavychlid[u] = hc_id;
-
362 }
+
336
+
341template <typename X>
+
+
342class HLD : public Tree<X>, public SG<X> {
+
343 private:
+
344 int label;
+
345 std::vector<int> h_label,
+ + +
348
+
+
356 void dfs_hc(int u, int p = -1) {
+
357 int hc_size = -1, hc_id = -1;
+
358 for (const int &v : Tree<X>::t_adj[u]) {
+
359 if (v ^ p) {
+
360 dfs_hc(v, u);
+
361 if (Tree<X>::t_size[v] > hc_size) {
+
362 hc_size = Tree<X>::t_size[v];
+
363 hc_id = v;
+
364 }
+
365 }
+
366 }
+
367 h_heavychlid[u] = hc_id;
+
368 }
-
363
-
-
371 void dfs_par(int u, int p = -1) {
-
372 if (h_heavychlid[u] != -1) {
- -
374 dfs_par(h_heavychlid[u], u);
-
375 }
-
376 for (const int &v : Tree<X>::t_adj[u]) {
-
377 if (v ^ p and v ^ h_heavychlid[u]) {
-
378 dfs_par(v, u);
-
379 }
-
380 }
-
381 }
+
369
+
+
377 void dfs_par(int u, int p = -1) {
+
378 if (h_heavychlid[u] != -1) {
+ +
380 dfs_par(h_heavychlid[u], u);
+
381 }
+
382 for (const int &v : Tree<X>::t_adj[u]) {
+
383 if (v ^ p and v ^ h_heavychlid[u]) {
+
384 dfs_par(v, u);
+
385 }
+
386 }
+
387 }
-
382
-
-
390 void dfs_labels(int u, int p = -1) {
-
391 h_label[u] = label++;
-
392 if (h_heavychlid[u] != -1) {
- -
394 }
-
395 for (const int &v : Tree<X>::t_adj[u]) {
-
396 if (v ^ p and v ^ h_heavychlid[u]) {
-
397 dfs_labels(v, u);
-
398 }
-
399 }
-
400 }
+
388
+
+
396 void dfs_labels(int u, int p = -1) {
+
397 h_label[u] = label++;
+
398 if (h_heavychlid[u] != -1) {
+ +
400 }
+
401 for (const int &v : Tree<X>::t_adj[u]) {
+
402 if (v ^ p and v ^ h_heavychlid[u]) {
+
403 dfs_labels(v, u);
+
404 }
+
405 }
+
406 }
-
401
-
-
409 X chain_query(int a, int b) {
-
410 X ret = SG<X>::sret_init;
- -
412 std::swap(a, b);
-
413 }
-
414 while (Tree<X>::t_depth[a] >= Tree<X>::t_depth[b]) {
-
415 int l = h_label[h_parent[a]];
-
416 int r = h_label[a];
- - -
419 }
-
420 ret = SG<X>::combine(ret, SG<X>::query(l, r));
-
421 a = Tree<X>::t_par[h_parent[a]][0];
-
422 if (a == -1) {
-
423 break;
-
424 }
-
425 }
-
426 return ret;
-
427 }
+
407
+
+
415 X chain_query(int a, int b) {
+
416 X ret = SG<X>::sret_init;
+ +
418 std::swap(a, b);
+
419 }
+
420 while (Tree<X>::t_depth[a] >= Tree<X>::t_depth[b]) {
+
421 int l = h_label[h_parent[a]];
+
422 int r = h_label[a];
+ + +
425 }
+
426 ret = SG<X>::combine(ret, SG<X>::query(l, r));
+
427 a = Tree<X>::t_par[h_parent[a]][0];
+
428 if (a == -1) {
+
429 break;
+
430 }
+
431 }
+
432 return ret;
+
433 }
-
428
-
429public:
-
435 explicit HLD<X>(int nodes) : Tree<X>(nodes), SG<X>(nodes) {
-
436 /* Initialization and resize vectors */
-
437 label = 0;
-
438 h_label.assign(Tree<X>::t_nodes, -1);
-
439 h_heavychlid.assign(Tree<X>::t_nodes, -1);
- -
441 iota(h_parent.begin(), h_parent.end(), 0);
-
442 }
-
443
-
-
450 void init() {
- -
452
-
453 // Fill the heavy child, greatest parent, and labels
-
454 label = 0;
- - - +
434
+
435 public:
+
+
441 explicit HLD(int nodes) : Tree<X>(nodes), SG<X>(nodes) {
+
442 /* Initialization and resize vectors */
+
443 label = 0;
+
444 h_label.assign(Tree<X>::t_nodes, -1);
+
445 h_heavychlid.assign(Tree<X>::t_nodes, -1);
+ +
447 iota(h_parent.begin(), h_parent.end(), 0);
+
448 }
+
+
449
+
+
456 void init() {
+
458
-
459 // Segment Tree Initialization
-
460 for (int i = 0; i < Tree<X>::t_nodes; i++) {
- -
462 }
-
463 for (int i = Tree<X>::t_nodes - 1; i > 0; i--) {
-
464 SG<X>::s_tree[i] =
-
465 SG<X>::combine(SG<X>::s_tree[i << 1], SG<X>::s_tree[i << 1 | 1]);
-
466 }
-
467 }
+
459 // Fill the heavy child, greatest parent, and labels
+
460 label = 0;
+ + + +
464
+
465 // Segment Tree Initialization
+
466 for (int i = 0; i < Tree<X>::t_nodes; i++) {
+ +
468 }
+
469 for (int i = Tree<X>::t_nodes - 1; i > 0; i--) {
+ +
471 SG<X>::s_tree[i << 1 | 1]);
+
472 }
+
473 }
-
468
-
-
475 void update(int node, X val) {
-
476 X diff = val - Tree<X>::t_val[node];
-
477 SG<X>::update(h_label[node], diff);
-
478 Tree<X>::t_val[node] = val;
-
479 }
+
474
+
+
481 void update(int node, X val) {
+
482 X diff = val - Tree<X>::t_val[node];
+
483 SG<X>::update(h_label[node], diff);
+
484 Tree<X>::t_val[node] = val;
+
485 }
-
480
-
-
489 X query(int a, int b) {
-
490 int lc = Tree<X>::lca(a, b);
-
491 X ret = SG<X>::sret_init;
-
492 assert(lc != -1);
-
493 ret += chain_query(a, lc);
-
494 ret += chain_query(b, lc);
-
495 return ret - Tree<X>::t_val[lc];
-
496 }
+
486
+
+
495 X query(int a, int b) {
+
496 int lc = Tree<X>::lca(a, b);
+
497 X ret = SG<X>::sret_init;
+
498 assert(lc != -1);
+
499 ret += chain_query(a, lc);
+
500 ret += chain_query(b, lc);
+
501 return ret - Tree<X>::t_val[lc];
+
502 }
-
497};
+
503};
-
498} // namespace heavy_light_decomposition
-
499} // namespace range_queries
-
500
-
-
505static void test_1() {
-
506 std::cout << "Test 1:\n";
-
507
-
508 // Test details
-
509 int n = 5;
-
510 std::vector<int64_t> node_values = {4, 2, 5, 2, 1};
-
511 std::vector<std::vector<int>> edges = {{1, 2}, {1, 3}, {3, 4}, {3, 5}};
-
512 std::vector<std::vector<int>> queries = {
-
513 {2, 1, 4},
-
514 {1, 3, 2},
-
515 {2, 1, 4},
-
516 };
-
517 std::vector<int> expected_result = {11, 8};
-
518 std::vector<int> code_result;
-
519
- -
521 hld.set_node_val(node_values);
-
522 for (int i = 0; i < n - 1; i++) {
-
523 int u = edges[i][0], v = edges[i][1];
-
524 hld.add_edge(u - 1, v - 1);
-
525 }
-
526 hld.init();
-
527 for (const auto &q : queries) {
-
528 int type = q[0];
-
529 if (type == 1) {
-
530 int p = q[1], x = q[2];
-
531 hld.update(p - 1, x);
-
532 } else if (type == 2) {
-
533 int a = q[1], b = q[2];
-
534 code_result.push_back(hld.query(a - 1, b - 1));
-
535 } else {
-
536 continue;
-
537 }
-
538 }
-
539 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
-
540 assert(expected_result[i] == code_result[i]);
-
541 }
-
542 std::cout << "\nTest 1 passed!\n";
-
543}
+
504} // namespace heavy_light_decomposition
+
505} // namespace range_queries
+
506
+
+
511static void test_1() {
+
512 std::cout << "Test 1:\n";
+
513
+
514 // Test details
+
515 int n = 5;
+
516 std::vector<int64_t> node_values = {4, 2, 5, 2, 1};
+
517 std::vector<std::vector<int>> edges = {{1, 2}, {1, 3}, {3, 4}, {3, 5}};
+
518 std::vector<std::vector<int>> queries = {
+
519 {2, 1, 4},
+
520 {1, 3, 2},
+
521 {2, 1, 4},
+
522 };
+
523 std::vector<int> expected_result = {11, 8};
+
524 std::vector<int> code_result;
+
525
+ +
527 hld.set_node_val(node_values);
+
528 for (int i = 0; i < n - 1; i++) {
+
529 int u = edges[i][0], v = edges[i][1];
+
530 hld.add_edge(u - 1, v - 1);
+
531 }
+
532 hld.init();
+
533 for (const auto &q : queries) {
+
534 int type = q[0];
+
535 if (type == 1) {
+
536 int p = q[1], x = q[2];
+
537 hld.update(p - 1, x);
+
538 } else if (type == 2) {
+
539 int a = q[1], b = q[2];
+
540 code_result.push_back(hld.query(a - 1, b - 1));
+
541 } else {
+
542 continue;
+
543 }
+
544 }
+
545 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
+
546 assert(expected_result[i] == code_result[i]);
+
547 }
+
548 std::cout << "\nTest 1 passed!\n";
+
549}
-
544
-
-
549static void test_2() {
-
550 std::cout << "Test 2:\n";
-
551
-
552 // Test details (Bamboo)
-
553 int n = 10;
-
554 std::vector<int64_t> node_values = {1, 8, 6, 8, 6, 2, 9, 2, 3, 2};
-
555 std::vector<std::vector<int>> edges = {
-
556 {10, 5}, {6, 2}, {10, 7}, {5, 2}, {3, 9}, {8, 3}, {1, 4}, {6, 4}, {8, 7}};
-
557 std::vector<std::vector<int>> queries = {
-
558 {2, 1, 10}, {2, 1, 6}, {1, 3, 4}, {2, 1, 9}, {1, 5, 3},
-
559 {1, 7, 8}, {2, 1, 4}, {2, 1, 8}, {1, 1, 4}, {1, 2, 7}};
-
560 std::vector<int> expected_result = {27, 11, 45, 9, 34};
-
561 std::vector<int> code_result;
-
562
- -
564 hld.set_node_val(node_values);
-
565 for (int i = 0; i < n - 1; i++) {
-
566 int u = edges[i][0], v = edges[i][1];
-
567 hld.add_edge(u - 1, v - 1);
-
568 }
-
569 hld.init();
-
570 for (const auto &q : queries) {
-
571 int type = q[0];
-
572 if (type == 1) {
-
573 int p = q[1], x = q[2];
-
574 hld.update(p - 1, x);
-
575 } else if (type == 2) {
-
576 int a = q[1], b = q[2];
-
577 code_result.push_back(hld.query(a - 1, b - 1));
-
578 } else {
-
579 continue;
-
580 }
-
581 }
-
582 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
-
583 assert(expected_result[i] == code_result[i]);
-
584 }
-
585 std::cout << "\nTest2 passed!\n";
-
586}
+
550
+
+
555static void test_2() {
+
556 std::cout << "Test 2:\n";
+
557
+
558 // Test details (Bamboo)
+
559 int n = 10;
+
560 std::vector<int64_t> node_values = {1, 8, 6, 8, 6, 2, 9, 2, 3, 2};
+
561 std::vector<std::vector<int>> edges = {{10, 5}, {6, 2}, {10, 7},
+
562 {5, 2}, {3, 9}, {8, 3},
+
563 {1, 4}, {6, 4}, {8, 7}};
+
564 std::vector<std::vector<int>> queries = {
+
565 {2, 1, 10}, {2, 1, 6}, {1, 3, 4}, {2, 1, 9}, {1, 5, 3},
+
566 {1, 7, 8}, {2, 1, 4}, {2, 1, 8}, {1, 1, 4}, {1, 2, 7}};
+
567 std::vector<int> expected_result = {27, 11, 45, 9, 34};
+
568 std::vector<int> code_result;
+
569
+ +
571 hld.set_node_val(node_values);
+
572 for (int i = 0; i < n - 1; i++) {
+
573 int u = edges[i][0], v = edges[i][1];
+
574 hld.add_edge(u - 1, v - 1);
+
575 }
+
576 hld.init();
+
577 for (const auto &q : queries) {
+
578 int type = q[0];
+
579 if (type == 1) {
+
580 int p = q[1], x = q[2];
+
581 hld.update(p - 1, x);
+
582 } else if (type == 2) {
+
583 int a = q[1], b = q[2];
+
584 code_result.push_back(hld.query(a - 1, b - 1));
+
585 } else {
+
586 continue;
+
587 }
+
588 }
+
589 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
+
590 assert(expected_result[i] == code_result[i]);
+
591 }
+
592 std::cout << "\nTest2 passed!\n";
+
593}
-
587
-
-
592static void test_3() {
-
593 std::cout << "Test 3:\n";
-
594
-
595 // Test details
-
596 int n = 8;
-
597 std::vector<int64_t> node_values = {1, 8, 6, 8, 6, 2, 9, 2};
-
598 std::vector<std::vector<int>> edges = {{1, 2}, {2, 3}, {3, 4}, {1, 5},
-
599 {6, 3}, {7, 5}, {8, 7}};
-
600 std::vector<std::vector<int>> queries = {
-
601 {2, 6, 8}, {2, 3, 6}, {1, 3, 4}, {2, 7, 1}, {1, 5, 3},
-
602 {1, 7, 8}, {2, 6, 4}, {2, 7, 8}, {1, 1, 4}, {1, 2, 7}};
-
603 std::vector<int> expected_result = {34, 8, 16, 14, 10};
-
604 std::vector<int> code_result;
-
605
- -
607 hld.set_node_val(node_values);
-
608 for (int i = 0; i < n - 1; i++) {
-
609 int u = edges[i][0], v = edges[i][1];
-
610 hld.add_edge(u - 1, v - 1);
-
611 }
-
612 hld.init();
-
613 for (const auto &q : queries) {
-
614 int type = q[0];
-
615 if (type == 1) {
-
616 int p = q[1], x = q[2];
-
617 hld.update(p - 1, x);
-
618 } else if (type == 2) {
-
619 int a = q[1], b = q[2];
-
620 code_result.push_back(hld.query(a - 1, b - 1));
-
621 } else {
-
622 continue;
-
623 }
-
624 }
-
625 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
-
626 assert(expected_result[i] == code_result[i]);
-
627 }
-
628 std::cout << "\nTest3 passed!\n";
-
629}
+
594
+
+
599static void test_3() {
+
600 std::cout << "Test 3:\n";
+
601
+
602 // Test details
+
603 int n = 8;
+
604 std::vector<int64_t> node_values = {1, 8, 6, 8, 6, 2, 9, 2};
+
605 std::vector<std::vector<int>> edges = {{1, 2}, {2, 3}, {3, 4}, {1, 5},
+
606 {6, 3}, {7, 5}, {8, 7}};
+
607 std::vector<std::vector<int>> queries = {
+
608 {2, 6, 8}, {2, 3, 6}, {1, 3, 4}, {2, 7, 1}, {1, 5, 3},
+
609 {1, 7, 8}, {2, 6, 4}, {2, 7, 8}, {1, 1, 4}, {1, 2, 7}};
+
610 std::vector<int> expected_result = {34, 8, 16, 14, 10};
+
611 std::vector<int> code_result;
+
612
+ +
614 hld.set_node_val(node_values);
+
615 for (int i = 0; i < n - 1; i++) {
+
616 int u = edges[i][0], v = edges[i][1];
+
617 hld.add_edge(u - 1, v - 1);
+
618 }
+
619 hld.init();
+
620 for (const auto &q : queries) {
+
621 int type = q[0];
+
622 if (type == 1) {
+
623 int p = q[1], x = q[2];
+
624 hld.update(p - 1, x);
+
625 } else if (type == 2) {
+
626 int a = q[1], b = q[2];
+
627 code_result.push_back(hld.query(a - 1, b - 1));
+
628 } else {
+
629 continue;
+
630 }
+
631 }
+
632 for (int i = 0; i < static_cast<int>(expected_result.size()); i++) {
+
633 assert(expected_result[i] == code_result[i]);
+
634 }
+
635 std::cout << "\nTest3 passed!\n";
+
636}
-
630
-
-
634int main() {
-
635 test_1();
-
636 test_2();
-
637 test_3();
-
638 return 0;
-
639}
+
637
+
+
641int main() {
+
642 test_1();
+
643 test_2();
+
644 test_3();
+
645 return 0;
+
646}
- -
void dfs_labels(int u, int p=-1)
Utility function to lable the nodes so that heavy chains have a contigous lable.
-
std::vector< int > h_parent
stores the top of the heavy chain from a node
-
void dfs_par(int u, int p=-1)
Utility function to assign highest parent that can be reached though heavy chains.
-
X query(int a, int b)
This function returns the sum of node values in the simple path from from node_1 to node_2.
-
HLD(int nodes)
Class parameterized constructor. Resizes the and initilizes the data members.
-
int label
utility member to assign labels in dfs_labels()
-
X chain_query(int a, int b)
Utility function to break down a path query into two chain queries.
-
std::vector< int > h_heavychlid
stores the heavy child of a node
-
void update(int node, X val)
This function updates the value at node with val.
-
std::vector< int > h_label
stores the label of a node
-
void init()
This function must be called after the tree adjacency list and node values are populated The function...
-
void dfs_hc(int u, int p=-1)
Utility function to assign heavy child to each node (-1 for a leaf node)
- -
X query(int l, int r)
Make a range query from node label l to node label r.
-
void update(int p, X v)
Update the value at a node.
-
X combine(X lhs, X rhs)
Function that specifies the type of operation involved when segments are combined.
- -
int s_size
number of leaves in the segment tree
-
void set_sret_init(X new_sret_init)
Set the initialization for the query data type, based on requirement.
-
SG(int size)
Class parameterized constructor. Resizes the and initilizes the data members.
-
std::vector< X > s_tree
Everything here is private, and can only be accessed through the methods, in the derived class (HLD)
-
A Basic Tree, which supports binary lifting.
-
void set_node_val(const std::vector< X > &node_val)
Set the values for all the nodes.
-
std::vector< int > t_depth
a vector to store the depth of a node,
- -
std::vector< std::vector< int > > t_par
a matrix to store every node's 2^kth parent
-
void add_edge(const int u, const int v)
Adds an undirected edge from node u to node v in the tree.
-
Tree(int nodes)
Class parameterized constructor, resizes the and initializes the data members.
-
int kth_ancestor(int p, const int &dist)
The function returns the kth ancestor of a node.
-
void dfs_size(int u, int p=-1)
Utility function to compute sub-tree sizes.
- -
std::vector< std::list< int > > t_adj
an adjacency list to stores the tree edges
-
const int t_maxlift
maximum possible height of the tree
-
void change_root(int new_root)
Set the root for the tree.
-
void lift(int *const p, int dist)
The function lifts a node, k units up the tree. The lifting is done in place, and the result is store...
-
void init()
This function must be called after the tree adjacency list and node values are populated The function...
-
std::vector< int > t_size
a vector to store the subtree size rooted at node
-
int lca(int a, int b)
The function returns the least common ancestor of two nodes.
- -
void dfs_lca(int u, int p=-1)
Utility function to populate the t_par vector.
-
static void test_1()
-
static void test_2()
- -
static void test_3()
+ +
void dfs_labels(int u, int p=-1)
Utility function to lable the nodes so that heavy chains have a contigous lable.
+
std::vector< int > h_parent
stores the top of the heavy chain from a node
+
void dfs_par(int u, int p=-1)
Utility function to assign highest parent that can be reached though heavy chains.
+
X query(int a, int b)
This function returns the sum of node values in the simple path from from node_1 to node_2.
+
HLD(int nodes)
Class parameterized constructor. Resizes the and initilizes the data members.
+
int label
utility member to assign labels in dfs_labels()
+
X chain_query(int a, int b)
Utility function to break down a path query into two chain queries.
+
std::vector< int > h_heavychlid
stores the heavy child of a node
+
void update(int node, X val)
This function updates the value at node with val.
+
std::vector< int > h_label
stores the label of a node
+
void init()
This function must be called after the tree adjacency list and node values are populated The function...
+
void dfs_hc(int u, int p=-1)
Utility function to assign heavy child to each node (-1 for a leaf node)
+
X query(int l, int r)
Make a range query from node label l to node label r.
+
void update(int p, X v)
Update the value at a node.
+
X combine(X lhs, X rhs)
Function that specifies the type of operation involved when segments are combined.
+ +
int s_size
number of leaves in the segment tree
+
void set_sret_init(X new_sret_init)
Set the initialization for the query data type, based on requirement.
+
SG(int size)
Class parameterized constructor. Resizes the and initilizes the data members.
+
std::vector< X > s_tree
Everything here is private, and can only be accessed through the methods, in the derived class (HLD)
+
void set_node_val(const std::vector< X > &node_val)
Set the values for all the nodes.
+
std::vector< int > t_depth
a vector to store the depth of a node,
+ +
std::vector< std::vector< int > > t_par
a matrix to store every node's 2^kth parent
+
void add_edge(const int u, const int v)
Adds an undirected edge from node u to node v in the tree.
+
Tree(int nodes)
Class parameterized constructor, resizes the and initializes the data members.
+
int kth_ancestor(int p, const int &dist)
The function returns the kth ancestor of a node.
+
void dfs_size(int u, int p=-1)
Utility function to compute sub-tree sizes.
+ +
std::vector< std::list< int > > t_adj
an adjacency list to stores the tree edges
+
const int t_maxlift
maximum possible height of the tree
+
void change_root(int new_root)
Set the root for the tree.
+
void lift(int *const p, int dist)
The function lifts a node, k units up the tree. The lifting is done in place, and the result is store...
+
void init()
This function must be called after the tree adjacency list and node values are populated The function...
+
std::vector< int > t_size
a vector to store the subtree size rooted at node
+
int lca(int a, int b)
The function returns the least common ancestor of two nodes.
+ +
void dfs_lca(int u, int p=-1)
Utility function to populate the t_par vector.
+
static void test_1()
+
static void test_2()
+ +
static void test_3()
#define ll
Heavy light decomposition algorithm.
for std::vector
diff --git a/d3/d2a/structmachine__learning_1_1aystar__search_1_1_ay_star_search_1_1comparison__operator.html b/d3/d2a/structmachine__learning_1_1aystar__search_1_1_ay_star_search_1_1comparison__operator.html index befc5e88a..0c7ff5e17 100644 --- a/d3/d2a/structmachine__learning_1_1aystar__search_1_1_ay_star_search_1_1comparison__operator.html +++ b/d3/d2a/structmachine__learning_1_1aystar__search_1_1_ay_star_search_1_1comparison__operator.html @@ -128,7 +128,7 @@ Public Member Functions
template<typename Puzzle>
struct machine_learning::aystar_search::AyStarSearch< Puzzle >::comparison_operator

Custom comparator for open_list.

-

Definition at line 370 of file a_star_search.cpp.

+

Definition at line 372 of file a_star_search.cpp.

Member Function Documentation

◆ operator()()

@@ -159,11 +159,11 @@ template<typename Puzzle>
-

Definition at line 371 of file a_star_search.cpp.

-
372 {
-
373 return *(a->state) < *(b->state);
-
374 }
-
A class defining A* search algorithm. for some initial state and final state.
+

Definition at line 373 of file a_star_search.cpp.

+
374 {
+
375 return *(a->state) < *(b->state);
+
376 }
+
A class defining A* search algorithm. for some initial state and final state.
diff --git a/d5/d58/persistent__seg__tree__lazy__prop_8cpp.html b/d5/d58/persistent__seg__tree__lazy__prop_8cpp.html index 672b9ee6f..834707626 100644 --- a/d5/d58/persistent__seg__tree__lazy__prop_8cpp.html +++ b/d5/d58/persistent__seg__tree__lazy__prop_8cpp.html @@ -119,13 +119,14 @@ $(function(){initNavTree('d5/d58/persistent__seg__tree__lazy__prop_8cpp.html','.

Persistent segment tree with range updates (lazy propagation) More...

-
#include <iostream>
+
#include <cstdint>
+#include <iostream>
#include <memory>
#include <vector>
Include dependency graph for persistent_seg_tree_lazy_prop.cpp:
-
+

Go to the source code of this file.

@@ -173,12 +174,12 @@ Functions

Main function.

Returns
0 on exit
-

Definition at line 318 of file persistent_seg_tree_lazy_prop.cpp.

-
318 {
-
319 test(); // run self-test implementations
-
320 return 0;
-
321}
-
static void test()
Test implementations.
+

Definition at line 319 of file persistent_seg_tree_lazy_prop.cpp.

+
319 {
+
320 test(); // run self-test implementations
+
321 return 0;
+
322}
+
static void test()
Test implementations.
@@ -208,57 +209,57 @@ Functions

Test implementations.

Returns
void
-

Definition at line 268 of file persistent_seg_tree_lazy_prop.cpp.

-
268 {
-
269 std::vector<int64_t> arr = {-5, 2, 3, 11, -2, 7, 0, 1};
- -
271 std::cout << "Elements before any updates are {";
-
272 for (uint32_t i = 0; i < arr.size(); ++i) {
-
273 std::cout << arr[i];
-
274 if (i != arr.size() - 1) {
-
275 std::cout << ",";
-
276 }
-
277 }
-
278 std::cout << "}\n";
-
279 tree.construct(
-
280 arr); // constructing the original segment tree (version = 0)
-
281 std::cout << "Querying range sum on version 0 from index 2 to 4 = 3+11-2 = "
-
282 << tree.query(2, 4, 0) << '\n';
-
283 std::cout
-
284 << "Subtract 7 from all elements from index 1 to index 5 inclusive\n";
-
285 tree.update(1, 5, -7); // subtracting 7 from index 1 to index 5
-
286 std::cout << "Elements of the segment tree whose version = 1 (after 1 "
-
287 "update) are {";
-
288 for (uint32_t i = 0; i < arr.size(); ++i) {
-
289 std::cout << tree.query(i, i, 1);
-
290 if (i != arr.size() - 1) {
-
291 std::cout << ",";
-
292 }
-
293 }
-
294 std::cout << "}\n";
-
295 std::cout << "Add 10 to all elements from index 0 to index 7 inclusive\n";
-
296 tree.update(0, 7, 10); // adding 10 to all elements
-
297 std::cout << "Elements of the segment tree whose version = 2 (after 2 "
-
298 "updates) are {";
-
299 for (uint32_t i = 0; i < arr.size(); ++i) {
-
300 std::cout << tree.query(i, i, 2);
-
301 if (i != arr.size() - 1) {
-
302 std::cout << ",";
-
303 }
-
304 }
-
305 std::cout << "}\n";
-
306 std::cout << "Number of segment trees (versions) now = " << tree.size()
-
307 << '\n';
-
308 std::cout << "Querying range sum on version 0 from index 3 to 5 = 11-2+7 = "
-
309 << tree.query(3, 5, 0) << '\n';
-
310 std::cout << "Querying range sum on version 1 from index 3 to 5 = 4-9+0 = "
-
311 << tree.query(3, 5, 1) << '\n';
-
312}
-
Range query here is range sum, but the code can be modified to make different queries like range max ...
-
uint32_t size()
Getting the number of versions after updates so far which is equal to the size of the pointers vector...
-
std::shared_ptr< Node > update(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, const int64_t &value, std::shared_ptr< Node > const &curr)
Doing range update, checking at every node if it has some value to be propagated. All nodes affected ...
-
std::shared_ptr< Node > construct(const uint32_t &i, const uint32_t &j)
Constructing the segment tree with the early passed vector. Every call creates a node to hold the sum...
-
int64_t query(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, std::shared_ptr< Node > const &curr)
Querying the range from index l to index r, checking at every node if it has some value to be propaga...
+

Definition at line 269 of file persistent_seg_tree_lazy_prop.cpp.

+
269 {
+
270 std::vector<int64_t> arr = {-5, 2, 3, 11, -2, 7, 0, 1};
+ +
272 std::cout << "Elements before any updates are {";
+
273 for (uint32_t i = 0; i < arr.size(); ++i) {
+
274 std::cout << arr[i];
+
275 if (i != arr.size() - 1) {
+
276 std::cout << ",";
+
277 }
+
278 }
+
279 std::cout << "}\n";
+
280 tree.construct(
+
281 arr); // constructing the original segment tree (version = 0)
+
282 std::cout << "Querying range sum on version 0 from index 2 to 4 = 3+11-2 = "
+
283 << tree.query(2, 4, 0) << '\n';
+
284 std::cout
+
285 << "Subtract 7 from all elements from index 1 to index 5 inclusive\n";
+
286 tree.update(1, 5, -7); // subtracting 7 from index 1 to index 5
+
287 std::cout << "Elements of the segment tree whose version = 1 (after 1 "
+
288 "update) are {";
+
289 for (uint32_t i = 0; i < arr.size(); ++i) {
+
290 std::cout << tree.query(i, i, 1);
+
291 if (i != arr.size() - 1) {
+
292 std::cout << ",";
+
293 }
+
294 }
+
295 std::cout << "}\n";
+
296 std::cout << "Add 10 to all elements from index 0 to index 7 inclusive\n";
+
297 tree.update(0, 7, 10); // adding 10 to all elements
+
298 std::cout << "Elements of the segment tree whose version = 2 (after 2 "
+
299 "updates) are {";
+
300 for (uint32_t i = 0; i < arr.size(); ++i) {
+
301 std::cout << tree.query(i, i, 2);
+
302 if (i != arr.size() - 1) {
+
303 std::cout << ",";
+
304 }
+
305 }
+
306 std::cout << "}\n";
+
307 std::cout << "Number of segment trees (versions) now = " << tree.size()
+
308 << '\n';
+
309 std::cout << "Querying range sum on version 0 from index 3 to 5 = 11-2+7 = "
+
310 << tree.query(3, 5, 0) << '\n';
+
311 std::cout << "Querying range sum on version 1 from index 3 to 5 = 4-9+0 = "
+
312 << tree.query(3, 5, 1) << '\n';
+
313}
+
Range query here is range sum, but the code can be modified to make different queries like range max ...
+
uint32_t size()
Getting the number of versions after updates so far which is equal to the size of the pointers vector...
+
std::shared_ptr< Node > update(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, const int64_t &value, std::shared_ptr< Node > const &curr)
Doing range update, checking at every node if it has some value to be propagated. All nodes affected ...
+
std::shared_ptr< Node > construct(const uint32_t &i, const uint32_t &j)
Constructing the segment tree with the early passed vector. Every call creates a node to hold the sum...
+
int64_t query(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, std::shared_ptr< Node > const &curr)
Querying the range from index l to index r, checking at every node if it has some value to be propaga...
diff --git a/d5/d58/persistent__seg__tree__lazy__prop_8cpp_source.html b/d5/d58/persistent__seg__tree__lazy__prop_8cpp_source.html index 269ae15d4..a2c514caf 100644 --- a/d5/d58/persistent__seg__tree__lazy__prop_8cpp_source.html +++ b/d5/d58/persistent__seg__tree__lazy__prop_8cpp_source.html @@ -117,239 +117,240 @@ $(function(){initNavTree('d5/d58/persistent__seg__tree__lazy__prop_8cpp_source.h
Go to the documentation of this file.
1
-
25#include <iostream>
-
26#include <memory>
-
27#include <vector>
-
28
-
33namespace range_queries {
-
34
-
- -
40 private:
-
-
41 class Node {
-
42 public:
-
43 std::shared_ptr<Node> left = nullptr;
-
44 std::shared_ptr<Node> right = nullptr;
-
45 int64_t val = 0,
-
46 prop = 0;
-
50 };
+
25#include <cstdint>
+
26#include <iostream>
+
27#include <memory>
+
28#include <vector>
+
29
+
34namespace range_queries {
+
35
+
+ +
41 private:
+
+
42 class Node {
+
43 public:
+
44 std::shared_ptr<Node> left = nullptr;
+
45 std::shared_ptr<Node> right = nullptr;
+
46 int64_t val = 0,
+
47 prop = 0;
+
51 };
-
51
-
52 uint32_t n = 0;
-
53 std::vector<std::shared_ptr<Node>>
-
54 ptrs{};
-
57 std::vector<int64_t> vec{};
-
59
-
-
65 std::shared_ptr<Node> newKid(std::shared_ptr<Node> const &curr) {
-
66 auto newNode = std::make_shared<Node>(Node());
-
67 newNode->left = curr->left;
-
68 newNode->right = curr->right;
-
69 newNode->prop = curr->prop;
-
70 newNode->val = curr->val;
-
71 return newNode;
-
72 }
+
52
+
53 uint32_t n = 0;
+
54 std::vector<std::shared_ptr<Node>>
+
55 ptrs{};
+
58 std::vector<int64_t> vec{};
+
60
+
+
66 std::shared_ptr<Node> newKid(std::shared_ptr<Node> const &curr) {
+
67 auto newNode = std::make_shared<Node>(Node());
+
68 newNode->left = curr->left;
+
69 newNode->right = curr->right;
+
70 newNode->prop = curr->prop;
+
71 newNode->val = curr->val;
+
72 return newNode;
+
73 }
-
73
-
-
83 void lazy(const uint32_t &i, const uint32_t &j,
-
84 std::shared_ptr<Node> const &curr) {
-
85 if (!curr->prop) {
-
86 return;
-
87 }
-
88 curr->val += (j - i + 1) * curr->prop;
-
89 if (i != j) {
-
90 curr->left = newKid(curr->left);
-
91 curr->right = newKid(curr->right);
-
92 curr->left->prop += curr->prop;
-
93 curr->right->prop += curr->prop;
-
94 }
-
95 curr->prop = 0;
-
96 }
+
74
+
+
84 void lazy(const uint32_t &i, const uint32_t &j,
+
85 std::shared_ptr<Node> const &curr) {
+
86 if (!curr->prop) {
+
87 return;
+
88 }
+
89 curr->val += (j - i + 1) * curr->prop;
+
90 if (i != j) {
+
91 curr->left = newKid(curr->left);
+
92 curr->right = newKid(curr->right);
+
93 curr->left->prop += curr->prop;
+
94 curr->right->prop += curr->prop;
+
95 }
+
96 curr->prop = 0;
+
97 }
-
97
-
-
106 std::shared_ptr<Node> construct(const uint32_t &i, const uint32_t &j) {
-
107 auto newNode = std::make_shared<Node>(Node());
-
108 if (i == j) {
-
109 newNode->val = vec[i];
-
110 } else {
-
111 uint32_t mid = i + (j - i) / 2;
-
112 auto leftt = construct(i, mid);
-
113 auto right = construct(mid + 1, j);
-
114 newNode->val = leftt->val + right->val;
-
115 newNode->left = leftt;
-
116 newNode->right = right;
-
117 }
-
118 return newNode;
-
119 }
+
98
+
+
107 std::shared_ptr<Node> construct(const uint32_t &i, const uint32_t &j) {
+
108 auto newNode = std::make_shared<Node>(Node());
+
109 if (i == j) {
+
110 newNode->val = vec[i];
+
111 } else {
+
112 uint32_t mid = i + (j - i) / 2;
+
113 auto leftt = construct(i, mid);
+
114 auto right = construct(mid + 1, j);
+
115 newNode->val = leftt->val + right->val;
+
116 newNode->left = leftt;
+
117 newNode->right = right;
+
118 }
+
119 return newNode;
+
120 }
-
120
-
-
135 std::shared_ptr<Node> update(const uint32_t &i, const uint32_t &j,
-
136 const uint32_t &l, const uint32_t &r,
-
137 const int64_t &value,
-
138 std::shared_ptr<Node> const &curr) {
-
139 lazy(i, j, curr);
-
140 if (i >= l && j <= r) {
-
141 std::shared_ptr<Node> newNode = newKid(curr);
-
142 newNode->prop += value;
-
143 lazy(i, j, newNode);
-
144 return newNode;
-
145 }
-
146 if (i > r || j < l) {
-
147 return curr;
-
148 }
-
149 auto newNode = std::make_shared<Node>(Node());
-
150 uint32_t mid = i + (j - i) / 2;
-
151 newNode->left = update(i, mid, l, r, value, curr->left);
-
152 newNode->right = update(mid + 1, j, l, r, value, curr->right);
-
153 newNode->val = newNode->left->val + newNode->right->val;
-
154 return newNode;
-
155 }
+
121
+
+
136 std::shared_ptr<Node> update(const uint32_t &i, const uint32_t &j,
+
137 const uint32_t &l, const uint32_t &r,
+
138 const int64_t &value,
+
139 std::shared_ptr<Node> const &curr) {
+
140 lazy(i, j, curr);
+
141 if (i >= l && j <= r) {
+
142 std::shared_ptr<Node> newNode = newKid(curr);
+
143 newNode->prop += value;
+
144 lazy(i, j, newNode);
+
145 return newNode;
+
146 }
+
147 if (i > r || j < l) {
+
148 return curr;
+
149 }
+
150 auto newNode = std::make_shared<Node>(Node());
+
151 uint32_t mid = i + (j - i) / 2;
+
152 newNode->left = update(i, mid, l, r, value, curr->left);
+
153 newNode->right = update(mid + 1, j, l, r, value, curr->right);
+
154 newNode->val = newNode->left->val + newNode->right->val;
+
155 return newNode;
+
156 }
-
156
-
-
171 int64_t query(const uint32_t &i, const uint32_t &j, const uint32_t &l,
-
172 const uint32_t &r, std::shared_ptr<Node> const &curr) {
-
173 lazy(i, j, curr);
-
174 if (j < l || r < i) {
-
175 return 0;
-
176 }
-
177 if (i >= l && j <= r) {
-
178 return curr->val;
-
179 }
-
180 uint32_t mid = i + (j - i) / 2;
-
181 return query(i, mid, l, r, curr->left) +
-
182 query(mid + 1, j, l, r, curr->right);
-
183 }
+
157
+
+
172 int64_t query(const uint32_t &i, const uint32_t &j, const uint32_t &l,
+
173 const uint32_t &r, std::shared_ptr<Node> const &curr) {
+
174 lazy(i, j, curr);
+
175 if (j < l || r < i) {
+
176 return 0;
+
177 }
+
178 if (i >= l && j <= r) {
+
179 return curr->val;
+
180 }
+
181 uint32_t mid = i + (j - i) / 2;
+
182 return query(i, mid, l, r, curr->left) +
+
183 query(mid + 1, j, l, r, curr->right);
+
184 }
-
184
-
189 public:
-
-
197 void construct(const std::vector<int64_t>
-
198 &vec) // the segment tree will be built from the values
-
199 // in "vec", "vec" is 0 indexed
-
200 {
-
201 if (vec.empty()) {
-
202 return;
-
203 }
-
204 n = vec.size();
-
205 this->vec = vec;
-
206 auto root = construct(0, n - 1);
-
207 ptrs.push_back(root);
-
208 }
+
185
+
190 public:
+
+
198 void construct(const std::vector<int64_t>
+
199 &vec) // the segment tree will be built from the values
+
200 // in "vec", "vec" is 0 indexed
+
201 {
+
202 if (vec.empty()) {
+
203 return;
+
204 }
+
205 n = vec.size();
+
206 this->vec = vec;
+
207 auto root = construct(0, n - 1);
+
208 ptrs.push_back(root);
+
209 }
-
209
-
-
219 void update(const uint32_t &l, const uint32_t &r,
-
220 const int64_t
-
221 &value) // all elements from index "l" to index "r" would
-
222 // by updated by "value", "l" and "r" are 0 indexed
-
223 {
-
224 ptrs.push_back(update(
-
225 0, n - 1, l, r, value,
-
226 ptrs[ptrs.size() -
-
227 1])); // saving the root pointer to the new segment tree
-
228 }
+
210
+
+
220 void update(const uint32_t &l, const uint32_t &r,
+
221 const int64_t
+
222 &value) // all elements from index "l" to index "r" would
+
223 // by updated by "value", "l" and "r" are 0 indexed
+
224 {
+
225 ptrs.push_back(update(
+
226 0, n - 1, l, r, value,
+
227 ptrs[ptrs.size() -
+
228 1])); // saving the root pointer to the new segment tree
+
229 }
-
229
-
-
241 int64_t query(
-
242 const uint32_t &l, const uint32_t &r,
-
243 const uint32_t
-
244 &version) // querying the range from "l" to "r" in a segment tree
-
245 // after "version" updates, "l" and "r" are 0 indexed
-
246 {
-
247 return query(0, n - 1, l, r, ptrs[version]);
-
248 }
+
230
+
+
242 int64_t query(
+
243 const uint32_t &l, const uint32_t &r,
+
244 const uint32_t
+
245 &version) // querying the range from "l" to "r" in a segment tree
+
246 // after "version" updates, "l" and "r" are 0 indexed
+
247 {
+
248 return query(0, n - 1, l, r, ptrs[version]);
+
249 }
-
249
-
-
255 uint32_t size() // returns the number of segment trees (versions) , the
-
256 // number of updates done so far = returned value - 1
-
257 // ,because one of the trees is the original segment tree
-
258 {
-
259 return ptrs.size();
-
260 }
+
250
+
+
256 uint32_t size() // returns the number of segment trees (versions) , the
+
257 // number of updates done so far = returned value - 1
+
258 // ,because one of the trees is the original segment tree
+
259 {
+
260 return ptrs.size();
+
261 }
-
261};
+
262};
-
262} // namespace range_queries
-
263
-
-
268static void test() {
-
269 std::vector<int64_t> arr = {-5, 2, 3, 11, -2, 7, 0, 1};
- -
271 std::cout << "Elements before any updates are {";
-
272 for (uint32_t i = 0; i < arr.size(); ++i) {
-
273 std::cout << arr[i];
-
274 if (i != arr.size() - 1) {
-
275 std::cout << ",";
-
276 }
-
277 }
-
278 std::cout << "}\n";
-
279 tree.construct(
-
280 arr); // constructing the original segment tree (version = 0)
-
281 std::cout << "Querying range sum on version 0 from index 2 to 4 = 3+11-2 = "
-
282 << tree.query(2, 4, 0) << '\n';
-
283 std::cout
-
284 << "Subtract 7 from all elements from index 1 to index 5 inclusive\n";
-
285 tree.update(1, 5, -7); // subtracting 7 from index 1 to index 5
-
286 std::cout << "Elements of the segment tree whose version = 1 (after 1 "
-
287 "update) are {";
-
288 for (uint32_t i = 0; i < arr.size(); ++i) {
-
289 std::cout << tree.query(i, i, 1);
-
290 if (i != arr.size() - 1) {
-
291 std::cout << ",";
-
292 }
-
293 }
-
294 std::cout << "}\n";
-
295 std::cout << "Add 10 to all elements from index 0 to index 7 inclusive\n";
-
296 tree.update(0, 7, 10); // adding 10 to all elements
-
297 std::cout << "Elements of the segment tree whose version = 2 (after 2 "
-
298 "updates) are {";
-
299 for (uint32_t i = 0; i < arr.size(); ++i) {
-
300 std::cout << tree.query(i, i, 2);
-
301 if (i != arr.size() - 1) {
-
302 std::cout << ",";
-
303 }
-
304 }
-
305 std::cout << "}\n";
-
306 std::cout << "Number of segment trees (versions) now = " << tree.size()
-
307 << '\n';
-
308 std::cout << "Querying range sum on version 0 from index 3 to 5 = 11-2+7 = "
-
309 << tree.query(3, 5, 0) << '\n';
-
310 std::cout << "Querying range sum on version 1 from index 3 to 5 = 4-9+0 = "
-
311 << tree.query(3, 5, 1) << '\n';
-
312}
+
263} // namespace range_queries
+
264
+
+
269static void test() {
+
270 std::vector<int64_t> arr = {-5, 2, 3, 11, -2, 7, 0, 1};
+ +
272 std::cout << "Elements before any updates are {";
+
273 for (uint32_t i = 0; i < arr.size(); ++i) {
+
274 std::cout << arr[i];
+
275 if (i != arr.size() - 1) {
+
276 std::cout << ",";
+
277 }
+
278 }
+
279 std::cout << "}\n";
+
280 tree.construct(
+
281 arr); // constructing the original segment tree (version = 0)
+
282 std::cout << "Querying range sum on version 0 from index 2 to 4 = 3+11-2 = "
+
283 << tree.query(2, 4, 0) << '\n';
+
284 std::cout
+
285 << "Subtract 7 from all elements from index 1 to index 5 inclusive\n";
+
286 tree.update(1, 5, -7); // subtracting 7 from index 1 to index 5
+
287 std::cout << "Elements of the segment tree whose version = 1 (after 1 "
+
288 "update) are {";
+
289 for (uint32_t i = 0; i < arr.size(); ++i) {
+
290 std::cout << tree.query(i, i, 1);
+
291 if (i != arr.size() - 1) {
+
292 std::cout << ",";
+
293 }
+
294 }
+
295 std::cout << "}\n";
+
296 std::cout << "Add 10 to all elements from index 0 to index 7 inclusive\n";
+
297 tree.update(0, 7, 10); // adding 10 to all elements
+
298 std::cout << "Elements of the segment tree whose version = 2 (after 2 "
+
299 "updates) are {";
+
300 for (uint32_t i = 0; i < arr.size(); ++i) {
+
301 std::cout << tree.query(i, i, 2);
+
302 if (i != arr.size() - 1) {
+
303 std::cout << ",";
+
304 }
+
305 }
+
306 std::cout << "}\n";
+
307 std::cout << "Number of segment trees (versions) now = " << tree.size()
+
308 << '\n';
+
309 std::cout << "Querying range sum on version 0 from index 3 to 5 = 11-2+7 = "
+
310 << tree.query(3, 5, 0) << '\n';
+
311 std::cout << "Querying range sum on version 1 from index 3 to 5 = 4-9+0 = "
+
312 << tree.query(3, 5, 1) << '\n';
+
313}
-
313
-
-
318int main() {
-
319 test(); // run self-test implementations
-
320 return 0;
-
321}
+
314
+
+
319int main() {
+
320 test(); // run self-test implementations
+
321 return 0;
+
322}
- -
std::shared_ptr< Node > right
pointer to the left node
- -
Range query here is range sum, but the code can be modified to make different queries like range max ...
-
std::shared_ptr< Node > newKid(std::shared_ptr< Node > const &curr)
Creating a new node with the same values of curr node.
-
uint32_t size()
Getting the number of versions after updates so far which is equal to the size of the pointers vector...
-
std::vector< std::shared_ptr< Node > > ptrs
number of elements/leaf nodes in the segment tree
-
std::shared_ptr< Node > update(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, const int64_t &value, std::shared_ptr< Node > const &curr)
Doing range update, checking at every node if it has some value to be propagated. All nodes affected ...
-
std::shared_ptr< Node > construct(const uint32_t &i, const uint32_t &j)
Constructing the segment tree with the early passed vector. Every call creates a node to hold the sum...
- -
void construct(const std::vector< int64_t > &vec)
Constructing the segment tree with the values in the passed vector. Returned root pointer is pushed i...
-
void lazy(const uint32_t &i, const uint32_t &j, std::shared_ptr< Node > const &curr)
If there is some value to be propagated to the passed node, value is added to the node and the childr...
-
int64_t query(const uint32_t &l, const uint32_t &r, const uint32_t &version)
Querying the range from index l to index r, getting the sum of the elements whose index x satisfies l...
-
int64_t query(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, std::shared_ptr< Node > const &curr)
Querying the range from index l to index r, checking at every node if it has some value to be propaga...
-
void update(const uint32_t &l, const uint32_t &r, const int64_t &value)
Doing range update by passing the left and right indexes of the range as well as the value to be adde...
+ +
std::shared_ptr< Node > right
pointer to the left node
+ +
Range query here is range sum, but the code can be modified to make different queries like range max ...
+
std::shared_ptr< Node > newKid(std::shared_ptr< Node > const &curr)
Creating a new node with the same values of curr node.
+
uint32_t size()
Getting the number of versions after updates so far which is equal to the size of the pointers vector...
+
std::vector< std::shared_ptr< Node > > ptrs
number of elements/leaf nodes in the segment tree
+
std::shared_ptr< Node > update(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, const int64_t &value, std::shared_ptr< Node > const &curr)
Doing range update, checking at every node if it has some value to be propagated. All nodes affected ...
+
std::shared_ptr< Node > construct(const uint32_t &i, const uint32_t &j)
Constructing the segment tree with the early passed vector. Every call creates a node to hold the sum...
+ +
void construct(const std::vector< int64_t > &vec)
Constructing the segment tree with the values in the passed vector. Returned root pointer is pushed i...
+
void lazy(const uint32_t &i, const uint32_t &j, std::shared_ptr< Node > const &curr)
If there is some value to be propagated to the passed node, value is added to the node and the childr...
+
int64_t query(const uint32_t &l, const uint32_t &r, const uint32_t &version)
Querying the range from index l to index r, getting the sum of the elements whose index x satisfies l...
+
int64_t query(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, std::shared_ptr< Node > const &curr)
Querying the range from index l to index r, checking at every node if it has some value to be propaga...
+
void update(const uint32_t &l, const uint32_t &r, const int64_t &value)
Doing range update by passing the left and right indexes of the range as well as the value to be adde...
for std::vector
-
static void test()
Test implementations.
-
int main()
Main function.
+
static void test()
Test implementations.
+
int main()
Main function.
diff --git a/d5/d66/classrange__queries_1_1per_seg_tree_1_1_node.html b/d5/d66/classrange__queries_1_1per_seg_tree_1_1_node.html index fd75ec7da..b2601722f 100644 --- a/d5/d66/classrange__queries_1_1per_seg_tree_1_1_node.html +++ b/d5/d66/classrange__queries_1_1per_seg_tree_1_1_node.html @@ -133,7 +133,7 @@ Public Attributes

Detailed Description

-

Definition at line 41 of file persistent_seg_tree_lazy_prop.cpp.

+

Definition at line 42 of file persistent_seg_tree_lazy_prop.cpp.

Member Data Documentation

◆ left

@@ -147,7 +147,7 @@ Public Attributes
-

Definition at line 43 of file persistent_seg_tree_lazy_prop.cpp.

+

Definition at line 44 of file persistent_seg_tree_lazy_prop.cpp.

@@ -163,7 +163,7 @@ Public Attributes
-

Definition at line 46 of file persistent_seg_tree_lazy_prop.cpp.

+

Definition at line 47 of file persistent_seg_tree_lazy_prop.cpp.

@@ -181,7 +181,7 @@ Public Attributes

pointer to the left node

-

Definition at line 44 of file persistent_seg_tree_lazy_prop.cpp.

+

Definition at line 45 of file persistent_seg_tree_lazy_prop.cpp.

@@ -199,7 +199,7 @@ Public Attributes

pointer to the right node

-

Definition at line 45 of file persistent_seg_tree_lazy_prop.cpp.

+

Definition at line 46 of file persistent_seg_tree_lazy_prop.cpp.

diff --git a/d5/d83/lcm__sum_8cpp.html b/d5/d83/lcm__sum_8cpp.html index ae52e914f..6ad063afb 100644 --- a/d5/d83/lcm__sum_8cpp.html +++ b/d5/d83/lcm__sum_8cpp.html @@ -234,9 +234,9 @@ Functions
89 assert(test_5 == 1110);
90 std::cout << "Passed Test 5!" << std::endl;
91}
-
static void test_1()
-
static void test_2()
-
static void test_3()
+
static void test_1()
+
static void test_2()
+
static void test_3()
uint64_t lcmSum(const uint16_t &num)
Definition lcm_sum.cpp:30
diff --git a/d5/d83/lcm__sum_8cpp_source.html b/d5/d83/lcm__sum_8cpp_source.html index e97edf190..a6406a2c3 100644 --- a/d5/d83/lcm__sum_8cpp_source.html +++ b/d5/d83/lcm__sum_8cpp_source.html @@ -192,9 +192,9 @@ $(function(){initNavTree('d5/d83/lcm__sum_8cpp_source.html','../../',''); });
99 return 0;
100}
-
static void test_1()
-
static void test_2()
-
static void test_3()
+
static void test_1()
+
static void test_2()
+
static void test_3()
static void test()
Definition lcm_sum.cpp:66
int main()
Main function.
Definition lcm_sum.cpp:97
for assert
diff --git a/d7/d83/trie__tree_8cpp.html b/d7/d83/trie__tree_8cpp.html index fd2c0f5cd..e6d3a252e 100644 --- a/d7/d83/trie__tree_8cpp.html +++ b/d7/d83/trie__tree_8cpp.html @@ -121,13 +121,14 @@ $(function(){initNavTree('d7/d83/trie__tree_8cpp.html','../../',''); }); More...

#include <array>
#include <cassert>
+#include <cstdint>
#include <iostream>
#include <memory>
#include <string>
Include dependency graph for trie_tree.cpp:
-
+

Go to the source code of this file.

@@ -177,13 +178,13 @@ Functions

Main function.

Returns
0 on exit
-

Definition at line 204 of file trie_tree.cpp.

-
204 {
-
205 test();
-
206
-
207 return 0;
-
208}
-
static void test()
Testing function.
+

Definition at line 205 of file trie_tree.cpp.

+
205 {
+
206 test();
+
207
+
208 return 0;
+
209}
+
static void test()
Testing function.
@@ -213,31 +214,31 @@ Functions

Testing function.

Returns
void
-

Definition at line 177 of file trie_tree.cpp.

-
177 {
- -
179 root.insert("Hello");
-
180 root.insert("World");
-
181
-
182 assert(!root.search("hello", 0));
-
183 std::cout << "hello - " << root.search("hello", 0) << "\n";
-
184
-
185 assert(root.search("Hello", 0));
-
186 std::cout << "Hello - " << root.search("Hello", 0) << "\n";
-
187
-
188 assert(!root.search("Word", 0));
-
189 std::cout << "Word - " << root.search("Word", 0) << "\n";
-
190
-
191 assert(root.search("World", 0));
-
192 std::cout << "World - " << root.search("World", 0) << "\n";
-
193
-
194 // Following lines of code give erroneous output
-
195 // root.deleteString("hello", 0);
-
196 // assert(!root.search("hello", 0));
-
197 // std::cout << "hello - " << root.search("world", 0) << "\n";
-
198}
-
Trie implementation for small-case English alphabets a-z
Definition trie_tree.cpp:24
-
void insert(const std::string &str)
Definition trie_tree.cpp:76
+

Definition at line 178 of file trie_tree.cpp.

+
178 {
+ +
180 root.insert("Hello");
+
181 root.insert("World");
+
182
+
183 assert(!root.search("hello", 0));
+
184 std::cout << "hello - " << root.search("hello", 0) << "\n";
+
185
+
186 assert(root.search("Hello", 0));
+
187 std::cout << "Hello - " << root.search("Hello", 0) << "\n";
+
188
+
189 assert(!root.search("Word", 0));
+
190 std::cout << "Word - " << root.search("Word", 0) << "\n";
+
191
+
192 assert(root.search("World", 0));
+
193 std::cout << "World - " << root.search("World", 0) << "\n";
+
194
+
195 // Following lines of code give erroneous output
+
196 // root.deleteString("hello", 0);
+
197 // assert(!root.search("hello", 0));
+
198 // std::cout << "hello - " << root.search("world", 0) << "\n";
+
199}
+
Trie implementation for small-case English alphabets a-z
Definition trie_tree.cpp:25
+
void insert(const std::string &str)
Definition trie_tree.cpp:77
diff --git a/d7/d83/trie__tree_8cpp_source.html b/d7/d83/trie__tree_8cpp_source.html index 672dc25bb..a1f3f63fc 100644 --- a/d7/d83/trie__tree_8cpp_source.html +++ b/d7/d83/trie__tree_8cpp_source.html @@ -119,183 +119,184 @@ $(function(){initNavTree('d7/d83/trie__tree_8cpp_source.html','../../',''); }); Go to the documentation of this file.
1
10#include <array>
11#include <cassert>
-
12#include <iostream>
-
13#include <memory>
-
14#include <string>
-
15
-
19namespace data_structures {
-
-
24class trie {
-
25 private:
-
26 static constexpr uint8_t NUM_CHARS = 26;
-
28 std::array<std::shared_ptr<trie>, NUM_CHARS << 1> arr;
-
29 bool isEndofWord = false;
-
30
-
-
37 uint8_t char_to_int(const char& ch) const {
-
38 if (ch >= 'A' && ch <= 'Z') {
-
39 return ch - 'A';
-
40 } else if (ch >= 'a' && ch <= 'z') {
-
41 return ch - 'a' + NUM_CHARS;
-
42 }
-
43
-
44 std::cerr << "Invalid character present. Exiting...";
-
45 std::exit(EXIT_FAILURE);
-
46 return 0;
-
47 }
+
12#include <cstdint>
+
13#include <iostream>
+
14#include <memory>
+
15#include <string>
+
16
+
20namespace data_structures {
+
+
25class trie {
+
26 private:
+
27 static constexpr uint8_t NUM_CHARS = 26;
+
29 std::array<std::shared_ptr<trie>, NUM_CHARS << 1> arr;
+
30 bool isEndofWord = false;
+
31
+
+
38 uint8_t char_to_int(const char& ch) const {
+
39 if (ch >= 'A' && ch <= 'Z') {
+
40 return ch - 'A';
+
41 } else if (ch >= 'a' && ch <= 'z') {
+
42 return ch - 'a' + NUM_CHARS;
+
43 }
+
44
+
45 std::cerr << "Invalid character present. Exiting...";
+
46 std::exit(EXIT_FAILURE);
+
47 return 0;
+
48 }
-
48
-
-
55 bool search(const std::shared_ptr<trie>& root, const std::string& str,
-
56 int index) {
-
57 if (index == str.length()) {
-
58 if (!root->isEndofWord) {
-
59 return false;
-
60 }
-
61 return true;
-
62 }
-
63 int j = char_to_int(str[index]);
-
64 if (!root->arr[j]) {
-
65 return false;
-
66 }
-
67 return search(root->arr[j], str, index + 1);
-
68 }
+
49
+
+
56 bool search(const std::shared_ptr<trie>& root, const std::string& str,
+
57 int index) {
+
58 if (index == str.length()) {
+
59 if (!root->isEndofWord) {
+
60 return false;
+
61 }
+
62 return true;
+
63 }
+
64 int j = char_to_int(str[index]);
+
65 if (!root->arr[j]) {
+
66 return false;
+
67 }
+
68 return search(root->arr[j], str, index + 1);
+
69 }
-
69
-
70 public:
-
71 trie() = default;
-
72
-
-
76 void insert(const std::string& str) {
-
77 std::shared_ptr<trie> root(nullptr);
-
78
-
79 for (const char& ch : str) {
-
80 int j = char_to_int(ch);
-
81 if (root) {
-
82 if (root->arr[j]) {
-
83 root = root->arr[j];
-
84 } else {
-
85 std::shared_ptr<trie> temp(new trie());
-
86 root->arr[j] = temp;
-
87 root = temp;
-
88 }
-
89 } else if (arr[j]) {
-
90 root = arr[j];
-
91 } else {
-
92 std::shared_ptr<trie> temp(new trie());
-
93 arr[j] = temp;
-
94 root = temp;
-
95 }
-
96 }
-
97 root->isEndofWord = true;
-
98 }
+
70
+
71 public:
+
72 trie() = default;
+
73
+
+
77 void insert(const std::string& str) {
+
78 std::shared_ptr<trie> root(nullptr);
+
79
+
80 for (const char& ch : str) {
+
81 int j = char_to_int(ch);
+
82 if (root) {
+
83 if (root->arr[j]) {
+
84 root = root->arr[j];
+
85 } else {
+
86 std::shared_ptr<trie> temp(new trie());
+
87 root->arr[j] = temp;
+
88 root = temp;
+
89 }
+
90 } else if (arr[j]) {
+
91 root = arr[j];
+
92 } else {
+
93 std::shared_ptr<trie> temp(new trie());
+
94 arr[j] = temp;
+
95 root = temp;
+
96 }
+
97 }
+
98 root->isEndofWord = true;
+
99 }
-
99
-
-
106 bool search(const std::string& str, int index) {
-
107 if (index == str.length()) {
-
108 if (!isEndofWord) {
-
109 return false;
-
110 }
-
111 return true;
-
112 }
-
113 int j = char_to_int(str[index]);
-
114 if (!arr[j]) {
-
115 return false;
-
116 }
-
117 return search(arr[j], str, index + 1);
-
118 }
+
100
+
+
107 bool search(const std::string& str, int index) {
+
108 if (index == str.length()) {
+
109 if (!isEndofWord) {
+
110 return false;
+
111 }
+
112 return true;
+
113 }
+
114 int j = char_to_int(str[index]);
+
115 if (!arr[j]) {
+
116 return false;
+
117 }
+
118 return search(arr[j], str, index + 1);
+
119 }
-
119
-
-
133 bool deleteString(const std::string& str, int index) {
-
134 if (index == str.length()) {
-
135 if (!isEndofWord) {
-
136 return false;
-
137 }
-
138 isEndofWord = false;
-
139 // following lines - possible source of error?
-
140 // for (int i = 0; i < NUM_CHARS; i++)
-
141 // if (!arr[i])
-
142 // return false;
-
143 return true;
-
144 }
-
145 int j = char_to_int(str[index]);
-
146 if (!arr[j]) {
-
147 return false;
-
148 }
-
149 bool var = deleteString(str, index + 1);
-
150 if (var) {
-
151 arr[j].reset();
-
152 if (isEndofWord) {
-
153 return false;
-
154 } else {
-
155 int i = 0;
-
156 for (i = 0; i < NUM_CHARS; i++) {
-
157 if (arr[i]) {
-
158 return false;
-
159 }
-
160 }
-
161 return true;
-
162 }
-
163 }
-
164
-
165 /* should not return here */
-
166 std::cout << __func__ << ":" << __LINE__
-
167 << "Should not reach this line\n";
-
168 return false;
-
169 }
+
120
+
+
134 bool deleteString(const std::string& str, int index) {
+
135 if (index == str.length()) {
+
136 if (!isEndofWord) {
+
137 return false;
+
138 }
+
139 isEndofWord = false;
+
140 // following lines - possible source of error?
+
141 // for (int i = 0; i < NUM_CHARS; i++)
+
142 // if (!arr[i])
+
143 // return false;
+
144 return true;
+
145 }
+
146 int j = char_to_int(str[index]);
+
147 if (!arr[j]) {
+
148 return false;
+
149 }
+
150 bool var = deleteString(str, index + 1);
+
151 if (var) {
+
152 arr[j].reset();
+
153 if (isEndofWord) {
+
154 return false;
+
155 } else {
+
156 int i = 0;
+
157 for (i = 0; i < NUM_CHARS; i++) {
+
158 if (arr[i]) {
+
159 return false;
+
160 }
+
161 }
+
162 return true;
+
163 }
+
164 }
+
165
+
166 /* should not return here */
+
167 std::cout << __func__ << ":" << __LINE__
+
168 << "Should not reach this line\n";
+
169 return false;
+
170 }
-
170};
+
171};
-
171} // namespace data_structures
-
172
-
-
177static void test() {
- -
179 root.insert("Hello");
-
180 root.insert("World");
-
181
-
182 assert(!root.search("hello", 0));
-
183 std::cout << "hello - " << root.search("hello", 0) << "\n";
-
184
-
185 assert(root.search("Hello", 0));
-
186 std::cout << "Hello - " << root.search("Hello", 0) << "\n";
-
187
-
188 assert(!root.search("Word", 0));
-
189 std::cout << "Word - " << root.search("Word", 0) << "\n";
-
190
-
191 assert(root.search("World", 0));
-
192 std::cout << "World - " << root.search("World", 0) << "\n";
-
193
-
194 // Following lines of code give erroneous output
-
195 // root.deleteString("hello", 0);
-
196 // assert(!root.search("hello", 0));
-
197 // std::cout << "hello - " << root.search("world", 0) << "\n";
-
198}
+
172} // namespace data_structures
+
173
+
+
178static void test() {
+ +
180 root.insert("Hello");
+
181 root.insert("World");
+
182
+
183 assert(!root.search("hello", 0));
+
184 std::cout << "hello - " << root.search("hello", 0) << "\n";
+
185
+
186 assert(root.search("Hello", 0));
+
187 std::cout << "Hello - " << root.search("Hello", 0) << "\n";
+
188
+
189 assert(!root.search("Word", 0));
+
190 std::cout << "Word - " << root.search("Word", 0) << "\n";
+
191
+
192 assert(root.search("World", 0));
+
193 std::cout << "World - " << root.search("World", 0) << "\n";
+
194
+
195 // Following lines of code give erroneous output
+
196 // root.deleteString("hello", 0);
+
197 // assert(!root.search("hello", 0));
+
198 // std::cout << "hello - " << root.search("world", 0) << "\n";
+
199}
-
199
-
-
204int main() {
-
205 test();
-
206
-
207 return 0;
-
208}
+
200
+
+
205int main() {
+
206 test();
+
207
+
208 return 0;
+
209}
-
Trie implementation for small-case English alphabets a-z
Definition trie_tree.cpp:24
-
void insert(const std::string &str)
Definition trie_tree.cpp:76
-
std::array< std::shared_ptr< trie >, NUM_CHARS<< 1 > arr
Recursive tree nodes as an array of shared-pointers.
Definition trie_tree.cpp:28
-
bool search(const std::string &str, int index)
-
static constexpr uint8_t NUM_CHARS
Number of alphabets.
Definition trie_tree.cpp:26
-
bool isEndofWord
identifier if a node is terminal node
Definition trie_tree.cpp:29
+
Trie implementation for small-case English alphabets a-z
Definition trie_tree.cpp:25
+
void insert(const std::string &str)
Definition trie_tree.cpp:77
+
std::array< std::shared_ptr< trie >, NUM_CHARS<< 1 > arr
Recursive tree nodes as an array of shared-pointers.
Definition trie_tree.cpp:29
+
bool search(const std::string &str, int index)
+
static constexpr uint8_t NUM_CHARS
Number of alphabets.
Definition trie_tree.cpp:27
+
bool isEndofWord
identifier if a node is terminal node
Definition trie_tree.cpp:30
trie()=default
Class default constructor.
-
bool search(const std::shared_ptr< trie > &root, const std::string &str, int index)
Definition trie_tree.cpp:55
-
uint8_t char_to_int(const char &ch) const
Convert a character to integer for indexing.
Definition trie_tree.cpp:37
-
bool deleteString(const std::string &str, int index)
+
bool search(const std::shared_ptr< trie > &root, const std::string &str, int index)
Definition trie_tree.cpp:56
+
uint8_t char_to_int(const char &ch) const
Convert a character to integer for indexing.
Definition trie_tree.cpp:38
+
bool deleteString(const std::string &str, int index)
for IO operations
for std::assert
-
static void test()
Testing function.
-
int main()
Main function.
+
static void test()
Testing function.
+
int main()
Main function.
diff --git a/d8/d28/classrange__queries_1_1per_seg_tree.html b/d8/d28/classrange__queries_1_1per_seg_tree.html index 1f85d5776..0e4ea17a5 100644 --- a/d8/d28/classrange__queries_1_1per_seg_tree.html +++ b/d8/d28/classrange__queries_1_1per_seg_tree.html @@ -163,7 +163,7 @@ Private Attributes

Detailed Description

Range query here is range sum, but the code can be modified to make different queries like range max or min.

-

Definition at line 39 of file persistent_seg_tree_lazy_prop.cpp.

+

Definition at line 40 of file persistent_seg_tree_lazy_prop.cpp.

Member Function Documentation

◆ construct() [1/2]

@@ -197,19 +197,19 @@ Private Attributes
Returns
void
-

Definition at line 197 of file persistent_seg_tree_lazy_prop.cpp.

-
200 {
-
201 if (vec.empty()) {
-
202 return;
-
203 }
-
204 n = vec.size();
-
205 this->vec = vec;
-
206 auto root = construct(0, n - 1);
-
207 ptrs.push_back(root);
-
208 }
-
std::vector< std::shared_ptr< Node > > ptrs
number of elements/leaf nodes in the segment tree
-
std::shared_ptr< Node > construct(const uint32_t &i, const uint32_t &j)
Constructing the segment tree with the early passed vector. Every call creates a node to hold the sum...
- +

Definition at line 198 of file persistent_seg_tree_lazy_prop.cpp.

+
201 {
+
202 if (vec.empty()) {
+
203 return;
+
204 }
+
205 n = vec.size();
+
206 this->vec = vec;
+
207 auto root = construct(0, n - 1);
+
208 ptrs.push_back(root);
+
209 }
+
std::vector< std::shared_ptr< Node > > ptrs
number of elements/leaf nodes in the segment tree
+
std::shared_ptr< Node > construct(const uint32_t &i, const uint32_t &j)
Constructing the segment tree with the early passed vector. Every call creates a node to hold the sum...
+
@@ -250,22 +250,22 @@ Private Attributes
Returns
pointer to the newly created node
-

Definition at line 106 of file persistent_seg_tree_lazy_prop.cpp.

-
106 {
-
107 auto newNode = std::make_shared<Node>(Node());
-
108 if (i == j) {
-
109 newNode->val = vec[i];
-
110 } else {
-
111 uint32_t mid = i + (j - i) / 2;
-
112 auto leftt = construct(i, mid);
-
113 auto right = construct(mid + 1, j);
-
114 newNode->val = leftt->val + right->val;
-
115 newNode->left = leftt;
-
116 newNode->right = right;
-
117 }
-
118 return newNode;
-
119 }
- +

Definition at line 107 of file persistent_seg_tree_lazy_prop.cpp.

+
107 {
+
108 auto newNode = std::make_shared<Node>(Node());
+
109 if (i == j) {
+
110 newNode->val = vec[i];
+
111 } else {
+
112 uint32_t mid = i + (j - i) / 2;
+
113 auto leftt = construct(i, mid);
+
114 auto right = construct(mid + 1, j);
+
115 newNode->val = leftt->val + right->val;
+
116 newNode->left = leftt;
+
117 newNode->right = right;
+
118 }
+
119 return newNode;
+
120 }
+
@@ -312,21 +312,21 @@ Private Attributes
Returns
void
-

Definition at line 83 of file persistent_seg_tree_lazy_prop.cpp.

-
84 {
-
85 if (!curr->prop) {
-
86 return;
-
87 }
-
88 curr->val += (j - i + 1) * curr->prop;
-
89 if (i != j) {
-
90 curr->left = newKid(curr->left);
-
91 curr->right = newKid(curr->right);
-
92 curr->left->prop += curr->prop;
-
93 curr->right->prop += curr->prop;
-
94 }
-
95 curr->prop = 0;
-
96 }
-
std::shared_ptr< Node > newKid(std::shared_ptr< Node > const &curr)
Creating a new node with the same values of curr node.
+

Definition at line 84 of file persistent_seg_tree_lazy_prop.cpp.

+
85 {
+
86 if (!curr->prop) {
+
87 return;
+
88 }
+
89 curr->val += (j - i + 1) * curr->prop;
+
90 if (i != j) {
+
91 curr->left = newKid(curr->left);
+
92 curr->right = newKid(curr->right);
+
93 curr->left->prop += curr->prop;
+
94 curr->right->prop += curr->prop;
+
95 }
+
96 curr->prop = 0;
+
97 }
+
std::shared_ptr< Node > newKid(std::shared_ptr< Node > const &curr)
Creating a new node with the same values of curr node.
@@ -362,15 +362,15 @@ Private Attributes
Returns
the new node
-

Definition at line 65 of file persistent_seg_tree_lazy_prop.cpp.

-
65 {
-
66 auto newNode = std::make_shared<Node>(Node());
-
67 newNode->left = curr->left;
-
68 newNode->right = curr->right;
-
69 newNode->prop = curr->prop;
-
70 newNode->val = curr->val;
-
71 return newNode;
-
72 }
+

Definition at line 66 of file persistent_seg_tree_lazy_prop.cpp.

+
66 {
+
67 auto newNode = std::make_shared<Node>(Node());
+
68 newNode->left = curr->left;
+
69 newNode->right = curr->right;
+
70 newNode->prop = curr->prop;
+
71 newNode->val = curr->val;
+
72 return newNode;
+
73 }
@@ -429,21 +429,21 @@ Private Attributes
Returns
sum of elements whose index x satisfies l<=x<=r
-

Definition at line 171 of file persistent_seg_tree_lazy_prop.cpp.

-
172 {
-
173 lazy(i, j, curr);
-
174 if (j < l || r < i) {
-
175 return 0;
-
176 }
-
177 if (i >= l && j <= r) {
-
178 return curr->val;
-
179 }
-
180 uint32_t mid = i + (j - i) / 2;
-
181 return query(i, mid, l, r, curr->left) +
-
182 query(mid + 1, j, l, r, curr->right);
-
183 }
-
void lazy(const uint32_t &i, const uint32_t &j, std::shared_ptr< Node > const &curr)
If there is some value to be propagated to the passed node, value is added to the node and the childr...
-
int64_t query(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, std::shared_ptr< Node > const &curr)
Querying the range from index l to index r, checking at every node if it has some value to be propaga...
+

Definition at line 172 of file persistent_seg_tree_lazy_prop.cpp.

+
173 {
+
174 lazy(i, j, curr);
+
175 if (j < l || r < i) {
+
176 return 0;
+
177 }
+
178 if (i >= l && j <= r) {
+
179 return curr->val;
+
180 }
+
181 uint32_t mid = i + (j - i) / 2;
+
182 return query(i, mid, l, r, curr->left) +
+
183 query(mid + 1, j, l, r, curr->right);
+
184 }
+
void lazy(const uint32_t &i, const uint32_t &j, std::shared_ptr< Node > const &curr)
If there is some value to be propagated to the passed node, value is added to the node and the childr...
+
int64_t query(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, std::shared_ptr< Node > const &curr)
Querying the range from index l to index r, checking at every node if it has some value to be propaga...
@@ -490,10 +490,10 @@ Private Attributes
Returns
sum of elements whose index x satisfies l<=x<=r
-

Definition at line 241 of file persistent_seg_tree_lazy_prop.cpp.

-
246 {
-
247 return query(0, n - 1, l, r, ptrs[version]);
-
248 }
+

Definition at line 242 of file persistent_seg_tree_lazy_prop.cpp.

+
247 {
+
248 return query(0, n - 1, l, r, ptrs[version]);
+
249 }
@@ -523,10 +523,10 @@ Private Attributes

Getting the number of versions after updates so far which is equal to the size of the pointers vector.

Returns
the number of versions
-

Definition at line 255 of file persistent_seg_tree_lazy_prop.cpp.

-
258 {
-
259 return ptrs.size();
-
260 }
+

Definition at line 256 of file persistent_seg_tree_lazy_prop.cpp.

+
259 {
+
260 return ptrs.size();
+
261 }
@@ -591,26 +591,26 @@ Private Attributes
Returns
pointer to the current newly created node
-

Definition at line 135 of file persistent_seg_tree_lazy_prop.cpp.

-
138 {
-
139 lazy(i, j, curr);
-
140 if (i >= l && j <= r) {
-
141 std::shared_ptr<Node> newNode = newKid(curr);
-
142 newNode->prop += value;
-
143 lazy(i, j, newNode);
-
144 return newNode;
-
145 }
-
146 if (i > r || j < l) {
-
147 return curr;
-
148 }
-
149 auto newNode = std::make_shared<Node>(Node());
-
150 uint32_t mid = i + (j - i) / 2;
-
151 newNode->left = update(i, mid, l, r, value, curr->left);
-
152 newNode->right = update(mid + 1, j, l, r, value, curr->right);
-
153 newNode->val = newNode->left->val + newNode->right->val;
-
154 return newNode;
-
155 }
-
std::shared_ptr< Node > update(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, const int64_t &value, std::shared_ptr< Node > const &curr)
Doing range update, checking at every node if it has some value to be propagated. All nodes affected ...
+

Definition at line 136 of file persistent_seg_tree_lazy_prop.cpp.

+
139 {
+
140 lazy(i, j, curr);
+
141 if (i >= l && j <= r) {
+
142 std::shared_ptr<Node> newNode = newKid(curr);
+
143 newNode->prop += value;
+
144 lazy(i, j, newNode);
+
145 return newNode;
+
146 }
+
147 if (i > r || j < l) {
+
148 return curr;
+
149 }
+
150 auto newNode = std::make_shared<Node>(Node());
+
151 uint32_t mid = i + (j - i) / 2;
+
152 newNode->left = update(i, mid, l, r, value, curr->left);
+
153 newNode->right = update(mid + 1, j, l, r, value, curr->right);
+
154 newNode->val = newNode->left->val + newNode->right->val;
+
155 return newNode;
+
156 }
+
std::shared_ptr< Node > update(const uint32_t &i, const uint32_t &j, const uint32_t &l, const uint32_t &r, const int64_t &value, std::shared_ptr< Node > const &curr)
Doing range update, checking at every node if it has some value to be propagated. All nodes affected ...
@@ -657,13 +657,13 @@ Private Attributes
Returns
void
-

Definition at line 219 of file persistent_seg_tree_lazy_prop.cpp.

-
223 {
-
224 ptrs.push_back(update(
-
225 0, n - 1, l, r, value,
-
226 ptrs[ptrs.size() -
-
227 1])); // saving the root pointer to the new segment tree
-
228 }
+

Definition at line 220 of file persistent_seg_tree_lazy_prop.cpp.

+
224 {
+
225 ptrs.push_back(update(
+
226 0, n - 1, l, r, value,
+
227 ptrs[ptrs.size() -
+
228 1])); // saving the root pointer to the new segment tree
+
229 }
@@ -688,7 +688,7 @@ Private Attributes
-

Definition at line 52 of file persistent_seg_tree_lazy_prop.cpp.

+

Definition at line 53 of file persistent_seg_tree_lazy_prop.cpp.

@@ -714,8 +714,8 @@ Private Attributes

number of elements/leaf nodes in the segment tree

-

Definition at line 54 of file persistent_seg_tree_lazy_prop.cpp.

-
54{};
+

Definition at line 55 of file persistent_seg_tree_lazy_prop.cpp.

+
55{};
@@ -740,8 +740,8 @@ Private Attributes

ptrs[i] holds a root pointer to the segment tree after the ith update. ptrs[0] holds a root pointer to the segment tree before any updates

-

Definition at line 57 of file persistent_seg_tree_lazy_prop.cpp.

-
57{};
+

Definition at line 58 of file persistent_seg_tree_lazy_prop.cpp.

+
58{};
diff --git a/d8/d77/namespacemachine__learning.html b/d8/d77/namespacemachine__learning.html index 4d7e91798..6b0472fda 100644 --- a/d8/d77/namespacemachine__learning.html +++ b/d8/d77/namespacemachine__learning.html @@ -186,7 +186,7 @@ Variables

for std::vector

Machine learning algorithms.

A* is an informed search algorithm, or a best-first search, meaning that it is formulated in terms of weighted graphs: starting from a specific starting node of a graph (initial state), it aims to find a path to the given goal node having the smallest cost (least distance travelled, shortest time, etc.). It evaluates by maintaining a tree of paths originating at the start node and extending those paths one edge at a time until it reaches the final state. The weighted edges (or cost) is evaluated on two factors, G score (cost required from starting node or initial state to current state) and H score (cost required from current state to final state). The F(state), then is evaluated as: F(state) = G(state) + H(state).

-

To solve the given search with shortest cost or path possible is to inspect values having minimum F(state).

Author
Ashish Daulatabad for std::reverse function for std::array, representing EightPuzzle board for assert for std::function STL for IO operations for std::map STL for std::shared_ptr for std::set STL for std::vector STL
+

To solve the given search with shortest cost or path possible is to inspect values having minimum F(state).

Author
Ashish Daulatabad for std::reverse function for std::array, representing EightPuzzle board for assert for std::uint32_t for std::function STL for IO operations for std::map STL for std::shared_ptr for std::set STL for std::vector STL

Machine learning algorithms

for std::transform and std::sort for assert for std::pow and std::sqrt for std::cout for std::accumulate for std::unordered_map

Machine learning algorithms

diff --git a/d9/d35/classrange__queries_1_1heavy__light__decomposition_1_1_s_g.html b/d9/d35/classrange__queries_1_1heavy__light__decomposition_1_1_s_g.html index 1182f038a..dffc23641 100644 --- a/d9/d35/classrange__queries_1_1heavy__light__decomposition_1_1_s_g.html +++ b/d9/d35/classrange__queries_1_1heavy__light__decomposition_1_1_s_g.html @@ -167,7 +167,7 @@ class range_queries::heavy_light_decomposition::SG< X >

Segment -

Definition at line 254 of file heavy_light_decomposition.cpp.

+

Definition at line 257 of file heavy_light_decomposition.cpp.

Constructor & Destructor Documentation

◆ SG()

@@ -203,14 +203,14 @@ template<typename X>
Returns
void
-

Definition at line 282 of file heavy_light_decomposition.cpp.

-
282 {
-
283 s_size = size;
-
284 s_tree.assign(2 * s_size, 0ll);
-
285 }
- -
int s_size
number of leaves in the segment tree
-
std::vector< X > s_tree
Everything here is private, and can only be accessed through the methods, in the derived class (HLD)
+

Definition at line 286 of file heavy_light_decomposition.cpp.

+
286 {
+
287 s_size = size;
+
288 s_tree.assign(2 * s_size, 0ll);
+
289 }
+ +
int s_size
number of leaves in the segment tree
+
std::vector< X > s_tree
Everything here is private, and can only be accessed through the methods, in the derived class (HLD)
@@ -254,8 +254,8 @@ template<typename X>
Returns
the combined result
-

Definition at line 274 of file heavy_light_decomposition.cpp.

-
274{ return lhs + rhs; }
+

Definition at line 278 of file heavy_light_decomposition.cpp.

+
278{ return lhs + rhs; }
@@ -298,21 +298,21 @@ template<typename X>
Returns
void
-

Definition at line 305 of file heavy_light_decomposition.cpp.

-
305 {
- -
307 for (l += s_size, r += s_size + 1; l < r; l >>= 1, r >>= 1) {
-
308 if (l & 1) {
-
309 lhs = combine(lhs, s_tree[l++]);
-
310 }
-
311 if (r & 1) {
-
312 rhs = combine(s_tree[--r], rhs);
-
313 }
-
314 }
-
315 return combine(lhs, rhs);
-
316 }
-
X combine(X lhs, X rhs)
Function that specifies the type of operation involved when segments are combined.
- +

Definition at line 309 of file heavy_light_decomposition.cpp.

+
309 {
+ +
311 for (l += s_size, r += s_size + 1; l < r; l >>= 1, r >>= 1) {
+
312 if (l & 1) {
+
313 lhs = combine(lhs, s_tree[l++]);
+
314 }
+
315 if (r & 1) {
+
316 rhs = combine(s_tree[--r], rhs);
+
317 }
+
318 }
+
319 return combine(lhs, rhs);
+
320 }
+
X combine(X lhs, X rhs)
Function that specifies the type of operation involved when segments are combined.
+
@@ -355,8 +355,8 @@ template<typename X>
-

Definition at line 329 of file heavy_light_decomposition.cpp.

-
+

Definition at line 334 of file heavy_light_decomposition.cpp.

+
@@ -399,12 +399,12 @@ template<typename X>
Returns
void
-

Definition at line 293 of file heavy_light_decomposition.cpp.

-
293 {
-
294 for (p += s_size; p > 0; p >>= 1) {
-
295 s_tree[p] += v;
-
296 }
-
297 }
+

Definition at line 297 of file heavy_light_decomposition.cpp.

+
297 {
+
298 for (p += s_size; p > 0; p >>= 1) {
+
299 s_tree[p] += v;
+
300 }
+
301 }
@@ -433,7 +433,7 @@ template<typename T>
-

Definition at line 265 of file heavy_light_decomposition.cpp.

+

Definition at line 269 of file heavy_light_decomposition.cpp.

@@ -462,7 +462,7 @@ template<typename X>

number of leaves in the segment tree

-

Definition at line 263 of file heavy_light_decomposition.cpp.

+

Definition at line 266 of file heavy_light_decomposition.cpp.

@@ -491,7 +491,7 @@ template<typename X>

Everything here is private, and can only be accessed through the methods, in the derived class (HLD)

the segment tree, stored as a vector

-

Definition at line 262 of file heavy_light_decomposition.cpp.

+

Definition at line 265 of file heavy_light_decomposition.cpp.

@@ -519,7 +519,7 @@ template<typename X>

inital query return value

-

Definition at line 264 of file heavy_light_decomposition.cpp.

+

Definition at line 267 of file heavy_light_decomposition.cpp.

diff --git a/da/d02/classmachine__learning_1_1aystar__search_1_1_ay_star_search.html b/da/d02/classmachine__learning_1_1aystar__search_1_1_ay_star_search.html index 1d779ece8..2f154a804 100644 --- a/da/d02/classmachine__learning_1_1aystar__search_1_1_ay_star_search.html +++ b/da/d02/classmachine__learning_1_1aystar__search_1_1_ay_star_search.html @@ -171,7 +171,7 @@ class machine_learning::aystar_search::AyStarSearch< Puzzle >

A cl -

Definition at line 288 of file a_star_search.cpp.

+

Definition at line 290 of file a_star_search.cpp.

Member Typedef Documentation

◆ MapOfPuzzleInfoWithInteger

@@ -188,9 +188,9 @@ template<typename Puzzle>
Initial value:
std::map<std::shared_ptr<Info>, uint32_t, comparison_operator>
- +
-

Definition at line 382 of file a_star_search.cpp.

+

Definition at line 384 of file a_star_search.cpp.

@@ -211,7 +211,7 @@ template<typename Puzzle>
std::map<std::shared_ptr<Info>, std::shared_ptr<Info>,
comparison_operator>
-

Definition at line 378 of file a_star_search.cpp.

+

Definition at line 380 of file a_star_search.cpp.

@@ -231,7 +231,7 @@ template<typename Puzzle> Initial value:
std::set<std::shared_ptr<Info>, comparison_operator>
-

Definition at line 385 of file a_star_search.cpp.

+

Definition at line 387 of file a_star_search.cpp.

@@ -274,12 +274,12 @@ template<typename Puzzle> -

Definition at line 392 of file a_star_search.cpp.

-
392 {
-
393 Initial = std::make_shared<Info>(initial);
-
394 Final = std::make_shared<Info>(final);
-
395 }
-
A class defining A* search algorithm. for some initial state and final state.
+

Definition at line 394 of file a_star_search.cpp.

+
394 {
+ +
396 Final = std::make_shared<Info>(final);
+
397 }
+
A class defining A* search algorithm. for some initial state and final state.
@@ -325,103 +325,103 @@ template<typename Puzzle>

Stores the list to explore

Stores the list that are explored

-

Definition at line 429 of file a_star_search.cpp.

-
431 {
-
432 MapOfPuzzleInfoWithPuzzleInfo
-
433 parent_of;
-
434 MapOfPuzzleInfoWithInteger g_score;
-
435 SetOfPuzzleInfo open_list;
-
436 SetOfPuzzleInfo closed_list;
-
437
-
438 // Before starting the AyStartSearch, initialize the set and maps
-
439 open_list.emplace(Initial);
-
440 parent_of[Initial] = nullptr;
-
441 g_score[Initial] = 0;
-
442
-
443 while (!open_list.empty()) {
-
444 // Iterator for state having having lowest f_score.
-
445 typename SetOfPuzzleInfo::iterator it_low_f_score;
-
446 uint32_t min_f_score = 1e9;
-
447 for (auto iter = open_list.begin(); iter != open_list.end();
-
448 ++iter) {
-
449 // f score here is evaluated by g score (depth) and h score
-
450 // (distance between current state and final state)
-
451 uint32_t f_score = (*iter)->heuristic_value + (*iter)->depth;
-
452 if (f_score < min_f_score) {
-
453 min_f_score = f_score;
-
454 it_low_f_score = iter;
-
455 }
-
456 }
-
457
-
458 // current_state, stores lowest f score so far for this state.
-
459 std::shared_ptr<Info> current_state = *it_low_f_score;
-
460
-
461 // if this current state is equal to final, return
-
462 if (*(current_state->state) == *(Final->state)) {
-
463 return Solution(current_state, parent_of);
-
464 }
-
465 // else remove from open list as visited.
-
466 open_list.erase(it_low_f_score);
-
467 // if current_state has exceeded the allowed depth, skip
-
468 // neighbor checking
-
469 if (current_state->depth >= permissible_depth) {
-
470 continue;
-
471 }
-
472 // Generate all possible moves (neighbors) given the current
-
473 // state
-
474 std::vector<Puzzle> total_possible_moves =
-
475 current_state->state->generate_possible_moves();
-
476
-
477 for (Puzzle &neighbor : total_possible_moves) {
-
478 // calculate score of neighbors with respect to
-
479 // current_state
-
480 std::shared_ptr<Info> Neighbor = std::make_shared<Info>(
-
481 neighbor, dist(neighbor, *(Final->state)),
-
482 current_state->depth + 1U);
-
483 uint32_t temp_g_score = Neighbor->depth;
-
484
-
485 // Check whether this state is explored.
-
486 // If this state is discovered at greater depth, then discard,
-
487 // else remove from closed list and explore the node
-
488 auto closed_list_iter = closed_list.find(Neighbor);
-
489 if (closed_list_iter != closed_list.end()) {
-
490 // 1. If state in closed list has higher depth, then remove
-
491 // from list since we have found better option,
-
492 // 2. Else don't explore this state.
-
493 if (Neighbor->depth < (*closed_list_iter)->depth) {
-
494 closed_list.erase(closed_list_iter);
-
495 } else {
-
496 continue;
-
497 }
-
498 }
-
499 auto neighbor_g_score_iter = g_score.find(Neighbor);
-
500 // if the neighbor is already created and has minimum
-
501 // g_score, then update g_score and f_score else insert new
-
502 if (neighbor_g_score_iter != g_score.end()) {
-
503 if (neighbor_g_score_iter->second > temp_g_score) {
-
504 neighbor_g_score_iter->second = temp_g_score;
-
505 parent_of[Neighbor] = current_state;
-
506 }
-
507 } else {
-
508 g_score[Neighbor] = temp_g_score;
-
509 parent_of[Neighbor] = current_state;
-
510 }
-
511 // If this is a new state, insert into open_list
-
512 // else update if the this state has better g score than
-
513 // existing one.
-
514 auto iter = open_list.find(Neighbor);
-
515 if (iter == open_list.end()) {
-
516 open_list.emplace(Neighbor);
-
517 } else if ((*iter)->depth > Neighbor->depth) {
-
518 (*iter)->depth = Neighbor->depth;
-
519 }
-
520 }
-
521 closed_list.emplace(current_state);
-
522 }
-
523 // Cannot find the solution, return empty vector
-
524 return std::vector<Puzzle>(0);
-
525 }
-
std::vector< Puzzle > Solution(std::shared_ptr< Info > FinalState, const MapOfPuzzleInfoWithPuzzleInfo &parent_of)
A helper solution: launches when a solution for AyStarSearch is found.
+

Definition at line 431 of file a_star_search.cpp.

+
433 {
+
434 MapOfPuzzleInfoWithPuzzleInfo
+
435 parent_of;
+
436 MapOfPuzzleInfoWithInteger g_score;
+
437 SetOfPuzzleInfo open_list;
+
438 SetOfPuzzleInfo closed_list;
+
439
+
440 // Before starting the AyStartSearch, initialize the set and maps
+
441 open_list.emplace(Initial);
+
442 parent_of[Initial] = nullptr;
+
443 g_score[Initial] = 0;
+
444
+
445 while (!open_list.empty()) {
+
446 // Iterator for state having having lowest f_score.
+
447 typename SetOfPuzzleInfo::iterator it_low_f_score;
+ +
449 for (auto iter = open_list.begin(); iter != open_list.end();
+
450 ++iter) {
+
451 // f score here is evaluated by g score (depth) and h score
+
452 // (distance between current state and final state)
+
453 uint32_t f_score = (*iter)->heuristic_value + (*iter)->depth;
+
454 if (f_score < min_f_score) {
+ + +
457 }
+
458 }
+
459
+
460 // current_state, stores lowest f score so far for this state.
+ +
462
+
463 // if this current state is equal to final, return
+
464 if (*(current_state->state) == *(Final->state)) {
+ +
466 }
+
467 // else remove from open list as visited.
+ +
469 // if current_state has exceeded the allowed depth, skip
+
470 // neighbor checking
+
471 if (current_state->depth >= permissible_depth) {
+
472 continue;
+
473 }
+
474 // Generate all possible moves (neighbors) given the current
+
475 // state
+ +
477 current_state->state->generate_possible_moves();
+
478
+ +
480 // calculate score of neighbors with respect to
+
481 // current_state
+ +
483 neighbor, dist(neighbor, *(Final->state)),
+
484 current_state->depth + 1U);
+ +
486
+
487 // Check whether this state is explored.
+
488 // If this state is discovered at greater depth, then discard,
+
489 // else remove from closed list and explore the node
+ +
491 if (closed_list_iter != closed_list.end()) {
+
492 // 1. If state in closed list has higher depth, then remove
+
493 // from list since we have found better option,
+
494 // 2. Else don't explore this state.
+
495 if (Neighbor->depth < (*closed_list_iter)->depth) {
+ +
497 } else {
+
498 continue;
+
499 }
+
500 }
+ +
502 // if the neighbor is already created and has minimum
+
503 // g_score, then update g_score and f_score else insert new
+
504 if (neighbor_g_score_iter != g_score.end()) {
+
505 if (neighbor_g_score_iter->second > temp_g_score) {
+ + +
508 }
+
509 } else {
+ + +
512 }
+
513 // If this is a new state, insert into open_list
+
514 // else update if the this state has better g score than
+
515 // existing one.
+
516 auto iter = open_list.find(Neighbor);
+
517 if (iter == open_list.end()) {
+
518 open_list.emplace(Neighbor);
+
519 } else if ((*iter)->depth > Neighbor->depth) {
+
520 (*iter)->depth = Neighbor->depth;
+
521 }
+
522 }
+
523 closed_list.emplace(current_state);
+
524 }
+
525 // Cannot find the solution, return empty vector
+
526 return std::vector<Puzzle>(0);
+
527 }
+
std::vector< Puzzle > Solution(std::shared_ptr< Info > FinalState, const MapOfPuzzleInfoWithPuzzleInfo &parent_of)
A helper solution: launches when a solution for AyStarSearch is found.
@@ -464,21 +464,21 @@ template<typename Puzzle>
Returns
the list of moves denoting moves from final state to initial state (in reverse)
-

Definition at line 405 of file a_star_search.cpp.

-
407 {
-
408 // Useful for traversing from final state to current state.
-
409 auto current_state = FinalState;
-
410 /*
-
411 * For storing the solution tree starting from initial state to
-
412 * final state
-
413 */
-
414 std::vector<Puzzle> answer;
-
415 while (current_state != nullptr) {
-
416 answer.emplace_back(*current_state->state);
-
417 current_state = parent_of.find(current_state)->second;
-
418 }
-
419 return answer;
-
420 }
+

Definition at line 407 of file a_star_search.cpp.

+
409 {
+
410 // Useful for traversing from final state to current state.
+ +
412 /*
+
413 * For storing the solution tree starting from initial state to
+
414 * final state
+
415 */
+ +
417 while (current_state != nullptr) {
+
418 answer.emplace_back(*current_state->state);
+
419 current_state = parent_of.find(current_state)->second;
+
420 }
+
421 return answer;
+
422 }
@@ -505,7 +505,7 @@ template<typename Puzzle>
-

Definition at line 366 of file a_star_search.cpp.

+

Definition at line 368 of file a_star_search.cpp.

@@ -531,7 +531,7 @@ template<typename Puzzle>
-

Definition at line 365 of file a_star_search.cpp.

+

Definition at line 367 of file a_star_search.cpp.

diff --git a/da/d70/structmachine__learning_1_1aystar__search_1_1_ay_star_search_1_1_info.html b/da/d70/structmachine__learning_1_1aystar__search_1_1_ay_star_search_1_1_info.html index 1b46cb7d8..67e61b8ec 100644 --- a/da/d70/structmachine__learning_1_1aystar__search_1_1_ay_star_search_1_1_info.html +++ b/da/d70/structmachine__learning_1_1aystar__search_1_1_ay_star_search_1_1_info.html @@ -157,7 +157,7 @@ Public Attributes
template<typename Puzzle>
struct machine_learning::aystar_search::AyStarSearch< Puzzle >::Info

Struct that handles all the information related to the current state.

-

Definition at line 293 of file a_star_search.cpp.

+

Definition at line 295 of file a_star_search.cpp.

Constructor & Destructor Documentation

◆ Info() [1/5]

@@ -222,9 +222,9 @@ template<typename Puzzle> -

Definition at line 307 of file a_star_search.cpp.

-
307: state(std::make_shared<Puzzle>(A)) {}
-
A class defining A* search algorithm. for some initial state and final state.
+

Definition at line 309 of file a_star_search.cpp.

+
309: state(std::make_shared<Puzzle>(A)) {}
+
A class defining A* search algorithm. for some initial state and final state.
@@ -272,12 +272,12 @@ template<typename Puzzle> -

Definition at line 315 of file a_star_search.cpp.

-
316 : state(std::make_shared<Puzzle>(A)),
-
317 heuristic_value(h_value),
-
318 depth(d) {}
-
size_t depth
stores h score
-
size_t heuristic_value
Holds the current state.
+

Definition at line 317 of file a_star_search.cpp.

+
318 : state(std::make_shared<Puzzle>(A)),
+ +
320 depth(d) {}
+ +
@@ -314,10 +314,10 @@ template<typename Puzzle> -

Definition at line 324 of file a_star_search.cpp.

-
325 : state(std::make_shared<Puzzle>(A.state)),
-
326 heuristic_value(A.heuristic_value),
-
327 depth(A.depth) {}
+

Definition at line 326 of file a_star_search.cpp.

+
327 : state(std::make_shared<Puzzle>(A.state)),
+
328 heuristic_value(A.heuristic_value),
+
329 depth(A.depth) {}
@@ -354,10 +354,10 @@ template<typename Puzzle> -

Definition at line 333 of file a_star_search.cpp.

-
334 : state(std::make_shared<Puzzle>(std::move(A.state))),
-
335 heuristic_value(std::move(A.heuristic_value)),
-
336 depth(std::move(A.depth)) {}
+

Definition at line 335 of file a_star_search.cpp.

+
336 : state(std::make_shared<Puzzle>(std::move(A.state))),
+
337 heuristic_value(std::move(A.heuristic_value)),
+
338 depth(std::move(A.depth)) {}
@@ -395,13 +395,13 @@ template<typename Puzzle> -

Definition at line 342 of file a_star_search.cpp.

-
342 {
-
343 state = A.state;
-
344 heuristic_value = A.heuristic_value;
-
345 depth = A.depth;
-
346 return *this;
-
347 }
+

Definition at line 344 of file a_star_search.cpp.

+
344 {
+
345 state = A.state;
+
346 heuristic_value = A.heuristic_value;
+
347 depth = A.depth;
+
348 return *this;
+
349 }
@@ -438,13 +438,13 @@ template<typename Puzzle> -

Definition at line 353 of file a_star_search.cpp.

-
353 {
-
354 state = std::move(A.state);
-
355 heuristic_value = std::move(A.heuristic_value);
-
356 depth = std::move(A.depth);
-
357 return *this;
-
358 }
+

Definition at line 355 of file a_star_search.cpp.

+
355 {
+
356 state = std::move(A.state);
+
357 heuristic_value = std::move(A.heuristic_value);
+
358 depth = std::move(A.depth);
+
359 return *this;
+
360 }
@@ -465,7 +465,7 @@ template<typename Puzzle>

stores h score

-

Definition at line 296 of file a_star_search.cpp.

+

Definition at line 298 of file a_star_search.cpp.

@@ -485,7 +485,7 @@ template<typename Puzzle>

Holds the current state.

-

Definition at line 295 of file a_star_search.cpp.

+

Definition at line 297 of file a_star_search.cpp.

@@ -503,7 +503,7 @@ template<typename Puzzle>
-

Definition at line 294 of file a_star_search.cpp.

+

Definition at line 296 of file a_star_search.cpp.

diff --git a/dd/d69/namespacerange__queries.html b/dd/d69/namespacerange__queries.html index 17de602ba..351d8e8c8 100644 --- a/dd/d69/namespacerange__queries.html +++ b/dd/d69/namespacerange__queries.html @@ -134,7 +134,7 @@ Classes

Algorithms and Data Structures that support range queries and updates.

for assert for IO operations

Range Queries

-

for IO operations to manage dynamic memory for std::vector

+

for std::uint32_t for IO operations to manage dynamic memory for std::vector

for assert for IO operations

Range Queries algorithms

diff --git a/dd/d9c/classmachine__learning_1_1aystar__search_1_1_eight_puzzle.html b/dd/d9c/classmachine__learning_1_1aystar__search_1_1_eight_puzzle.html index 762463dd1..5af4410a4 100644 --- a/dd/d9c/classmachine__learning_1_1aystar__search_1_1_eight_puzzle.html +++ b/dd/d9c/classmachine__learning_1_1aystar__search_1_1_eight_puzzle.html @@ -189,7 +189,7 @@ class machine_learning::aystar_search::EightPuzzle< N >

A class de -

Definition at line 60 of file a_star_search.cpp.

+

Definition at line 62 of file a_star_search.cpp.

Constructor & Destructor Documentation

◆ EightPuzzle() [1/4]

@@ -218,15 +218,15 @@ template<size_t N = 3>

Default constructor for EightPuzzle.

-

Definition at line 121 of file a_star_search.cpp.

-
121 {
-
122 for (size_t i = 0; i < N; ++i) {
-
123 for (size_t j = 0; j < N; ++j) {
-
124 board[i][j] = ((i * 3 + j + 1) % (N * N));
-
125 }
-
126 }
-
127 }
-
A class defining EightPuzzle/15-Puzzle game.
+

Definition at line 123 of file a_star_search.cpp.

+
123 {
+
124 for (size_t i = 0; i < N; ++i) {
+
125 for (size_t j = 0; j < N; ++j) {
+
126 board[i][j] = ((i * 3 + j + 1) % (N * N));
+
127 }
+
128 }
+
129 }
+
A class defining EightPuzzle/15-Puzzle game.
@@ -263,8 +263,8 @@ template<size_t N = 3> -

Definition at line 132 of file a_star_search.cpp.

-
133 : board(init) {}
+

Definition at line 134 of file a_star_search.cpp.

+
135 : board(init) {}
@@ -301,8 +301,8 @@ template<size_t N = 3> -

Definition at line 139 of file a_star_search.cpp.

-
139: board(A.board) {}
+

Definition at line 141 of file a_star_search.cpp.

+
141: board(A.board) {}
@@ -339,8 +339,8 @@ template<size_t N = 3> -

Definition at line 145 of file a_star_search.cpp.

-
146 : board(std::move(A.board)) {}
+

Definition at line 147 of file a_star_search.cpp.

+
148 : board(std::move(A.board)) {}
@@ -373,17 +373,17 @@ template<size_t N = 3>

A helper array to evaluate the next state from current state;.

Finds an empty space in puzzle (in this case; a zero)

Returns
a pair indicating integer distances from top and right respectively, else returns -1, -1
-

Definition at line 75 of file a_star_search.cpp.

-
75 {
-
76 for (size_t i = 0; i < N; ++i) {
-
77 for (size_t j = 0; j < N; ++j) {
-
78 if (!board[i][j]) {
-
79 return {i, j};
-
80 }
-
81 }
-
82 }
-
83 return {-1, -1};
-
84 }
+

Definition at line 77 of file a_star_search.cpp.

+
77 {
+
78 for (size_t i = 0; i < N; ++i) {
+
79 for (size_t j = 0; j < N; ++j) {
+
80 if (!board[i][j]) {
+
81 return {i, j};
+
82 }
+
83 }
+
84 }
+
85 return {-1, -1};
+
86 }
@@ -416,30 +416,30 @@ template<size_t N = 3>
Returns
list of vector containing all possible next moves
Note
the implementation is compulsory to create A* search
-

Definition at line 176 of file a_star_search.cpp.

-
176 {
-
177 auto zero_pos = find_zero();
-
178 // vector which will contain all possible state from current state
-
179 std::vector<EightPuzzle<N>> NewStates;
-
180 for (auto &move : moves) {
-
181 if (in_range(zero_pos.first + move.first) &&
-
182 in_range(zero_pos.second + move.second)) {
-
183 // swap with the possible moves
-
184 std::array<std::array<uint32_t, N>, N> new_config = board;
-
185 std::swap(new_config[zero_pos.first][zero_pos.second],
-
186 new_config[zero_pos.first + move.first]
-
187 [zero_pos.second + move.second]);
-
188 EightPuzzle<N> new_state(new_config);
-
189 // Store new state and calculate heuristic value, and depth
-
190 NewStates.emplace_back(new_state);
-
191 }
-
192 }
-
193 return NewStates;
-
194 }
-
EightPuzzle()
Default constructor for EightPuzzle.
-
bool in_range(const uint32_t value) const
check whether the index value is bounded within the puzzle area
-
std::pair< uint32_t, uint32_t > find_zero()
A helper array to evaluate the next state from current state;.
-
std::vector< std::pair< int8_t, int8_t > > moves
N x N array to store the current state of the Puzzle.
+

Definition at line 178 of file a_star_search.cpp.

+
178 {
+
179 auto zero_pos = find_zero();
+
180 // vector which will contain all possible state from current state
+ +
182 for (auto &move : moves) {
+
183 if (in_range(zero_pos.first + move.first) &&
+
184 in_range(zero_pos.second + move.second)) {
+
185 // swap with the possible moves
+ +
187 std::swap(new_config[zero_pos.first][zero_pos.second],
+
188 new_config[zero_pos.first + move.first]
+
189 [zero_pos.second + move.second]);
+ +
191 // Store new state and calculate heuristic value, and depth
+
192 NewStates.emplace_back(new_state);
+
193 }
+
194 }
+
195 return NewStates;
+
196 }
+
EightPuzzle()
Default constructor for EightPuzzle.
+
bool in_range(const uint32_t value) const
check whether the index value is bounded within the puzzle area
+
std::pair< uint32_t, uint32_t > find_zero()
A helper array to evaluate the next state from current state;.
+
std::vector< std::pair< int8_t, int8_t > > moves
N x N array to store the current state of the Puzzle.
@@ -484,13 +484,13 @@ template<size_t N = 3>
-1 if invalid i or j position
-

Definition at line 102 of file a_star_search.cpp.

-
102 {
-
103 if (in_range(i) && in_range(j)) {
-
104 return board[i][j];
-
105 }
-
106 return -1;
-
107 }
+

Definition at line 104 of file a_star_search.cpp.

+
104 {
+
105 if (in_range(i) && in_range(j)) {
+
106 return board[i][j];
+
107 }
+
108 return -1;
+
109 }
@@ -522,8 +522,8 @@ template<size_t N = 3>

returns the size of the EightPuzzle (number of row / column)

Returns
N, the size of the puzzle.
-

Definition at line 117 of file a_star_search.cpp.

-
117{ return N; }
+

Definition at line 119 of file a_star_search.cpp.

+
119{ return N; }
@@ -554,8 +554,8 @@ template<size_t N = 3>

Returns the current state of the board.

-

Definition at line 111 of file a_star_search.cpp.

-
111{ return board; }
+

Definition at line 113 of file a_star_search.cpp.

+
113{ return board; }
@@ -593,8 +593,8 @@ template<size_t N = 3>
Returns
true if index is within the board, else false
-

Definition at line 90 of file a_star_search.cpp.

-
90{ return value < N; }
+

Definition at line 92 of file a_star_search.cpp.

+
92{ return value < N; }
@@ -626,17 +626,17 @@ template<size_t N = 3>

check whether one board is lexicographically smaller

Returns
true if this->state is lexicographically smaller than check.state, else false
-

Definition at line 218 of file a_star_search.cpp.

-
218 {
-
219 for (size_t i = 0; i < N; ++i) {
-
220 for (size_t j = 0; j < N; ++j) {
-
221 if (board[i][j] != check.board[i][j]) {
-
222 return board[i][j] < check.board[i][j];
-
223 }
-
224 }
-
225 }
-
226 return false;
-
227 }
+

Definition at line 220 of file a_star_search.cpp.

+
220 {
+
221 for (size_t i = 0; i < N; ++i) {
+
222 for (size_t j = 0; j < N; ++j) {
+
223 if (board[i][j] != check.board[i][j]) {
+
224 return board[i][j] < check.board[i][j];
+
225 }
+
226 }
+
227 }
+
228 return false;
+
229 }
@@ -668,17 +668,17 @@ template<size_t N = 3>

check whether one board is lexicographically smaller or equal

Returns
true if this->state is lexicographically smaller than check.state or same, else false
-

Definition at line 233 of file a_star_search.cpp.

-
233 {
-
234 for (size_t i = 0; i < N; ++i) {
-
235 for (size_t j = 0; j < N; ++j) {
-
236 if (board[i][j] != check.board[i][j]) {
-
237 return board[i][j] < check.board[i][j];
-
238 }
-
239 }
-
240 }
-
241 return true;
-
242 }
+

Definition at line 235 of file a_star_search.cpp.

+
235 {
+
236 for (size_t i = 0; i < N; ++i) {
+
237 for (size_t j = 0; j < N; ++j) {
+
238 if (board[i][j] != check.board[i][j]) {
+
239 return board[i][j] < check.board[i][j];
+
240 }
+
241 }
+
242 }
+
243 return true;
+
244 }
@@ -715,11 +715,11 @@ template<size_t N = 3> -

Definition at line 156 of file a_star_search.cpp.

-
156 {
-
157 board = A.board;
-
158 return *this;
-
159 }
+

Definition at line 158 of file a_star_search.cpp.

+
158 {
+
159 board = A.board;
+
160 return *this;
+
161 }
@@ -756,11 +756,11 @@ template<size_t N = 3> -

Definition at line 165 of file a_star_search.cpp.

-
165 {
-
166 board = std::move(A.board);
-
167 return *this;
-
168 }
+

Definition at line 167 of file a_star_search.cpp.

+
167 {
+
168 board = std::move(A.board);
+
169 return *this;
+
170 }
@@ -792,21 +792,21 @@ template<size_t N = 3>

check whether two boards are equal

Returns
true if check.state is equal to this->state, else false
-

Definition at line 200 of file a_star_search.cpp.

-
200 {
-
201 if (check.get_size() != N) {
-
202 return false;
-
203 }
-
204 for (size_t i = 0; i < N; ++i) {
-
205 for (size_t j = 0; j < N; ++j) {
-
206 if (board[i][j] != check.board[i][j]) {
-
207 return false;
-
208 }
-
209 }
-
210 }
-
211 return true;
-
212 }
-
size_t get_size() const
returns the size of the EightPuzzle (number of row / column)
+

Definition at line 202 of file a_star_search.cpp.

+
202 {
+
203 if (check.get_size() != N) {
+
204 return false;
+
205 }
+
206 for (size_t i = 0; i < N; ++i) {
+
207 for (size_t j = 0; j < N; ++j) {
+
208 if (board[i][j] != check.board[i][j]) {
+
209 return false;
+
210 }
+
211 }
+
212 }
+
213 return true;
+
214 }
+
size_t get_size() const
returns the size of the EightPuzzle (number of row / column)
@@ -850,16 +850,16 @@ template<size_t N = 3>
Returns
ostream operator op
-

Definition at line 250 of file a_star_search.cpp.

-
251 {
-
252 for (size_t i = 0; i < N; ++i) {
-
253 for (size_t j = 0; j < N; ++j) {
-
254 op << SomeState.board[i][j] << " ";
-
255 }
-
256 op << "\n";
-
257 }
-
258 return op;
-
259 }
+

Definition at line 252 of file a_star_search.cpp.

+
253 {
+
254 for (size_t i = 0; i < N; ++i) {
+
255 for (size_t j = 0; j < N; ++j) {
+
256 op << SomeState.board[i][j] << " ";
+
257 }
+
258 op << "\n";
+
259 }
+
260 return op;
+
261 }
@@ -886,7 +886,7 @@ template<size_t N = 3>
-

Definition at line 62 of file a_star_search.cpp.

+

Definition at line 64 of file a_star_search.cpp.

@@ -920,13 +920,13 @@ template<size_t N = 3>

N x N array to store the current state of the Puzzle.

-

Definition at line 64 of file a_star_search.cpp.

-
64 {
-
65 {0, 1},
-
66 {1, 0},
-
67 {0, -1},
-
68 {-1,
-
69 0}};
+

Definition at line 66 of file a_star_search.cpp.

+
66 {
+
67 {0, 1},
+
68 {1, 0},
+
69 {0, -1},
+
70 {-1,
+
71 0}};
diff --git a/dd/dec/a__star__search_8cpp_source.html b/dd/dec/a__star__search_8cpp_source.html index 220c05e77..d41b22dc5 100644 --- a/dd/dec/a__star__search_8cpp_source.html +++ b/dd/dec/a__star__search_8cpp_source.html @@ -120,576 +120,578 @@ $(function(){initNavTree('dd/dec/a__star__search_8cpp_source.html','../../','');
22#include <algorithm>
23#include <array>
24#include <cassert>
-
25#include <functional>
-
26#include <iostream>
-
27#include <map>
-
28#include <memory>
-
29#include <set>
-
30#include <vector>
-
35namespace machine_learning {
-
41namespace aystar_search {
-
59template <size_t N = 3>
-
-
60class EightPuzzle {
-
61 std::array<std::array<uint32_t, N>, N>
-
62 board;
-
63
-
-
64 std::vector<std::pair<int8_t, int8_t>> moves = {
-
65 {0, 1},
-
66 {1, 0},
-
67 {0, -1},
-
68 {-1,
-
69 0}};
+
25#include <cstdint>
+
26#include <functional>
+
27#include <iostream>
+
28#include <map>
+
29#include <memory>
+
30#include <set>
+
31#include <vector>
+
32
+
37namespace machine_learning {
+
43namespace aystar_search {
+
61template <size_t N = 3>
+
+ +
63 std::array<std::array<uint32_t, N>, N>
+
64 board;
+
65
+
+
66 std::vector<std::pair<int8_t, int8_t>> moves = {
+
67 {0, 1},
+
68 {1, 0},
+
69 {0, -1},
+
70 {-1,
+
71 0}};
-
70
-
-
75 std::pair<uint32_t, uint32_t> find_zero() {
-
76 for (size_t i = 0; i < N; ++i) {
-
77 for (size_t j = 0; j < N; ++j) {
-
78 if (!board[i][j]) {
-
79 return {i, j};
-
80 }
-
81 }
-
82 }
-
83 return {-1, -1};
-
84 }
+
72
+
+
77 std::pair<uint32_t, uint32_t> find_zero() {
+
78 for (size_t i = 0; i < N; ++i) {
+
79 for (size_t j = 0; j < N; ++j) {
+
80 if (!board[i][j]) {
+
81 return {i, j};
+
82 }
+
83 }
+
84 }
+
85 return {-1, -1};
+
86 }
-
85
-
90 inline bool in_range(const uint32_t value) const { return value < N; }
-
91
-
92 public:
-
-
102 uint32_t get(size_t i, size_t j) const {
-
103 if (in_range(i) && in_range(j)) {
-
104 return board[i][j];
-
105 }
-
106 return -1;
-
107 }
+
87
+
92 inline bool in_range(const uint32_t value) const { return value < N; }
+
93
+
94 public:
+
+
104 uint32_t get(size_t i, size_t j) const {
+
105 if (in_range(i) && in_range(j)) {
+
106 return board[i][j];
+
107 }
+
108 return -1;
+
109 }
-
108
-
111 std::array<std::array<uint32_t, N>, N> get_state() { return board; }
-
112
-
117 inline size_t get_size() const { return N; }
-
- -
122 for (size_t i = 0; i < N; ++i) {
-
123 for (size_t j = 0; j < N; ++j) {
-
124 board[i][j] = ((i * 3 + j + 1) % (N * N));
-
125 }
-
126 }
-
127 }
+
110
+
113 std::array<std::array<uint32_t, N>, N> get_state() { return board; }
+
114
+
119 inline size_t get_size() const { return N; }
+
+ +
124 for (size_t i = 0; i < N; ++i) {
+
125 for (size_t j = 0; j < N; ++j) {
+
126 board[i][j] = ((i * 3 + j + 1) % (N * N));
+
127 }
+
128 }
+
129 }
-
128
-
-
132 explicit EightPuzzle(const std::array<std::array<uint32_t, N>, N> &init)
-
133 : board(init) {}
+
130
+
+
134 explicit EightPuzzle(const std::array<std::array<uint32_t, N>, N> &init)
+
135 : board(init) {}
-
134
-
139 EightPuzzle(const EightPuzzle<N> &A) : board(A.board) {}
-
140
-
-
145 EightPuzzle(const EightPuzzle<N> &&A) noexcept
-
146 : board(std::move(A.board)) {}
+
136
+
141 EightPuzzle(const EightPuzzle<N> &A) : board(A.board) {}
+
142
+
+
147 EightPuzzle(const EightPuzzle<N> &&A) noexcept
+
148 : board(std::move(A.board)) {}
-
147
-
150 ~EightPuzzle() = default;
-
151
-
- -
157 board = A.board;
-
158 return *this;
-
159 }
+
149
+
152 ~EightPuzzle() = default;
+
153
+
+ +
159 board = A.board;
+
160 return *this;
+
161 }
-
160
-
- -
166 board = std::move(A.board);
-
167 return *this;
-
168 }
+
162
+
+ +
168 board = std::move(A.board);
+
169 return *this;
+
170 }
-
169
-
-
176 std::vector<EightPuzzle<N>> generate_possible_moves() {
-
177 auto zero_pos = find_zero();
-
178 // vector which will contain all possible state from current state
-
179 std::vector<EightPuzzle<N>> NewStates;
-
180 for (auto &move : moves) {
-
181 if (in_range(zero_pos.first + move.first) &&
-
182 in_range(zero_pos.second + move.second)) {
-
183 // swap with the possible moves
-
184 std::array<std::array<uint32_t, N>, N> new_config = board;
-
185 std::swap(new_config[zero_pos.first][zero_pos.second],
-
186 new_config[zero_pos.first + move.first]
-
187 [zero_pos.second + move.second]);
-
188 EightPuzzle<N> new_state(new_config);
-
189 // Store new state and calculate heuristic value, and depth
-
190 NewStates.emplace_back(new_state);
-
191 }
-
192 }
-
193 return NewStates;
-
194 }
+
171
+
+
178 std::vector<EightPuzzle<N>> generate_possible_moves() {
+
179 auto zero_pos = find_zero();
+
180 // vector which will contain all possible state from current state
+
181 std::vector<EightPuzzle<N>> NewStates;
+
182 for (auto &move : moves) {
+
183 if (in_range(zero_pos.first + move.first) &&
+
184 in_range(zero_pos.second + move.second)) {
+
185 // swap with the possible moves
+
186 std::array<std::array<uint32_t, N>, N> new_config = board;
+
187 std::swap(new_config[zero_pos.first][zero_pos.second],
+
188 new_config[zero_pos.first + move.first]
+
189 [zero_pos.second + move.second]);
+
190 EightPuzzle<N> new_state(new_config);
+
191 // Store new state and calculate heuristic value, and depth
+
192 NewStates.emplace_back(new_state);
+
193 }
+
194 }
+
195 return NewStates;
+
196 }
-
195
-
-
200 bool operator==(const EightPuzzle<N> &check) const {
-
201 if (check.get_size() != N) {
-
202 return false;
-
203 }
-
204 for (size_t i = 0; i < N; ++i) {
-
205 for (size_t j = 0; j < N; ++j) {
-
206 if (board[i][j] != check.board[i][j]) {
-
207 return false;
-
208 }
-
209 }
-
210 }
-
211 return true;
-
212 }
+
197
+
+
202 bool operator==(const EightPuzzle<N> &check) const {
+
203 if (check.get_size() != N) {
+
204 return false;
+
205 }
+
206 for (size_t i = 0; i < N; ++i) {
+
207 for (size_t j = 0; j < N; ++j) {
+
208 if (board[i][j] != check.board[i][j]) {
+
209 return false;
+
210 }
+
211 }
+
212 }
+
213 return true;
+
214 }
-
213
-
-
218 bool operator<(const EightPuzzle<N> &check) const {
-
219 for (size_t i = 0; i < N; ++i) {
-
220 for (size_t j = 0; j < N; ++j) {
-
221 if (board[i][j] != check.board[i][j]) {
-
222 return board[i][j] < check.board[i][j];
-
223 }
-
224 }
-
225 }
-
226 return false;
-
227 }
+
215
+
+
220 bool operator<(const EightPuzzle<N> &check) const {
+
221 for (size_t i = 0; i < N; ++i) {
+
222 for (size_t j = 0; j < N; ++j) {
+
223 if (board[i][j] != check.board[i][j]) {
+
224 return board[i][j] < check.board[i][j];
+
225 }
+
226 }
+
227 }
+
228 return false;
+
229 }
-
228
-
-
233 bool operator<=(const EightPuzzle<N> &check) const {
-
234 for (size_t i = 0; i < N; ++i) {
-
235 for (size_t j = 0; j < N; ++j) {
-
236 if (board[i][j] != check.board[i][j]) {
-
237 return board[i][j] < check.board[i][j];
-
238 }
-
239 }
-
240 }
-
241 return true;
-
242 }
+
230
+
+
235 bool operator<=(const EightPuzzle<N> &check) const {
+
236 for (size_t i = 0; i < N; ++i) {
+
237 for (size_t j = 0; j < N; ++j) {
+
238 if (board[i][j] != check.board[i][j]) {
+
239 return board[i][j] < check.board[i][j];
+
240 }
+
241 }
+
242 }
+
243 return true;
+
244 }
-
243
-
-
250 friend std::ostream &operator<<(std::ostream &op,
-
251 const EightPuzzle<N> &SomeState) {
-
252 for (size_t i = 0; i < N; ++i) {
-
253 for (size_t j = 0; j < N; ++j) {
-
254 op << SomeState.board[i][j] << " ";
-
255 }
-
256 op << "\n";
-
257 }
-
258 return op;
-
259 }
+
245
+
+
252 friend std::ostream &operator<<(std::ostream &op,
+
253 const EightPuzzle<N> &SomeState) {
+
254 for (size_t i = 0; i < N; ++i) {
+
255 for (size_t j = 0; j < N; ++j) {
+
256 op << SomeState.board[i][j] << " ";
+
257 }
+
258 op << "\n";
+
259 }
+
260 return op;
+
261 }
-
260};
+
262};
-
261
-
287template <typename Puzzle>
-
- -
-
293 typedef struct Info {
-
294 std::shared_ptr<Puzzle> state;
-
295 size_t heuristic_value = 0;
-
296 size_t depth = 0;
-
297
-
301 Info() = default;
-
302
-
307 explicit Info(const Puzzle &A) : state(std::make_shared<Puzzle>(A)) {}
-
308
-
-
315 Info(const Puzzle &A, size_t h_value, size_t d)
-
316 : state(std::make_shared<Puzzle>(A)),
-
317 heuristic_value(h_value),
-
318 depth(d) {}
+
263
+
289template <typename Puzzle>
+
+ +
+
295 typedef struct Info {
+
296 std::shared_ptr<Puzzle> state;
+
297 size_t heuristic_value = 0;
+
298 size_t depth = 0;
+
299
+
303 Info() = default;
+
304
+
309 explicit Info(const Puzzle &A) : state(std::make_shared<Puzzle>(A)) {}
+
310
+
+
317 Info(const Puzzle &A, size_t h_value, size_t d)
+
318 : state(std::make_shared<Puzzle>(A)),
+
319 heuristic_value(h_value),
+
320 depth(d) {}
-
319
-
-
324 Info(const Info &A)
-
325 : state(std::make_shared<Puzzle>(A.state)),
- -
327 depth(A.depth) {}
+
321
+
+
326 Info(const Info &A)
+
327 : state(std::make_shared<Puzzle>(A.state)),
+ +
329 depth(A.depth) {}
-
328
-
-
333 Info(const Info &&A) noexcept
-
334 : state(std::make_shared<Puzzle>(std::move(A.state))),
-
335 heuristic_value(std::move(A.heuristic_value)),
-
336 depth(std::move(A.depth)) {}
+
330
+
+
335 Info(const Info &&A) noexcept
+
336 : state(std::make_shared<Puzzle>(std::move(A.state))),
+
337 heuristic_value(std::move(A.heuristic_value)),
+
338 depth(std::move(A.depth)) {}
-
337
-
-
342 Info &operator=(const Info &A) {
-
343 state = A.state;
- -
345 depth = A.depth;
-
346 return *this;
-
347 }
+
339
+
+
344 Info &operator=(const Info &A) {
+
345 state = A.state;
+ +
347 depth = A.depth;
+
348 return *this;
+
349 }
-
348
-
-
353 Info &operator=(Info &&A) noexcept {
-
354 state = std::move(A.state);
-
355 heuristic_value = std::move(A.heuristic_value);
-
356 depth = std::move(A.depth);
-
357 return *this;
-
358 }
+
350
+
+
355 Info &operator=(Info &&A) noexcept {
+
356 state = std::move(A.state);
+
357 heuristic_value = std::move(A.heuristic_value);
+
358 depth = std::move(A.depth);
+
359 return *this;
+
360 }
-
359
-
362 ~Info() = default;
- +
361
+
364 ~Info() = default;
+
-
364
-
365 std::shared_ptr<Info> Initial; // Initial state of the AyStarSearch
-
366 std::shared_ptr<Info> Final; // Final state of the AyStarSearch
-
- -
371 bool operator()(const std::shared_ptr<Info> &a,
-
372 const std::shared_ptr<Info> &b) const {
-
373 return *(a->state) < *(b->state);
-
374 }
-
375 };
+
366
+
367 std::shared_ptr<Info> Initial; // Initial state of the AyStarSearch
+
368 std::shared_ptr<Info> Final; // Final state of the AyStarSearch
+
+ +
373 bool operator()(const std::shared_ptr<Info> &a,
+
374 const std::shared_ptr<Info> &b) const {
+
375 return *(a->state) < *(b->state);
+
376 }
+
377 };
-
376
-
377 public:
-
378 using MapOfPuzzleInfoWithPuzzleInfo =
-
379 std::map<std::shared_ptr<Info>, std::shared_ptr<Info>,
- -
381
-
382 using MapOfPuzzleInfoWithInteger =
-
383 std::map<std::shared_ptr<Info>, uint32_t, comparison_operator>;
-
384
-
385 using SetOfPuzzleInfo =
-
386 std::set<std::shared_ptr<Info>, comparison_operator>;
-
-
392 AyStarSearch(const Puzzle &initial, const Puzzle &final) {
-
393 Initial = std::make_shared<Info>(initial);
-
394 Final = std::make_shared<Info>(final);
-
395 }
+
378
+
379 public:
+
380 using MapOfPuzzleInfoWithPuzzleInfo =
+
381 std::map<std::shared_ptr<Info>, std::shared_ptr<Info>,
+ +
383
+
384 using MapOfPuzzleInfoWithInteger =
+
385 std::map<std::shared_ptr<Info>, uint32_t, comparison_operator>;
+
386
+
387 using SetOfPuzzleInfo =
+
388 std::set<std::shared_ptr<Info>, comparison_operator>;
+
+
394 AyStarSearch(const Puzzle &initial, const Puzzle &final) {
+
395 Initial = std::make_shared<Info>(initial);
+
396 Final = std::make_shared<Info>(final);
+
397 }
-
396
-
-
405 std::vector<Puzzle> Solution(
-
406 std::shared_ptr<Info> FinalState,
-
407 const MapOfPuzzleInfoWithPuzzleInfo &parent_of) {
-
408 // Useful for traversing from final state to current state.
-
409 auto current_state = FinalState;
-
410 /*
-
411 * For storing the solution tree starting from initial state to
-
412 * final state
-
413 */
-
414 std::vector<Puzzle> answer;
-
415 while (current_state != nullptr) {
-
416 answer.emplace_back(*current_state->state);
-
417 current_state = parent_of.find(current_state)->second;
-
418 }
-
419 return answer;
-
420 }
+
398
+
+
407 std::vector<Puzzle> Solution(
+
408 std::shared_ptr<Info> FinalState,
+
409 const MapOfPuzzleInfoWithPuzzleInfo &parent_of) {
+
410 // Useful for traversing from final state to current state.
+
411 auto current_state = FinalState;
+
412 /*
+
413 * For storing the solution tree starting from initial state to
+
414 * final state
+
415 */
+
416 std::vector<Puzzle> answer;
+
417 while (current_state != nullptr) {
+
418 answer.emplace_back(*current_state->state);
+
419 current_state = parent_of.find(current_state)->second;
+
420 }
+
421 return answer;
+
422 }
-
421
-
-
429 std::vector<Puzzle> a_star_search(
-
430 const std::function<uint32_t(const Puzzle &, const Puzzle &)> &dist,
-
431 const uint32_t permissible_depth = 30) {
-
432 MapOfPuzzleInfoWithPuzzleInfo
-
433 parent_of;
-
434 MapOfPuzzleInfoWithInteger g_score;
-
435 SetOfPuzzleInfo open_list;
-
436 SetOfPuzzleInfo closed_list;
-
437
-
438 // Before starting the AyStartSearch, initialize the set and maps
-
439 open_list.emplace(Initial);
-
440 parent_of[Initial] = nullptr;
-
441 g_score[Initial] = 0;
-
442
-
443 while (!open_list.empty()) {
-
444 // Iterator for state having having lowest f_score.
-
445 typename SetOfPuzzleInfo::iterator it_low_f_score;
-
446 uint32_t min_f_score = 1e9;
-
447 for (auto iter = open_list.begin(); iter != open_list.end();
-
448 ++iter) {
-
449 // f score here is evaluated by g score (depth) and h score
-
450 // (distance between current state and final state)
-
451 uint32_t f_score = (*iter)->heuristic_value + (*iter)->depth;
-
452 if (f_score < min_f_score) {
-
453 min_f_score = f_score;
-
454 it_low_f_score = iter;
-
455 }
-
456 }
-
457
-
458 // current_state, stores lowest f score so far for this state.
-
459 std::shared_ptr<Info> current_state = *it_low_f_score;
-
460
-
461 // if this current state is equal to final, return
-
462 if (*(current_state->state) == *(Final->state)) {
-
463 return Solution(current_state, parent_of);
-
464 }
-
465 // else remove from open list as visited.
-
466 open_list.erase(it_low_f_score);
-
467 // if current_state has exceeded the allowed depth, skip
-
468 // neighbor checking
-
469 if (current_state->depth >= permissible_depth) {
-
470 continue;
-
471 }
-
472 // Generate all possible moves (neighbors) given the current
-
473 // state
-
474 std::vector<Puzzle> total_possible_moves =
-
475 current_state->state->generate_possible_moves();
-
476
-
477 for (Puzzle &neighbor : total_possible_moves) {
-
478 // calculate score of neighbors with respect to
-
479 // current_state
-
480 std::shared_ptr<Info> Neighbor = std::make_shared<Info>(
-
481 neighbor, dist(neighbor, *(Final->state)),
-
482 current_state->depth + 1U);
-
483 uint32_t temp_g_score = Neighbor->depth;
-
484
-
485 // Check whether this state is explored.
-
486 // If this state is discovered at greater depth, then discard,
-
487 // else remove from closed list and explore the node
-
488 auto closed_list_iter = closed_list.find(Neighbor);
-
489 if (closed_list_iter != closed_list.end()) {
-
490 // 1. If state in closed list has higher depth, then remove
-
491 // from list since we have found better option,
-
492 // 2. Else don't explore this state.
-
493 if (Neighbor->depth < (*closed_list_iter)->depth) {
-
494 closed_list.erase(closed_list_iter);
-
495 } else {
-
496 continue;
-
497 }
-
498 }
-
499 auto neighbor_g_score_iter = g_score.find(Neighbor);
-
500 // if the neighbor is already created and has minimum
-
501 // g_score, then update g_score and f_score else insert new
-
502 if (neighbor_g_score_iter != g_score.end()) {
-
503 if (neighbor_g_score_iter->second > temp_g_score) {
-
504 neighbor_g_score_iter->second = temp_g_score;
-
505 parent_of[Neighbor] = current_state;
-
506 }
-
507 } else {
-
508 g_score[Neighbor] = temp_g_score;
-
509 parent_of[Neighbor] = current_state;
-
510 }
-
511 // If this is a new state, insert into open_list
-
512 // else update if the this state has better g score than
-
513 // existing one.
-
514 auto iter = open_list.find(Neighbor);
-
515 if (iter == open_list.end()) {
-
516 open_list.emplace(Neighbor);
-
517 } else if ((*iter)->depth > Neighbor->depth) {
-
518 (*iter)->depth = Neighbor->depth;
-
519 }
-
520 }
-
521 closed_list.emplace(current_state);
-
522 }
-
523 // Cannot find the solution, return empty vector
-
524 return std::vector<Puzzle>(0);
-
525 }
+
423
+
+
431 std::vector<Puzzle> a_star_search(
+
432 const std::function<uint32_t(const Puzzle &, const Puzzle &)> &dist,
+
433 const uint32_t permissible_depth = 30) {
+
434 MapOfPuzzleInfoWithPuzzleInfo
+
435 parent_of;
+
436 MapOfPuzzleInfoWithInteger g_score;
+
437 SetOfPuzzleInfo open_list;
+
438 SetOfPuzzleInfo closed_list;
+
439
+
440 // Before starting the AyStartSearch, initialize the set and maps
+
441 open_list.emplace(Initial);
+
442 parent_of[Initial] = nullptr;
+
443 g_score[Initial] = 0;
+
444
+
445 while (!open_list.empty()) {
+
446 // Iterator for state having having lowest f_score.
+
447 typename SetOfPuzzleInfo::iterator it_low_f_score;
+
448 uint32_t min_f_score = 1e9;
+
449 for (auto iter = open_list.begin(); iter != open_list.end();
+
450 ++iter) {
+
451 // f score here is evaluated by g score (depth) and h score
+
452 // (distance between current state and final state)
+
453 uint32_t f_score = (*iter)->heuristic_value + (*iter)->depth;
+
454 if (f_score < min_f_score) {
+
455 min_f_score = f_score;
+
456 it_low_f_score = iter;
+
457 }
+
458 }
+
459
+
460 // current_state, stores lowest f score so far for this state.
+
461 std::shared_ptr<Info> current_state = *it_low_f_score;
+
462
+
463 // if this current state is equal to final, return
+
464 if (*(current_state->state) == *(Final->state)) {
+
465 return Solution(current_state, parent_of);
+
466 }
+
467 // else remove from open list as visited.
+
468 open_list.erase(it_low_f_score);
+
469 // if current_state has exceeded the allowed depth, skip
+
470 // neighbor checking
+
471 if (current_state->depth >= permissible_depth) {
+
472 continue;
+
473 }
+
474 // Generate all possible moves (neighbors) given the current
+
475 // state
+
476 std::vector<Puzzle> total_possible_moves =
+
477 current_state->state->generate_possible_moves();
+
478
+
479 for (Puzzle &neighbor : total_possible_moves) {
+
480 // calculate score of neighbors with respect to
+
481 // current_state
+
482 std::shared_ptr<Info> Neighbor = std::make_shared<Info>(
+
483 neighbor, dist(neighbor, *(Final->state)),
+
484 current_state->depth + 1U);
+
485 uint32_t temp_g_score = Neighbor->depth;
+
486
+
487 // Check whether this state is explored.
+
488 // If this state is discovered at greater depth, then discard,
+
489 // else remove from closed list and explore the node
+
490 auto closed_list_iter = closed_list.find(Neighbor);
+
491 if (closed_list_iter != closed_list.end()) {
+
492 // 1. If state in closed list has higher depth, then remove
+
493 // from list since we have found better option,
+
494 // 2. Else don't explore this state.
+
495 if (Neighbor->depth < (*closed_list_iter)->depth) {
+
496 closed_list.erase(closed_list_iter);
+
497 } else {
+
498 continue;
+
499 }
+
500 }
+
501 auto neighbor_g_score_iter = g_score.find(Neighbor);
+
502 // if the neighbor is already created and has minimum
+
503 // g_score, then update g_score and f_score else insert new
+
504 if (neighbor_g_score_iter != g_score.end()) {
+
505 if (neighbor_g_score_iter->second > temp_g_score) {
+
506 neighbor_g_score_iter->second = temp_g_score;
+
507 parent_of[Neighbor] = current_state;
+
508 }
+
509 } else {
+
510 g_score[Neighbor] = temp_g_score;
+
511 parent_of[Neighbor] = current_state;
+
512 }
+
513 // If this is a new state, insert into open_list
+
514 // else update if the this state has better g score than
+
515 // existing one.
+
516 auto iter = open_list.find(Neighbor);
+
517 if (iter == open_list.end()) {
+
518 open_list.emplace(Neighbor);
+
519 } else if ((*iter)->depth > Neighbor->depth) {
+
520 (*iter)->depth = Neighbor->depth;
+
521 }
+
522 }
+
523 closed_list.emplace(current_state);
+
524 }
+
525 // Cannot find the solution, return empty vector
+
526 return std::vector<Puzzle>(0);
+
527 }
-
526};
+
528};
-
527} // namespace aystar_search
-
528} // namespace machine_learning
-
529
-
534static void test() {
-
535 // Renaming for simplicity
-
536 using matrix3 = std::array<std::array<uint32_t, 3>, 3>;
-
537 using row3 = std::array<uint32_t, 3>;
-
538 using matrix4 = std::array<std::array<uint32_t, 4>, 4>;
-
539 using row4 = std::array<uint32_t, 4>;
-
540 // 1st test: A* search for simple EightPuzzle problem
-
541 matrix3 puzzle;
-
542 puzzle[0] = row3({0, 2, 3});
-
543 puzzle[1] = row3({1, 5, 6});
-
544 puzzle[2] = row3({4, 7, 8});
-
545
-
546 matrix3 ideal;
-
547 ideal[0] = row3({1, 2, 3});
-
548 ideal[1] = row3({4, 5, 6});
-
549 ideal[2] = row3({7, 8, 0});
-
550
-
551 /*
-
552 * Heuristic function: Manhattan distance
-
553 */
-
554 auto manhattan_distance =
- - -
557 uint32_t ret = 0;
-
558 for (size_t i = 0; i < first.get_size(); ++i) {
-
559 for (size_t j = 0; j < first.get_size(); ++j) {
-
560 uint32_t find = first.get(i, j);
-
561 size_t m = first.get_size(), n = first.get_size();
-
562 for (size_t k = 0; k < second.get_size(); ++k) {
-
563 for (size_t l = 0; l < second.get_size(); ++l) {
-
564 if (find == second.get(k, l)) {
-
565 std::tie(m, n) = std::make_pair(k, l);
-
566 break;
-
567 }
-
568 }
-
569 if (m != first.get_size()) {
-
570 break;
-
571 }
-
572 }
-
573 if (m != first.get_size()) {
-
574 ret += (std::max(m, i) - std::min(m, i)) +
-
575 (std::max(n, j) - std::min(n, j));
-
576 }
-
577 }
-
578 }
-
579 return ret;
-
580 };
-
581
- - - - -
586 search(Puzzle, Ideal);
-
587
-
588 std::vector<matrix3> answer;
+
529} // namespace aystar_search
+
530} // namespace machine_learning
+
531
+
536static void test() {
+
537 // Renaming for simplicity
+
538 using matrix3 = std::array<std::array<uint32_t, 3>, 3>;
+
539 using row3 = std::array<uint32_t, 3>;
+
540 using matrix4 = std::array<std::array<uint32_t, 4>, 4>;
+
541 using row4 = std::array<uint32_t, 4>;
+
542 // 1st test: A* search for simple EightPuzzle problem
+
543 matrix3 puzzle;
+
544 puzzle[0] = row3({0, 2, 3});
+
545 puzzle[1] = row3({1, 5, 6});
+
546 puzzle[2] = row3({4, 7, 8});
+
547
+
548 matrix3 ideal;
+
549 ideal[0] = row3({1, 2, 3});
+
550 ideal[1] = row3({4, 5, 6});
+
551 ideal[2] = row3({7, 8, 0});
+
552
+
553 /*
+
554 * Heuristic function: Manhattan distance
+
555 */
+
556 auto manhattan_distance =
+ + +
559 uint32_t ret = 0;
+
560 for (size_t i = 0; i < first.get_size(); ++i) {
+
561 for (size_t j = 0; j < first.get_size(); ++j) {
+
562 uint32_t find = first.get(i, j);
+
563 size_t m = first.get_size(), n = first.get_size();
+
564 for (size_t k = 0; k < second.get_size(); ++k) {
+
565 for (size_t l = 0; l < second.get_size(); ++l) {
+
566 if (find == second.get(k, l)) {
+
567 std::tie(m, n) = std::make_pair(k, l);
+
568 break;
+
569 }
+
570 }
+
571 if (m != first.get_size()) {
+
572 break;
+
573 }
+
574 }
+
575 if (m != first.get_size()) {
+
576 ret += (std::max(m, i) - std::min(m, i)) +
+
577 (std::max(n, j) - std::min(n, j));
+
578 }
+
579 }
+
580 }
+
581 return ret;
+
582 };
+
583
+ + + + +
588 search(Puzzle, Ideal);
589
-
590 answer.push_back(
-
591 matrix3({row3({0, 2, 3}), row3({1, 5, 6}), row3({4, 7, 8})}));
+
590 std::vector<matrix3> answer;
+
591
592 answer.push_back(
-
593 matrix3({row3({1, 2, 3}), row3({0, 5, 6}), row3({4, 7, 8})}));
+
593 matrix3({row3({0, 2, 3}), row3({1, 5, 6}), row3({4, 7, 8})}));
594 answer.push_back(
-
595 matrix3({row3({1, 2, 3}), row3({4, 5, 6}), row3({0, 7, 8})}));
+
595 matrix3({row3({1, 2, 3}), row3({0, 5, 6}), row3({4, 7, 8})}));
596 answer.push_back(
-
597 matrix3({row3({1, 2, 3}), row3({4, 5, 6}), row3({7, 0, 8})}));
+
597 matrix3({row3({1, 2, 3}), row3({4, 5, 6}), row3({0, 7, 8})}));
598 answer.push_back(
-
599 matrix3({row3({1, 2, 3}), row3({4, 5, 6}), row3({7, 8, 0})}));
-
600
-
601 auto Solution = search.a_star_search(manhattan_distance);
-
602 std::cout << Solution.size() << std::endl;
-
603
-
604 assert(Solution.size() == answer.size());
+
599 matrix3({row3({1, 2, 3}), row3({4, 5, 6}), row3({7, 0, 8})}));
+
600 answer.push_back(
+
601 matrix3({row3({1, 2, 3}), row3({4, 5, 6}), row3({7, 8, 0})}));
+
602
+
603 auto Solution = search.a_star_search(manhattan_distance);
+
604 std::cout << Solution.size() << std::endl;
605
-
606 uint32_t i = 0;
-
607 for (auto it = Solution.rbegin(); it != Solution.rend(); ++it) {
-
608 assert(it->get_state() == answer[i]);
-
609 ++i;
-
610 }
-
611
-
612 // 2nd test: A* search for complicated EightPuzzle problem
-
613 // Initial state
-
614 puzzle[0] = row3({5, 7, 3});
-
615 puzzle[1] = row3({2, 0, 6});
-
616 puzzle[2] = row3({1, 4, 8});
-
617 // Final state
-
618 ideal[0] = row3({1, 2, 3});
-
619 ideal[1] = row3({4, 5, 6});
-
620 ideal[2] = row3({7, 8, 0});
-
621
- - -
624
-
625 // Initialize the search object
- - -
628
-
629 Solution = search.a_star_search(manhattan_distance);
-
630 std::cout << Solution.size() << std::endl;
-
631 // Static assertion due to large solution
-
632 assert(13 == Solution.size());
-
633 // Check whether the final state is equal to expected one
-
634 assert(Solution[0].get_state() == ideal);
-
635 for (auto it = Solution.rbegin(); it != Solution.rend(); ++it) {
-
636 std::cout << *it << std::endl;
-
637 }
-
638
-
639 // 3rd test: A* search for 15-Puzzle
-
640 // Initial State of the puzzle
-
641 matrix4 puzzle2;
-
642 puzzle2[0] = row4({10, 1, 6, 2});
-
643 puzzle2[1] = row4({5, 8, 4, 3});
-
644 puzzle2[2] = row4({13, 0, 7, 11});
-
645 puzzle2[3] = row4({14, 9, 15, 12});
-
646 // Final state of the puzzle
-
647 matrix4 ideal2;
-
648 ideal2[0] = row4({1, 2, 3, 4});
-
649 ideal2[1] = row4({5, 6, 7, 8});
-
650 ideal2[2] = row4({9, 10, 11, 12});
-
651 ideal2[3] = row4({13, 14, 15, 0});
-
652
-
653 // Instantiate states for a*, initial state and final states
- -
655 Ideal2(ideal2);
-
656 // Initialize the search object
- - -
659 search2(Puzzle2, Ideal2);
-
663 auto manhattan_distance2 =
- - -
666 uint32_t ret = 0;
-
667 for (size_t i = 0; i < first.get_size(); ++i) {
-
668 for (size_t j = 0; j < first.get_size(); ++j) {
-
669 uint32_t find = first.get(i, j);
-
670 size_t m = first.get_size(), n = first.get_size();
-
671 for (size_t k = 0; k < second.get_size(); ++k) {
-
672 for (size_t l = 0; l < second.get_size(); ++l) {
-
673 if (find == second.get(k, l)) {
-
674 std::tie(m, n) = std::make_pair(k, l);
-
675 break;
-
676 }
-
677 }
-
678 if (m != first.get_size()) {
-
679 break;
-
680 }
-
681 }
-
682 if (m != first.get_size()) {
-
683 ret += (std::max(m, i) - std::min(m, i)) +
-
684 (std::max(n, j) - std::min(n, j));
-
685 }
-
686 }
-
687 }
-
688 return ret;
-
689 };
-
690
-
691 auto sol2 = search2.a_star_search(manhattan_distance2);
-
692 std::cout << sol2.size() << std::endl;
-
693
-
694 // Static assertion due to large solution
-
695 assert(24 == sol2.size());
-
696 // Check whether the final state is equal to expected one
-
697 assert(sol2[0].get_state() == ideal2);
-
698
-
699 for (auto it = sol2.rbegin(); it != sol2.rend(); ++it) {
-
700 std::cout << *it << std::endl;
-
701 }
-
702}
-
707int main() {
-
708 test(); // run self-test implementations
-
709 return 0;
-
710}
+
606 assert(Solution.size() == answer.size());
+
607
+
608 uint32_t i = 0;
+
609 for (auto it = Solution.rbegin(); it != Solution.rend(); ++it) {
+
610 assert(it->get_state() == answer[i]);
+
611 ++i;
+
612 }
+
613
+
614 // 2nd test: A* search for complicated EightPuzzle problem
+
615 // Initial state
+
616 puzzle[0] = row3({5, 7, 3});
+
617 puzzle[1] = row3({2, 0, 6});
+
618 puzzle[2] = row3({1, 4, 8});
+
619 // Final state
+
620 ideal[0] = row3({1, 2, 3});
+
621 ideal[1] = row3({4, 5, 6});
+
622 ideal[2] = row3({7, 8, 0});
+
623
+ + +
626
+
627 // Initialize the search object
+ + +
630
+
631 Solution = search.a_star_search(manhattan_distance);
+
632 std::cout << Solution.size() << std::endl;
+
633 // Static assertion due to large solution
+
634 assert(13 == Solution.size());
+
635 // Check whether the final state is equal to expected one
+
636 assert(Solution[0].get_state() == ideal);
+
637 for (auto it = Solution.rbegin(); it != Solution.rend(); ++it) {
+
638 std::cout << *it << std::endl;
+
639 }
+
640
+
641 // 3rd test: A* search for 15-Puzzle
+
642 // Initial State of the puzzle
+
643 matrix4 puzzle2;
+
644 puzzle2[0] = row4({10, 1, 6, 2});
+
645 puzzle2[1] = row4({5, 8, 4, 3});
+
646 puzzle2[2] = row4({13, 0, 7, 11});
+
647 puzzle2[3] = row4({14, 9, 15, 12});
+
648 // Final state of the puzzle
+
649 matrix4 ideal2;
+
650 ideal2[0] = row4({1, 2, 3, 4});
+
651 ideal2[1] = row4({5, 6, 7, 8});
+
652 ideal2[2] = row4({9, 10, 11, 12});
+
653 ideal2[3] = row4({13, 14, 15, 0});
+
654
+
655 // Instantiate states for a*, initial state and final states
+ +
657 Ideal2(ideal2);
+
658 // Initialize the search object
+ + +
661 search2(Puzzle2, Ideal2);
+
665 auto manhattan_distance2 =
+ + +
668 uint32_t ret = 0;
+
669 for (size_t i = 0; i < first.get_size(); ++i) {
+
670 for (size_t j = 0; j < first.get_size(); ++j) {
+
671 uint32_t find = first.get(i, j);
+
672 size_t m = first.get_size(), n = first.get_size();
+
673 for (size_t k = 0; k < second.get_size(); ++k) {
+
674 for (size_t l = 0; l < second.get_size(); ++l) {
+
675 if (find == second.get(k, l)) {
+
676 std::tie(m, n) = std::make_pair(k, l);
+
677 break;
+
678 }
+
679 }
+
680 if (m != first.get_size()) {
+
681 break;
+
682 }
+
683 }
+
684 if (m != first.get_size()) {
+
685 ret += (std::max(m, i) - std::min(m, i)) +
+
686 (std::max(n, j) - std::min(n, j));
+
687 }
+
688 }
+
689 }
+
690 return ret;
+
691 };
+
692
+
693 auto sol2 = search2.a_star_search(manhattan_distance2);
+
694 std::cout << sol2.size() << std::endl;
+
695
+
696 // Static assertion due to large solution
+
697 assert(24 == sol2.size());
+
698 // Check whether the final state is equal to expected one
+
699 assert(sol2[0].get_state() == ideal2);
+
700
+
701 for (auto it = sol2.rbegin(); it != sol2.rend(); ++it) {
+
702 std::cout << *it << std::endl;
+
703 }
+
704}
+
709int main() {
+
710 test(); // run self-test implementations
+
711 return 0;
+
712}
-
A class defining A* search algorithm. for some initial state and final state.
-
std::vector< Puzzle > Solution(std::shared_ptr< Info > FinalState, const MapOfPuzzleInfoWithPuzzleInfo &parent_of)
A helper solution: launches when a solution for AyStarSearch is found.
+
A class defining A* search algorithm. for some initial state and final state.
+
std::vector< Puzzle > Solution(std::shared_ptr< Info > FinalState, const MapOfPuzzleInfoWithPuzzleInfo &parent_of)
A helper solution: launches when a solution for AyStarSearch is found.
struct machine_learning::aystar_search::AyStarSearch::Info Info
Struct that handles all the information related to the current state.
-
std::vector< Puzzle > a_star_search(const std::function< uint32_t(const Puzzle &, const Puzzle &)> &dist, const uint32_t permissible_depth=30)
-
AyStarSearch(const Puzzle &initial, const Puzzle &final)
Parameterized constructor for AyStarSearch.
-
A class defining EightPuzzle/15-Puzzle game.
-
EightPuzzle & operator=(EightPuzzle &&A) noexcept
Move assignment operator.
+
std::vector< Puzzle > a_star_search(const std::function< uint32_t(const Puzzle &, const Puzzle &)> &dist, const uint32_t permissible_depth=30)
+
AyStarSearch(const Puzzle &initial, const Puzzle &final)
Parameterized constructor for AyStarSearch.
+
A class defining EightPuzzle/15-Puzzle game.
+
EightPuzzle & operator=(EightPuzzle &&A) noexcept
Move assignment operator.
~EightPuzzle()=default
Destructor of EightPuzzle.
-
std::vector< EightPuzzle< N > > generate_possible_moves()
Find all possible states after processing all possible moves, given the current state of the puzzle.
-
EightPuzzle()
Default constructor for EightPuzzle.
-
EightPuzzle & operator=(const EightPuzzle &A)
Copy assignment operator.
-
bool in_range(const uint32_t value) const
check whether the index value is bounded within the puzzle area
-
bool operator<(const EightPuzzle< N > &check) const
check whether one board is lexicographically smaller
-
std::pair< uint32_t, uint32_t > find_zero()
A helper array to evaluate the next state from current state;.
-
friend std::ostream & operator<<(std::ostream &op, const EightPuzzle< N > &SomeState)
friend operator to display EightPuzzle<>
-
bool operator==(const EightPuzzle< N > &check) const
check whether two boards are equal
-
uint32_t get(size_t i, size_t j) const
get the value from i units from right and j units from left side of the board
-
std::vector< std::pair< int8_t, int8_t > > moves
N x N array to store the current state of the Puzzle.
-
EightPuzzle(const std::array< std::array< uint32_t, N >, N > &init)
Parameterized Constructor for EightPuzzle.
-
EightPuzzle(const EightPuzzle< N > &A)
Copy constructor.
-
std::array< std::array< uint32_t, N >, N > get_state()
Returns the current state of the board.
-
size_t get_size() const
returns the size of the EightPuzzle (number of row / column)
-
EightPuzzle(const EightPuzzle< N > &&A) noexcept
Move constructor.
-
bool operator<=(const EightPuzzle< N > &check) const
check whether one board is lexicographically smaller or equal
+
std::vector< EightPuzzle< N > > generate_possible_moves()
Find all possible states after processing all possible moves, given the current state of the puzzle.
+
EightPuzzle()
Default constructor for EightPuzzle.
+
EightPuzzle & operator=(const EightPuzzle &A)
Copy assignment operator.
+
bool in_range(const uint32_t value) const
check whether the index value is bounded within the puzzle area
+
bool operator<(const EightPuzzle< N > &check) const
check whether one board is lexicographically smaller
+
std::pair< uint32_t, uint32_t > find_zero()
A helper array to evaluate the next state from current state;.
+
friend std::ostream & operator<<(std::ostream &op, const EightPuzzle< N > &SomeState)
friend operator to display EightPuzzle<>
+
bool operator==(const EightPuzzle< N > &check) const
check whether two boards are equal
+
uint32_t get(size_t i, size_t j) const
get the value from i units from right and j units from left side of the board
+
std::vector< std::pair< int8_t, int8_t > > moves
N x N array to store the current state of the Puzzle.
+
EightPuzzle(const std::array< std::array< uint32_t, N >, N > &init)
Parameterized Constructor for EightPuzzle.
+
EightPuzzle(const EightPuzzle< N > &A)
Copy constructor.
+
std::array< std::array< uint32_t, N >, N > get_state()
Returns the current state of the board.
+
size_t get_size() const
returns the size of the EightPuzzle (number of row / column)
+
EightPuzzle(const EightPuzzle< N > &&A) noexcept
Move constructor.
+
bool operator<=(const EightPuzzle< N > &check) const
check whether one board is lexicographically smaller or equal
double k(double x)
Another test function.
double l(double x)
Another test function.
static void test()
Self-test implementations.
@@ -698,17 +700,17 @@ $(function(){initNavTree('dd/dec/a__star__search_8cpp_source.html','../../','');
A* search algorithm
for std::assert
- - - -
Info(const Puzzle &A)
constructor having Puzzle as parameter
-
Info(const Info &&A) noexcept
Move constructor.
+ + + +
Info(const Puzzle &A)
constructor having Puzzle as parameter
+
Info(const Info &&A) noexcept
Move constructor.
-
Info & operator=(const Info &A)
copy assignment operator
-
Info(const Puzzle &A, size_t h_value, size_t d)
constructor having three parameters
-
Info & operator=(Info &&A) noexcept
move assignment operator
- +
Info & operator=(const Info &A)
copy assignment operator
+
Info(const Puzzle &A, size_t h_value, size_t d)
constructor having three parameters
+
Info & operator=(Info &&A) noexcept
move assignment operator
+
diff --git a/de/d99/trie__tree_8cpp__incl.map b/de/d99/trie__tree_8cpp__incl.map index 8c05e991a..45a02ab7c 100644 --- a/de/d99/trie__tree_8cpp__incl.map +++ b/de/d99/trie__tree_8cpp__incl.map @@ -1,13 +1,15 @@ - + - + - - - - - - - + + + + + + + + + diff --git a/de/d99/trie__tree_8cpp__incl.md5 b/de/d99/trie__tree_8cpp__incl.md5 index 9d58e1060..b5cc593c8 100644 --- a/de/d99/trie__tree_8cpp__incl.md5 +++ b/de/d99/trie__tree_8cpp__incl.md5 @@ -1 +1 @@ -25563c9822626b8e6bcc2e85f96288f5 \ No newline at end of file +940b2d7c888c712661bea1640683da0f \ No newline at end of file diff --git a/de/d99/trie__tree_8cpp__incl.svg b/de/d99/trie__tree_8cpp__incl.svg index 54e63d753..857cf974d 100644 --- a/de/d99/trie__tree_8cpp__incl.svg +++ b/de/d99/trie__tree_8cpp__incl.svg @@ -4,8 +4,8 @@ - + @@ -23,9 +23,9 @@ Node1 - -data_structures/trie -_tree.cpp + +data_structures/trie +_tree.cpp @@ -42,8 +42,8 @@ Node1->Node2 - - + + @@ -60,8 +60,8 @@ Node1->Node3 - - + + @@ -69,8 +69,8 @@ Node4 - -iostream + +cstdint @@ -78,8 +78,8 @@ Node1->Node4 - - + + @@ -87,8 +87,8 @@ Node5 - -memory + +iostream @@ -96,8 +96,8 @@ Node1->Node5 - - + + @@ -105,8 +105,8 @@ Node6 - -string + +memory @@ -114,8 +114,26 @@ Node1->Node6 - - + + + + + + + +Node7 + + +string + + + + + +Node1->Node7 + + + diff --git a/de/d99/trie__tree_8cpp__incl_org.svg b/de/d99/trie__tree_8cpp__incl_org.svg index db14705cc..caa1f46ab 100644 --- a/de/d99/trie__tree_8cpp__incl_org.svg +++ b/de/d99/trie__tree_8cpp__incl_org.svg @@ -4,17 +4,17 @@ - + data_structures/trie_tree.cpp Node1 - -data_structures/trie -_tree.cpp + +data_structures/trie +_tree.cpp @@ -31,8 +31,8 @@ Node1->Node2 - - + + @@ -49,8 +49,8 @@ Node1->Node3 - - + + @@ -58,8 +58,8 @@ Node4 - -iostream + +cstdint @@ -67,8 +67,8 @@ Node1->Node4 - - + + @@ -76,8 +76,8 @@ Node5 - -memory + +iostream @@ -85,8 +85,8 @@ Node1->Node5 - - + + @@ -94,8 +94,8 @@ Node6 - -string + +memory @@ -103,8 +103,26 @@ Node1->Node6 - - + + + + + + + +Node7 + + +string + + + + + +Node1->Node7 + + + diff --git a/de/dc3/fibonacci__sum_8cpp.html b/de/dc3/fibonacci__sum_8cpp.html index 770229ec2..117ed3ad0 100644 --- a/de/dc3/fibonacci__sum_8cpp.html +++ b/de/dc3/fibonacci__sum_8cpp.html @@ -417,9 +417,9 @@ Functions
130 std::cout << "Passed Test 5!" << std::endl;
131}
uint64_t fiboSum(uint64_t n, uint64_t m)
-
static void test_1()
-
static void test_2()
-
static void test_3()
+
static void test_1()
+
static void test_2()
+
static void test_3()
diff --git a/de/dc3/fibonacci__sum_8cpp_source.html b/de/dc3/fibonacci__sum_8cpp_source.html index b3914f47e..f695f5f31 100644 --- a/de/dc3/fibonacci__sum_8cpp_source.html +++ b/de/dc3/fibonacci__sum_8cpp_source.html @@ -221,9 +221,9 @@ $(function(){initNavTree('de/dc3/fibonacci__sum_8cpp_source.html','../../','');
static void test()
uint64_t result(uint64_t n)
int main()
Main function.
-
static void test_1()
-
static void test_2()
-
static void test_3()
+
static void test_1()
+
static void test_2()
+
static void test_3()
std::vector< std::valarray< T > > matrix
Functions for the sum of the Fibonacci Sequence: .
for assert