48#include <lal/graphs/tree_type.hpp>
49#include <lal/graphs/tree.hpp>
50#include <lal/detail/graphs/traversal.hpp>
51#include <lal/detail/array.hpp>
52#include <lal/detail/queue_array.hpp>
53#include <lal/detail/type_traits/conditional_list.hpp>
57namespace maximum_subtrees {
58namespace caterpillar {
97template <
class tree_t>
108 const auto n = t.get_num_nodes();
113 num_vertices_in_path.fill(0);
114 num_vertices_in_path[start_at] = 1;
115 uint64_t max_num_vertices = 0;
117 node farthest = n + 1;
119 bfs.set_process_neighbour(
120 [&](
const auto&,
node u,
node v,
bool) {
121 num_vertices_in_path[v] = num_vertices_in_path[u] + weight[u] + 1;
122 if (max_num_vertices < num_vertices_in_path[v]) {
123 max_num_vertices = num_vertices_in_path[v];
128 bfs.start_at(start_at);
150 std::enable_if_t< std::is_base_of_v<graphs::tree, tree_t>,
bool > =
true
161 std::tuple<uint64_t, std::vector<char>>,
162 std::tuple<uint64_t, std::vector<node>, std::vector<char>>
167 typedef std::vector<node> path;
169 const auto n = t.get_num_nodes();
177 return {0, std::vector<char>(n, 1)};
194 return {0, {0}, {1}};
205 return {0, {0,1}, {1,1}};
213 for (
node u = 0; u < n; ++u) {
214 weight[u] = (t.get_degree(u) == 1 ? 0 : t.get_degree(u) - 2);
236 const auto caterpillar_distance = n - num_vertices_in_path[w_star];
241 return caterpillar_distance;
247 if (caterpillar_distance == 0) {
249 return {caterpillar_distance, std::vector<char>(n,1)};
262 path maximum_caterpillar_backbone;
265 std::vector<char> is_node_in_maximum_caterpillar(n, 0);
268 path path_to_current;
273 path_queue.
push({w_star});
279 bfs.set_process_current(
280 [&](
const auto&,
node) {
281 path_to_current = path_queue.
pop();
285 [&](
const auto&,
node u) {
287 maximum_caterpillar_backbone = std::move(path_to_current);
292 bfs.set_process_neighbour(
293 [&](
const auto&,
node,
node v,
bool) {
294 auto path_to_v = path_to_current;
295 path_to_v.push_back(v);
296 path_queue.
push(std::move(path_to_v));
300 bfs.start_at(w_star);
303 for (
node u : maximum_caterpillar_backbone) {
304 is_node_in_maximum_caterpillar[u] = 1;
306 for (
node v : t.get_neighbors(u)) {
307 is_node_in_maximum_caterpillar[v] = 1;
311 for (
node v : t.get_in_neighbors(u)) {
312 is_node_in_maximum_caterpillar[v] = 1;
314 for (
node v : t.get_out_neighbors(u)) {
315 is_node_in_maximum_caterpillar[v] = 1;
322 caterpillar_distance,
323 std::move(is_node_in_maximum_caterpillar)
328 caterpillar_distance,
329 std::move(maximum_caterpillar_backbone),
330 std::move(is_node_in_maximum_caterpillar)
Abstract graph Breadth-First Search traversal.
Definition traversal.hpp:89
Simple array-like fixed-size queue.
Definition queue_array.hpp:69
void init(const std::size_t n) noexcept
Initializes the queue to hold n elements.
Definition queue_array.hpp:72
value_t && pop() noexcept
Pops the first element of the queue.
Definition queue_array.hpp:98
void push(const value_t &v) noexcept
Insert a new element to the queue.
Definition queue_array.hpp:79
conditional_list_t< bool_sequence< ret_type==result::distance, ret_type==result::distance_vertices, ret_type==result::distance_structure >, type_sequence< uint64_t, std::tuple< uint64_t, std::vector< char > >, std::tuple< uint64_t, std::vector< node >, std::vector< char > > > > max_subtree(const tree_t &t) noexcept
Calculate the maximum spanning caterpillar of a tree.
Definition tree_maximum_caterpillar.hpp:165
result
What is to be calculated when finding the maximum spanning caterpillar.
Definition tree_maximum_caterpillar.hpp:61
@ distance_structure
Caterpillar distance plus the caterpillar's structure.
Definition tree_maximum_caterpillar.hpp:81
@ distance
Only the caterpillar distance.
Definition tree_maximum_caterpillar.hpp:63
@ distance_vertices
Caterpillar distance plus caterpillar structure's nodes.
Definition tree_maximum_caterpillar.hpp:70
node find_farthest_vertex(const tree_t &t, const node start_at, BFS< tree_t > &bfs, array< uint64_t > &num_vertices_in_path, array< uint64_t > &weight) noexcept
Find the farthest vertex from start_at in the tree.
Definition tree_maximum_caterpillar.hpp:99
typename conditional_list< bool_seq, type_seq >::type conditional_list_t
Shorthand for conditional_list.
Definition conditional_list.hpp:78
@ caterpillar
Caterpillar trees.
Main namespace of the library.
Definition basic_types.hpp:48
uint64_t node
Node type. See Node / Vertex page for further details.
Definition basic_types.hpp:51
Wrapper of a C array for automatic deallocation of memory.
Definition array.hpp:59
A sequence of Boolean values.
Definition bool_sequence.hpp:52
A sequence of types.
Definition type_sequence.hpp:51