LAL: Linear Arrangement Library 23.01.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 - 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#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
68class directed_graph : virtual public graph {
69public:
70 /* CONSTRUCTORS */
71
73 directed_graph() noexcept : graph() { }
78 directed_graph(uint64_t n) noexcept {
79 init(n);
80 }
85 directed_graph(const directed_graph& g) noexcept : graph() {
87 }
88
94 move_full_directed_graph(std::forward<directed_graph>(g));
95 }
96
98 virtual ~directed_graph() noexcept { }
99
100 /* OPERATORS */
101
108 return *this;
109 }
115 move_full_directed_graph(std::forward<directed_graph>(g));
116 return *this;
117 }
118
119 /* MODIFIERS */
120
121 void normalise() noexcept;
122
123 bool check_normalised() noexcept;
124
139 (node u, bool norm = true, bool check_norm = true) noexcept;
140
160 (node s, node t, bool norm = true, bool check_norm = true) noexcept;
161
175
176 void finish_bulk_add(bool norm = true, bool check = true) noexcept;
177
199 (const std::vector<edge>& edges, bool norm = true, bool check_norm = true)
200 noexcept;
201
228 (const std::vector<edge>& edges, bool norm = true, bool check_norm = true)
229 noexcept;
230
249 (node s, node t, bool norm = true, bool check_norm = true) noexcept;
250
273 (const std::vector<edge>& edges, bool norm = true, bool check_norm = true)
274 noexcept;
275
296 (node u, bool norm = true, bool check_norm = true)
297 noexcept;
298
310 void disjoint_union(const directed_graph& g) noexcept;
311
312 /* SETTERS */
313
314 /* GETTERS */
315
316 std::vector<edge_pair> get_Q() const noexcept;
317
318 std::vector<edge> get_edges() const noexcept;
319
321 bool has_edge(node u, node v) const noexcept;
322
328 const neighbourhood& get_out_neighbours(node u) const noexcept {
329#if defined DEBUG
330 assert(has_node(u));
331#endif
332 return m_adjacency_list[u];
333 }
339 const neighbourhood& get_in_neighbours(node u) const noexcept {
340#if defined DEBUG
341 assert(has_node(u));
342#endif
343 return m_in_adjacency_list[u];
344 }
345
354 uint64_t get_degree(node u) const noexcept
355 { return get_out_degree(u) + get_in_degree(u); }
356
358 uint64_t get_out_degree(node u) const noexcept {
359#if defined DEBUG
360 assert(has_node(u));
361#endif
362 return m_adjacency_list[u].size();
363 }
365 uint64_t get_in_degree(node u) const noexcept {
366#if defined DEBUG
367 assert(has_node(u));
368#endif
369 return m_in_adjacency_list[u].size();
370 }
371
372 bool is_directed() const noexcept { return true; }
373 bool is_undirected() const noexcept { return false; }
374
389 (bool norm = true, bool check = true) const noexcept;
390
391protected:
394
395protected:
397 virtual void _init(uint64_t n) noexcept {
398 graph::_init(n);
399 m_in_adjacency_list = std::vector<neighbourhood>(n);
400 }
402 virtual void _clear() noexcept {
404 m_in_adjacency_list.clear();
405 }
406
408 void copy_full_directed_graph(const directed_graph& d) noexcept {
409 // copy parent class
411
412 // copy this class' members
413 m_in_adjacency_list = d.m_in_adjacency_list;
414 }
417 // move-assign parent class
418 move_full_graph(std::forward<directed_graph>(d));
419
420 // move-assign this class' members
421 m_in_adjacency_list = std::move(d.m_in_adjacency_list);
422 }
423
424private:
433 (node u, node v, neighbourhood& out_u, neighbourhood& in_v) noexcept;
434};
435
436} // -- namespace graphs
437} // -- namespace lal
Directed graph class.
Definition: directed_graph.hpp:68
std::vector< neighbourhood > m_in_adjacency_list
In-neighbours for every node.
Definition: directed_graph.hpp:393
virtual directed_graph & add_edges(const std::vector< edge > &edges, bool norm=true, bool check_norm=true) noexcept
Adds a list of directed edges to the graph.
directed_graph(uint64_t n) noexcept
Constructor with number of nodes.
Definition: directed_graph.hpp:78
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:416
const neighbourhood & get_out_neighbours(node u) const noexcept
Returns the out-neighbours of node u.
Definition: directed_graph.hpp:328
directed_graph() noexcept
Empty constructor.
Definition: directed_graph.hpp:73
void remove_single_edge(node u, node v, neighbourhood &out_u, neighbourhood &in_v) noexcept
Removes a single edge.
void copy_full_directed_graph(const directed_graph &d) noexcept
Copies all members of this class and the parent class.
Definition: directed_graph.hpp:408
virtual void _init(uint64_t n) noexcept
Initialises memory of directed_graph and graph classes.
Definition: directed_graph.hpp:397
directed_graph(const directed_graph &g) noexcept
Copy constructor.
Definition: directed_graph.hpp:85
const neighbourhood & get_in_neighbours(node u) const noexcept
Returns the in-neighbours of node u.
Definition: directed_graph.hpp:339
void disjoint_union(const directed_graph &g) noexcept
Disjoint union of graphs.
bool is_directed() const noexcept
Returns whether this graph is directed or not.
Definition: directed_graph.hpp:372
virtual directed_graph & remove_edges(const std::vector< edge > &edges, bool norm=true, bool check_norm=true) noexcept
Remove an edge from this graph.
bool has_edge(node u, node v) const noexcept
Returns true if the edge exists in the graph.
bool is_undirected() const noexcept
Returns whether this graph is undirected or not.
Definition: directed_graph.hpp:373
virtual directed_graph & remove_edge(node s, node t, bool norm=true, bool check_norm=true) noexcept
Remove an edge from this graph.
virtual directed_graph & remove_node(node u, bool norm=true, bool check_norm=true) noexcept
Remove a node from this graph.
virtual void _clear() noexcept
Clears the memory of directed_graph and graph classes.
Definition: directed_graph.hpp:402
bool check_normalised() noexcept
Checks if the graph is normalised.
directed_graph & add_edge_bulk(node s, node t) noexcept
Adds an edge to the graph.
directed_graph & operator=(const directed_graph &g) noexcept
Copy assignment operator.
Definition: directed_graph.hpp:106
undirected_graph to_undirected(bool norm=true, bool check=true) const noexcept
Converts this directed graph into an undirected graph.
uint64_t get_out_degree(node u) const noexcept
Returns the out-degree of a node.
Definition: directed_graph.hpp:358
virtual directed_graph & remove_edges_incident_to(node u, bool norm=true, bool check_norm=true) noexcept
Remove all edges incident to a given vertex.
virtual ~directed_graph() noexcept
Destructor.
Definition: directed_graph.hpp:98
virtual directed_graph & add_edge(node s, node t, bool norm=true, bool check_norm=true) noexcept
Adds a directed edge to the graph.
virtual directed_graph & set_edges(const std::vector< edge > &edges, bool norm=true, bool check_norm=true) noexcept
Sets the list of edges to the graph.
uint64_t get_degree(node u) const noexcept
Returns the in-degree plus the out-degree of this vertex.
Definition: directed_graph.hpp:354
void finish_bulk_add(bool norm=true, bool check=true) noexcept
Completes the inner structure of the graph after adding a bulk of edges.
directed_graph(directed_graph &&g) noexcept
Move constructor.
Definition: directed_graph.hpp:93
uint64_t get_in_degree(node u) const noexcept
Returns the in-degree of a node.
Definition: directed_graph.hpp:365
void normalise() noexcept
Normalises the graph.
std::vector< edge > get_edges() const noexcept
Returns all edges of this graph.
Abstract class for graphs.
Definition: graph.hpp:69
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
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
graph() noexcept
Empty constructor.
Definition: graph.hpp:74
void move_full_graph(graph &&g) noexcept
Moves all members of this class.
Definition: graph.hpp:261
std::vector< neighbourhood > m_adjacency_list
Data structure that implements the graph.
Definition: graph.hpp:223
Undirected graph class.
Definition: undirected_graph.hpp:67
Main namespace of the library.
Definition: basic_types.hpp:50
std::pair< edge, edge > edge_pair
Edge pair type.
Definition: basic_types.hpp:60
std::pair< node, node > edge
See Edge page for further details.
Definition: basic_types.hpp:58
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