LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
Anderson.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 - 2024
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 (lluis.alemany.puig@upc.edu)
28 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
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 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office 220, 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// lal includes
45#include <lal/graphs/rooted_tree.hpp>
46#include <lal/linarr/chunking/chunk.hpp>
47#include <lal/detail/linarr/chunking/generic.hpp>
48
49namespace lal {
50namespace detail {
51
62template <class arrangement_t>
63class chunks_Anderson : public chunks_generic<arrangement_t> {
64public:
70 chunks_Anderson(const graphs::rooted_tree& rt, const arrangement_t& arr)
71 noexcept
72 : generic(rt, arr)
73 {
74 }
75
82 void chunk_input_tree() noexcept {
84
85 if (m_rt.get_num_nodes() == 1) {
86 m_sequence.set_chunk_index(0, 0);
89 }
90 else {
91 // assign chunk indices
92 std::size_t chunk_idx = 0;
94
95 // relabel chunk indices from 0 to k so that chunk 0 is the
96 // leftmost in the linear arrangement, and k is the rightmost
98
99 // make chunks out of the map
100 make_chunks();
101 }
102 }
103
104private:
105
107 [[nodiscard]] bool can_be_added(const node r, const node u) const noexcept {
108 return m_rt.get_out_degree(u) == 0 and m_rt.has_edge(r, u);
109 }
110
116 void assign_chunk_indices(const node_t r, std::size_t& chunk_idx) noexcept {
117 ++chunk_idx;
118
119#if defined DEBUG
120 assert(m_rt.get_out_degree(*r) > 0);
121#endif
122
123 set_chunk_index(*r, chunk_idx);
124
125 // march leftwards in the arrangement assigning to the root's children
126 // the same chunk index as the root
127 position_t p_root = m_arr[r];
128 if (p_root > 0ull) {
129 position_t pl = p_root - 1ull;
130 while (pl > 0ull and can_be_added(*r, m_arr[pl])) {
131 set_chunk_index(m_arr[pl], chunk_idx);
132 --pl;
133 }
134 // pl == 0
135 if (can_be_added(*r, m_arr[pl])) {
136 set_chunk_index(m_arr[pl], chunk_idx);
137 }
138 }
139 // march rightwards in the arrangement assigning to the root's children
140 // the same chunk index as the root
141 if (p_root < m_n - 1) {
142 position_t pr = p_root + 1ull;
143 while (pr < m_n and can_be_added(*r, m_arr[pr])) {
144 set_chunk_index(m_arr[pr], chunk_idx);
145 ++pr;
146 }
147 }
148
149 // assign new chunk indices to the terminal, unassigned children of 'r'
150 for (node v : m_rt.get_out_neighbors(*r)) {
151 if (node_to_chunk(v) > m_n and m_rt.get_out_degree(v) == 0) {
152 set_chunk_index(v, chunk_idx);
153 ++chunk_idx;
154 }
155 }
156
157 // traverse down the tree and recursively build new chunks
158 for (node_t v : m_rt.get_out_neighbors(*r)) {
159 if (m_rt.get_out_degree(*v) > 0) {
160 assign_chunk_indices(v, chunk_idx);
161 }
162 }
163 }
164
170 void relabel_chunks() noexcept {
171 std::size_t chunk_idx = 0;
172 position_t p = 0ull;
173
174 while (p < m_n) {
175 const std::size_t current_index = node_to_chunk(m_arr[p]);
176
177 set_chunk_index(m_arr[p], chunk_idx);
178 ++p;
179
180 while (p < m_n and current_index == node_to_chunk(m_arr[p])) {
181 set_chunk_index(m_arr[p], chunk_idx);
182 ++p;
183 }
184
185 ++chunk_idx;
186 }
187 }
188
195 void make_chunks() noexcept {
197
198 position_t p = 0ull;
199 while (p < m_n) {
200
201 const std::size_t current_index = node_to_chunk(m_arr[p]);
202
204 c.add_node(m_arr[p]);
205
206 ++p;
207 while (p < m_n and current_index == node_to_chunk(m_arr[p])) {
208 c.add_node(m_arr[p]);
209 ++p;
210 }
211
213
214 if (p < m_n) {
216 }
217 }
218 }
219
220private:
221
224
225 // member variables
227 using generic::m_n;
228 using generic::m_arr;
229 using generic::m_rt;
230
231 // member functions
236};
237
238} // -- namespace detail
239} // -- namespace lal
Implementation of Anderson's algorithm for chunking.
Definition Anderson.hpp:63
void assign_chunk_indices(const node_t r, std::size_t &chunk_idx) noexcept
Assign chunk indices to all vertices of the subtree rooted at r.
Definition Anderson.hpp:116
const uint64_t m_n
Number of vertices of the tree.
Definition generic.hpp:147
const arrangement_t m_arr
Linear arrangement.
Definition generic.hpp:145
void relabel_chunks() noexcept
Relabels the chunks obtained in method assign_chunk_indices.
Definition Anderson.hpp:170
chunks_Anderson(const graphs::rooted_tree &rt, const arrangement_t &arr) noexcept
Constructor.
Definition Anderson.hpp:70
linarr::chunk_sequence m_sequence
The sequence of chunks obtained.
Definition generic.hpp:150
void chunk_input_tree() noexcept
Main method of this class.
Definition Anderson.hpp:82
void set_chunk_index(const node u, const std::size_t i) noexcept
Sets the chunk index of node u to index i.
Definition generic.hpp:104
void make_chunks() noexcept
Actually builds the sequence of chunks.
Definition Anderson.hpp:195
void set_parent_chunk(linarr::chunk &c) noexcept
Set the parent node of a chunks.
Definition generic.hpp:109
linarr::chunk & last_chunk() noexcept
Returns a reference to the last chunk in the sentence.
Definition generic.hpp:95
std::size_t node_to_chunk(const node u) const noexcept
Returns the chunk index of node u.
Definition generic.hpp:100
const graphs::rooted_tree & m_rt
Input rooted tree.
Definition generic.hpp:143
bool can_be_added(const node r, const node u) const noexcept
Can node u be added to the same chunk as r.
Definition Anderson.hpp:107
Basic algorithms existent in every definition of chunking.
Definition generic.hpp:61
const uint64_t m_n
Number of vertices of the tree.
Definition generic.hpp:147
const arrangement_t m_arr
Linear arrangement.
Definition generic.hpp:145
linarr::chunk_sequence m_sequence
The sequence of chunks obtained.
Definition generic.hpp:150
void set_chunk_index(const node u, const std::size_t i) noexcept
Sets the chunk index of node u to index i.
Definition generic.hpp:104
void set_parent_chunk(linarr::chunk &c) noexcept
Set the parent node of a chunks.
Definition generic.hpp:109
linarr::chunk & last_chunk() noexcept
Returns a reference to the last chunk in the sentence.
Definition generic.hpp:95
std::size_t node_to_chunk(const node u) const noexcept
Returns the chunk index of node u.
Definition generic.hpp:100
const graphs::rooted_tree & m_rt
Input rooted tree.
Definition generic.hpp:143
uint64_t get_out_degree(const node u) const noexcept
Returns the out-degree of a node.
Definition directed_graph.hpp:420
const neighbourhood & get_out_neighbors(const node u) const noexcept
Returns the out-neighbors of node u.
Definition directed_graph.hpp:390
bool has_edge(const node u, const node v) const noexcept
Returns true if the edge exists in the graph.
uint64_t get_num_nodes() const noexcept
Returns the number of ndoes.
Definition graph.hpp:207
Rooted tree graph class.
Definition rooted_tree.hpp:109
node get_root() const noexcept
Return the root of this tree.
Definition rooted_tree.hpp:637
void push_chunk() noexcept
Adds a new chunk to the collection.
Definition chunk_sequence.hpp:155
void init(const std::size_t n) noexcept
Initializes this chunk sequence.
Definition chunk_sequence.hpp:121
Definition of a chunk.
Definition chunk.hpp:64
void set_root_node(const node u) noexcept
Sets the root node of this chunk.
Definition chunk.hpp:114
void add_node(const node u) noexcept
Adds a new node to this chunk.
Definition chunk.hpp:78
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
Typesafe node type.
Definition basic_types.hpp:70
Typesafe position type.
Definition basic_types.hpp:244