LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
undirected_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#if defined DEBUG
46#include <cassert>
47#endif
48#include <vector>
49
50// lal includes
51#include <lal/basic_types.hpp>
52#include <lal/graphs/graph.hpp>
53
54namespace lal {
55namespace graphs {
56
66class undirected_graph : virtual public graph {
67public:
68 /* CONSTRUCTORS */
69
71 undirected_graph() noexcept : graph() { }
76 undirected_graph(const uint64_t n) noexcept {
77 init(n);
78 }
86
92 move_full_undirected_graph(std::forward<undirected_graph>(g));
93 }
94
96 virtual ~undirected_graph() noexcept { }
97
98 /* OPERATORS */
99
106 return *this;
107 }
113 move_full_undirected_graph(std::forward<undirected_graph>(g));
114 return *this;
115 }
116
117 /* MODIFIERS */
118
127 void reserve_degree(const node u, const uint64_t d) noexcept {
128#if defined DEBUG
129 assert(u < get_num_nodes());
130#endif
131 m_adjacency_list[u].reserve(d);
132 }
133
135 virtual undirected_graph& add_node() noexcept {
137 return *this;
138 }
139
154 (
155 const node u,
156 const bool norm = true,
157 const bool check_norm = true
158 )
159 noexcept;
160
178 (
179 const node s,
180 const node t,
181 const bool norm = true,
182 const bool check_norm = true
183 )
184 noexcept;
185
198 undirected_graph& add_edge_bulk(const node s, const node t) noexcept;
199
200 void finish_bulk_add(const bool norm = true, const bool check = true) noexcept;
201
219 (
220 const std::vector<edge>& edges,
221 const bool norm = true,
222 const bool check_norm = true
223 )
224 noexcept;
225
249 (
250 const std::vector<edge>& edges,
251 const bool norm = true,
252 const bool check_norm = true
253 )
254 noexcept;
255
273 (
274 const node s,
275 const node t,
276 const bool norm = true,
277 const bool check_norm = true
278 )
279 noexcept;
280
293 virtual undirected_graph& remove_edge_bulk(const node s, const node t) noexcept;
294
295 void finish_bulk_remove(const bool norm = true, const bool check = true) noexcept;
296
314 (
315 const std::vector<edge>& edges,
316 const bool norm = true,
317 const bool check_norm = true
318 )
319 noexcept;
320
339 (
340 const node u,
341 const bool norm = true,
342 const bool check_norm = true
343 )
344 noexcept;
345
358
359 /* SETTERS */
360
361 /* GETTERS */
362
363 [[nodiscard]] std::vector<edge_pair> get_Q() const noexcept;
364
365 [[nodiscard]] std::vector<edge> get_edges() const noexcept;
366
372 [[nodiscard]] const neighbourhood& get_neighbors(const node u) const noexcept {
373#if defined DEBUG
374 assert(has_node(u));
375#endif
376 return m_adjacency_list[u];
377 }
378
384 [[nodiscard]] uint64_t get_degree(const node u) const noexcept {
385#if defined DEBUG
386 assert(has_node(u));
387#endif
388 return m_adjacency_list[u].size();
389 }
390
392 [[nodiscard]] bool has_edge(const node u, const node v) const noexcept;
393
394 [[nodiscard]] bool is_directed() const noexcept { return false; }
395 [[nodiscard]] bool is_undirected() const noexcept { return true; }
396
398 [[nodiscard]] std::vector<undirected_graph> get_connected_components() const noexcept;
399
400protected:
409 virtual void _init(const uint64_t n) noexcept {
410 graph::_init(n);
411 }
413 virtual void _clear() noexcept {
415 }
416
417 virtual void actions_after_add_edge(const node u, const node v) noexcept;
418
419 virtual void actions_after_add_edges(const edge_list& e) noexcept;
420
421 virtual void actions_after_add_edges_bulk() noexcept;
422
423 virtual void actions_after_remove_edge(const node u, const node v) noexcept;
424
425 virtual void actions_after_remove_edges(const edge_list& e) noexcept;
426
427 virtual void actions_after_remove_edges_bulk() noexcept;
428
429 virtual void actions_before_remove_edges_incident_to(const node u) noexcept;
430
431 virtual void actions_after_remove_node(const node u) noexcept;
432
435 // copy parent class
437
438 // copy this class' members
439 }
442 // move-assign parent class
443 move_full_graph(std::forward<undirected_graph>(u));
444
445 // move-assign this class' members
446 }
447
448private:
457 (
458 const node u,
459 const node v,
460 neighbourhood& out_u,
461 neighbourhood& in_v
462 )
463 noexcept;
464};
465
466} // -- namespace graphs
467} // -- namespace lal
Abstract class for graphs.
Definition graph.hpp:68
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
uint64_t get_num_nodes() const noexcept
Returns the number of ndoes.
Definition graph.hpp:207
virtual void _clear() noexcept
Clears memory for the graph class.
Definition graph.hpp:261
graph() noexcept
Empty constructor.
Definition graph.hpp:73
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
void move_full_graph(graph &&g) noexcept
Moves all members of this class.
Definition graph.hpp:274
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
Undirected graph class.
Definition undirected_graph.hpp:66
virtual void actions_after_remove_edges(const edge_list &e) noexcept
Do some extra work after the removal of several edges.
virtual undirected_graph & remove_edges_incident_to(const node u, const bool norm=true, const bool check_norm=true) noexcept
Remove all edges incident to a given vertex.
void finish_bulk_add(const bool norm=true, const bool check=true) noexcept
Completes the inner structure of the graph after adding a bulk of edges.
virtual void actions_before_remove_edges_incident_to(const node u) noexcept
Do some work before all edges incident to a node is removed.
virtual void actions_after_add_edges_bulk() noexcept
Do some extra work after the addition of several edges in bulk.
virtual void _clear() noexcept
Clears the memory of undirected_graph and graph classes.
Definition undirected_graph.hpp:413
void copy_full_undirected_graph(const undirected_graph &u) noexcept
Copies all members of this class and the parent class.
Definition undirected_graph.hpp:434
void move_full_undirected_graph(undirected_graph &&u) noexcept
Moves all members of this class and the parent class.
Definition undirected_graph.hpp:441
undirected_graph & add_edge_bulk(const node s, const node t) noexcept
Adds an edge to the graph.
bool is_undirected() const noexcept
Returns whether this graph is undirected or not.
Definition undirected_graph.hpp:395
virtual void actions_after_remove_edges_bulk() noexcept
Do some extra work after the removal of several edges in bulk.
virtual undirected_graph & add_edge(const node s, const node t, const bool norm=true, const bool check_norm=true) noexcept
Adds an edge to the graph.
virtual void actions_after_remove_node(const node u) noexcept
Do some work before the removal of a vertex.
void remove_single_edge(const node u, const node v, neighbourhood &out_u, neighbourhood &in_v) noexcept
Removes a single edge.
virtual void actions_after_add_edges(const edge_list &e) noexcept
Do some extra work after the addition of several edges.
virtual void _init(const uint64_t n) noexcept
Initializes the memory in the graph hierarchy.
Definition undirected_graph.hpp:409
uint64_t get_degree(const node u) const noexcept
Returns the number of neighbors of u.
Definition undirected_graph.hpp:384
bool is_directed() const noexcept
Returns whether this graph is directed or not.
Definition undirected_graph.hpp:394
virtual undirected_graph & remove_edge_bulk(const node s, const node t) noexcept
Removes an edge from the graph.
std::vector< edge_pair > get_Q() const noexcept
Returns all independent pairs of edges of this graph.
bool has_edge(const node u, const node v) const noexcept
Returns true if the edge exists in the graph.
virtual undirected_graph & remove_edges(const std::vector< edge > &edges, const bool norm=true, const bool check_norm=true) noexcept
Remove an edge from this graph.
undirected_graph(const uint64_t n) noexcept
Constructor with number of nodes.
Definition undirected_graph.hpp:76
virtual undirected_graph & remove_edge(const node s, const node t, const bool norm=true, const bool check_norm=true) noexcept
Remove an edge from this graph.
virtual undirected_graph & add_node() noexcept
Adds a vertex to the graph.
Definition undirected_graph.hpp:135
virtual void actions_after_add_edge(const node u, const node v) noexcept
Do some extra work after the addition of an edge.
undirected_graph(const undirected_graph &g) noexcept
Copy constructor.
Definition undirected_graph.hpp:83
undirected_graph & disjoint_union(const undirected_graph &g) noexcept
Disjoint union of graphs.
virtual ~undirected_graph() noexcept
Destructor.
Definition undirected_graph.hpp:96
void reserve_degree(const node u, const uint64_t d) noexcept
Predicts that the degree of node u is d.
Definition undirected_graph.hpp:127
virtual void actions_after_remove_edge(const node u, const node v) noexcept
Do some extra work after the removal of an edge.
virtual undirected_graph & remove_node(const node u, const bool norm=true, const bool check_norm=true) noexcept
Remove a node from this graph.
const neighbourhood & get_neighbors(const node u) const noexcept
Returns the neighbourhood of node u.
Definition undirected_graph.hpp:372
std::vector< edge > get_edges() const noexcept
Returns all edges of this graph.
undirected_graph & operator=(const undirected_graph &g) noexcept
Copy assignment operator.
Definition undirected_graph.hpp:104
undirected_graph() noexcept
Empty constructor.
Definition undirected_graph.hpp:71
void finish_bulk_remove(const bool norm=true, const bool check=true) noexcept
Completes the inner structure of the graph after removing edges in bulk.
undirected_graph(undirected_graph &&g) noexcept
Move constructor.
Definition undirected_graph.hpp:91
std::vector< undirected_graph > get_connected_components() const noexcept
Returns all the connected components of this graph as individual graphs.
virtual undirected_graph & set_edges(const std::vector< edge > &edges, const bool norm=true, const bool check_norm=true) noexcept
Sets the edges to the graph.
virtual undirected_graph & add_edges(const std::vector< edge > &edges, const bool norm=true, const bool check_norm=true) noexcept
Adds a list of edges to the graph.
Main namespace of the library.
Definition basic_types.hpp:48
std::pair< edge, edge > edge_pair
Edge pair type.
Definition basic_types.hpp:62
std::pair< node, node > edge
See Edge page for further details.
Definition basic_types.hpp:56
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