LAL: Linear Arrangement Library 21.07.01
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 - 2021
7 *
8 * This file is part of Linear Arrangement Library. To see the full code
9 * visit the webpage:
10 * https://github.com/lluisalemanypuig/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/definitions.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(uint32_t n) noexcept {
79 init(n);
80 }
85 directed_graph(const directed_graph& g) noexcept : graph() {
87 }
88#ifndef SWIG
94 move_full_directed_graph(std::move(g));
95 }
96#endif
98 virtual ~directed_graph() noexcept { }
99
100 /* OPERATORS */
101
102#ifndef SWIG
109 return *this;
110 }
116 move_full_directed_graph(std::move(g));
117 return *this;
118 }
119#endif
120
121 /* MODIFIERS */
122
123 void normalise() noexcept;
124
125 bool check_normalised() noexcept;
126
146 (node s, node t, bool norm = true, bool check_norm = true) noexcept;
147
161
162 void finish_bulk_add(bool norm = true, bool check = true) noexcept;
163
185 (const std::vector<edge>& edges, bool norm = true, bool check_norm = true)
186 noexcept;
187
214 (const std::vector<edge>& edges, bool norm = true, bool check_norm = true)
215 noexcept;
216
235 (node s, node t, bool norm = false, bool check_norm = true) noexcept;
236
259 (const std::vector<edge>& edges, bool norm = true, bool check_norm = true)
260 noexcept;
261
282 (node u, bool norm = true, bool check_norm = true)
283 noexcept;
284
296 void disjoint_union(const directed_graph& g) noexcept;
297
298 /* SETTERS */
299
300 /* GETTERS */
301
302 std::vector<edge_pair> get_Q() const noexcept;
303
304 std::vector<edge> get_edges() const noexcept;
305
307 bool has_edge(node u, node v) const noexcept;
308
314 inline const neighbourhood& get_out_neighbours(node u) const noexcept {
315#if defined DEBUG
316 assert(has_node(u));
317#endif
318 return m_adjacency_list[u];
319 }
325 inline const neighbourhood& get_in_neighbours(node u) const noexcept {
326#if defined DEBUG
327 assert(has_node(u));
328#endif
329 return m_in_adjacency_list[u];
330 }
331
340 inline uint32_t get_degree(node u) const noexcept
341 { return get_out_degree(u) + get_in_degree(u); }
342
344 inline uint32_t get_out_degree(node u) const noexcept {
345#if defined DEBUG
346 assert(has_node(u));
347#endif
348 return static_cast<uint32_t>(m_adjacency_list[u].size());
349 }
351 inline uint32_t get_in_degree(node u) const noexcept {
352#if defined DEBUG
353 assert(has_node(u));
354#endif
355 return static_cast<uint32_t>(m_in_adjacency_list[u].size());
356 }
357
358 inline bool is_directed() const noexcept { return true; }
359 inline bool is_undirected() const noexcept { return false; }
360
375 (bool norm = true, bool check = true) const noexcept;
376
377protected:
380
381protected:
383 virtual void _init(uint32_t n) noexcept;
385 virtual void _clear() noexcept;
386
391
392private:
401 (node u, node v, neighbourhood& out_u, neighbourhood& in_v) noexcept;
402};
403
404} // -- namespace graphs
405} // -- 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:379
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.
std::vector< edge_pair > get_Q() const noexcept
Returns all independent pairs of edges of this graph.
uint32_t get_degree(node u) const noexcept
Returns the in-degree plus the out-degree of this vertex.
Definition directed_graph.hpp:340
void move_full_directed_graph(directed_graph &&d) noexcept
Moves all members of this class and the parent class.
const neighbourhood & get_out_neighbours(node u) const noexcept
Returns the out-neighbours of node u.
Definition directed_graph.hpp:314
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.
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:325
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:358
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:359
virtual void _clear() noexcept
Clears the memory of directed_graph and graph classes.
virtual directed_graph & remove_edge(node s, node t, bool norm=false, bool check_norm=true) noexcept
Remove an edge from this graph.
bool check_normalised() noexcept
Checks if the graph is normalised.
directed_graph(uint32_t n) noexcept
Constructor with number of nodes.
Definition directed_graph.hpp:78
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:107
undirected_graph to_undirected(bool norm=true, bool check=true) const noexcept
Converts this directed graph into an undirected graph.
uint32_t get_in_degree(node u) const noexcept
Returns the in-degree of a node.
Definition directed_graph.hpp:351
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.
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
virtual void _init(uint32_t n) noexcept
Initialises memory of directed_graph and graph classes.
void normalise() noexcept
Normalises the graph.
uint32_t get_out_degree(node u) const noexcept
Returns the out-degree of a node.
Definition directed_graph.hpp:344
std::vector< edge > get_edges() const noexcept
Returns all edges of this graph.
Abstract class for graphs.
Definition graph.hpp:69
bool has_node(node u) const noexcept
Returns true if node u is in this graph.
Definition graph.hpp:188
graph() noexcept
Empty constructor.
Definition graph.hpp:74
virtual void init(uint32_t n) noexcept
Allocates the necessary memory for this class.
std::vector< neighbourhood > m_adjacency_list
Data structure that implements the graph.
Definition graph.hpp:219
Undirected graph class.
Definition undirected_graph.hpp:67
Main namespace of the library.
Definition definitions.hpp:48
std::pair< edge, edge > edge_pair
Edge pair type.
Definition definitions.hpp:77
std::pair< node, node > edge
Edge type.
Definition definitions.hpp:75
uint32_t node
Node type.
Definition definitions.hpp:51
std::vector< node > neighbourhood
List of nodes.
Definition definitions.hpp:79