70 using Dnode = Dgraph::Node;
73 constexpr size_t No_Id = std::numeric_limits<size_t>::max();
82 explicit Test_Tree(
const size_t n)
84 for (
size_t i = 0; i < n; ++i)
86 for (
size_t i = 1; i < n; ++i)
93 const Test_Tree & tree_of(
const size_t n)
96 for (
auto it = trees.
get_it(); it.has_curr(); it.next_ne())
97 if (it.get_curr().nodes.size() == n)
99 return trees.
append(Test_Tree(n));
103 struct Test_Node_Tree
107 explicit Test_Node_Tree(
const size_t n)
109 for (
size_t i = 0; i < n; ++i)
110 nodes.append(
new Tnode(
static_cast<int>(i % 7)));
111 for (
size_t i = 1; i < n; ++i)
112 nodes((i - 1) / 2)->insert_rightmost_child(
nodes(i));
116 Test_Node_Tree(Test_Node_Tree &&) =
default;
120 if (not
nodes.is_empty())
125 const Test_Node_Tree & node_tree_of(
const size_t n)
128 for (
auto it = trees.
get_it(); it.has_curr(); it.next_ne())
129 if (it.get_curr().nodes.size() == n)
130 return it.get_curr();
131 return trees.
append(Test_Node_Tree(n));
140 explicit Test_Digraph(
const size_t n)
142 for (
size_t i = 0; i < n; ++i)
143 nodes.append(g.insert_node(
static_cast<int>(i)));
144 for (
size_t i = 0; i < n; ++i)
145 g.insert_arc(
nodes(i),
nodes((i + 1) % n),
static_cast<int>(1 + i % 3));
149 const Test_Digraph & digraph_of(
const size_t n)
152 for (
auto it = digraphs.
get_it(); it.has_curr(); it.next_ne())
153 if (it.get_curr().nodes.size() == n)
154 return it.get_curr();
155 return digraphs.
append(Test_Digraph(n));
162 static Dnode *
const p = g.insert_node(0);
171 static L make(
const size_t n) {
return L(tree_of(n).g); }
180 for (
size_t id = 0;
id <
l.
size(); ++id)
182 EXPECT_EQ(
l.id_of(
l.node_of(
id)),
id);
183 EXPECT_EQ(
l.lca_id(
l.root_id(),
id),
l.root_id());
184 EXPECT_EQ(
l.distance_id(
l.root_id(),
id),
l.depth_of_id(
id));
185 EXPECT_EQ(
l.kth_ancestor_id(
id,
l.depth_of_id(
id)),
l.root_id());
186 EXPECT_TRUE(
id ==
l.root_id() or
l.is_ancestor_id(
l.parent_id(
id),
id));
188 EXPECT_THROW((
void)
l.node_of(
l.
size()), std::out_of_range);
196 static L make(
const size_t n) {
return L(tree_of(n).g); }
205 for (
size_t id = 0;
id <
l.
size(); ++id)
207 EXPECT_EQ(
l.id_of(
l.node_of(
id)),
id);
208 EXPECT_EQ(
l.lca_id(
l.root_id(),
id),
l.root_id());
209 EXPECT_EQ(
l.distance_id(
l.root_id(),
id),
l.depth_of_id(
id));
210 EXPECT_TRUE(
id ==
l.root_id() or
l.lca_id(
l.parent_id(
id),
id) ==
l.parent_id(
id));
213 EXPECT_EQ(
l.euler_tour().
size(),
l.euler_tour_size());
214 EXPECT_THROW((
void)
l.node_of(
l.
size()), std::out_of_range);
224 return H(tree_of(n).g, [](Gnode * p) {
return p->get_info(); });
229 return h.is_empty()
and h.size() == 0
and h.num_chains() == 0
and h.root() ==
nullptr;
235 for (
size_t id = 0;
id <
h.size(); ++id)
237 EXPECT_EQ(
h.id_of(
h.node_of(
id)),
id);
238 EXPECT_EQ(
h.lca_id(
h.root_id(),
id),
h.root_id());
239 EXPECT_EQ(
h.path_query_id(
id,
id),
h.get_value_at_id(
id));
240 h.point_update_id(
id,
h.get_value_at_id(
id) + 1);
241 total +=
h.get_value_at_id(
id);
242 (void)
h.path_query_id(
h.root_id(), id);
244 EXPECT_TRUE(
h.is_empty() or
h.subtree_query_id(
h.root_id()) == total);
245 EXPECT_THROW((
void)
h.node_of(
h.size()), std::out_of_range);
253 static H make(
const size_t n) {
return H(tree_of(n).g); }
257 return h.is_empty()
and h.size() == 0
and h.root() ==
nullptr;
262 for (
size_t id = 0;
id <
h.size(); ++id)
264 EXPECT_EQ(
h.id_of(
h.node_of(
id)),
id);
265 EXPECT_LT(
h.position_of_id(
id),
h.size());
266 const auto range =
h.subtree_range_id(
id);
267 EXPECT_EQ(
range.right -
range.left + 1,
h.subtree_size_of_id(
id));
268 EXPECT_EQ(
h.lca_id(
h.root_id(),
id),
h.root_id());
269 EXPECT_EQ(
h.distance_id(
h.root_id(),
id),
h.depth_of_id(
id));
270 EXPECT_LT(
h.head_id(
id),
h.size());
272 h.for_each_path_segment_id(
h.root_id(),
id,
273 [&segments](
size_t,
size_t,
bool) { ++segments; });
274 EXPECT_GT(segments, 0u);
276 EXPECT_THROW((
void)
h.node_of(
h.size()), std::out_of_range);
284 static Q make(
const size_t n) {
return Q(tree_of(n).g, 0); }
294 for (
size_t id = 0;
id < q.
size(); ++id)
308 static C make(
const size_t n) {
return C(tree_of(n).g); }
320 for (
size_t id = 0;
id < c.
size(); ++id)
339 const Test_Tree & t = tree_of(n);
340 return D(t.g, n == 0 ?
nullptr : t.nodes(0), [](Gnode *) {
return 1L; },
341 [](Gnode *,
const long & acc, Gnode *,
const long & child) {
return acc + child; });
349 for (
size_t id = 0;
id < d.
size(); ++id)
352 EXPECT_EQ(d.
id_of(p),
id);
354 largest = std::max(largest, d.
value(p));
356 EXPECT_EQ(largest,
static_cast<long>(d.
size()));
368 const Test_Tree & t = tree_of(n);
369 return R(t.g, n == 0 ?
nullptr : t.nodes(0), 0L, [](Gnode *) {
return 0L; },
370 [](
const long & a,
const long & b) {
return std::max(a, b); },
371 [](Gnode *, Gnode *,
const long & v) {
return v + 1; });
374 static bool is_empty(
const R &
r) {
return r.size() == 0
and r.values().size() == 0; }
378 for (
size_t id = 0;
id <
r.size(); ++id)
380 Gnode * p =
r.node_of(
id);
381 EXPECT_EQ(
r.id_of(p),
id);
382 EXPECT_EQ(
r.value(p),
r.values()[
id]);
383 EXPECT_LT(
r.value(p),
static_cast<long>(
r.size()));
385 EXPECT_EQ(
r.values().size(),
r.size());
397 const Test_Tree & t = tree_of(n);
398 return M(t.g, n == 0 ?
nullptr : t.nodes(0));
405 const Test_Tree & t = tree_of(
m.
size());
408 for (
size_t i = 0; i <
m.
size(); i += 1 +
m.
size() / 8)
411 pairs.
append(std::make_pair(t.nodes(0), t.nodes(i)));
413 EXPECT_EQ(
m.subtree_solve(roots).
size(), roots.
size());
414 EXPECT_EQ(
m.path_solve(pairs).
size(), pairs.
size());
416 EXPECT_TRUE(
m.
is_empty() or
m.subtree_solve({t.nodes(0)})(0) == std::min<size_t>(
m.
size(), 7));
426 static M make(
const size_t n) {
return M(node_tree_of(std::max<size_t>(n, 1)).
nodes(0)); }
432 const Test_Node_Tree & t = node_tree_of(
m.
size());
435 for (
size_t i = 0; i <
m.
size(); i += 1 +
m.
size() / 8)
438 pairs.
append(std::make_pair(t.nodes(i), t.nodes(
m.
size() - 1)));
440 EXPECT_EQ(
m.subtree_solve(roots).
size(), roots.
size());
441 EXPECT_EQ(
m.path_solve(pairs).
size(), pairs.
size());
442 EXPECT_TRUE(
m.
is_empty() or
m.subtree_solve({t.nodes(0)})(0) == std::min<size_t>(
m.
size(), 7));
451 static F make(
const size_t n) {
return F(digraph_of(std::clamp<size_t>(n, 1, 30)).g); }
462 for (
long i = 0; i < n; ++i)
463 for (
long j = 0; j < n; ++j)
467 EXPECT_EQ(path.
size() == 1, i == j);
470 EXPECT_THROW((
void) f.
get_min_path(n, 0), std::out_of_range);
480 static I make(
const size_t n) {
return I(digraph_of(n).g); }
487 EXPECT_THROW((
void) x.
in_degree(stranger()), std::domain_error);
489 const Test_Digraph & d = digraph_of(x.
size());
491 for (
size_t i = 0; i < d.nodes.size(); ++i)
494 for (
auto it = x.
get_it(d.nodes(i)); it.has_curr(); it.next_ne(), ++
k)
495 EXPECT_EQ(d.g.get_tgt_node(it.get_curr_ne()), d.nodes(i));
499 EXPECT_EQ(total, x.
size());
508 static long cost(
const size_t i,
const size_t j) {
return static_cast<long>((i * 31 + j * 17) % 97); }
512 const size_t rows = std::clamp<size_t>(n, 1, 40);
514 for (
size_t i = 0; i <
rows; ++i)
515 for (
size_t j = 0; j <
rows; ++j)
516 c.
write(i, j, cost(i, j));
522 return h.rows() == 0
and h.cols() == 0
and h.dimension() == 0
and h.get_total_cost() == 0
523 and h.get_row_assignments().size() == 0
and h.get_col_assignments().size() == 0;
529 for (
size_t i = 0; i <
h.rows(); ++i)
531 const long j =
h.get_assignment(i);
533 EXPECT_EQ(
h.get_col_assignments()[
static_cast<size_t>(j)],
static_cast<long>(i));
534 total += cost(i,
static_cast<size_t>(j));
536 EXPECT_EQ(total,
h.get_total_cost());
537 EXPECT_EQ(
h.get_assignments().size(),
h.rows());
538 EXPECT_THROW((
void)
h.get_assignment(
h.rows()), std::out_of_range);
552 row(j) = 1.0 + j % 2;
559 S s(std::clamp<size_t>(n, 1, 50));
562 add_sum_restriction(s);
575 for (
size_t j = 0; j < n; ++j)
581 for (
size_t j = 0; j < n; ++j)
586 add_sum_restriction(s);
588 EXPECT_EQ(s.
solve(), S::Solved);
600 static S make(
const size_t n) {
return S(std::clamp<size_t>(n, 1, 50), 2); }
611 for (
size_t j = 0; j < n; ++j)
615 for (
size_t j = 0; j < n; ++j)
622 for (
size_t j = 0; j < n; ++j)
Floyd-Warshall algorithm for all-pairs shortest paths.
Heavy-Light Decomposition for tree path queries on Aleph graphs.
Hungarian (Kuhn-Munkres) algorithm for the optimal assignment problem.
Lowest Common Ancestor (LCA) on rooted trees represented as Aleph graphs.
Simplex algorithm for linear programming.
Generic tree dynamic programming and rerooting algorithms.
Heavy-Light and Centroid decomposition on rooted trees represented as Aleph graphs.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
Generic directed graph (digraph) wrapper template.
Node belonging to a double circular linked list with header node.
size_t size() const noexcept
Return the current dimension of array.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
Dynamic matrix with sparse storage.
T & write(const size_t i, const size_t j, const T &data)
Write a value to position (i, j).
constexpr size_t rows() const noexcept
Get the number of rows.
Compute the all-pairs shortest path distance matrix and the path reconstruction matrix for a graph g ...
Path< GT > get_min_path(const long src_idx, const long tgt_idx) const
Reconstruct the shortest path between two nodes by matrix indices.
const DynMatrix< Distance_Type > & get_dist_mat() const noexcept
Get the distance matrix.
Node * select_node(long i) const noexcept
Get the graph node at a given matrix index.
constexpr bool has_negative_cycle() const noexcept
Check if the graph contains a negative cycle.
const DynArray< Node * > & get_nodes_ref() const noexcept
Get the array of graph nodes as a constant reference.
const DynMatrix< long > & get_path_mat() const noexcept
Get the path reconstruction matrix.
LCA via binary lifting on a rooted tree.
Centroid decomposition over a rooted tree represented as an Aleph graph.
bool is_empty() const noexcept
Node * centroid_root() const noexcept
Root centroid node of the centroid tree (or nullptr if empty).
bool is_centroid_ancestor_id(const size_t ancestor, const size_t node) const
True iff ancestor is in the centroid-ancestor chain of node.
size_t centroid_root_id() const noexcept
Root centroid ID of the centroid tree (or NONE if empty).
const Array< size_t > & centroid_ancestors_of_id(const size_t id) const
Centroid ancestors of node ID from centroid root down to local centroid.
size_t centroid_path_length_of_id(const size_t id) const
Number of centroid ancestors stored for node ID.
size_t max_centroid_level() const noexcept
Maximum centroid-tree level.
size_t distance_to_centroid_id(const size_t node, const size_t centroid) const
Distance from node ID to a centroid ancestor ID.
void for_each_centroid_ancestor_id(const size_t node, F &&f) const
Visit each centroid ancestor of node ID.
size_t size() const noexcept
LCA via Euler tour + RMQ on depth in a rooted tree.
Segment-tree-powered path/subtree queries over an HLD layout.
const Gen_Heavy_Light_Decomposition< GT, SA > & decomposition() const noexcept
T query_path_id(const size_t u, const size_t v) const
Path query between two node IDs in O(log^2 n).
T get_node_id(const size_t id) const
Return current value at node ID.
size_t size() const noexcept
T query_subtree_id(const size_t id) const
Subtree query for node ID in O(log n).
void update_node_id(const size_t id, const T &delta)
Point update: a[id] = Op(a[id], delta).
bool is_empty() const noexcept
Heavy-Light Decomposition over a rooted tree represented as an Aleph graph.
Offline subtree and path queries on N-ary trees (Tree_Node).
Offline subtree and path queries on trees via Mo's algorithm.
Generic rerooting dynamic programming (all-roots DP).
Generic bottom-up tree dynamic programming.
const Array< T > & values() const noexcept
Returns all DP values (indexed by internal node ID).
Node * node_of(size_t id) const
Returns the node pointer for a given internal ID.
size_t id_of(Node *node) const
Returns the internal ID for a given node pointer.
size_t size() const noexcept
Returns the number of nodes in the tree.
const T & value(Node *node) const
Returns the DP value for a given node.
constexpr bool is_empty() const noexcept
Implementation of the Hungarian (Munkres) algorithm.
Index of the arcs that go into each node of a graph.
size_t in_degree(const Node *p) const
Return the number of arcs into p that the filter showed.
Iterator get_it(const Node *p) const
Return an iterator on the arcs into p that the filter showed.
size_t size() const noexcept
Return the number of arcs of the index: those that the filter showed.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
size_t size() const noexcept
Return the path length in nodes.
Node * get_first_node() const
Return the first node of path; throws overflow_error if path is empty.
Revised Simplex algorithm for linear programming.
void set_objective(size_t j, T coef)
Sets objective coefficient.
size_t get_num_vars() const noexcept
Gets number of variables.
size_t get_num_constraints() const noexcept
Gets number of constraints.
State get_state() const noexcept
Gets current state.
T get_solution(size_t j) const
Gets solution value for variable j.
void set_constraint(size_t i, size_t j, T coef)
Sets constraint coefficient.
State solve()
Solves the linear program.
void set_rhs(size_t i, T rhs)
Sets RHS value for a constraint.
bool verify_solution() const
Verifies solution satisfies all constraints.
Linear program solver using the Simplex method.
T & get_restriction_coef(const size_t rest_num, size_t idx)
Gets a specific coefficient from a constraint.
bool verify_solution() const
Verifies that the solution satisfies all constraints.
T * put_restriction(const T *coefs=nullptr, ConstraintType type=ConstraintType::LE)
Adds a constraint to the linear program.
void prepare_linear_program()
Prepares the linear program for solving.
size_t get_num_restrictions() const noexcept
Gets the number of constraints.
void put_objetive_function_coef(size_t i, const T &coef)
Sets a coefficient in the objective function.
void load_solution() noexcept
Loads the solution values into the solution array.
size_t get_num_vars() const noexcept
Gets the number of decision variables.
State get_state() const noexcept
Gets the current state of the solver.
State solve()
Solves the linear program.
const T & get_objetive_function_coef(size_t i) const
Gets a coefficient from the objective function.
Forward declaration used by CRTP helpers before the full node definition.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
The typed suite of the moved-from tests.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
constexpr bool is_empty() const noexcept
Checks if the table is empty.
DynArray< Graph::Node * > nodes
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
The pattern test of the moved-from objects.
::testing::Types< Binary_Lifting_LCA< Graph >, Euler_RMQ_LCA< Graph >, HLD_Sum< Graph, int >, Heavy_Light_Decomposition< Graph >, HLD_Path_Query< Graph, int, Aleph::plus< int > >, Centroid_Decomposition< Graph >, Tree_DP< Graph, long >, Reroot_DP< Graph, long >, Distinct_Count_Mo_On_Trees< Graph >, Distinct_Count_Mo_On_Tree_Node< int >, Floyd_All_Shortest_Paths< Dgraph >, In_Arcs_Index< Dgraph >, Hungarian_Assignment< long >, Simplex< double >, RevisedSimplex< double > > Graphs
INSTANTIATE_TYPED_TEST_SUITE_P(Graphs, MovedFrom, Graphs)
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
Arc of graph implemented with double-linked adjacency lists.
HLD with path/subtree sum queries.
static L make(const size_t n)
static bool is_empty(const L &l)
static C make(const size_t n)
static bool is_empty(const C &c)
static bool is_empty(const M &m)
static M make(const size_t n)
static M make(const size_t n)
static bool is_empty(const M &m)
static bool is_empty(const L &l)
static L make(const size_t n)
static bool is_empty(const F &f)
static F make(const size_t n)
static Q make(const size_t n)
static bool is_empty(const Q &q)
static H make(const size_t n)
static bool is_empty(const H &h)
static bool is_empty(const H &h)
static H make(const size_t n)
static H make(const size_t n)
static bool is_empty(const H &h)
static long cost(const size_t i, const size_t j)
static bool is_empty(const I &x)
static I make(const size_t n)
static bool is_empty(const R &r)
static R make(const size_t n)
static bool is_empty(const S &s)
static S make(const size_t n)
static S make(const size_t n)
static bool is_empty(const S &s)
static void add_sum_restriction(S &s)
static D make(const size_t n)
static bool is_empty(const D &d)
How the moved-from tests build, check and use objects of a class.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.
Dynamic matrix with lazy allocation.
Generic graph and digraph implementations.
Mo's algorithm on trees: offline subtree and path queries.
General tree (n-ary tree) node.