LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
connected_components.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#include <vector>
49
50// lal includes
51#include <lal/detail/array.hpp>
52#include <lal/basic_types.hpp>
53
54namespace lal {
55namespace properties {
56
64template <class graph_t>
66public:
68 typedef typename std::vector<graph_t>::const_iterator const_iterator;
70 typedef typename std::vector<graph_t>::iterator iterator;
71
72public:
73
75 [[nodiscard]] graph_t& operator[] (const std::size_t i) noexcept {
76#if defined DEBUG
77 assert(i < m_connected_components.size());
78#endif
79 return m_connected_components[i];
80 }
81
83 [[nodiscard]] const graph_t& operator[] (const std::size_t i) const noexcept {
84#if defined DEBUG
85 assert(i < m_connected_components.size());
86#endif
87 return m_connected_components[i];
88 }
89
90 /* MODIFIERS */
91
97 void init(const std::size_t n) noexcept {
98 m_node_to_cc.resize(n, n + 1);
100 }
101
103 void add_graph(graph_t&& g) noexcept {
104 const auto n = g.get_num_nodes();
105 m_connected_components.push_back(std::forward<graph_t>(g));
106 m__cc_node__to__graph_node.emplace_back(n, n + 1);
107 }
109 void add_graph(const graph_t& g) noexcept {
110 const auto n = g.get_num_nodes();
111 m_connected_components.push_back(g);
112 m__cc_node__to__graph_node.emplace_back(n, n + 1);
113 }
114
120 void set_node_cc(const node u, const std::size_t label) noexcept {
121 m_node_to_cc[u] = label;
122 }
123
129 void set_label_graph_node_to_cc_node(const node u, const std::size_t label) noexcept {
131 }
139 (
140 const std::size_t cc_idx,
141 const node u,
142 const std::size_t label
143 )
144 noexcept
145 {
146 m__cc_node__to__graph_node[cc_idx][u] = label;
147 }
148
149 /* GETTERS */
150
152 [[nodiscard]] std::size_t size() const noexcept {
153 return m_connected_components.size();
154 }
155
162 [[nodiscard]] std::size_t get_cc_node(const node u) const noexcept {
163 return m_node_to_cc[u];
164 }
165
171 [[nodiscard]] std::size_t get_label_graph_node_to_cc_node(const node u)
172 const noexcept
173 {
175 }
182 [[nodiscard]] std::size_t get_label_cc_node_to_graph_node
183 (const std::size_t cc_idx, const node u)
184 const noexcept
185 {
186 return m__cc_node__to__graph_node[cc_idx][u];
187 }
188
190 [[nodiscard]] const_iterator begin() const noexcept
191 { return m_connected_components.begin(); }
193 [[nodiscard]] iterator begin() noexcept
194 { return m_connected_components.begin(); }
195
197 [[nodiscard]] const_iterator end() const noexcept
198 { return m_connected_components.end(); }
200 [[nodiscard]] iterator end() noexcept
201 { return m_connected_components.end(); }
202
203private:
205 std::vector<graph_t> m_connected_components;
206
209 std::vector<detail::array<std::size_t>> m__cc_node__to__graph_node;
213
216};
217
218} // -- namespace properties
219} // -- namespace lal
The connected components of a graph.
Definition connected_components.hpp:65
void set_label_graph_node_to_cc_node(const node u, const std::size_t label) noexcept
Relates vertex u to the corresponding vertex within its connected component.
Definition connected_components.hpp:129
std::size_t get_cc_node(const node u) const noexcept
Returns the label of the connected component u belongs to.
Definition connected_components.hpp:162
const_iterator begin() const noexcept
A pointer to the beginning of the sequence of connected components.
Definition connected_components.hpp:190
const_iterator end() const noexcept
A pointer to the end of the sequence of connected components.
Definition connected_components.hpp:197
std::vector< graph_t >::iterator iterator
Useful typedef for non-constant iterators.
Definition connected_components.hpp:70
std::size_t get_label_cc_node_to_graph_node(const std::size_t cc_idx, const node u) const noexcept
The corresponding vertex within its connected component.
Definition connected_components.hpp:183
detail::array< std::size_t > m__graph_node__to__cc_node
Definition connected_components.hpp:212
void add_graph(graph_t &&g) noexcept
Add a graph to the list of connected components.
Definition connected_components.hpp:103
void init(const std::size_t n) noexcept
Initializes this object.
Definition connected_components.hpp:97
std::size_t size() const noexcept
Returns the number of connected components.
Definition connected_components.hpp:152
detail::array< std::size_t > m_node_to_cc
The label of the connected component a vertex belongs to.
Definition connected_components.hpp:215
void add_graph(const graph_t &g) noexcept
Add a graph to the list of connected components.
Definition connected_components.hpp:109
std::vector< graph_t >::const_iterator const_iterator
Useful typedef for constant iterators.
Definition connected_components.hpp:68
std::vector< graph_t > m_connected_components
The connected components of the graph.
Definition connected_components.hpp:205
iterator end() noexcept
A pointer to the end of the sequence of connected components.
Definition connected_components.hpp:200
std::vector< detail::array< std::size_t > > m__cc_node__to__graph_node
Definition connected_components.hpp:209
void set_label_cc_node_to_graph_node(const std::size_t cc_idx, const node u, const std::size_t label) noexcept
Relates vertex u to the corresponding vertex within its connected component.
Definition connected_components.hpp:139
graph_t & operator[](const std::size_t i) noexcept
Access operator.
Definition connected_components.hpp:75
iterator begin() noexcept
A pointer to the beginning of the sequence of connected components.
Definition connected_components.hpp:193
std::size_t get_label_graph_node_to_cc_node(const node u) const noexcept
The corresponding vertex within its connected component.
Definition connected_components.hpp:171
void set_node_cc(const node u, const std::size_t label) noexcept
Relates vertex u to the label of its connected component.
Definition connected_components.hpp:120
Main namespace of the library.
Definition basic_types.hpp:48
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
void resize(const std::size_t new_size) noexcept
Resize the array.
Definition array.hpp:187