LAL: Linear Arrangement Library 23.01.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 - 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#include <vector>
46
47// lal includes
48#include <lal/basic_types.hpp>
49
50namespace lal {
51namespace graphs {
52
69class graph {
70public:
71 /* CONSTRUCTORS */
72
74 graph() noexcept { }
80 graph(uint64_t n) noexcept {
81 _init(n);
82 }
83
88 graph(const graph& g) noexcept {
90 }
91
96 graph(graph&& g) noexcept {
97 move_full_graph(std::forward<graph>(g));
98 }
99
101 virtual ~graph() noexcept { }
102
103 /* OPERATORS */
104
109 graph& operator= (const graph& g) noexcept {
111 return *this;
112 }
113
118 graph& operator= (graph&& g) noexcept {
119 move_full_graph(std::forward<graph>(g));
120 return *this;
121 }
122
123 /* MODIFIERS */
124
131 virtual void init(uint64_t n) noexcept;
138 virtual void clear() noexcept;
139
150 virtual void normalise() noexcept;
151
159 virtual bool check_normalised() noexcept;
160
173 virtual void finish_bulk_add(bool norm = true, bool check = true) noexcept = 0;
174
175 /* SETTERS */
176
178 void set_normalised(bool v = true) noexcept { m_normalised = v; }
179
180 /* GETTERS */
181
189 virtual std::vector<edge_pair> get_Q() const noexcept = 0;
190
192 bool has_node(node u) const noexcept { return u < get_num_nodes(); }
193
195 virtual bool has_edge(node u, node v) const = 0;
196
198 uint64_t get_num_nodes() const noexcept
199 { return m_adjacency_list.size(); }
200
202 uint64_t get_num_edges() const noexcept { return m_num_edges; }
203
205 virtual std::vector<edge> get_edges() const noexcept = 0;
206
214 bool is_normalised() const noexcept { return m_normalised; }
215
217 virtual bool is_directed() const noexcept = 0;
219 virtual bool is_undirected() const noexcept = 0;
220
221protected:
225 uint64_t m_num_edges = 0;
238 bool m_normalised = true;
239
240protected:
242 virtual void _init(uint64_t n) noexcept {
243 m_num_edges = 0;
244 m_normalised = true;
245 m_adjacency_list = std::vector<neighbourhood>(n);
246 }
248 virtual void _clear() noexcept {
249 m_num_edges = 0;
250 m_normalised = true;
251 m_adjacency_list.clear();
252 }
253
255 void copy_full_graph(const graph& g) noexcept {
256 m_adjacency_list = g.m_adjacency_list;
257 m_num_edges = g.m_num_edges;
258 m_normalised = g.m_normalised;
259 }
261 void move_full_graph(graph&& g) noexcept {
262 m_adjacency_list = std::move(g.m_adjacency_list);
263 m_num_edges = std::move(g.m_num_edges);
264 m_normalised = std::move(g.m_normalised);
265
266 g.m_num_edges = 0;
267 g.m_normalised = false;
268 }
269
283 void __disjoint_union(const graph& g) noexcept;
284
289 virtual void actions_after_remove_node(node u) noexcept;
290
296
302 virtual void actions_after_add_edge(node u, node v) noexcept;
308 virtual void actions_after_remove_edge(node u, node v) noexcept;
309
311 void normalise_after_edge_addition(bool norm, bool check) noexcept;
313 void normalise_after_edge_removal(bool norm, bool check) noexcept;
314};
315
316} // -- namespace graphs
317} // -- namespace lal
Abstract class for graphs.
Definition: graph.hpp:69
uint64_t m_num_edges
Amount of edges of this graph.
Definition: graph.hpp:225
void copy_full_graph(const graph &g) noexcept
Copies all members of this class.
Definition: graph.hpp:255
bool has_node(node u) const noexcept
Returns true if node u is in this graph.
Definition: graph.hpp:192
bool is_normalised() const noexcept
Returns whether this graph is normalised or not.
Definition: graph.hpp:214
virtual ~graph() noexcept
Destructor.
Definition: graph.hpp:101
void __disjoint_union(const graph &g) noexcept
Disjoint union of graphs.
virtual void actions_after_remove_node(node u) noexcept
Do some work before an node is removed.
virtual void normalise() noexcept
Normalises the graph.
uint64_t get_num_edges() const noexcept
Returns the number of edges.
Definition: graph.hpp:202
graph(const graph &g) noexcept
Copy constructor.
Definition: graph.hpp:88
virtual bool has_edge(node u, node v) const =0
Returns true if the undirected edge (u, v) exists in the graph.
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:109
uint64_t get_num_nodes() const noexcept
Returns the number of ndoes.
Definition: graph.hpp:198
virtual void init(uint64_t n) noexcept
Allocates the necessary memory for this class.
virtual void _init(uint64_t n) noexcept
Initialises memory of graph class.
Definition: graph.hpp:242
virtual void _clear() noexcept
Clears memory for the graph class.
Definition: graph.hpp:248
virtual bool is_undirected() const noexcept=0
Returns whether this graph is undirected or not.
void normalise_after_edge_removal(bool norm, bool check) noexcept
Normalise the graph after one (or more) edges have been removed.
virtual void actions_after_add_edge(node u, node v) noexcept
Do some extra work after an edge has been added.
virtual void finish_bulk_add(bool norm=true, bool check=true) noexcept=0
Completes the inner structure of the graph after adding a bulk of edges.
graph() noexcept
Empty constructor.
Definition: graph.hpp:74
graph(graph &&g) noexcept
Move constructor.
Definition: graph.hpp:96
virtual std::vector< edge_pair > get_Q() const noexcept=0
Returns all independent pairs of edges of this graph.
void move_full_graph(graph &&g) noexcept
Moves all members of this class.
Definition: graph.hpp:261
graph(uint64_t n) noexcept
Constructor with number of nodes.
Definition: graph.hpp:80
std::vector< neighbourhood > m_adjacency_list
Data structure that implements the graph.
Definition: graph.hpp:223
void set_normalised(bool v=true) noexcept
Sets whether this graph is normalised or not.
Definition: graph.hpp:178
virtual std::vector< edge > get_edges() const noexcept=0
Returns all edges of this graph.
void normalise_after_edge_addition(bool norm, bool check) noexcept
Normalise the graph after one (or more) edges have been added.
virtual void actions_after_remove_edge(node u, node v) noexcept
Do some extra work after an edge has been removed.
virtual void actions_before_remove_edges_incident_to(node u) noexcept
Do some work before all edges incident to a node is removed.
virtual void clear() noexcept
Frees the memory occupied by this graph.
virtual bool check_normalised() noexcept
Checks if the graph is normalised.
bool m_normalised
Is this graph normalised?
Definition: graph.hpp:238
Main namespace of the library.
Definition: basic_types.hpp:50
uint64_t node
Node type. See Node / Vertex page for further details.
Definition: basic_types.hpp:53
std::vector< node > neighbourhood
List of nodes.
Definition: basic_types.hpp:62