LAL: Linear Arrangement Library 23.01.00
A library focused on algorithms on linear arrangements of graphs.
|
Abstract graph Breadth-First Search traversal. More...
#include <traversal.hpp>
Public Types | |
typedef std::function< void(const BFS< graph_t > &, node)> | BFS_process_one |
Single node processing function. | |
typedef std::function< void(const BFS< graph_t > &, node, node, bool)> | BFS_process_two |
Two nodes processing function. | |
typedef std::function< bool(const BFS< graph_t > &, node)> | BFS_bool_one |
One node decision function. | |
typedef std::function< bool(const BFS< graph_t > &, node, node)> | BFS_bool_two |
Two nodes decision function. | |
Public Member Functions | |
BFS (const graph_t &g) noexcept | |
Constructor. | |
~BFS () noexcept=default | |
Destructor. | |
void | reset () noexcept |
Set the graph traversal to its default state. More... | |
void | start_at (node source) noexcept |
Start traversal at a given node. More... | |
void | start_at (const std::vector< node > &sources) noexcept |
Start the traversal at every given node. More... | |
void | set_use_rev_edges (bool use) noexcept |
Set whether the traversal can use reversed edges. | |
void | set_terminate_default () noexcept |
Set the default value of m_term. | |
void | set_terminate (const BFS_bool_one &f) noexcept |
Set the function that controls the termination of the loop. | |
void | set_process_current_default () noexcept |
Set the default value of m_proc_cur. | |
void | set_process_current (const BFS_process_one &f) noexcept |
Set the function that controls the processing of the current node. | |
void | set_process_neighbour_default () noexcept |
Set the default value of m_proc_neigh. | |
void | set_process_neighbour (const BFS_process_two &f) noexcept |
Set the function that controls the processing of the current neighbour. | |
void | set_node_add_default () noexcept |
Set the default value of m_add_node. | |
void | set_node_add (const BFS_bool_two &f) noexcept |
Set the function that controls when a node is to be added to the queue. | |
void | set_process_visited_neighbours (bool v) noexcept |
Should the algorithm call the neighbour processing function for already visited neighbours? More... | |
void | clear_visited () noexcept |
Sets all nodes to not visited. More... | |
void | clear_queue () noexcept |
Clear the memory allocated for this structure. More... | |
void | set_visited (node u, char vis) noexcept |
Set node u as visited or not. More... | |
bool | node_was_visited (node u) const noexcept |
Returns whether or not node u has been visited. | |
bool | all_visited () const noexcept |
Have all nodes been visited? | |
const graph_t & | get_graph () const noexcept |
Returns a constant reference to the graph. | |
const data_array< char > & | get_visited () const noexcept |
Return visited nodes information. | |
Static Public Attributes | |
static constexpr bool | is_graph_directed |
Is the graph used to initiliaze the object directed? More... | |
Protected Member Functions | |
void | deal_with_neighbour (node s, node t, bool ltr) noexcept |
Deal with a neighbour of an input node. More... | |
void | process_neighbours (node s) noexcept |
Process the neighbours of node s. | |
void | do_traversal () noexcept |
Traversal through the graph's vertices. More... | |
Protected Attributes | |
const graph_t & | m_G |
Constant reference to the graph. | |
linear_queue< node > | m_queue |
The structure of the traversal. | |
data_array< char > | m_vis |
The set of visited nodes. | |
bool | m_proc_vis_neighs = false |
Should the traversal process previously-visitied neighbours? | |
bool | m_use_rev_edges = false |
In directed graphs, traverse edges in the reverse direction. More... | |
BFS_bool_one | m_term |
graph_traversal early terminating function. More... | |
BFS_process_one | m_proc_cur |
graph_traversal node processing function. More... | |
BFS_process_two | m_proc_neigh |
graph_traversal neighbour node processing function. More... | |
BFS_bool_two | m_add_node |
graph_traversal node addition function. More... | |
Abstract graph Breadth-First Search traversal.
The traversal can be controlled by setting custom control-flow functions:
This graph_traversal traversal can also use "reversed edges" when doing traversals on directed graphs. A back edge in a directed graph of a node u is a node that points to u, namely, the directed edge is of the form (v,u), for another node v of the graph. This can be set via the set_use_rev_edges method.
An example of usage is as follows:
graph_t | Type of graph. |
|
inlinenoexcept |
Clear the memory allocated for this structure.
When using this function, users might also want to call clear_visited.
|
inlinenoexcept |
Sets all nodes to not visited.
When using this function, users might also want to call clear_queue.
|
inlineprotectednoexcept |
Deal with a neighbour of an input node.
Processes the neighbour and pushes it into the queue.
The neighbour is processed if it has not been visited before. In case the node was visited in a previous iteration, it is processed only if m_proc_vis_neighs has been set via method set_process_visited_neighbours.
Node t is pushed into the queue only if it has not been visited before and the user function m_add_node allows it.
s | Input node. |
t | The neighbour of the input node. |
ltr | Left-to-right orientation of the edge \(\{s,t\}\). If ltr is true then the edge has orientation from s to t. If ltr is false, the edge has orientation from t to s. |
|
inlineprotectednoexcept |
Traversal through the graph's vertices.
The graph_traversal traversal is implemented as follows:
ProcessNeighbourhood(graph, u, Nv): 1. for each w in Nv do 2. if w has not been visited before, or it has been but 3. already-visited nodes have to be processed 4. then 5. proc_neigh(u,w) 6. endif 7. 8. if w not visited before and node_add(w) then 9. push w into X 10. mark w as visited in vis 11. endif 12. endfor
graph_traversal(graph, source): . // set of |V(graph)| bits set to false 1. vis = {false} . // structure of the traversal, initialised with the source, . // either a stack or a queue. 2. X = {source} 3. while X is not empty do 4. v = X.front or X.top 5. remove X's top 6. proc_curr(v) 7. if terminate(v) then Finish traversal 8. else 9. Nv = out-neighbourhood of v 10. ProcessNeighbourhood(graph, v, Nv) 11. If graph is directed and process reverse edges then 12. Nv = in-neighbourhood of v 13. ProcessNeighbourhood(graph, v, Nv) 14. endif 15. endif 16. endwhile
Note that lines (...) extend the neighbourhood of a node "Nv" depending of the type of graph. If the graph is directed and the user wants to process "reversed edges", then the neighbourhood is not limited to "out-neighbours", but also to "in-neighbours".
|
inlinenoexcept |
Set the graph traversal to its default state.
This includes the node processing functions:
the termination function m_term, and the attributes:
|
inlinenoexcept |
Should the algorithm call the neighbour processing function for already visited neighbours?
v | Either true or false. |
|
inlinenoexcept |
Set node u as visited or not.
u | Node to set as visitied. |
vis | A 0-1 value. |
|
inlinenoexcept |
Start the traversal at every given node.
sources | List of node. |
|
inlinenoexcept |
Start traversal at a given node.
source | Node. |
|
staticconstexpr |
Is the graph used to initiliaze the object directed?
A graph is directed if it is a base class of lal::graphs::directed_graph.
|
protected |
graph_traversal node addition function.
Determines whether a node s should be added to the queue or not.
For more details on when this function is called see do_traversal.
|
protected |
graph_traversal node processing function.
Processes the current node visited.
For more details on when this function is called see do_traversal.
|
protected |
graph_traversal neighbour node processing function.
Processes the next visited node. The direction of the nodes visited passed as parameters is given by the boolean parameter. If it is true then the edge is s -> t. If it is false then the edge is t -> s.
For more details on when this function is called see do_traversal.
|
protected |
graph_traversal early terminating function.
Returns true if the graph_traversal algorithm should terminate.
For more details on when this function is called see do_traversal.
|
protected |
In directed graphs, traverse edges in the reverse direction.
Besides reaching neighbours following out-edges, reach neighbours following in-neighbours. If vertex s has out-neighbours {1,2,3} and in-neighbours {4,5}, this attribute controls whether vertices {4,5} should also be included in the traversal.