LAL: Linear Arrangement Library 24.10.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 - 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// C++ includes
45#if defined DEBUG
46#include <cassert>
47#endif
48
49// lal includes
50#include <lal/graphs/rooted_tree.hpp>
51#include <lal/linarr/C/C.hpp>
52#include <lal/iterators/E_iterator.hpp>
53#include <lal/properties/bipartite_graph_coloring.hpp>
54#include <lal/properties/bipartite_graph_colorability.hpp>
55#include <lal/detail/array.hpp>
56#include <lal/detail/arrangement_wrapper.hpp>
57
58namespace lal {
59namespace linarr {
60
70[[nodiscard]] inline bool is_permutation(const linear_arrangement& arr = {})
71noexcept
72{
73 // identity arrangement is always a permutation
74 if (arr.size() == 0) { return true; }
75 // an arrangement of a single element is a permutation
76 // if its only element is within range
77 if (arr.size() == 1) {
78 return arr[position_t{0ull}] == 0;
79 }
80 // ensure that no position has been used twice
81 detail::array<char> d(arr.size(), 0);
82 for (node_t u = 0ull; u < arr.size(); ++u) {
83 const position p = arr[u];
84 // ensure all elements are within range
85 if (p >= arr.size()) { return false; }
86 // if a value already exists, this is not a permutation
87 if (d[p] > 0) { return false; }
88 d[p] += 1;
89 }
90 return true;
91}
92
103template <class graph_t>
104[[nodiscard]] bool is_arrangement(const graph_t& g, const linear_arrangement& arr)
105noexcept
106{
107 if constexpr (std::is_base_of_v<graph_t, graphs::tree>) {
108#if defined DEBUG
109 assert(g.is_tree());
110#endif
111 }
112
113 // identity arrangement is always a permutation
114 if (arr.size() == 0) { return true; }
115 // if sizes differ then the arrangement is not a permutation
116 if (g.get_num_nodes() != arr.size()) { return false; }
117 // ensure that the input arrangement is a permutation
118 if (not is_permutation(arr)) { return false; }
119 // the largest number must be exactly one less than the size
120 const position max_pos =
121 *std::max_element(arr.begin_direct(), arr.end_direct());
122
123 return max_pos == arr.size() - 1;
124}
125
138[[nodiscard]] bool is_bipartite
139(
142 const linear_arrangement& arr = {}
143)
144noexcept;
157[[nodiscard]] bool is_bipartite
158(
159 const graphs::directed_graph& g,
161 const linear_arrangement& arr = {}
162)
163noexcept;
164
176[[nodiscard]] bool is_bipartite
177(const graphs::undirected_graph& g, const linear_arrangement& arr = {})
178noexcept;
190[[nodiscard]] bool is_bipartite
191(const graphs::directed_graph& g, const linear_arrangement& arr = {})
192noexcept;
193
204template <class graph_t>
205[[nodiscard]] bool is_planar(const graph_t& g, const linear_arrangement& arr = {})
206noexcept
207{
208#if defined DEBUG
209 assert(is_arrangement(g, arr));
210#endif
211
212 return is_num_crossings_lesseq_than(g, arr, 0) <= 0;
213}
214
229[[nodiscard]] bool is_root_covered
230(const graphs::rooted_tree& rt, const linear_arrangement& arr)
231noexcept;
232
247[[nodiscard]] inline bool is_projective
248(const graphs::rooted_tree& rt, const linear_arrangement& arr)
249noexcept
250{
251#if defined DEBUG
252 assert(rt.is_rooted_tree());
253#endif
254
255 // check for planarity
256 // this function already checks that an arrangement must be valid
257 if (not is_planar(rt, arr)) { return false; }
258 return not is_root_covered(rt, arr);
259}
260
261} // -- namespace linarr
262} // -- namespace lal
Directed graph class.
Definition directed_graph.hpp:67
Rooted tree graph class.
Definition rooted_tree.hpp:109
Undirected graph class.
Definition undirected_graph.hpp:66
Linear arrangement of vertices.
Definition linear_arrangement.hpp:103
A class to represent a coloring of the vertices of a bipartite graph.
Definition bipartite_graph_coloring.hpp:60
bool is_arrangement(const graph_t &g, const linear_arrangement &arr) noexcept
Is a given arrangement valid?
Definition formal_constraints.hpp:104
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:205
bool is_bipartite(const graphs::undirected_graph &g, const properties::bipartite_graph_coloring &c, const linear_arrangement &arr={}) noexcept
Is a given arrangement bipartite?
uint64_t is_num_crossings_lesseq_than(const graphs::directed_graph &G, const 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?
bool is_projective(const graphs::rooted_tree &rt, const linear_arrangement &arr) noexcept
Is a given arrangement projective?
Definition formal_constraints.hpp:248
Main namespace of the library.
Definition basic_types.hpp:48
uint64_t position
Node's position type.
Definition basic_types.hpp:53