LAL: Linear Arrangement Library 21.07.01
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 - 2021
7 *
8 * This file is part of Linear Arrangement Library. To see the full code
9 * visit the webpage:
10 * https://github.com/lluisalemanypuig/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/internal/graphs/traversal.hpp>
53#include <lal/internal/data_array.hpp>
54
55namespace lal {
56namespace internal {
57
58template<
59 class T,
60 std::enable_if_t<
61 std::is_base_of_v<graphs::free_tree, T> ||
62 std::is_base_of_v<graphs::rooted_tree, T>,
63 bool> = true
64>
65uint32_t tree_diameter(const T& t) {
66 if (t.get_num_nodes_component(0) == 1) { return 0; }
67
68 const uint64_t n = t.get_num_nodes();
69
70 BFS bfs(t);
71
72 if constexpr (std::is_base_of_v<graphs::rooted_tree, T>) {
73 bfs.set_use_rev_edges(true);
74 }
75 else {
76 bfs.set_use_rev_edges(false);
77 }
78
79 node farthest_from_0;
80
81 // calculate the farthest vertex from a starting 'random' vertex
82 bfs.set_process_neighbour
83 ( [&](const auto&, node, node v, bool) { farthest_from_0 = v; } );
84 bfs.start_at(0);
85
86 // calculate the longest distance from 'farthest_from_0'
87 uint32_t diameter = 0;
88 data_array<uint32_t> distance(n, 0);
89
90 bfs.reset();
91 if constexpr (std::is_base_of_v<graphs::rooted_tree, T>) {
92 bfs.set_use_rev_edges(true);
93 }
94 else {
95 bfs.set_use_rev_edges(false);
96 }
97
98 bfs.set_process_neighbour(
99 [&](const auto&, node u, node v, bool) {
100 distance[v] = distance[u] + 1; diameter = std::max(diameter, distance[v]); }
101 );
102 bfs.start_at(farthest_from_0);
103
104 return diameter;
105}
106
107} // -- namespace internal
108} // -- namespace lal
Main namespace of the library.
Definition definitions.hpp:48
uint32_t node
Node type.
Definition definitions.hpp:51