LAL: Linear Arrangement Library 24.10.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 > &, const node)> | BFS_process_one |
Single node processing function. | |
typedef std::function< void(const BFS< graph_t > &, const node, const node, const bool)> | BFS_process_two |
Two nodes processing function. | |
typedef std::function< bool(const BFS< graph_t > &, const node)> | BFS_bool_one |
One node decision function. | |
typedef std::function< bool(const BFS< graph_t > &, const node, const node, const bool)> | 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. | |
void | start_at (const node source) noexcept |
Start traversal at a given node. | |
void | start_at (const std::vector< node > &sources) noexcept |
Start the traversal at every given node. | |
void | set_use_rev_edges (const bool use) noexcept |
Set whether the traversal can use reversed edges. | |
void | set_terminate_default () noexcept |
Set the default value of m_terminate. | |
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_process_current. | |
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_process_neighbour. | |
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_neighbors (const bool v) noexcept |
Should the algorithm call the neighbour processing function for already visited neighbors? | |
void | clear_visited () noexcept |
Sets all nodes to not visited. | |
void | clear_queue () noexcept |
Clear the memory allocated for this structure. | |
void | set_visited (const node u, const char vis) noexcept |
Set node u as visited or not. | |
bool | node_was_visited (const 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 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? | |
Protected Member Functions | |
void | deal_with_neighbour (const node s, const node t, const bool ltr) noexcept |
Deal with a neighbour of an input node. | |
void | process_neighbors (const node s) noexcept |
Process the neighbors of node s. | |
void | do_traversal () noexcept |
Traversal through the graph's vertices. | |
Protected Attributes | |
const graph_t & | m_G |
Constant reference to the graph. | |
queue_array< node > | m_queue |
The structure of the traversal. | |
array< char > | m_vis |
The set of visited nodes. | |
bool | m_process_visited_neighbors = false |
Should the traversal process previously-visitied neighbors? | |
bool | m_use_rev_edges = false |
In directed graphs, traverse edges in the reverse direction. | |
BFS_bool_one | m_terminate |
Early terminating function. | |
bool | m_is_terminate_set |
Is function m_terminate set? | |
BFS_process_one | m_process_current |
Node processing function. | |
bool | m_is_process_current_set |
Is function m_process_current set? | |
BFS_process_two | m_process_neighbour |
Node processing function. | |
bool | m_is_process_neighbour_set |
Is function m_process_neighbour set? | |
BFS_bool_two | m_add_node |
Node addition function. | |
bool | m_is_add_node_set |
Is function m_add_node set? | |
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_process_visited_neighbors has been set via method set_process_visited_neighbors.
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, Q, v, 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(v,w) 6. endif 7. 8. if w not visited before and node_add(v,w) then 9. push w into Q 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, initialized with the source, a queue. 2. Q = {source} 3. while Q is not empty do 4. v = Q.front 5. remove Q's front 6. proc_curr(v) 7. if terminate(v) then Finish traversal 8. else 9. Nv = out-neighbourhood of v 10. ProcessNeighbourhood(graph, Q, v, Nv) 11. If graph is directed and process reverse edges then 12. Nv = in-neighbourhood of v 13. ProcessNeighbourhood(graph, Q, 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-neighbors", but also to "in-neighbors".
|
inlinenoexcept |
Set the graph traversal to its default state.
This includes the node processing functions:
the termination function m_terminate, and the attributes:
|
inlinenoexcept |
Should the algorithm call the neighbour processing function for already visited neighbors?
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 traversal at a given node.
source | Node. |
|
inlinenoexcept |
Start the traversal at every given node.
sources | List of 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 |
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 |
Node processing function.
Processes the current node visited.
For more details on when this function is called see do_traversal.
|
protected |
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 |
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 neighbors following out-edges, reach neighbors following in-neighbors. If vertex s has out-neighbors {1,2,3} and in-neighbors {4,5}, this attribute controls whether vertices {4,5} should also be included in the traversal.