LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
necessary_conditions.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/detail/linarr/level_signature.hpp>
46
47namespace lal {
48namespace detail {
49
68template <class graph_t, level_signature_type t>
69[[nodiscard]] inline bool is_level_signature_nonincreasing
70(
71 const graph_t& g,
72 const level_signature<t>& levels,
73 const linear_arrangement& arr
74)
75noexcept
76{
77 const auto n = g.get_num_nodes();
78 if constexpr (t == level_signature_type::per_position) {
79 for (position_t p = 0ull; p < n - 1ull; ++p) {
80 if (levels[p] < levels[p + 1ull]) {
81 return false;
82 }
83 }
84 }
85 else {
86 for (position_t p = 0ull; p < n - 1ull; ++p) {
87 const node_t u = (arr.size() == 0 ? *p : arr[p]);
88 const node_t v = (arr.size() == 0 ? *p + 1 : arr[p + 1ull]);
89 if (levels[u] < levels[v]) {
90 return false;
91 }
92 }
93 }
94 return true;
95}
96
113template <class graph_t, level_signature_type t>
115(
116 const graph_t& g,
117 const level_signature<t>& levels,
118 const linear_arrangement& arr
119)
120noexcept
121{
122 if constexpr (t == level_signature_type::per_position) {
123 for (iterators::E_iterator it(g); not it.end(); it.next()) {
124 const auto [u, v] = it.yield_edge_t();
125 const position_t pu = (arr.size() == 0 ? *u : arr[u]);
126 const position_t pv = (arr.size() == 0 ? *v : arr[v]);
127 if (levels[pu] == levels[pv]) {
128 return false;
129 }
130 }
131 }
132 else {
133 for (iterators::E_iterator it(g); not it.end(); it.next()) {
134 const auto [u, v] = it.yield_edge_t();
135 if (levels[u] == levels[v]) {
136 return false;
137 }
138 }
139 }
140 return true;
141}
142
160template <class graph_t, level_signature_type t>
161[[nodiscard]] inline bool no_vertex_in_antenna_is_thistle
162(
163 const graph_t& g,
164 const std::vector<properties::branchless_path>& bps,
165 const level_signature<t>& levels,
166 const linear_arrangement& arr
167)
168noexcept
169{
170
171 for (const properties::branchless_path& bp : bps) {
172 if (not bp.is_antenna(g)) { continue; }
173
174 const auto& seq = bp.get_vertex_sequence();
175 for (std::size_t i = 1; i < seq.size() - 1; ++i) {
176 const node_t u = seq[i];
177#if defined DEBUG
178 assert(g.get_degree(*u) == 2);
179#endif
180
181 if (is_thistle_vertex(g, levels, u, arr)) { return false; }
182 }
183 }
184 return true;
185}
186
204template <class graph_t, level_signature_type t>
205[[nodiscard]] inline bool at_most_one_thistle_in_bridges
206(
207 const graph_t& g,
208 const std::vector<properties::branchless_path>& bps,
209 const level_signature<t>& levels,
210 const linear_arrangement& arr
211)
212noexcept
213{
214
215 for (const properties::branchless_path& bp : bps) {
216 if (bp.is_antenna(g)) { continue; }
217
218 uint64_t num_thistles = 0;
219 const auto& seq = bp.get_vertex_sequence();
220 for (std::size_t i = 1; i < seq.size() - 1; ++i) {
221 const node_t u = seq[i];
222#if defined DEBUG
223 assert(g.get_degree(*u) == 2);
224#endif
225
226 num_thistles +=
227 (num_thistles == 2 ? 0ull : is_thistle_vertex(g, levels, u, arr));
228 }
229 if (num_thistles > 1) {
230 return false;
231 }
232 }
233 return true;
234}
235
236} // -- namespace detail
237} // -- namespace lal
A class that implements level signatures of an array.
Definition level_signature.hpp:90
Iterator over the set of edges of a graph.
Definition E_iterator.hpp:97
bool end() const noexcept
Returns true if the end of the iteration was reached.
Definition E_iterator.hpp:117
Linear arrangement of vertices.
Definition linear_arrangement.hpp:103
Branchless paths in trees.
Definition branchless_path.hpp:73
bool is_level_signature_nonincreasing(const graph_t &g, const level_signature< t > &levels, const linear_arrangement &arr) noexcept
Returns true if the level sequence follows that of a maximum arrangement.
Definition necessary_conditions.hpp:70
bool no_two_adjacent_vertices_have_same_level(const graph_t &g, const level_signature< t > &levels, const linear_arrangement &arr) noexcept
Returns true if no two adjacent vertices (in the graph) have the same level value.
Definition necessary_conditions.hpp:115
bool is_thistle_vertex(const graph_t &g, const level_signature< t > &levels, const node_t u, const linear_arrangement &arr={}) noexcept
Returns whether or not the input vertex is a thistle vertex.
Definition level_signature.hpp:279
@ per_position
Given per position.
bool at_most_one_thistle_in_bridges(const graph_t &g, const std::vector< properties::branchless_path > &bps, const level_signature< t > &levels, const linear_arrangement &arr) noexcept
Returns true if none of the vertices in the antennas of the graph is a thistle.
Definition necessary_conditions.hpp:206
bool no_vertex_in_antenna_is_thistle(const graph_t &g, const std::vector< properties::branchless_path > &bps, const level_signature< t > &levels, const linear_arrangement &arr) noexcept
Returns true if none of the vertices in the antennas of the graph is a thistle.
Definition necessary_conditions.hpp:162
Main namespace of the library.
Definition basic_types.hpp:48
Typesafe node type.
Definition basic_types.hpp:70
Typesafe position type.
Definition basic_types.hpp:244