A generic directed graph with a parametric node type.
More...
#include <graph.h>
+ Inheritance diagram for grapht< N >:
+ Collaboration diagram for grapht< N >:
template<typename... arguments>
Run depth-first search on the graph, starting from a single source node.
Run depth-first search on the graph, starting from multiple source nodes.
Removes any edges between nodes in a graph that are unreachable from a given start node.
Removes any edges between nodes in a graph that are unreachable from a vector of start nodes.
Run recursive depth-limited search on the graph, starting from multiple source nodes, to find the nodes reachable within n steps.
Run recursive depth-limited search on the graph, starting from multiple source nodes, to find the nodes reachable within n steps.
Ensure a graph is chordal (contains no 4+-cycles without an edge crossing the cycle) by adding extra edges.
Find connected subgraphs in an undirected graph.
Computes strongly-connected components of a graph and yields a vector expressing a mapping from nodes to components indices.
Find a topological order of the nodes if graph is DAG, return empty list for non-DAG or empty graph.
Protected Member Functions
Run recursive depth-limited search on the graph, starting from multiple source nodes, to find the nodes reachable within n steps.
Detailed Description
template<
class N = graph_nodet<empty_edget>>
class grapht< N >
A generic directed graph with a parametric node type.
The nodes of the graph are stored in the only field of the class, a std::vector named nodes. Nodes are instances of class graph_nodet<E> or a subclass of it. Graph edges contain a payload object of parametric type E (which has to be default-constructible). By default E is instantiated with an empty class (empty_edget).
Each node is identified by its offset inside the nodes vector. The incoming and outgoing edges of a node are stored as std::maps in the fields in and out of the graph_nodet<E>. These maps associate a node identifier (the offset) to the edge payload (of type E).
In fact, observe that two instances of E are stored per edge of the graph. For instance, assume that we want to create a graph with two nodes, A and B, and one edge from A to B, labelled by object e. We achieve this by inserting the pair (offsetof(B),e) in the map A.out and the pair (offsetof(A),e) in the map B.in.
Nodes are inserted in the graph using grapht::add_node(), which returns the identifier (offset) of the inserted node. Edges between nodes are created by grapht::add_edge(a,b), where a and b are the identifiers of nodes. Method add_edges adds a default-constructed payload object of type E. Adding a payload objet e to an edge seems to be only possibly by manually inserting e in the std::maps in and out of the two nodes associated to the edge.
Definition at line 166 of file graph.h.
Member Typedef Documentation
◆ edgest
template<
class N = graph_nodet<empty_edget>>
◆ edget
template<
class N = graph_nodet<empty_edget>>
◆ node_indext
template<
class N = graph_nodet<empty_edget>>
◆ nodest
template<
class N = graph_nodet<empty_edget>>
◆ nodet
template<
class N = graph_nodet<empty_edget>>
◆ patht
template<
class N = graph_nodet<empty_edget>>
Member Function Documentation
◆ add_edge()
template<
class N = graph_nodet<empty_edget>>
◆ add_node()
template<
class N = graph_nodet<empty_edget>>
template<typename... arguments>
◆ add_undirected_edge()
◆ clear()
template<
class N = graph_nodet<empty_edget>>
◆ connected_subgraphs()
Find connected subgraphs in an undirected graph.
- Parameters
-
[out] subgraph_nr will be resized to graph.size() and populated to map node indices onto subgraph numbers. The subgraph numbers are dense, in the range 0 - (number of subgraphs - 1)
- Returns
- Number of subgraphs found.
Definition at line 730 of file graph.h.
◆ depth_limited_search() [1/3]
std::vector<
typename N::node_indext >
grapht<
N >::depth_limited_search
(
std::vector<
typename N::node_indext > &
src,
std::size_t
limit
)
const
Run recursive depth-limited search on the graph, starting from multiple source nodes, to find the nodes reachable within n steps.
This function initialises the search.
- Parameters
-
src The nodes to start the search from.
limit limit on steps
- Returns
- a vector of reachable node indices
Definition at line 668 of file graph.h.
◆ depth_limited_search() [2/3]
std::vector<
typename N::node_indext >
grapht<
N >::depth_limited_search
(
std::vector<
typename N::node_indext > &
src,
std::size_t
limit,
std::vector<
bool > &
visited
)
const
protected
Run recursive depth-limited search on the graph, starting from multiple source nodes, to find the nodes reachable within n steps.
- Parameters
-
src The nodes to start the search from.
limit limit on steps
visited vector of booleans indicating whether a node has been visited
- Returns
- a vector of reachable node indices
Definition at line 691 of file graph.h.
◆ depth_limited_search() [3/3]
std::size_t
limit
)
const
Run recursive depth-limited search on the graph, starting from multiple source nodes, to find the nodes reachable within n steps.
This function initialises the search.
- Parameters
-
src The node to start the search from.
limit limit on steps
- Returns
- a vector of reachable node indices
Definition at line 653 of file graph.h.
◆ disconnect_unreachable() [1/2]
Removes any edges between nodes in a graph that are unreachable from a vector of start nodes.
- Parameters
-
src vector of indices of start nodes
Definition at line 537 of file graph.h.
◆ disconnect_unreachable() [2/2]
Removes any edges between nodes in a graph that are unreachable from a given start node.
Used for computing cone of influence, by disconnecting unreachable nodes and then performing backwards reachability. Note nodes are not actually removed from the vector nodes, because this requires renumbering node indices. This copies the way nodes are "removed" in make_chordal.
- Parameters
-
src start node
Definition at line 527 of file graph.h.
◆ edge()
template<
class N = graph_nodet<empty_edget>>
◆ empty()
template<
class N = graph_nodet<empty_edget>>
◆ for_each_predecessor()
◆ for_each_successor()
◆ get_predecessors()
◆ get_reachable() [1/2]
Run depth-first search on the graph, starting from multiple source nodes.
- Parameters
-
src The nodes to start the search from.
forwards true (false) if the forward (backward) reachability should be performed.
Definition at line 616 of file graph.h.
◆ get_reachable() [2/2]
Run depth-first search on the graph, starting from a single source node.
- Parameters
-
src The node to start the search from.
forwards true (false) if the forward (backward) reachability should be performed.
Definition at line 602 of file graph.h.
◆ get_successors()
◆ has_edge()
template<
class N = graph_nodet<empty_edget>>
◆ in()
template<
class N = graph_nodet<empty_edget>>
◆ is_dag()
template<
class N = graph_nodet<empty_edget>>
◆ make_chordal()
Ensure a graph is chordal (contains no 4+-cycles without an edge crossing the cycle) by adding extra edges.
Note this adds more edges than are required, including to acyclic graphs or acyclic subgraphs of cyclic graphs, but does at least ensure the graph is not chordal.
Definition at line 848 of file graph.h.
◆ operator[]() [1/2]
template<
class N = graph_nodet<empty_edget>>
◆ operator[]() [2/2]
template<
class N = graph_nodet<empty_edget>>
◆ out()
template<
class N = graph_nodet<empty_edget>>
◆ output_dot()
void grapht<
N >::output_dot
(
std::ostream &
out )
const
◆ remove_edge()
template<
class N = graph_nodet<empty_edget>>
◆ remove_edges()
template<
class N = graph_nodet<empty_edget>>
◆ remove_in_edges()
◆ remove_out_edges()
◆ remove_undirected_edge()
◆ resize()
template<
class N = graph_nodet<empty_edget>>
◆ SCCs()
Computes strongly-connected components of a graph and yields a vector expressing a mapping from nodes to components indices.
For example, if nodes 1 and 3 are in SCC 0, and nodes 0, 2 and 4 are in SCC 1, this will leave subgraph_nr holding { 1, 0, 1, 0, 1 }, and the function will return 2 (the number of distinct SCCs). Lower-numbered SCCs are closer to the leaves, so in the particular case of a DAG, sorting by SCC number gives a topological sort, and for a cyclic graph the SCCs are topologically sorted but arbitrarily ordered internally.
- Parameters
-
[in,out] subgraph_nr should be pre-allocated with enough storage for one entry per graph node. Will be populated with the SCC indices of each node.
- Returns
- the number of distinct SCCs.
Definition at line 832 of file graph.h.
◆ shortest_loop()
template<
class N = graph_nodet<empty_edget>>
◆ shortest_path() [1/2]
template<
class N = graph_nodet<empty_edget>>
◆ shortest_path() [2/2]
◆ size()
template<
class N = graph_nodet<empty_edget>>
std::size_t
grapht<
N >::size
(
)
const
inline
◆ swap()
template<
class N = graph_nodet<empty_edget>>
◆ tarjan()
◆ topsort()
Find a topological order of the nodes if graph is DAG, return empty list for non-DAG or empty graph.
Uses Kahn's algorithm running in O(n_edges+n_nodes).
Definition at line 879 of file graph.h.
◆ visit_reachable()
Member Data Documentation
◆ nodes
template<
class N = graph_nodet<empty_edget>>
The documentation for this class was generated from the following file:
- /home/runner/work/cbmc/cbmc/src/util/graph.h