LAL: Linear Arrangement Library 23.01.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 - 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 // identity arrangement is always a permutation
72 if (arr.size() == 0) { return true; }
73 // an arrangement of a single element is a permutation
74 // if its only element is within range
75 if (arr.size() == 1) {
76 return arr[lal::position_t{0ull}] == 0;
77 }
78 // ensure that no position has been used twice
79 detail::data_array<char> d(arr.size(), 0);
80 for (node_t u = 0ull; u < arr.size(); ++u) {
81 const position p = arr[u];
82 // ensure all elements are within range
83 if (p >= arr.size()) { return false; }
84 // if a value already exists, this is not a permutation
85 if (d[p] > 0) { return false; }
86 d[p] += 1;
87 }
88 return true;
89}
90
101template <class graph_t>
102bool is_arrangement(const graph_t& g, const linear_arrangement& arr) noexcept
103{
104 if constexpr (std::is_base_of_v<graph_t, graphs::tree>) {
105#if defined DEBUG
106 assert(g.is_tree());
107#endif
108 }
109
110 // identity arrangement is always a permutation
111 if (arr.size() == 0) { return true; }
112 // if sizes differ then the arrangement is not a permutation
113 if (g.get_num_nodes() != arr.size()) { return false; }
114 // ensure that the input arrangement is a permutation
115 if (not is_permutation(arr)) { return false; }
116 // the largest number must be exactly one less than the size
117 const position max_pos =
118 *std::max_element(arr.begin_direct(), arr.end_direct());
119
120 return max_pos == arr.size() - 1;
121}
122
135template <class graph_t>
136bool is_planar(const graph_t& g, const linear_arrangement& arr = {}) noexcept {
137#if defined DEBUG
138 assert(is_arrangement(g, arr));
139#endif
140
141 return is_num_crossings_lesseq_than(g, arr, 0) <= 0;
142}
143
160noexcept;
161
182inline
184noexcept
185{
186#if defined DEBUG
187 assert(rt.is_rooted_tree());
188#endif
189
190 // check for planarity
191 // this function already checks that an arrangement must be valid
192 if (not is_planar(rt, arr)) { return false; }
193 return not is_root_covered(rt, arr);
194}
195
196} // -- namespace linarr
197} // -- 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:102
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:136
bool is_projective(const graphs::rooted_tree &rt, const linear_arrangement &arr) noexcept
Is a given arrangement projective?
Definition: formal_constraints.hpp:183
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
Typesafe position type.
Definition: basic_types.hpp:168