LAL: Linear Arrangement Library 21.07.01
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 - 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#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/internal/data_array.hpp>
55
56namespace lal {
57namespace linarr {
58
68inline bool is_permutation(const linear_arrangement& arr = {}) noexcept {
69 if (arr.size() == 0) { return true; }
70 // ensure that no position has been used twice
71 internal::data_array<position> d(arr.size(), 0);
72 for (const position p : arr) {
73 if (p >= arr.size()) { return false; }
74 if (d[p] > 0) { return false; }
75 d[p] += 1;
76 }
77 return true;
78}
79
90template<class G>
91inline bool is_arrangement(const G& g, const linear_arrangement& arr) noexcept
92{
93 if constexpr (std::is_base_of_v<G, graphs::tree>) {
94#if defined DEBUG
95 assert(g.is_tree());
96#endif
97 }
98 // identity arrangement is always a permutation
99 if (arr.size() == 0) { return true; }
100 // if sizes differ then the arrangement is not a permutation
101 if (g.get_num_nodes() != arr.size()) { return false; }
102 // ensure that the input arrangement is a permutation
103 if (not is_permutation(arr)) { return false; }
104 // the largest number must be exactly one less than the size
105 const position max_pos = *std::max_element(arr.begin(), arr.end());
106 return max_pos == arr.size() - 1;
107}
108
121template<class G>
122inline bool is_planar(const G& g, const linear_arrangement& arr = {}) noexcept {
123#if defined DEBUG
124 assert(is_arrangement(g, arr));
125#endif
126
127 const uint32_t invalid = g.get_num_edges()*g.get_num_edges() + 1;
128 return is_num_crossings_lesseq_than(g, arr, 0) < invalid;
129}
130
151inline bool is_projective
152(const graphs::rooted_tree& rt, const linear_arrangement& arr = {})
153noexcept
154{
155#if defined DEBUG
156 assert(rt.is_rooted_tree());
157#endif
158
159 // check for planarity
160 // this function already checks that an arrangement must be valid
161 if (not is_planar(rt, arr)) { return false; }
162
163 if (arr.size() == 0) {
164 const node r = rt.get_root();
165 iterators::E_iterator e_it(rt);
166 while (not e_it.end()) {
167 const auto [s,t] = e_it.get_edge();
168 const bool r_covered_st = s < r and r < t;
169 const bool r_covered_ts = t < r and r < s;
170 if (r_covered_st or r_covered_ts) { return false; }
171 e_it.next();
172 }
173 return true;
174 }
175
176 const node r = rt.get_root();
177 iterators::E_iterator e_it(rt);
178 while (not e_it.end()) {
179 const auto [s,t] = e_it.get_edge();
180 const bool r_covered_st = arr[s] < arr[r] and arr[r] < arr[t];
181 const bool r_covered_ts = arr[t] < arr[r] and arr[r] < arr[s];
182 if (r_covered_st or r_covered_ts) { return false; }
183 e_it.next();
184 }
185 return true;
186}
187
188} // -- namespace linarr
189} // -- namespace lal
Rooted tree graph class.
Definition rooted_tree.hpp:107
node get_root() const noexcept
Return the root of this tree.
Definition rooted_tree.hpp:485
bool is_rooted_tree() const noexcept
Is this tree a valid rooted tree?
Definition rooted_tree.hpp:470
bool is_arrangement(const G &g, const linear_arrangement &arr) noexcept
Is a given arrangement valid?
Definition formal_constraints.hpp:91
bool is_permutation(const linear_arrangement &arr={}) noexcept
Is a given input arrangement a permutation?
Definition formal_constraints.hpp:68
uint32_t is_num_crossings_lesseq_than(const graphs::directed_graph &G, uint32_t upper_bound, const algorithms_C &A=algorithms_C::ladder) noexcept
Is the number of crossings in the linear arrangement less than a constant?
bool is_planar(const G &g, const linear_arrangement &arr={}) noexcept
Is a given arrangement planar?
Definition formal_constraints.hpp:122
bool is_projective(const graphs::rooted_tree &rt, const linear_arrangement &arr={}) noexcept
Is a given arrangement projective?
Definition formal_constraints.hpp:152
Main namespace of the library.
Definition definitions.hpp:48
uint32_t position
Node's position type.
Definition definitions.hpp:53
uint32_t node
Node type.
Definition definitions.hpp:51
std::vector< position > linear_arrangement
A linear arrangement of the nodes of a graph.
Definition definitions.hpp:72