LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
bipartite_opt_utils.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 * Juan Luis Esteban (esteban@cs.upc.edu)
34 * LOGPROG: Logics and Programming Research Group
35 * Office 110, Omega building
36 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
37 * Webpage: https://www.cs.upc.edu/~esteban/
38 *
39 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
40 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
41 * CQL (Complexity and Quantitative Linguistics Lab)
42 * Office 220, Omega building
43 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
44 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
45 *
46 ********************************************************************/
47
48#pragma once
49
50// lal includes
51#include <lal/linear_arrangement.hpp>
52#include <lal/detail/array.hpp>
53#include <lal/iterators/E_iterator.hpp>
54#include <lal/detail/graphs/size_subtrees.hpp>
55#include <lal/detail/sorting/counting_sort.hpp>
56#include <lal/detail/properties/tree_centroid.hpp>
57
58namespace lal {
59namespace detail {
60
62namespace bipartite_opt_utils {
63
81template <
82 bool make_arrangement,
84 class graph_t,
85 class bipartite_coloring_t
86>
87[[nodiscard]] std::conditional_t<
88 make_arrangement,
89 std::pair<uint64_t, linear_arrangement>,
90 uint64_t
91>
93(const graph_t& g, const bipartite_coloring_t& c)
94noexcept
95{
96 static_assert(std::is_base_of_v<graphs::graph, graph_t>);
97
98 const auto n = g.get_num_nodes();
99
100 // annoying corner case
101 if (n == 1) {
102 if constexpr (make_arrangement) {
103 return {0, linear_arrangement::identity(n)};
104 }
105 else {
106 return 0;
107 }
108 }
109
110 array<node> vertices_color_1(n - 1);
111 std::size_t size_1 = 0;
112 array<node> vertices_color_2(n - 1);
113 std::size_t size_2 = 0;
114
115 {
116 const auto first_color = c[0];
117 for (node u = 0; u < n; ++u) {
118 if (c[u] == first_color) {
119 vertices_color_1[size_1++] = u;
120 }
121 else {
122 vertices_color_2[size_2++] = u;
123 }
124 }
125
126 const auto sort_nodes =
127 [&](array<node>& nodes, std::size_t s) {
129 <node, type>
130 (
131 nodes.begin(), nodes.begin() + s,
132 n - 1,
133 s,
134 [&](const node u) -> std::size_t {
135 // in directed graphs, this function returns the sum of the
136 // in-degree plus the out-degree of the vertex.
137 return g.get_degree(u);
138 }
139 );
140 };
141 sort_nodes(vertices_color_1, size_1);
142 sort_nodes(vertices_color_2, size_2);
143 }
144
145 uint64_t D = 0;
147
148 if constexpr (make_arrangement) {
149 arr.resize(n);
150 }
151
152 position p = 0;
153 for (std::size_t i = size_1 - 1; i > 0; --i) {
154 const node u = vertices_color_1[i];
155 if constexpr (make_arrangement) {
156 arr.assign(u, p);
157 ++p;
158 }
159 D += (n - p)*g.get_degree(u);
160 }
161
162 {
163 const node u = vertices_color_1[0];
164 if constexpr (make_arrangement) {
165 arr.assign(vertices_color_1[0], p);
166 ++p;
167 }
168 D += (n - p)*g.get_degree(u);
169 }
170
171 for (std::size_t i = 0; i < size_2; ++i) {
172 const node u = vertices_color_2[i];
173 if constexpr (make_arrangement) {
174 arr.assign(u, p);
175 ++p;
176 }
177 D -= (n - p)*g.get_degree(u);
178 }
179
180 if constexpr (make_arrangement) {
181 return {D, std::move(arr)};
182 }
183 else {
184 return D;
185 }
186}
187
188} // -- namespcae bipartite_opt_utils
189} // -- namespace detail
190} // -- namespace lal
Linear arrangement of vertices.
Definition linear_arrangement.hpp:103
void assign(const NODE u, const POSITION p) noexcept
Assigns a node u to position p.
Definition linear_arrangement.hpp:379
void resize(std::size_t n) noexcept
Changes the size of the arrangement.
Definition linear_arrangement.hpp:355
void identity() noexcept
Makes this arrangement an identity arrangement.
Definition linear_arrangement.hpp:507
std::conditional_t< make_arrangement, std::pair< uint64_t, linear_arrangement >, uint64_t > optimal_bipartite_arrangement_AEF(const graph_t &g, const bipartite_coloring_t &c) noexcept
Optimal bipartite arrangement.
Definition bipartite_opt_utils.hpp:93
sort_type
The different types of sorting patterns.
Definition sorting_types.hpp:49
void counting_sort(const value_iterator_t begin, const value_iterator_t end, const std::size_t largest_key_plus_1, const Callable &key, countingsort::memory< value_t > &mem) noexcept
Counting sort algorithm with reusable memory.
Definition counting_sort.hpp:152
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
T * begin() noexcept
Non-constant raw pointer to first element.
Definition array.hpp:300