我有一个来自 mygraph 的顶点向量,我对顶点进行拓扑排序。
typedef typename boost::adjacency_list<boost::listS, boost::vecS,
boost::directedS,Vertex_t*> Graph_t;
typedef typename boost::graph_traits<Graph_t>::vertex_descriptor Vd_t;
Graph_t mygraph;
std::vector<Vd_t> v1;
//initialize the vertices and get a copy of vertices in vector v1
//...
//and then i use
std::vector<Vd_t> v2;
boost::topological_sort(mygraph,std::back_inserter(v2));
对顶点“按顺序”进行拓扑排序。那么,比较 v1 和 v2 以测试我的原始顶点向量(v1)是“有序”还是“无序”的最佳方法是什么。
编辑:例如:如果向量 v1 有顶点 {A, B, C} 并且我们有边 (A,B) (C,A)
所以在拓扑排序之后,我将在 v2 中
{出租车}
在某些情况下,我可能无法获得唯一的拓扑顺序,例如,如果我有 (A,C) 作为唯一的边,那么 v2 可能以 {A , B , C} 或 {B , A , C} 结尾
现在我需要查看 v1 和 v2 以确定 v1 和 v2 是否代表相同的订单。
第二次编辑:我尝试了一种方法,它现在有效,请告诉我这是否是一个好方法。
template <typename Vertex_t>
void DepAnalyzer<Vertex_t>::CheckTotalOrder()
{
//Vd_t is the vertex descriptor type
typename std::vector<Vd_t> topo_order;
typename std::vector<Vd_t>::iterator ordered_vertices_iter;
//puts the topologically sorted vertices into topo_order
DoTopologicalSort(topo_order);
//will point to the vertices in the order they were inserted
typename boost::graph_traits<Graph_t>::vertex_iterator vi, vi_end;
std::unordered_map<Vertex_t*,int,std::hash<Vertex_t*>,
VertexEqual> orig_order_map, topo_order_map;
int i = 0;
for(std::tie(vi, vi_end) = boost::vertices(depGraph); vi != vi_end; ++vi) {
orig_order_map[depGraph[*vi]] = ++i;
//std::cout<<depGraph[*vi]->get_identifier_str()<<"\n";
}
i = 0;
//will point to the vertices in the topological order
ordered_vertices_iter = topo_order.begin();
for(;ordered_vertices_iter != topo_order.end(); ordered_vertices_iter++) {
topo_order_map[depGraph[*ordered_vertices_iter]] = ++i;
//std::cout<<depGraph[*ordered_vertices_iter]->get_identifier_str()<<"\n";
}
//checking the order now
ordered_vertices_iter = topo_order.begin();
for(;ordered_vertices_iter != topo_order.end(); ordered_vertices_iter++) {
//if the vertex in topologically sorted list occurs before(w.r.t. position)
//the macro in the original list of vertices, then it's okay
if(orig_order_map[depGraph[*ordered_vertices_iter]] >
topo_order_map[depGraph[*ordered_vertices_iter]]) {
std::cerr << depGraph[*ordered_vertices_iter]->get_identifier_str()
<< ": out of order\n";
}
}
}