首先,如果我违反规则,请先道歉,因为我知道我的问题已经在这里以修改的方式提出:Lengauer Tarjan Algorithm in BGL (boost graph library)。但是,我(仍然)无法使用答案才能正确显示结果。
更确切地说:我按照链接的答案并成功地将Lengauer-Tarjan算法应用于我的图表(为方便起见,这是Boost文档的一部分:http://www.boost.org/doc/libs/1_55_0/libs/graph/test/dominator_tree_test.cpp)。现在,如果正确理解代码,关于支配树的相关信息存储在domTreePredMap中,该类型为PredMap:
const int numOfVertices = testSet[i].numOfVertices;
//See example for test_sets - it just the same routine
G g(
testSet[i].edges.begin(), testSet[i].edges.end(),
numOfVertices);
typedef graph_traits<G>::vertex_descriptor Vertex;
typedef property_map<G, vertex_index_t>::type IndexMap;
typedef
iterator_property_map<vector<Vertex>::iterator, IndexMap>
PredMap;
vector<Vertex> domTreePredVector, domTreePredVector2;
IndexMap indexMap(get(vertex_index, g));
graph_traits<G>::vertex_iterator uItr, uEnd;
int j = 0;
for (tie(uItr, uEnd) = vertices(g); uItr != uEnd; ++uItr, ++j)
{
put(indexMap, *uItr, j);
}
// Lengauer-Tarjan dominator tree algorithm
domTreePredVector =
vector<Vertex>(num_vertices(g), graph_traits<G>::null_vertex());
PredMap domTreePredMap =
make_iterator_property_map(domTreePredVector.begin(), indexMap);
lengauer_tarjan_dominator_tree(g, vertex(0, g), domTreePredMap);`
对我来说,Boost的一个主要优点是可以使用graphviz和write_graphviz(cout,g)自动生成图形输出,其中g是来自typedef G的图:
typedef adjacency_list<
listS,
listS,
bidirectionalS,
property<vertex_index_t, std::size_t>, no_property> G;
但是,我无法将DomTreePredMap转换为write_graphviz(cout,X)可以理解的内容。感谢任何帮助概述如何从domTreePredMap构建图形,可以使用graphviz打印。
感谢大家阅读所有这些并帮助我。
答案 0 :(得分:1)
很抱歉打扰你 - 我设法在增强纪录片中找到答案:
这是一个简单的工作示例来说明我的问题。基本上我想计算图形(左边一个)和它的支配树(右边),如下图所示:http://www.boost.org/doc/libs/1_40_0/libs/graph/doc/lengauer_tarjan_dominator.htm#fig:dominator-tree-example并使用graphviz打印两个图形。
根据这个例子,我设法计算并打印原始图并在其上执行Lengauer-Tarjan算法。关于支配树的信息存储在DomPredMap中,可以复制到整数向量中。在向量idom的位置i处,存储节点i的父节点的id。如果不存在父节点,则存储max_int。该信息可用于将idom [i]到i的边添加到testSet,最终可以从中构建图g2。感谢您的帮助和耐心。
#include <iostream>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dominator_tree.hpp>
#include <algorithm>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
struct DominatorCorrectnessTestSet
{
typedef pair<int, int> edge;
int numOfVertices;
vector<edge> edges;
vector<int> correctIdoms;
};
using namespace boost;
typedef adjacency_list<
listS,
listS,
bidirectionalS,
property<vertex_index_t, std::size_t>, no_property> G;
int main(int, char*[])
{
typedef DominatorCorrectnessTestSet::edge edge;
DominatorCorrectnessTestSet testSet[1];
testSet[0].numOfVertices = 8, //Orignal problem see left hand side
testSet[0].edges.push_back(edge(0, 1));
testSet[0].edges.push_back(edge(1, 2));
testSet[0].edges.push_back(edge(1, 3));
testSet[0].edges.push_back(edge(2, 7));
testSet[0].edges.push_back(edge(3, 4));
testSet[0].edges.push_back(edge(4, 5));
testSet[0].edges.push_back(edge(4, 6));
testSet[0].edges.push_back(edge(5, 7));
testSet[0].edges.push_back(edge(6, 4));
testSet[1].numOfVertices = 8; //Used to create Dominator Tree
const int numOfVertices = testSet[0].numOfVertices;
G g(
testSet[0].edges.begin(), testSet[0].edges.end(),
numOfVertices);
typedef graph_traits<G>::vertex_descriptor Vertex;
typedef property_map<G, vertex_index_t>::type IndexMap;
typedef
iterator_property_map<vector<Vertex>::iterator, IndexMap>
PredMap;
vector<Vertex> domTreePredVector, domTreePredVector2;
IndexMap indexMap(get(vertex_index, g));
graph_traits<G>::vertex_iterator uItr, uEnd;
int j = 0;
for (tie(uItr, uEnd) = vertices(g); uItr != uEnd; ++uItr, ++j)
{
put(indexMap, *uItr, j);
}
write_graphviz(cout, g);
// Lengauer-Tarjan dominator tree algorithm
domTreePredVector =
vector<Vertex>(num_vertices(g), graph_traits<G>::null_vertex());
PredMap domTreePredMap =
make_iterator_property_map(domTreePredVector.begin(), indexMap);
lengauer_tarjan_dominator_tree(g, vertex(0, g), domTreePredMap);
vector<int> idom(num_vertices(g));
for (tie(uItr, uEnd) = vertices(g); uItr != uEnd; ++uItr)
{
if (get(domTreePredMap, *uItr) != graph_traits<G>::null_vertex())
idom[get(indexMap, *uItr)] =
get(indexMap, get(domTreePredMap, *uItr));
else
idom[get(indexMap, *uItr)] = (numeric_limits<int>::max)();
}
for (int k =0; k <idom.size();k++){
if (k>0){
cout << idom[k] << " nach " << k << endl;
int t= idom[k];
testSet[1].edges.push_back(edge(t, k));
}
}
G g2(testSet[1].edges.begin(), testSet[1].edges.end(),8);
int jj=0;
for (tie(uItr, uEnd) = vertices(g2); uItr != uEnd; ++uItr, ++jj)
{
put(indexMap, *uItr, jj);
}
write_graphviz(cout, g2);
cout << endl;
return 0;
}