12 #ifndef CPROVER_UTIL_GRAPH_H 13 #define CPROVER_UTIL_GRAPH_H 34 template<
class E=empty_edget>
41 typedef std::map<node_indext, edget>
edgest;
47 in.insert(std::pair<node_indext, edget>(n,
edget()));
52 out.insert(std::pair<node_indext, edget>(n,
edget()));
125 while(it_a!=a.end() && it_b!=b.end())
166 template<
class N=graph_nodet<empty_edget> >
194 return nodes[i].out.find(j)!=
nodes[i].out.end();
219 return nodes.empty();
240 nodes[a].erase_out(b);
241 nodes[b].erase_in(a);
246 return nodes[a].out[b];
265 typedef std::list<node_indext>
patht;
286 std::vector<node_indext>
287 get_reachable(
const std::vector<node_indext> &src,
bool forwards)
const;
292 std::vector<typename N::node_indext>
296 std::vector<typename N::node_indext> &src,
297 std::size_t limit)
const;
303 std::vector<node_indext> &subgraph_nr);
306 std::size_t
SCCs(std::vector<node_indext> &subgraph_nr)
const;
313 std::list<node_indext>
topsort()
const;
323 std::vector<typename N::node_indext> &src,
325 std::vector<bool> &visited)
const;
339 tarjant(std::size_t n, std::vector<node_indext> &_subgraph_nr):
357 bool non_trivial)
const;
387 nodet &node=nodes[n];
390 for(
typename edgest::const_iterator
402 nodet &node=nodes[n];
405 for(
typename edgest::const_iterator
419 bool non_trivial)
const 421 std::vector<bool> visited;
422 std::vector<unsigned> distance;
423 std::vector<unsigned> previous;
426 visited.resize(nodes.size(),
false);
427 distance.resize(nodes.size(), (unsigned)(-1));
428 previous.resize(nodes.size(), 0);
438 std::vector<node_indext> frontier_set, new_frontier_set;
440 frontier_set.reserve(nodes.size());
442 frontier_set.push_back(src);
447 while(!frontier_set.empty() && !found)
451 new_frontier_set.clear();
452 new_frontier_set.reserve(nodes.size());
454 for(
typename std::vector<node_indext>::const_iterator
455 f_it=frontier_set.begin();
456 f_it!=frontier_set.end() && !found;
460 const nodet &n=nodes[i];
463 for(
typename edgest::const_iterator
465 o_it!=n.
out.end() && !found;
479 new_frontier_set.push_back(o);
484 frontier_set.swap(new_frontier_set);
492 if(distance[dest]==(
unsigned)(-1))
497 path.push_front(dest);
498 if(distance[dest]==0 ||
499 previous[dest]==src)
break;
501 dest != previous[dest],
"loops cannot be part of the shortest path");
509 std::vector<node_indext> reachable =
get_reachable(src,
true);
510 for(
const auto index : reachable)
511 nodes[index].visited =
true;
525 const std::vector<node_indext> source_nodes(1, src);
526 disconnect_unreachable(source_nodes);
535 std::vector<node_indext> reachable =
get_reachable(src,
true);
536 std::sort(reachable.begin(), reachable.end());
537 std::size_t reachable_idx = 0;
538 for(std::size_t i = 0; i < nodes.size(); i++)
546 reachable_idx >= reachable.size() || i <= reachable[reachable_idx],
547 "node index i is smaller or equal to the node index at " 548 "reachable[reachable_idx], when reachable_idx is within bounds");
550 if(reachable_idx >= reachable.size())
552 else if(i == reachable[reachable_idx])
568 template <
class Container,
typename nodet =
typename Container::value_type>
571 const std::function<
void(
572 const typename Container::value_type &,
573 const std::function<
void(
const typename Container::value_type &)> &)>
576 std::vector<nodet>
stack;
577 for(
const auto &elt :
set)
578 stack.push_back(elt);
580 while(!
stack.empty())
582 auto n =
stack.back();
584 for_each_successor(n, [&](
const nodet &node) {
585 if(
set.insert(node).second)
586 stack.push_back(node);
597 std::vector<typename N::node_indext>
600 std::vector<node_indext> src_vector;
601 src_vector.push_back(src);
613 const std::vector<node_indext> &src,
616 std::vector<node_indext> result;
617 std::vector<bool> visited(size(),
false);
619 std::stack<node_indext, std::vector<node_indext>> s(src);
632 const auto &node = nodes[n];
633 const auto &succs = forwards ? node.out : node.in;
634 for(
const auto succ : succs)
635 if(!visited[succ.first])
650 const typename N::node_indext src,
651 std::size_t limit)
const 653 std::vector<node_indext> start_vector(1, src);
654 return depth_limited_search(start_vector, limit);
665 std::vector<typename N::node_indext> &src,
666 std::size_t limit)
const 668 std::vector<bool> visited(nodes.size(),
false);
670 for(
const auto &node : src)
673 visited[node] =
true;
676 return depth_limited_search(src, limit, visited);
687 std::vector<typename N::node_indext> &src,
689 std::vector<bool> &visited)
const 694 std::vector<node_indext> next_ring;
696 for(
const auto &n : src)
698 for(
const auto &o : nodes[n].out)
700 if(!visited[o.first])
702 next_ring.push_back(o.first);
703 visited[o.first] =
true;
708 if(next_ring.empty())
713 for(
const auto &succ : depth_limited_search(next_ring, limit, visited))
726 std::vector<node_indext> &subgraph_nr)
728 std::vector<bool> visited;
730 visited.resize(nodes.size(),
false);
731 subgraph_nr.resize(nodes.size(), 0);
742 std::stack<node_indext> s;
753 const nodet &node=nodes[n];
755 for(
const auto &o : node.
out)
757 if(!visited[o.first])
773 t.depth[v]=t.max_dfs;
774 t.lowlink[v]=t.max_dfs;
778 const nodet &node=nodes[v];
779 for(
typename edgest::const_iterator
788 t.lowlink[v]=std::min(t.lowlink[v], t.lowlink[vp]);
790 else if(t.in_scc[vp])
791 t.lowlink[v]=std::min(t.lowlink[v], t.depth[vp]);
795 if(t.lowlink[v]==t.depth[v])
800 !t.scc_stack.empty(),
801 "stack of strongly connected components should have another component");
805 t.subgraph_nr[vp]=t.scc_count;
829 tarjant t(nodes.size(), subgraph_nr);
853 const nodet &n=tmp[i];
856 for(
const auto &o1 : n.
out)
857 for(
const auto &o2 : n.
out)
859 if(o1.first!=o2.first)
862 this->add_undirected_edge(o1.first, o2.first);
877 std::list<node_indext> nodelist;
879 std::queue<node_indext> indeg0_nodes;
881 std::vector<size_t> in_deg(nodes.size(), 0);
886 in_deg[idx]=in(idx).size();
888 indeg0_nodes.push(idx);
891 while(!indeg0_nodes.empty())
895 nodelist.push_back(source);
897 for(
const auto &edge : out(source))
900 INVARIANT(in_deg[target]!=0,
"in-degree of node cannot be zero here");
905 if(in_deg[target]==0)
906 indeg0_nodes.push(target);
912 if(nodelist.size()!=nodes.size())
917 template <
typename node_index_type>
920 const std::function<
void(std::function<
void(
const node_index_type &)>)>
923 void(
const node_index_type &, std::function<
void(
const node_index_type &)>)>
925 const std::function<std::string(
const node_index_type &)> node_to_string,
926 const std::function<std::string(
const node_index_type &)> node_to_pretty)
928 for_each_node([&](
const node_index_type &i) {
929 out << node_to_pretty(i) <<
";\n";
930 for_each_succ(i, [&](
const node_index_type &n) {
931 out << node_to_string(i) <<
" -> " << node_to_string(n) <<
'\n';
937 std::vector<typename grapht<N>::node_indext>
940 std::vector<node_indext> result;
942 nodes[n].out.begin(),
944 std::back_inserter(result),
945 [&](
const std::pair<node_indext, edget> &edge) {
return edge.first; });
955 nodes[n].out.begin(),
957 [&](
const std::pair<node_indext, edget> &edge) { f(edge.first); });
963 const auto for_each_node =
964 [
this](
const std::function<void(const node_indext &)> &f) {
969 const auto for_each_succ = [&](
970 const node_indext &i,
const std::function<void(const node_indext &)> &f) {
971 for_each_successor(i, f);
975 const auto to_pretty_string = [
this](
const node_indext &i) {
976 return nodes[i].pretty(i);
978 output_dot_generic<node_indext>(
979 out, for_each_node, for_each_succ,
to_string, to_pretty_string);
982 #endif // CPROVER_UTIL_GRAPH_H void remove_in_edges(node_indext n)
A generic directed graph with a parametric node type.
std::string pretty(const node_indext &idx) const
std::vector< unsigned > lowlink
std::size_t SCCs(std::vector< node_indext > &subgraph_nr) const
Computes strongly-connected components of a graph and yields a vector expressing a mapping from nodes...
void add_undirected_edge(node_indext a, node_indext b)
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
A node type with an extra bit.
std::size_t connected_subgraphs(std::vector< node_indext > &subgraph_nr)
Find connected subgraphs in an undirected graph.
void visit_reachable(node_indext src)
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
void shortest_path(node_indext src, node_indext dest, patht &path) const
bool has_edge(node_indext i, node_indext j) const
void remove_edges(node_indext n)
void erase_in(node_indext n)
std::vector< bool > visited
const edgest & out(node_indext n) const
std::list< path_nodet > patht
#define PRECONDITION(CONDITION)
std::vector< bool > in_scc
std::list< node_indext > patht
std::list< node_indext > topsort() const
Find a topological order of the nodes if graph is DAG, return empty list for non-DAG or empty graph...
nodet::node_indext node_indext
void remove_undirected_edge(node_indext a, node_indext b)
std::vector< node_indext > get_successors(const node_indext &n) const
void add_in(node_indext n)
edget & edge(node_indext a, node_indext b)
void output_dot(std::ostream &out) const
std::map< node_indext, edget > edgest
void tarjan(class tarjant &t, node_indext v) const
void make_chordal()
Ensure a graph is chordal (contains no 4+-cycles without an edge crossing the cycle) by adding extra ...
void erase_out(node_indext n)
void remove_edge(node_indext a, node_indext b)
const nodet & operator[](node_indext n) const
std::vector< node_indext > & subgraph_nr
std::vector< typename N::node_indext > depth_limited_search(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.
void for_each_successor(const node_indext &n, std::function< void(const node_indext &)> f) const
void remove_out_edges(node_indext n)
std::stack< node_indext > scc_stack
void shortest_loop(node_indext node, patht &path) const
void add_edge(node_indext a, node_indext b)
std::vector< unsigned > depth
void add_out(node_indext n)
const edgest & in(node_indext n) const
void intersection(const typename graph_nodet< E >::edgest &a, const typename graph_nodet< E >::edgest &b, typename graph_nodet< E >::edgest &dest)
Compute intersection of two edge sets, in linear time.
std::vector< nodet > nodest
tarjant(std::size_t n, std::vector< node_indext > &_subgraph_nr)
void resize(node_indext s)
virtual std::string dot_attributes(const node_indext &) const
Node with attributes suitable for Graphviz DOT format.
nodet & operator[](node_indext n)
void output_dot_generic(std::ostream &out, const std::function< void(std::function< void(const node_index_type &)>)> &for_each_node, const std::function< void(const node_index_type &, std::function< void(const node_index_type &)>)> &for_each_succ, const std::function< std::string(const node_index_type &)> node_to_string, const std::function< std::string(const node_index_type &)> node_to_pretty)
graph_nodet< E >::edgest edgest
This class represents a node in a directed graph.
graph_nodet< E >::edget edget
std::vector< node_indext > get_reachable(node_indext src, bool forwards) const
Run depth-first search on the graph, starting from a single source node.
void get_reachable(Container &set, const std::function< void(const typename Container::value_type &, const std::function< void(const typename Container::value_type &)> &)> &for_each_successor)
Add to set, nodes that are reachable from set.
void disconnect_unreachable(node_indext src)
Removes any edges between nodes in a graph that are unreachable from a given start node...