LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
predict.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/directed_graph.hpp>
51#include <lal/graphs/undirected_graph.hpp>
52#include <lal/numeric/rational.hpp>
53#include <lal/iterators/Q_iterator.hpp>
54#include <lal/detail/arrangement_wrapper.hpp>
55#include <lal/detail/macros/basic_convert.hpp>
56
57namespace lal {
58namespace detail {
59
70[[nodiscard]]
71inline constexpr
72uint64_t alpha(const int64_t n, const int64_t d1, const int64_t d2) noexcept {
73 int64_t f = 0;
74 // positions s1 < s2
75 if (1 <= n - (d1 + d2)) {
76 // sum(d1 - 1, i, 1, n - d2 - d1)
77 f += (d1 - 1)*(n - d2 - d1);
78 // sum(n - d2 - i, i, n - (d1 + d2) + 1, n - d2 - 1)
79 f += (d1*(d1 - 1))/2;
80 }
81 else {
82 // sum(n - i - d2, i, 1, n - d2 - 1)
83 f += ((d2 - n)*(d2 - n + 1))/2;
84 }
85
86 // positions s2 < s1
87 if (d1 + d2 <= n) {
88 f += (d1 - 1)*(n - d2 - d1);
89 }
90 if (1 + d2 - d1 >= 1) {
91 if (1 + d2 <= n - d1) {
92 f += (d1*(d1 - 1))/2;
93 }
94 else {
95 f += ((n - d2)*(n - d2 - 1))/2;
96 }
97 }
98
99#if defined DEBUG
100 assert(f >= 0);
101#endif
102 return to_uint64(f);
103}
104
115[[nodiscard]]
116inline constexpr
117uint64_t beta(const int64_t n, const int64_t d1, const int64_t d2) noexcept {
118 int64_t f = 0;
119
120 // positions s1 < s2
121 if (1 <= n - (d1 + d2)) {
122 // sum(n - i - d2 - 1, i, 1, n - d1 - d2)
123 f += (n - d2)*(n - d2) + 3*(d1 + d2 - n) - d1*d1;
124 // sum(n - i - d2, i, n - (d1 + d2) + 1, n - d2 - 1)
125 f += d1*(d1 - 1);
126 }
127 else {
128 // sum(n - i - d2, i, 1, n - d2 - 1)
129 f += (d2 - n)*(d2 - n + 1);
130 }
131
132 // positions s2 < s1
133 if (d1 < d2) {
134 if (1 + d2 <= n - d1) {
135 // sum(i - 3, i, 1 + d2, n - d1)
136 f += (n - d1)*(n - d1) - 5*(n - d1 - d2) - d2*d2;
137 }
138
139 if (d2 <= n - d1) {
140 // sum(i - 2, i, 1 + d2 - d1, d2)
141 f += d1*(2*d2 - d1 - 3);
142 }
143 else {
144 // sum(i - 2, i, 1 + d2 - d1, n - d1)
145 f += (d2 - n)*(2*d1 - d2 - n + 3);
146 }
147 }
148 else {
149 // these sums are the same as in the positive
150 // case above, but simplified assuming d1 = d2.
151
152 if (1 + 2*d1 <= n) {
153 f += n*(n - 3) + d1*(6 - 2*n);
154 }
155
156 if (2*d1 <= n) {
157 f += d1*(d1 - 1);
158 }
159 else {
160 f += (d1 - n)*(d1 - n + 1);
161 }
162 }
163
164#if defined DEBUG
165 assert(f >= 0);
166 assert(f%2 == 0);
167#endif
168 return to_uint64(f/2);
169}
170
183template <typename result_t, class graph_t, class arrangement_t>
184[[nodiscard]] inline result_t predict_C_using_edge_lengths
185(const graph_t& g, const arrangement_t& arr)
186noexcept
187{
188 result_t Ec2(0);
189 const uint64_t n = g.get_num_nodes();
190 const int64_t nn = to_int64(n);
191
193 while (not q.end()) {
194 const auto [st, uv] = q.get_edge_pair_t();
195 q.next();
196
197 const auto [s, t] = st;
198 const auto [u, v] = uv;
199
200 const int64_t len_st = to_int64(abs_diff(arr[s], arr[t]));
201 const int64_t len_uv = to_int64(abs_diff(arr[u], arr[v]));
202
203 const auto [al, be] =
204 (len_st <= len_uv ?
205 std::make_pair(alpha(nn, len_st, len_uv), beta(nn, len_st, len_uv))
206 :
207 std::make_pair(alpha(nn, len_uv, len_st), beta(nn, len_uv, len_st))
208 );
209
210 if constexpr (std::is_same_v<result_t, numeric::rational>) {
211 Ec2 += numeric::rational(al, be);
212 }
213 else {
214 Ec2 += to_double(al)/to_double(be);
215 }
216 }
217
218 return Ec2;
219}
220
221} // -- namespace detail
222} // -- namespace lal
Iterator over the set of pairs of independent edges of a graph.
Definition Q_iterator.hpp:107
bool end() const noexcept
Returns true if the end of the iteration was reached.
Definition Q_iterator.hpp:128
void next() noexcept
Moves the iterator to the next pair, if there is any.
Definition Q_iterator.hpp:146
edge_pair_t get_edge_pair_t() const noexcept
Returns the current edge pair.
Definition Q_iterator.hpp:134
Exact rational number.
Definition rational.hpp:63
constexpr uint64_t alpha(const int64_t n, const int64_t d1, const int64_t d2) noexcept
Amount of crossings pairs of edges of given lengths.
Definition predict.hpp:72
constexpr double to_double(const T &t) noexcept
Conversion to double.
Definition basic_convert.hpp:72
constexpr int64_t to_int64(const T &t) noexcept
Conversion to int64_t.
Definition basic_convert.hpp:57
result_t predict_C_using_edge_lengths(const graph_t &g, const arrangement_t &arr) noexcept
Predicted number of crossings based on the sum of edge lengths.
Definition predict.hpp:185
constexpr uint64_t to_uint64(const T &t) noexcept
Conversion to uint64_t.
Definition basic_convert.hpp:52
constexpr T abs_diff(const T &t1, const T &t2) noexcept
Absolute difference of two values.
Definition basic_convert.hpp:77
constexpr uint64_t beta(const int64_t n, const int64_t d1, const int64_t d2) noexcept
Amount of pairs of edges of given lengths.
Definition predict.hpp:117
Main namespace of the library.
Definition basic_types.hpp:48