LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
directed_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#include <lal/graphs/undirected_graph.hpp>
54
55namespace lal {
56namespace graphs {
57
67class directed_graph : virtual public graph {
68public:
69 /* CONSTRUCTORS */
70
72 directed_graph() noexcept : graph() { }
77 directed_graph(const uint64_t n) noexcept {
78 init(n);
79 }
84 directed_graph(const directed_graph& g) noexcept : graph() {
86 }
87
93 move_full_directed_graph(std::forward<directed_graph>(g));
94 }
95
97 virtual ~directed_graph() noexcept { }
98
99 /* OPERATORS */
100
107 return *this;
108 }
114 move_full_directed_graph(std::forward<directed_graph>(g));
115 return *this;
116 }
117
118 /* MODIFIERS */
119
128 void reserve_in_degree(const node u, const uint64_t d) noexcept {
129#if defined DEBUG
130 assert(u < get_num_nodes());
131#endif
132 m_in_adjacency_list[u].reserve(d);
133 }
142 void reserve_out_degree(const node u, const uint64_t d) noexcept {
143#if defined DEBUG
144 assert(u < get_num_nodes());
145#endif
146 m_adjacency_list[u].reserve(d);
147 }
148
149 void normalize() noexcept;
150
151 [[nodiscard]] bool check_normalized() noexcept;
152
154 virtual directed_graph& add_node() noexcept {
156 m_in_adjacency_list.emplace_back();
157 return *this;
158 }
159
174 (
175 const node u,
176 const bool norm = true,
177 const bool check_norm = true
178 )
179 noexcept;
180
198 (
199 const node s,
200 const node t,
201 const bool norm = true,
202 const bool check_norm = true
203 )
204 noexcept;
205
218 directed_graph& add_edge_bulk(const node s, const node t) noexcept;
219
220 void finish_bulk_add(const bool norm = true, const bool check = true) noexcept;
221
239 (
240 const std::vector<edge>& edges,
241 const bool norm = true,
242 const bool check_norm = true
243 )
244 noexcept;
245
272 (
273 const std::vector<edge>& edges,
274 const bool norm = true,
275 const bool check_norm = true
276 )
277 noexcept;
278
291 virtual directed_graph& remove_edge_bulk(const node s, const node t) noexcept;
292
293 void finish_bulk_remove(const bool norm = true, const bool check = true) noexcept;
294
312 (
313 const node s,
314 const node t,
315 const bool norm = true,
316 const bool check_norm = true
317 )
318 noexcept;
319
337 (const std::vector<edge>& edges, const bool norm = true, const bool check_norm = true)
338 noexcept;
339
358 (const node u, const bool norm = true, const bool check_norm = true)
359 noexcept;
360
373
374 /* SETTERS */
375
376 /* GETTERS */
377
378 [[nodiscard]] std::vector<edge_pair> get_Q() const noexcept;
379
380 [[nodiscard]] std::vector<edge> get_edges() const noexcept;
381
383 [[nodiscard]] bool has_edge(const node u, const node v) const noexcept;
384
390 [[nodiscard]] const neighbourhood& get_out_neighbors(const node u) const noexcept {
391#if defined DEBUG
392 assert(has_node(u));
393#endif
394 return m_adjacency_list[u];
395 }
401 [[nodiscard]] const neighbourhood& get_in_neighbors(const node u) const noexcept {
402#if defined DEBUG
403 assert(has_node(u));
404#endif
405 return m_in_adjacency_list[u];
406 }
407
416 [[nodiscard]] uint64_t get_degree(const node u) const noexcept
417 { return get_out_degree(u) + get_in_degree(u); }
418
420 [[nodiscard]] uint64_t get_out_degree(const node u) const noexcept {
421#if defined DEBUG
422 assert(has_node(u));
423#endif
424 return m_adjacency_list[u].size();
425 }
427 [[nodiscard]] uint64_t get_in_degree(const node u) const noexcept {
428#if defined DEBUG
429 assert(has_node(u));
430#endif
431 return m_in_adjacency_list[u].size();
432 }
433
434 [[nodiscard]] bool is_directed() const noexcept { return true; }
435 [[nodiscard]] bool is_undirected() const noexcept { return false; }
436
451 (const bool norm = true, const bool check = true)
452 const noexcept;
453
455 [[nodiscard]] std::vector<directed_graph> get_connected_components() const noexcept;
456
457protected:
460
461protected:
470 virtual void _init(const uint64_t n) noexcept {
471 graph::_init(n);
472 m_in_adjacency_list.resize(n);
473 }
475 virtual void _clear() noexcept {
477 m_in_adjacency_list.clear();
478 }
479
480 virtual void actions_after_add_edge(const node u, const node v) noexcept;
481
482 virtual void actions_after_add_edges(const edge_list& e) noexcept;
483
484 virtual void actions_after_add_edges_bulk() noexcept;
485
486 virtual void actions_after_remove_edge(const node u, const node v) noexcept;
487
488 virtual void actions_after_remove_edges(const edge_list& e) noexcept;
489
490 virtual void actions_after_remove_edges_bulk() noexcept;
491
492 virtual void actions_before_remove_edges_incident_to(const node u) noexcept;
493
494 virtual void actions_after_remove_node(const node u) noexcept;
495
497 void copy_full_directed_graph(const directed_graph& d) noexcept {
498 // copy parent class
500
501 // copy this class' members
502 m_in_adjacency_list = d.m_in_adjacency_list;
503 }
506 // move-assign parent class
507 move_full_graph(std::forward<directed_graph>(d));
508
509 // move-assign this class' members
510 m_in_adjacency_list = std::move(d.m_in_adjacency_list);
511 }
512
513private:
522 (
523 const node u,
524 const node v,
525 neighbourhood& out_u,
526 neighbourhood& in_v
527 )
528 noexcept;
529};
530
531} // -- namespace graphs
532} // -- namespace lal
Directed graph class.
Definition directed_graph.hpp:67
std::vector< neighbourhood > m_in_adjacency_list
In-neighbors for every node.
Definition directed_graph.hpp:459
std::vector< edge_pair > get_Q() const noexcept
Returns all independent pairs of edges of this graph.
void move_full_directed_graph(directed_graph &&d) noexcept
Moves all members of this class and the parent class.
Definition directed_graph.hpp:505
directed_graph() noexcept
Empty constructor.
Definition directed_graph.hpp:72
virtual void actions_after_add_edges(const edge_list &e) noexcept
Do some extra work after the addition of several edges.
virtual directed_graph & add_edges(const std::vector< edge > &edges, const bool norm=true, const bool check_norm=true) noexcept
Adds a list of directed edges to the graph.
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.
bool check_normalized() noexcept
Checks if the graph is normalized.
uint64_t get_out_degree(const node u) const noexcept
Returns the out-degree of a node.
Definition directed_graph.hpp:420
directed_graph & add_edge_bulk(const node s, const node t) noexcept
Adds an edge to the graph.
void copy_full_directed_graph(const directed_graph &d) noexcept
Copies all members of this class and the parent class.
Definition directed_graph.hpp:497
virtual void actions_after_remove_edge(const node u, const node v) noexcept
Do some extra work after the removal of an edge.
virtual directed_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.
directed_graph(const directed_graph &g) noexcept
Copy constructor.
Definition directed_graph.hpp:84
uint64_t get_degree(const node u) const noexcept
Returns the in-degree plus the out-degree of this vertex.
Definition directed_graph.hpp:416
virtual void actions_after_add_edges_bulk() noexcept
Do some extra work after the addition of several edges in bulk.
void reserve_in_degree(const node u, const uint64_t d) noexcept
Predicts that the in-degree of node u is d.
Definition directed_graph.hpp:128
virtual void _init(const uint64_t n) noexcept
Initializes the memory in the graph hierarchy.
Definition directed_graph.hpp:470
bool is_directed() const noexcept
Returns whether this graph is directed or not.
Definition directed_graph.hpp:434
void remove_single_edge(const node u, const node v, neighbourhood &out_u, neighbourhood &in_v) noexcept
Removes a single edge.
void normalize() noexcept
Normalizes the graph.
bool is_undirected() const noexcept
Returns whether this graph is undirected or not.
Definition directed_graph.hpp:435
virtual void _clear() noexcept
Clears the memory of directed_graph and graph classes.
Definition directed_graph.hpp:475
virtual directed_graph & set_edges(const std::vector< edge > &edges, const bool norm=true, const bool check_norm=true) noexcept
Sets the list of edges to the graph.
virtual directed_graph & remove_edge_bulk(const node s, const node t) noexcept
Removes an edge from the graph.
const neighbourhood & get_in_neighbors(const node u) const noexcept
Returns the in-neighbors of node u.
Definition directed_graph.hpp:401
virtual void actions_after_remove_node(const node u) noexcept
Do some work before the removal of a vertex.
virtual directed_graph & remove_edges(const std::vector< edge > &edges, const bool norm=true, const bool check_norm=true) noexcept
Remove an edge from this graph.
virtual directed_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.
directed_graph & operator=(const directed_graph &g) noexcept
Copy assignment operator.
Definition directed_graph.hpp:105
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.
virtual void actions_after_remove_edges_bulk() noexcept
Do some extra work after the removal of several edges in bulk.
const neighbourhood & get_out_neighbors(const node u) const noexcept
Returns the out-neighbors of node u.
Definition directed_graph.hpp:390
virtual directed_graph & add_node() noexcept
Adds a node to the graph.
Definition directed_graph.hpp:154
virtual ~directed_graph() noexcept
Destructor.
Definition directed_graph.hpp:97
uint64_t get_in_degree(const node u) const noexcept
Returns the in-degree of a node.
Definition directed_graph.hpp:427
virtual directed_graph & remove_node(const node u, const bool norm=true, const bool check_norm=true) noexcept
Remove a node from this graph.
bool has_edge(const node u, const node v) const noexcept
Returns true if the edge exists in the graph.
virtual void actions_after_add_edge(const node u, const node v) noexcept
Do some extra work after the addition of an edge.
directed_graph & disjoint_union(const directed_graph &g) noexcept
Disjoint union of graphs.
directed_graph(const uint64_t n) noexcept
Constructor with number of nodes.
Definition directed_graph.hpp:77
directed_graph(directed_graph &&g) noexcept
Move constructor.
Definition directed_graph.hpp:92
virtual directed_graph & add_edge(const node s, const node t, const bool norm=true, const bool check_norm=true) noexcept
Adds a directed edge to the graph.
void reserve_out_degree(const node u, const uint64_t d) noexcept
Predicts that the out-degree of node u is d.
Definition directed_graph.hpp:142
undirected_graph to_undirected(const bool norm=true, const bool check=true) const noexcept
Converts this directed graph into an undirected graph.
std::vector< directed_graph > get_connected_components() const noexcept
Returns all the connected components of this graph as individual graphs.
virtual void actions_after_remove_edges(const edge_list &e) noexcept
Do some extra work after the removal of several edges.
std::vector< edge > get_edges() const noexcept
Returns all edges of this graph.
virtual void actions_before_remove_edges_incident_to(const node u) noexcept
Do some work before all edges incident to a node is removed.
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
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