从根开始(第一个假设)
#include <boost/graph/adjacency_list.hpp>
struct GraphItem {
std::string name;
};
using Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, GraphItem>;
using Vertex = Graph::vertex_descriptor;
using Vertices = std::vector<Vertex>;
#include <boost/graph/filtered_graph.hpp>
#include <boost/function.hpp>
using Filtered = boost::filtered_graph<Graph, boost::keep_all, boost::function<bool(Vertex)>>;
#include <boost/graph/graphviz.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/graph_utility.hpp>
int main() {
Graph g;
auto names = get(&GraphItem::name, g);
enum {A,B,C,D,E,F,G,H,n};
Vertices vs {
add_vertex({"A"}, g),
add_vertex({"B"}, g),
add_vertex({"C"}, g),
add_vertex({"D"}, g),
add_vertex({"E"}, g),
add_vertex({"F"}, g),
add_vertex({"G"}, g),
add_vertex({"H"}, g),
};
assert(num_vertices(g) == n);
add_edge(vs[A], vs[B], g);
add_edge(vs[A], vs[C], g);
add_edge(vs[B], vs[D], g);
add_edge(vs[B], vs[E], g);
add_edge(vs[D], vs[G], g);
add_edge(vs[C], vs[F], g);
add_edge(vs[F], vs[H], g);
// write_graphviz(std::cout, g, make_label_writer(names));
// roots are nodes with a zero in-degree (no parents)
Vertices roots;
boost::remove_copy_if(vertices(g), back_inserter(roots), [&](Vertex v) { return boost::size(in_edges(v, g)); });
std::vector<Graph> subs;
for (Vertex root : roots) {
for (Vertex subroot : boost::make_iterator_range(adjacent_vertices(root, g))) {
std::set<Vertex> include;
std::vector<boost::default_color_type> colors(n);
boost::depth_first_visit(g, subroot,
boost::make_dfs_visitor(
boost::write_property(
boost::identity_property_map{},
inserter(include, include.end()),
boost::on_discover_vertex())
),
colors.data());
std::cout << "Root " << g[root].name << ": Subtree rooted at " << g[subroot].name << " includes " << include.size() << " nodes\n";
Filtered fg(g, boost::keep_all{}, [&](Vertex v) { return include.count(v); });
print_graph(fg, names);
}
}
}
从叶子开始(第二个假设,拓扑排序)
#include <boost/graph/adjacency_list.hpp>
struct GraphItem {
std::string name;
};
using Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::bidirectionalS, GraphItem>;
using Vertex = Graph::vertex_descriptor;
using Vertices = std::vector<Vertex>;
#include <boost/graph/topological_sort.hpp>
#include <iostream>
int main() {
Graph g;
auto names = get(&GraphItem::name, g);
{
enum {A,B,C,D,E,F,G,H,n};
Vertices vs {
add_vertex({"A"}, g),
add_vertex({"B"}, g),
add_vertex({"C"}, g),
add_vertex({"D"}, g),
add_vertex({"E"}, g),
add_vertex({"F"}, g),
add_vertex({"G"}, g),
add_vertex({"H"}, g),
};
assert(num_vertices(g) == n);
add_edge(vs[A], vs[B], g);
add_edge(vs[A], vs[C], g);
add_edge(vs[B], vs[D], g);
add_edge(vs[B], vs[E], g);
add_edge(vs[D], vs[G], g);
add_edge(vs[C], vs[F], g);
add_edge(vs[F], vs[H], g);
}
Vertices reverse_topological;
boost::topological_sort(g, back_inserter(reverse_topological));
bool in_path = true;
for (auto v : reverse_topological) {
if (0 == out_degree(v, g)) { // is leaf?
in_path = true;
std::cout << "\nLeaf:";
}
in_path &= (1 == in_degree(v, g));
if (in_path)
std::cout << " " << names[v];
else
std::cout << " [" << names[v] << "]";
}
}