LAL: Linear Arrangement Library 23.01.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
formal_constraints.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#include <numeric>
49
50// lal includes
51#include <lal/graphs/rooted_tree.hpp>
52#include <lal/linarr/C.hpp>
53#include <lal/iterators/E_iterator.hpp>
54#include <lal/detail/data_array.hpp>
55#include <lal/detail/identity_arrangement.hpp>
56
57namespace lal {
58namespace linarr {
59
69inline
70bool is_permutation(const linear_arrangement& arr = {}) noexcept {
71 if (arr.size() <= 1) { return true; }
72 // ensure that no position has been used twice
73 detail::data_array<uint64_t> d(arr.size(), 0);
74 for (node_t u = 0ull; u < arr.size(); ++u) {
75 const position p = arr[u];
76 if (p >= arr.size()) { return false; }
77 if (d[p] > 0) { return false; }
78 d[p] += 1;
79 }
80 return true;
81}
82
93template <class graph_t>
94bool is_arrangement(const graph_t& g, const linear_arrangement& arr) noexcept
95{
96 if constexpr (std::is_base_of_v<graph_t, graphs::tree>) {
97#if defined DEBUG
98 assert(g.is_tree());
99#endif
100 }
101
102 // identity arrangement is always a permutation
103 if (arr.size() == 0) { return true; }
104 // if sizes differ then the arrangement is not a permutation
105 if (g.get_num_nodes() != arr.size()) { return false; }
106 // ensure that the input arrangement is a permutation
107 if (not is_permutation(arr)) { return false; }
108 // the largest number must be exactly one less than the size
109 const position max_pos =
110 *std::max_element(arr.begin_direct(), arr.end_direct());
111
112 return max_pos == arr.size() - 1;
113}
114
127template <class graph_t>
128bool is_planar(const graph_t& g, const linear_arrangement& arr = {}) noexcept {
129#if defined DEBUG
130 assert(is_arrangement(g, arr));
131#endif
132
133 return is_num_crossings_lesseq_than(g, arr, 0) <= 0;
134}
135
152noexcept;
153
174inline
176noexcept
177{
178#if defined DEBUG
179 assert(rt.is_rooted_tree());
180#endif
181
182 // check for planarity
183 // this function already checks that an arrangement must be valid
184 if (not is_planar(rt, arr)) { return false; }
185 return not is_root_covered(rt, arr);
186}
187
188} // -- namespace linarr
189} // -- namespace lal
Rooted tree graph class.
Definition: rooted_tree.hpp:103
Linear arrangement of vertices.
Definition: linear_arrangement.hpp:103
bool is_arrangement(const graph_t &g, const linear_arrangement &arr) noexcept
Is a given arrangement valid?
Definition: formal_constraints.hpp:94
bool is_permutation(const linear_arrangement &arr={}) noexcept
Is a given input arrangement a permutation?
Definition: formal_constraints.hpp:70
bool is_root_covered(const graphs::rooted_tree &rt, const linear_arrangement &arr) noexcept
Is the root of a rooted tree covered in a given arrangement?
bool is_planar(const graph_t &g, const linear_arrangement &arr={}) noexcept
Is a given arrangement planar?
Definition: formal_constraints.hpp:128
bool is_projective(const graphs::rooted_tree &rt, const linear_arrangement &arr) noexcept
Is a given arrangement projective?
Definition: formal_constraints.hpp:175
uint64_t is_num_crossings_lesseq_than(const graphs::directed_graph &G, uint64_t upper_bound, const algorithms_C &A=algorithms_C::ladder) noexcept
Is the number of crossings in the linear arrangement less than a constant?
Main namespace of the library.
Definition: basic_types.hpp:50
uint64_t position
Node's position type.
Definition: basic_types.hpp:55