12 #ifndef CPROVER_UTIL_GRAPH_H 13 #define CPROVER_UTIL_GRAPH_H 33 template<
class E=empty_edget>
40 typedef std::map<node_indext, edget>
edgest;
46 in.insert(std::pair<node_indext, edget>(n,
edget()));
51 out.insert(std::pair<node_indext, edget>(n,
edget()));
91 while(it_a!=a.end() && it_b!=b.end())
132 template<
class N=graph_nodet<empty_edget> >
160 return nodes[i].out.find(j)!=
nodes[i].out.end();
185 return nodes.empty();
206 nodes[a].erase_out(b);
207 nodes[b].erase_in(a);
212 return nodes[a].out[b];
231 typedef std::list<node_indext>
patht;
252 std::vector<node_indext>
253 get_reachable(
const std::vector<node_indext> &src,
bool forwards)
const;
258 std::vector<typename N::node_indext>
262 std::vector<typename N::node_indext> &src,
263 std::size_t limit)
const;
269 std::vector<node_indext> &subgraph_nr);
272 std::size_t
SCCs(std::vector<node_indext> &subgraph_nr)
const;
279 std::list<node_indext>
topsort()
const;
289 std::vector<typename N::node_indext> &src,
291 std::vector<bool> &visited)
const;
305 tarjant(std::size_t n, std::vector<node_indext> &_subgraph_nr):
323 bool non_trivial)
const;
329 assert(a<nodes.size());
330 assert(b<nodes.size());
353 nodet &node=nodes[n];
356 for(
typename edgest::const_iterator
368 nodet &node=nodes[n];
371 for(
typename edgest::const_iterator
385 bool non_trivial)
const 387 std::vector<bool> visited;
388 std::vector<unsigned> distance;
389 std::vector<unsigned> previous;
392 visited.resize(nodes.size(),
false);
393 distance.resize(nodes.size(), (unsigned)(-1));
394 previous.resize(nodes.size(), 0);
404 std::vector<node_indext> frontier_set, new_frontier_set;
406 frontier_set.reserve(nodes.size());
408 frontier_set.push_back(src);
413 while(!frontier_set.empty() && !found)
417 new_frontier_set.clear();
418 new_frontier_set.reserve(nodes.size());
420 for(
typename std::vector<node_indext>::const_iterator
421 f_it=frontier_set.begin();
422 f_it!=frontier_set.end() && !found;
426 const nodet &n=nodes[i];
429 for(
typename edgest::const_iterator
431 o_it!=n.
out.end() && !found;
445 new_frontier_set.push_back(o);
450 frontier_set.swap(new_frontier_set);
458 if(distance[dest]==(
unsigned)(-1))
463 path.push_front(dest);
464 if(distance[dest]==0 ||
465 previous[dest]==src)
break;
466 assert(dest!=previous[dest]);
474 std::vector<node_indext> reachable =
get_reachable(src,
true);
475 for(
const auto index : reachable)
476 nodes[index].visited =
true;
490 const std::vector<node_indext> source_nodes(1, src);
491 disconnect_unreachable(source_nodes);
500 std::vector<node_indext> reachable =
get_reachable(src,
true);
501 std::sort(reachable.begin(), reachable.end());
502 std::size_t reachable_idx = 0;
503 for(std::size_t i = 0; i < nodes.size(); i++)
505 if(reachable_idx >= reachable.size())
507 else if(i == reachable[reachable_idx])
509 else if(i > reachable[reachable_idx])
510 throw "error disconnecting unreachable nodes";
525 template <
class Container,
typename nodet =
typename Container::value_type>
528 const std::function<
void(
529 const typename Container::value_type &,
530 const std::function<
void(
const typename Container::value_type &)> &)>
533 std::vector<nodet>
stack;
534 for(
const auto &elt :
set)
535 stack.push_back(elt);
537 while(!
stack.empty())
539 auto n =
stack.back();
541 for_each_successor(n, [&](
const nodet &node) {
542 if(
set.insert(node).second)
543 stack.push_back(node);
554 std::vector<typename N::node_indext>
557 std::vector<node_indext> src_vector;
558 src_vector.push_back(src);
570 const std::vector<node_indext> &src,
573 std::vector<node_indext> result;
574 std::vector<bool> visited(size(),
false);
576 std::stack<node_indext, std::vector<node_indext>> s(src);
589 const auto &node = nodes[n];
590 const auto &succs = forwards ? node.out : node.in;
591 for(
const auto succ : succs)
592 if(!visited[succ.first])
607 const typename N::node_indext src,
608 std::size_t limit)
const 610 std::vector<node_indext> start_vector(1, src);
611 return depth_limited_search(start_vector, limit);
622 std::vector<typename N::node_indext> &src,
623 std::size_t limit)
const 625 std::vector<bool> visited(nodes.size(),
false);
627 for(
const auto &node : src)
630 visited[node] =
true;
633 return depth_limited_search(src, limit, visited);
644 std::vector<typename N::node_indext> &src,
646 std::vector<bool> &visited)
const 651 std::vector<node_indext> next_ring;
653 for(
const auto &n : src)
655 for(
const auto &o : nodes[n].out)
657 if(!visited[o.first])
659 next_ring.push_back(o.first);
660 visited[o.first] =
true;
665 if(next_ring.empty())
670 for(
const auto &succ : depth_limited_search(next_ring, limit, visited))
683 std::vector<node_indext> &subgraph_nr)
685 std::vector<bool> visited;
687 visited.resize(nodes.size(),
false);
688 subgraph_nr.resize(nodes.size(), 0);
699 std::stack<node_indext> s;
710 const nodet &node=nodes[n];
712 for(
const auto &o : node.
out)
714 if(!visited[o.first])
730 t.depth[v]=t.max_dfs;
731 t.lowlink[v]=t.max_dfs;
735 const nodet &node=nodes[v];
736 for(
typename edgest::const_iterator
745 t.lowlink[v]=std::min(t.lowlink[v], t.lowlink[vp]);
747 else if(t.in_scc[vp])
748 t.lowlink[v]=std::min(t.lowlink[v], t.depth[vp]);
752 if(t.lowlink[v]==t.depth[v])
756 assert(!t.scc_stack.empty());
760 t.subgraph_nr[vp]=t.scc_count;
784 tarjant t(nodes.size(), subgraph_nr);
808 const nodet &n=tmp[i];
811 for(
const auto &o1 : n.
out)
812 for(
const auto &o2 : n.
out)
814 if(o1.first!=o2.first)
817 this->add_undirected_edge(o1.first, o2.first);
832 std::list<node_indext> nodelist;
834 std::queue<node_indext> indeg0_nodes;
836 std::vector<size_t> in_deg(nodes.size(), 0);
841 in_deg[idx]=in(idx).size();
843 indeg0_nodes.push(idx);
846 while(!indeg0_nodes.empty())
850 nodelist.push_back(source);
852 for(
const auto &edge : out(source))
855 INVARIANT(in_deg[target]!=0,
"in-degree of node cannot be zero here");
860 if(in_deg[target]==0)
861 indeg0_nodes.push(target);
867 if(nodelist.size()!=nodes.size())
872 template <
typename node_index_type>
875 const std::function<
void(std::function<
void(
const node_index_type &)>)>
878 void(
const node_index_type &, std::function<
void(
const node_index_type &)>)>
880 const std::function<std::string(
const node_index_type &)> node_to_string)
882 for_each_node([&](
const node_index_type &i) {
883 for_each_succ(i, [&](
const node_index_type &n) {
884 out << node_to_string(i) <<
" -> " << node_to_string(n) <<
'\n';
890 std::vector<typename grapht<N>::node_indext>
893 std::vector<node_indext> result;
895 nodes[n].out.begin(),
897 std::back_inserter(result),
898 [&](
const std::pair<node_indext, edget> &edge) {
return edge.first; });
908 nodes[n].out.begin(),
910 [&](
const std::pair<node_indext, edget> &edge) { f(edge.first); });
916 const auto for_each_node =
917 [&](
const std::function<void(const node_indext &)> &f) {
922 const auto for_each_succ = [&](
923 const node_indext &i,
const std::function<void(const node_indext &)> &f) {
924 for_each_successor(i, f);
928 output_dot_generic<node_indext>(out, for_each_node, for_each_succ,
to_string);
931 #endif // CPROVER_UTIL_GRAPH_H 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)
void remove_in_edges(node_indext n)
A generic directed graph with a parametric node type.
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)
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)
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
std::string to_string(const string_constraintt &expr)
Used for debug printing.
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)
nodet & operator[](node_indext n)
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...