LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
graph.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#include <vector>
46
47// lal includes
48#include <lal/basic_types.hpp>
49
50namespace lal {
51namespace graphs {
52
68class graph {
69public:
70 /* CONSTRUCTORS */
71
73 graph() noexcept { }
79 graph(const uint64_t n) noexcept {
80 _init(n);
81 }
82
87 graph(const graph& g) noexcept {
89 }
90
95 graph(graph&& g) noexcept {
96 move_full_graph(std::forward<graph>(g));
97 }
98
100 virtual ~graph() noexcept { }
101
102 /* OPERATORS */
103
108 graph& operator= (const graph& g) noexcept {
110 return *this;
111 }
112
117 graph& operator= (graph&& g) noexcept {
118 move_full_graph(std::forward<graph>(g));
119 return *this;
120 }
121
122 /* MODIFIERS */
123
130 virtual void init(const uint64_t n) noexcept;
137 virtual void clear() noexcept;
138
149 virtual void normalize() noexcept;
150
158 virtual bool check_normalized() noexcept;
159
169 virtual void finish_bulk_add(const bool norm = true, const bool check = true) noexcept = 0;
170
180 virtual void finish_bulk_remove(const bool norm = true, const bool check = true) noexcept = 0;
181
182 /* SETTERS */
183
185 void set_normalized(const bool v = true) noexcept {
186 m_is_normalized = v;
187 }
188
189 /* GETTERS */
190
198 [[nodiscard]] virtual std::vector<edge_pair> get_Q() const noexcept = 0;
199
201 [[nodiscard]] bool has_node(const node u) const noexcept { return u < get_num_nodes(); }
202
204 [[nodiscard]] virtual bool has_edge(const node u, const node v) const = 0;
205
207 [[nodiscard]] uint64_t get_num_nodes() const noexcept
208 { return m_adjacency_list.size(); }
209
211 [[nodiscard]] uint64_t get_num_edges() const noexcept { return m_num_edges; }
212
214 [[nodiscard]] virtual std::vector<edge> get_edges() const noexcept = 0;
215
223 [[nodiscard]] bool is_normalized() const noexcept { return m_is_normalized; }
224
226 [[nodiscard]] virtual bool is_directed() const noexcept = 0;
228 [[nodiscard]] virtual bool is_undirected() const noexcept = 0;
229
230protected:
234 uint64_t m_num_edges = 0;
247 bool m_is_normalized = true;
248
249protected:
255 virtual void _init(const uint64_t n) noexcept {
256 m_num_edges = 0;
257 m_is_normalized = true;
258 m_adjacency_list.resize(n);
259 }
261 virtual void _clear() noexcept {
262 m_num_edges = 0;
263 m_is_normalized = true;
264 m_adjacency_list.clear();
265 }
266
268 void copy_full_graph(const graph& g) noexcept {
269 m_adjacency_list = g.m_adjacency_list;
270 m_num_edges = g.m_num_edges;
271 m_is_normalized = g.m_is_normalized;
272 }
274 void move_full_graph(graph&& g) noexcept {
275 m_adjacency_list = std::move(g.m_adjacency_list);
276 m_num_edges = std::move(g.m_num_edges);
277 m_is_normalized = std::move(g.m_is_normalized);
278
279 g.m_num_edges = 0;
280 g.m_is_normalized = false;
281 }
282
284 void __add_node() noexcept {
285 m_adjacency_list.emplace_back();
286 }
287
301 void __disjoint_union(const graph& g) noexcept;
302
308 virtual void actions_after_add_edge(const node u, const node v) noexcept;
309
314 virtual void actions_after_add_edges(const edge_list& e) noexcept;
315
322 virtual void actions_after_add_edges_bulk() noexcept;
323
329 virtual void actions_after_remove_edge(const node u, const node v) noexcept;
330
335 virtual void actions_after_remove_edges(const edge_list& e) noexcept;
336
343 virtual void actions_after_remove_edges_bulk() noexcept;
344
349 virtual void actions_before_remove_edges_incident_to(const node u) noexcept;
350
355 virtual void actions_after_remove_node(const node u) noexcept;
356
358 void normalize_after_edge_addition(const bool norm, const bool check) noexcept;
360 void normalize_after_edge_removal(const bool norm, const bool check) noexcept;
361};
362
363} // -- namespace graphs
364} // -- namespace lal
Abstract class for graphs.
Definition graph.hpp:68
uint64_t m_num_edges
Amount of edges of this graph.
Definition graph.hpp:234
virtual bool check_normalized() noexcept
Checks if the graph is normalized.
virtual void init(const uint64_t n) noexcept
Allocates the necessary memory for this class.
void copy_full_graph(const graph &g) noexcept
Copies all members of this class.
Definition graph.hpp:268
virtual ~graph() noexcept
Destructor.
Definition graph.hpp:100
void __disjoint_union(const graph &g) noexcept
Disjoint union of graphs.
void normalize_after_edge_removal(const bool norm, const bool check) noexcept
Normalize the graph after one (or more) edges have been removed.
uint64_t get_num_edges() const noexcept
Returns the number of edges.
Definition graph.hpp:211
graph(const graph &g) noexcept
Copy constructor.
Definition graph.hpp:87
virtual void finish_bulk_remove(const bool norm=true, const bool check=true) noexcept=0
Completes the inner structure of the graph after removing edges in bulk.
bool m_is_normalized
Is this graph normalized?
Definition graph.hpp:247
virtual bool is_directed() const noexcept=0
Returns whether this graph is directed or not.
graph & operator=(const graph &g) noexcept
Copy assignment operator.
Definition graph.hpp:108
uint64_t get_num_nodes() const noexcept
Returns the number of ndoes.
Definition graph.hpp:207
void normalize_after_edge_addition(const bool norm, const bool check) noexcept
Normalize the graph after one (or more) edges have been added.
virtual void _clear() noexcept
Clears memory for the graph class.
Definition graph.hpp:261
virtual bool is_undirected() const noexcept=0
Returns whether this graph is undirected or not.
virtual void actions_after_add_edge(const node u, const node v) noexcept
Do some extra work after the addition of an edge.
graph() noexcept
Empty constructor.
Definition graph.hpp:73
virtual void actions_after_add_edges_bulk() noexcept
Do some extra work after the addition of several edges in bulk.
graph(graph &&g) noexcept
Move constructor.
Definition graph.hpp:95
virtual std::vector< edge_pair > get_Q() const noexcept=0
Returns all independent pairs of edges of this graph.
virtual void actions_after_remove_edges(const edge_list &e) noexcept
Do some extra work after the removal of several edges.
virtual void actions_after_remove_edge(const node u, const node v) noexcept
Do some extra work after the removal of an edge.
virtual void actions_before_remove_edges_incident_to(const node u) noexcept
Do some work before all edges incident to a node is removed.
bool has_node(const node u) const noexcept
Returns true if node u is in this graph.
Definition graph.hpp:201
virtual void _init(const uint64_t n) noexcept
Initializes memory of graph class.
Definition graph.hpp:255
bool is_normalized() const noexcept
Returns whether this graph is normalized or not.
Definition graph.hpp:223
void set_normalized(const bool v=true) noexcept
Sets whether this graph is normalized or not.
Definition graph.hpp:185
virtual void normalize() noexcept
Normalizes the graph.
virtual void actions_after_add_edges(const edge_list &e) noexcept
Do some extra work after the addition of several edges.
virtual bool has_edge(const node u, const node v) const =0
Returns true if the undirected edge (u, v) exists in the graph.
void move_full_graph(graph &&g) noexcept
Moves all members of this class.
Definition graph.hpp:274
virtual void actions_after_remove_node(const node u) noexcept
Do some work before the removal of a vertex.
std::vector< neighbourhood > m_adjacency_list
Data structure that implements the graph.
Definition graph.hpp:232
void __add_node() noexcept
Adds a node to the graph.
Definition graph.hpp:284
virtual std::vector< edge > get_edges() const noexcept=0
Returns all edges of this graph.
virtual void finish_bulk_add(const bool norm=true, const bool check=true) noexcept=0
Completes the inner structure of the graph after adding a bulk of edges.
virtual void actions_after_remove_edges_bulk() noexcept
Do some extra work after the removal of several edges in bulk.
graph(const uint64_t n) noexcept
Constructor with number of nodes.
Definition graph.hpp:79
virtual void clear() noexcept
Frees the memory occupied by this graph.
Main namespace of the library.
Definition basic_types.hpp:48
std::vector< edge > edge_list
See Edge list page for further details.
Definition basic_types.hpp:60
uint64_t node
Node type. See Node / Vertex page for further details.
Definition basic_types.hpp:51
std::vector< node > neighbourhood
List of nodes.
Definition basic_types.hpp:64