LAL: Linear Arrangement Library 23.01.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
predict_C.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
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/identity_arrangement.hpp>
55#include <lal/detail/macros/basic_convert.hpp>
56
57namespace lal {
58namespace detail {
59
70inline constexpr
71uint64_t alpha(const int64_t n, const int64_t d1, const int64_t d2) noexcept {
72 int64_t f = 0;
73 // positions s1 < s2
74 if (1 <= n - (d1 + d2)) {
75 // sum(d1 - 1, i, 1, n - d2 - d1)
76 f += (d1 - 1)*(n - d2 - d1);
77 // sum(n - d2 - i, i, n - (d1 + d2) + 1, n - d2 - 1)
78 f += (d1*(d1 - 1))/2;
79 }
80 else {
81 // sum(n - i - d2, i, 1, n - d2 - 1)
82 f += ((d2 - n)*(d2 - n + 1))/2;
83 }
84
85 // positions s2 < s1
86 if (d1 + d2 <= n) {
87 f += (d1 - 1)*(n - d2 - d1);
88 }
89 if (1 + d2 - d1 >= 1) {
90 if (1 + d2 <= n - d1) {
91 f += (d1*(d1 - 1))/2;
92 }
93 else {
94 f += ((n - d2)*(n - d2 - 1))/2;
95 }
96 }
97
98#if defined DEBUG
99 assert(f >= 0);
100#endif
101 return detail::to_uint64(f);
102}
103
114inline constexpr
115uint64_t beta(const int64_t n, const int64_t d1, const int64_t d2) noexcept {
116 int64_t f = 0;
117
118 // positions s1 < s2
119 if (1 <= n - (d1 + d2)) {
120 // sum(n - i - d2 - 1, i, 1, n - d1 - d2)
121 f += (n - d2)*(n - d2) + 3*(d1 + d2 - n) - d1*d1;
122 // sum(n - i - d2, i, n - (d1 + d2) + 1, n - d2 - 1)
123 f += d1*(d1 - 1);
124 }
125 else {
126 // sum(n - i - d2, i, 1, n - d2 - 1)
127 f += (d2 - n)*(d2 - n + 1);
128 }
129
130 // positions s2 < s1
131 if (d1 < d2) {
132 if (1 + d2 <= n - d1) {
133 // sum(i - 3, i, 1 + d2, n - d1)
134 f += (n - d1)*(n - d1) - 5*(n - d1 - d2) - d2*d2;
135 }
136
137 if (d2 <= n - d1) {
138 // sum(i - 2, i, 1 + d2 - d1, d2)
139 f += d1*(2*d2 - d1 - 3);
140 }
141 else {
142 // sum(i - 2, i, 1 + d2 - d1, n - d1)
143 f += (d2 - n)*(2*d1 - d2 - n + 3);
144 }
145 }
146 else {
147 // these sums are the same as in the positive
148 // case above, but simplified assuming d1 = d2.
149
150 if (1 + 2*d1 <= n) {
151 f += n*(n - 3) + d1*(6 - 2*n);
152 }
153
154 if (2*d1 <= n) {
155 f += d1*(d1 - 1);
156 }
157 else {
158 f += (d1 - n)*(d1 - n + 1);
159 }
160 }
161
162#if defined DEBUG
163 assert(f >= 0);
164 assert(f%2 == 0);
165#endif
166 return detail::to_uint64(f/2);
167}
168
181template <typename result_t, class graph_t, class arrangement_t>
183 const graph_t& g,
184 const arrangement_t& arr
185)
186noexcept
187{
188 result_t Ec2(0);
189 const uint64_t n = g.get_num_nodes();
190 const int64_t nn = detail::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 = detail::to_int64(detail::abs_diff(arr[s], arr[t]));
201 const int64_t len_uv = detail::to_int64(detail::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 {
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:125
void next() noexcept
Moves the iterator to the next pair, if there is any.
Definition: Q_iterator.hpp:143
edge_pair_t get_edge_pair_t() const noexcept
Returns the current edge pair.
Definition: Q_iterator.hpp:131
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_C.hpp:71
constexpr double to_double(const T &t) noexcept
Conversion to double.
Definition: basic_convert.hpp:62
constexpr int64_t to_int64(const T &t) noexcept
Conversion to int64_t.
Definition: basic_convert.hpp:52
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_C.hpp:182
constexpr uint64_t to_uint64(const T &t) noexcept
Conversion to uint64_t.
Definition: basic_convert.hpp:57
constexpr T abs_diff(const T &t1, const T &t2) noexcept
Absolute difference of two values.
Definition: basic_convert.hpp:67
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_C.hpp:115
Main namespace of the library.
Definition: basic_types.hpp:50