LAL: Linear Arrangement Library 23.01.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
tree_diameter.hpp
1/*********************************************************************
2 *
3 * Linear Arrangement Library - A library that implements a collection
4 * algorithms for linear arrangments of graphs.
5 *
6 * Copyright (C) 2019 - 2023
7 *
8 * This file is part of Linear Arrangement Library. The full code is available
9 * at:
10 * https://github.com/LAL-project/linear-arrangement-library.git
11 *
12 * Linear Arrangement Library is free software: you can redistribute it
13 * and/or modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * Linear Arrangement Library is distributed in the hope that it will be
18 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with Linear Arrangement Library. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * Contact:
26 *
27 * LluĂ­s Alemany Puig (lalemany@cs.upc.edu)
28 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
29 * CQL (Complexity and Quantitative Linguistics Lab)
30 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
31 * Webpage: https://cqllab.upc.edu/people/lalemany/
32 *
33 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
34 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office S124, Omega building
37 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
38 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
39 *
40 ********************************************************************/
41
42#pragma once
43
44// C++ includes
45#if defined DEBUG
46#include <cassert>
47#endif
48
49// lal includes
50#include <lal/graphs/tree.hpp>
51#include <lal/graphs/rooted_tree.hpp>
52#include <lal/detail/graphs/traversal.hpp>
53#include <lal/detail/data_array.hpp>
54
55namespace lal {
56namespace detail {
57
70template <
71 class tree_t,
72 std::enable_if_t< std::is_base_of_v<graphs::tree, tree_t>, bool> = true
73>
74uint64_t tree_diameter(const tree_t& t, node x) noexcept
75{
76 {
77 const auto ccsize = t.get_num_nodes_component(x);
78 if (ccsize == 1) { return 0; }
79 if (ccsize == 2) { return 1; }
80 if (ccsize == 3) { return 2; }
81 }
82
83 const uint64_t n = t.get_num_nodes();
84
85 BFS<tree_t> bfs(t);
86
87 if constexpr (std::is_base_of_v<graphs::rooted_tree, tree_t>) {
88 bfs.set_use_rev_edges(true);
89 }
90 else {
91 bfs.set_use_rev_edges(false);
92 }
93
94 node farthest_from_x;
95
96 // calculate the farthest vertex from a starting 'random' vertex
98 ( [&](const auto&, node, node v, bool) { farthest_from_x = v; } );
99 bfs.start_at(x);
100
101 // calculate the longest distance from 'farthest_from_0'
102 uint64_t diameter = 0;
103 data_array<uint64_t> distance(n, 0);
104
105 bfs.clear_visited();
106 bfs.clear_queue();
107
109 [&](const auto&, node u, node v, bool) {
110 distance[v] = distance[u] + 1; diameter = std::max(diameter, distance[v]); }
111 );
112 bfs.start_at(farthest_from_x);
113
114 return diameter;
115}
116
117} // -- namespace detail
118} // -- namespace lal
Abstract graph Breadth-First Search traversal.
Definition: traversal.hpp:89
void clear_queue() noexcept
Clear the memory allocated for this structure.
Definition: traversal.hpp:223
void start_at(node source) noexcept
Start traversal at a given node.
Definition: traversal.hpp:152
void set_process_neighbour(const BFS_process_two &f) noexcept
Set the function that controls the processing of the current neighbour.
Definition: traversal.hpp:193
void clear_visited() noexcept
Sets all nodes to not visited.
Definition: traversal.hpp:216
void set_use_rev_edges(bool use) noexcept
Set whether the traversal can use reversed edges.
Definition: traversal.hpp:173
uint64_t tree_diameter(const tree_t &t, node x) noexcept
Calculate the diameter of a tree.
Definition: tree_diameter.hpp:74
Main namespace of the library.
Definition: basic_types.hpp:50
uint64_t node
Node type. See Node / Vertex page for further details.
Definition: basic_types.hpp:53
Wrapper of a C array for autmatic deallocation of memory.
Definition: data_array.hpp:59