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// lal includes
45#include <lal/iterators/E_iterator.hpp>
46#include <lal/graphs/rooted_tree.hpp>
47#include <lal/properties/bipartite_graph_coloring.hpp>
48#include <lal/detail/arrangement_wrapper.hpp>
49
50namespace lal {
51namespace detail {
52
68template <class arrangement_t>
69[[nodiscard]] bool is_root_covered
70(const graphs::rooted_tree& rt, const arrangement_t& arr)
71noexcept
72{
73#if defined DEBUG
74 assert(rt.is_rooted_tree());
75#endif
76
77 // the linear arrangement is given
78 const position pr = arr[node_t{rt.get_root()}];
79
80 iterators::E_iterator e_it(rt);
81 while (not e_it.end()) {
82 const auto [s,t] = e_it.yield_edge_t();
83 const position ps = arr[s];
84 const position pt = arr[t];
85
86 const bool r_covered_st = ps < pr and pr < pt;
87 const bool r_covered_ts = pt < pr and pr < ps;
88 if (r_covered_st or r_covered_ts) { return true; }
89 }
90 return false;
91}
92
109template <class arrangement_t>
110[[nodiscard]] bool is_projective
111(const graphs::rooted_tree& rt, const arrangement_t& arr)
112noexcept
113{
114#if defined DEBUG
115 assert(rt.is_rooted_tree());
116#endif
117
118 // check for planarity
119 // this function already checks that an arrangement must be valid
120 if (not is_planar(rt, arr)) { return false; }
121 return not is_root_covered(rt, arr);
122}
123
138template <class arrangement_t>
139[[nodiscard]] bool is_bipartite__connected
140(const properties::bipartite_graph_coloring& c, const arrangement_t& arr)
141noexcept
142{
143 const auto n = c.size();
144 int num_changes = 0;
145 position_t p = 1ull;
146 while (p < n and num_changes <= 1) {
147 const node u = arr[p - 1ull];
148 const auto color_u = c.get_color_of(u);
149 const node v = arr[p];
150 const auto color_v = c.get_color_of(v);
151 num_changes += color_v != color_u;
152 ++p;
153 }
154 return num_changes <= 1;
155}
156
170template <class graph_t, class arrangement_t>
171[[nodiscard]] bool is_bipartite(const graph_t& g, const arrangement_t& arr)
172noexcept
173{
175 static constexpr color_t blue = properties::bipartite_graph_coloring::blue;
176 static constexpr color_t red = properties::bipartite_graph_coloring::red;
177 static constexpr color_t invalid = properties::bipartite_graph_coloring::invalid_color;
178
179 const auto n = g.get_num_nodes();
180 array<color_t> color_per_vertex(n, invalid);
181
182 const auto color_a_vertex = [&](node u) {
183 color_per_vertex[u] = blue;
184 if constexpr (std::is_base_of_v<graphs::directed_graph, graph_t>) {
185 for (node v : g.get_out_neighbors(u)) { color_per_vertex[v] = red; }
186 for (node v : g.get_in_neighbors(u)) { color_per_vertex[v] = red; }
187 }
188 else {
189 for (node v : g.get_neighbors(u)) { color_per_vertex[v] = red; }
190 }
191 };
192
193 color_a_vertex( arr[position_t{0ull}] );
194
195 int num_changes = 0;
196 position_t p = 1ull;
197 while (p < n and num_changes <= 1) {
198 const node u = arr[p];
199 if (color_per_vertex[u] == invalid) {
200 color_a_vertex(u);
201 }
202 const node v = arr[p - 1ull];
203 num_changes += color_per_vertex[v] != color_per_vertex[u];
204 ++p;
205 }
206 return num_changes <= 1;
207}
208
209} // -- namespace detail
210} // -- namespace lal
Rooted tree graph class.
Definition rooted_tree.hpp:109
Iterator over the set of edges of a graph.
Definition E_iterator.hpp:97
edge_t yield_edge_t() noexcept
Returns the current edge and advances the iterator.
Definition E_iterator.hpp:133
bool end() const noexcept
Returns true if the end of the iteration was reached.
Definition E_iterator.hpp:117
A class to represent a coloring of the vertices of a bipartite graph.
Definition bipartite_graph_coloring.hpp:60
static constexpr color_t invalid_color
An invalid color, used to initialize colors to an invalid value.
Definition bipartite_graph_coloring.hpp:70
uint64_t color_t
A useful type for colors.
Definition bipartite_graph_coloring.hpp:68
static constexpr color_t red
A color, called red, of value 0.
Definition bipartite_graph_coloring.hpp:72
static constexpr color_t blue
A color, called blue, of value 1.
Definition bipartite_graph_coloring.hpp:74
bool is_root_covered(const graphs::rooted_tree &rt, const arrangement_t &arr) noexcept
Is the root of a rooted tree covered in a given arrangement?
Definition formal_constraints.hpp:70
bool is_bipartite(const graph_t &g, const arrangement_t &arr) noexcept
Is a given arrangement bipartite?
Definition formal_constraints.hpp:171
bool is_bipartite__connected(const properties::bipartite_graph_coloring &c, const arrangement_t &arr) noexcept
Is a given arrangement bipartite?
Definition formal_constraints.hpp:140
bool is_projective(const graphs::rooted_tree &rt, const arrangement_t &arr) noexcept
Is a given arrangement projective?
Definition formal_constraints.hpp:111
Main namespace of the library.
Definition basic_types.hpp:48
uint64_t position
Node's position type.
Definition basic_types.hpp:53
uint64_t node
Node type. See Node / Vertex page for further details.
Definition basic_types.hpp:51
Wrapper of a C array for automatic deallocation of memory.
Definition array.hpp:59
Typesafe node type.
Definition basic_types.hpp:70
Typesafe position type.
Definition basic_types.hpp:244