LAL: Linear Arrangement Library 21.07.01
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
algorithms_crossings.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#include <vector>
46
47// lal includes
48#include <lal/definitions.hpp>
49#include <lal/graphs/graph.hpp>
50#include <lal/graphs/directed_graph.hpp>
51#include <lal/graphs/undirected_graph.hpp>
52
53namespace lal {
54namespace internal {
55
56/*
57 * @brief Computes the number of edge crossings in a linear arrangement.
58 *
59 * Given a graph, and a linear arrangement of its nodes, computes by
60 * brute force the number of edges that cross in such linear arrangement.
61 * If the arrangement is not specified, the identity arrangement is used.
62 * @param g Input graph.
63 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
64 * @returns The number of crossings \f$C\f$.
65 */
66uint32_t n_C_brute_force(
67 const graphs::directed_graph& g,
68 const linear_arrangement& pi
69) noexcept;
70uint32_t n_C_brute_force(
71 const graphs::undirected_graph& g,
72 const linear_arrangement& pi
73) noexcept;
74/*
75 * @brief Computes the number of edge crossings in a linear arrangement.
76 *
77 * Given a graph, and a linear arrangement of its nodes, computes by
78 * brute force the number of edges that cross in such linear arrangement.
79 * If the arrangement is not specified, the identity arrangement is used.
80 * @param g Input graph.
81 * @param pis List of \f$k\f$ linear arrangements of the nodes \f$\Pi = \{\pi_i\}_{i=1}^k\f$.
82 * @returns A list \f$L\f$ where \f$L_i = C_{\pi_i}(g)\f$.
83 * @pre None of the arrangements is empty.
84 */
85std::vector<uint32_t> n_C_brute_force(
86 const graphs::directed_graph& g,
87 const std::vector<linear_arrangement>& pis
88) noexcept;
89std::vector<uint32_t> n_C_brute_force(
90 const graphs::undirected_graph& g,
91 const std::vector<linear_arrangement>& pis
92) noexcept;
93
94/*
95 * @brief Returns whether the number of crossings is less than a given constant.
96 *
97 * Given a graph, and a linear arrangement of its nodes, tells by
98 * brute force whether the number of edges that cross in such linear arrangement
99 * is less than the given constant.
100 * If the arrangement is not specified, the identity arrangement is used.
101 * @param g Input graph.
102 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
103 * @param upper_bound Constant (upper bound).
104 * @returns The number of crossings \f$C\f$ if said number is less than or equal
105 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
106 */
107uint32_t is_n_C_brute_force_lesseq_than(
108 const graphs::directed_graph& g,
109 const linear_arrangement& pi,
110 uint32_t upper_bound
111) noexcept;
112uint32_t is_n_C_brute_force_lesseq_than(
113 const graphs::undirected_graph& g,
114 const linear_arrangement& pi,
115 uint32_t upper_bound
116) noexcept;
117
118/*
119 * @brief Returns whether the number of crossings is less than a given constant.
120 *
121 * Given a graph, and a linear arrangement of its nodes, tells by
122 * brute force whether the number of edges that cross in such linear arrangement
123 * is less than the given constant.
124 * If the arrangement is not specified, the identity arrangement is used.
125 * @param g Input graph.
126 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
127 * @param upper_bound Constant (upper bound).
128 * @returns The number of crossings \f$C\f$ if said number is less than or equal
129 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
130 */
131std::vector<uint32_t> is_n_C_brute_force_lesseq_than(
132 const graphs::directed_graph& g,
133 const std::vector<linear_arrangement>& pi,
134 uint32_t upper_bound
135) noexcept;
136std::vector<uint32_t> is_n_C_brute_force_lesseq_than(
137 const graphs::undirected_graph& g,
138 const std::vector<linear_arrangement>& pi,
139 uint32_t upper_bound
140) noexcept;
141
142/*
143 * @brief Returns whether the number of crossings is less than a given constant.
144 *
145 * Given a graph, and a linear arrangement of its nodes, tells by
146 * brute force whether the number of edges that cross in such linear arrangement
147 * is less than the given constant.
148 * If the arrangement is not specified, the identity arrangement is used.
149 * @param g Input graph.
150 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
151 * @param upper_bound Constant (upper bound).
152 * @returns The number of crossings \f$C\f$ if said number is less than or equal
153 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
154 */
155std::vector<uint32_t> is_n_C_brute_force_lesseq_than(
156 const graphs::directed_graph& g,
157 const std::vector<linear_arrangement>& pi,
158 const std::vector<uint32_t>& upper_bounds
159) noexcept;
160std::vector<uint32_t> is_n_C_brute_force_lesseq_than(
161 const graphs::undirected_graph& g,
162 const std::vector<linear_arrangement>& pi,
163 const std::vector<uint32_t>& upper_bounds
164) noexcept;
165
166// -----------------------------------------------------------------------------
167
168/*
169 * @brief Computes the number of edge crossings in a linear arrangement.
170 *
171 * Given a graph, and a linear arrangement of its nodes, computes using
172 * dynamic programming the number of edges that cross in such linear
173 * arrangement. If the arrangement is not specified, the identity arrangement
174 * is used.
175 * @param g Input graph.
176 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
177 * @returns The number of crossings \f$C\f$.
178 */
179uint32_t n_C_dynamic_programming(
180 const graphs::directed_graph& g,
181 const linear_arrangement& pi
182) noexcept;
183uint32_t n_C_dynamic_programming(
184 const graphs::undirected_graph& g,
185 const linear_arrangement& pi
186) noexcept;
187/*
188 * @brief Computes the number of edge crossings in a linear arrangement.
189 *
190 * Given a graph, and a list of linear arrangements of its nodes, computes
191 * using dynamic programming the number of edges that cross in every linear
192 * arrangement.
193 * @param g Input graph.
194 * @param pis List of \f$k\f$ linear arrangements of the nodes \f$\Pi = \{\pi_i\}_{i=1}^k\f$.
195 * @returns A list \f$L\f$ where \f$L_i = C_{\pi_i}(g)\f$.
196 * @pre None of the arrangements is empty.
197 */
198std::vector<uint32_t> n_C_dynamic_programming(
199 const graphs::directed_graph& g,
200 const std::vector<linear_arrangement>& pis
201) noexcept;
202std::vector<uint32_t> n_C_dynamic_programming(
203 const graphs::undirected_graph& g,
204 const std::vector<linear_arrangement>& pis
205) noexcept;
206
207/*
208 * @brief Returns whether the number of crossings is less than a given constant.
209 *
210 * Given a graph, and a linear arrangement of its nodes, tells by
211 * brute force whether the number of edges that cross in such linear arrangement
212 * is less than the given constant.
213 * If the arrangement is not specified, the identity arrangement is used.
214 * @param g Input graph.
215 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
216 * @param upper_bound Constant (upper bound).
217 * @returns The number of crossings \f$C\f$ if said number is less than or equal
218 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
219 */
220uint32_t is_n_C_dynamic_programming_lesseq_than(
221 const graphs::directed_graph& g,
222 const linear_arrangement& pi,
223 uint32_t upper_bound
224) noexcept;
225uint32_t is_n_C_dynamic_programming_lesseq_than(
226 const graphs::undirected_graph& g,
227 const linear_arrangement& pi,
228 uint32_t upper_bound
229) noexcept;
230
231/*
232 * @brief Returns whether the number of crossings is less than a given constant.
233 *
234 * Given a graph, and a linear arrangement of its nodes, tells by
235 * brute force whether the number of edges that cross in such linear arrangement
236 * is less than the given constant.
237 * If the arrangement is not specified, the identity arrangement is used.
238 * @param g Input graph.
239 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
240 * @param upper_bound Constant (upper bound).
241 * @returns The number of crossings \f$C\f$ if said number is less than or equal
242 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
243 */
244std::vector<uint32_t> is_n_C_dynamic_programming_lesseq_than(
245 const graphs::directed_graph& g,
246 const std::vector<linear_arrangement>& pi,
247 uint32_t upper_bound
248) noexcept;
249std::vector<uint32_t> is_n_C_dynamic_programming_lesseq_than(
250 const graphs::undirected_graph& g,
251 const std::vector<linear_arrangement>& pi,
252 uint32_t upper_bound
253) noexcept;
254
255/*
256 * @brief Returns whether the number of crossings is less than a given constant.
257 *
258 * Given a graph, and a linear arrangement of its nodes, tells by
259 * brute force whether the number of edges that cross in such linear arrangement
260 * is less than the given constant.
261 * If the arrangement is not specified, the identity arrangement is used.
262 * @param g Input graph.
263 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
264 * @param upper_bound Constant (upper bound).
265 * @returns The number of crossings \f$C\f$ if said number is less than or equal
266 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
267 */
268std::vector<uint32_t> is_n_C_dynamic_programming_lesseq_than(
269 const graphs::directed_graph& g,
270 const std::vector<linear_arrangement>& pi,
271 const std::vector<uint32_t>& upper_bounds
272) noexcept;
273std::vector<uint32_t> is_n_C_dynamic_programming_lesseq_than(
274 const graphs::undirected_graph& g,
275 const std::vector<linear_arrangement>& pi,
276 const std::vector<uint32_t>& upper_bounds
277) noexcept;
278
279// -----------------------------------------------------------------------------
280
281/*
282 * @brief Computes the number of edge crossings in a linear arrangement.
283 *
284 * Given a graph, and a permutation of its nodes, computes using
285 * dynamic programming the number of edges that cross in such linear
286 * arrangement. If the arrangement is not specified, the identity arrangement
287 * is used.
288 * @param g Input graph.
289 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
290 * @returns The number of crossings \f$C\f$.
291 */
292uint32_t n_C_ladder(
293 const graphs::directed_graph& g,
294 const linear_arrangement& pi
295) noexcept;
296uint32_t n_C_ladder(
297 const graphs::undirected_graph& g,
298 const linear_arrangement& pi
299) noexcept;
300/*
301 * @brief Computes the number of edge crossings in a linear arrangement.
302 *
303 * Given a graph, and a list of linear arrangements of its nodes, computes
304 * using dynamic programming the number of edges that cross in such linear
305 * arrangement.
306 * @param g Input graph.
307 * @param pis List of \f$k\f$ linear arrangements of the nodes \f$\Pi = \{\pi_i\}_{i=1}^k\f$.
308 * @returns A list \f$L\f$ where \f$L_i = C_{\pi_i}(g)\f$.
309 * @pre None of the arrangements is empty.
310 */
311std::vector<uint32_t> n_C_ladder(
312 const graphs::directed_graph& g,
313 const std::vector<linear_arrangement>& pis
314) noexcept;
315std::vector<uint32_t> n_C_ladder(
316 const graphs::undirected_graph& g,
317 const std::vector<linear_arrangement>& pis
318) noexcept;
319
320/*
321 * @brief Returns whether the number of crossings is less than a given constant.
322 *
323 * Given a graph, and a linear arrangement of its nodes, tells by
324 * brute force whether the number of edges that cross in such linear arrangement
325 * is less than the given constant.
326 * If the arrangement is not specified, the identity arrangement is used.
327 * @param g Input graph.
328 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
329 * @param upper_bound Constant (upper bound).
330 * @returns The number of crossings \f$C\f$ if said number is less than or equal
331 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
332 */
333uint32_t is_n_C_ladder_lesseq_than(
334 const graphs::directed_graph& g,
335 const linear_arrangement& pi,
336 uint32_t upper_bound
337) noexcept;
338uint32_t is_n_C_ladder_lesseq_than(
339 const graphs::undirected_graph& g,
340 const linear_arrangement& pi,
341 uint32_t upper_bound
342) noexcept;
343
344/*
345 * @brief Returns whether the number of crossings is less than a given constant.
346 *
347 * Given a graph, and a linear arrangement of its nodes, tells by
348 * brute force whether the number of edges that cross in such linear arrangement
349 * is less than the given constant.
350 * If the arrangement is not specified, the identity arrangement is used.
351 * @param g Input graph.
352 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
353 * @param upper_bound Constant (upper bound).
354 * @returns The number of crossings \f$C\f$ if said number is less than or equal
355 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
356 */
357std::vector<uint32_t> is_n_C_ladder_lesseq_than(
358 const graphs::directed_graph& g,
359 const std::vector<linear_arrangement>& pi,
360 uint32_t upper_bound
361) noexcept;
362std::vector<uint32_t> is_n_C_ladder_lesseq_than(
363 const graphs::undirected_graph& g,
364 const std::vector<linear_arrangement>& pi,
365 uint32_t upper_bound
366) noexcept;
367
368/*
369 * @brief Returns whether the number of crossings is less than a given constant.
370 *
371 * Given a graph, and a linear arrangement of its nodes, tells by
372 * brute force whether the number of edges that cross in such linear arrangement
373 * is less than the given constant.
374 * If the arrangement is not specified, the identity arrangement is used.
375 * @param g Input graph.
376 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
377 * @param upper_bound Constant (upper bound).
378 * @returns The number of crossings \f$C\f$ if said number is less than or equal
379 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
380 */
381std::vector<uint32_t> is_n_C_ladder_lesseq_than(
382 const graphs::directed_graph& g,
383 const std::vector<linear_arrangement>& pi,
384 const std::vector<uint32_t>& upper_bounds
385) noexcept;
386std::vector<uint32_t> is_n_C_ladder_lesseq_than(
387 const graphs::undirected_graph& g,
388 const std::vector<linear_arrangement>& pi,
389 const std::vector<uint32_t>& upper_bounds
390) noexcept;
391
392// -----------------------------------------------------------------------------
393
394/*
395 * @brief Computes the number of edge crossings in a linear arrangement.
396 *
397 * Given a graph, and a linear arrangements of its nodes, computes
398 * using the algorithm by K. Palios and G. Pitsiladis the number of edges that
399 * cross in such linear arrangement. If the arrangement is not specified, the
400 * identity arrangement is used.
401 * @param g Input graph.
402 * @param pi A linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
403 * @returns The number of crossings \f$C\f$.
404 */
405uint32_t n_C_stack_based(
406 const graphs::graph& g,
407 const linear_arrangement& pi
408) noexcept;
409/*
410 * @brief Computes the number of edge crossings in a linear arrangement.
411 *
412 * Given a graph, and a list of linear arrangements of its nodes, computes
413 * using the algorithm by K. Palios and G. Pitsiladis the number of edges that
414 * cross in such linear arrangement. If the arrangement is not specified, the
415 * identity arrangement is used.
416 * @param g Input graph.
417 * @param pis List of \f$k\f$ linear arrangements of the nodes \f$\Pi = \{\pi_i\}_{i=1}^k\f$.
418 * @returns A list \f$L\f$ where \f$L_i = C_{\pi_i}(g)\f$.
419 * @pre None of the arrangements is empty.
420 */
421std::vector<uint32_t> n_C_stack_based(
422 const graphs::graph& g,
423 const std::vector<linear_arrangement>& pis
424) noexcept;
425
426/*
427 * @brief Returns whether the number of crossings is less than a given constant.
428 *
429 * Given a graph, and a linear arrangement of its nodes, tells by
430 * brute force whether the number of edges that cross in such linear arrangement
431 * is less than the given constant.
432 * If the arrangement is not specified, the identity arrangement is used.
433 * @param g Input graph.
434 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
435 * @param upper_bound Constant (upper bound).
436 * @returns The number of crossings \f$C\f$ if said number is less than or equal
437 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
438 */
439uint32_t is_n_C_stack_based_lesseq_than(
440 const graphs::graph& g,
441 const linear_arrangement& pi,
442 uint32_t upper_bound
443) noexcept;
444
445/*
446 * @brief Returns whether the number of crossings is less than a given constant.
447 *
448 * Given a graph, and a linear arrangement of its nodes, tells by
449 * brute force whether the number of edges that cross in such linear arrangement
450 * is less than the given constant.
451 * If the arrangement is not specified, the identity arrangement is used.
452 * @param g Input graph.
453 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
454 * @param upper_bound Constant (upper bound).
455 * @returns The number of crossings \f$C\f$ if said number is less than or equal
456 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
457 */
458std::vector<uint32_t> is_n_C_stack_based_lesseq_than(
459 const graphs::graph& g,
460 const std::vector<linear_arrangement>& pi,
461 uint32_t upper_bound
462) noexcept;
463
464/*
465 * @brief Returns whether the number of crossings is less than a given constant.
466 *
467 * Given a graph, and a linear arrangement of its nodes, tells by
468 * brute force whether the number of edges that cross in such linear arrangement
469 * is less than the given constant.
470 * If the arrangement is not specified, the identity arrangement is used.
471 * @param g Input graph.
472 * @param pi Linear arrangement of the nodes. When omitted, \f$\pi_I\f$ is used.
473 * @param upper_bound Constant (upper bound).
474 * @returns The number of crossings \f$C\f$ if said number is less than or equal
475 * to the upper bound. The function returns \f$m^2+1\f$ if otherwise.
476 */
477std::vector<uint32_t> is_n_C_stack_based_lesseq_than(
478 const graphs::graph& g,
479 const std::vector<linear_arrangement>& pi,
480 const std::vector<uint32_t>& upper_bounds
481) noexcept;
482
483} // -- namespace internal
484} // -- namespace lal
Main namespace of the library.
Definition definitions.hpp:48
std::vector< position > linear_arrangement
A linear arrangement of the nodes of a graph.
Definition definitions.hpp:72