- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想使用 boost 图形库中提供的 kruskal_minimum_spanning_tree 算法构建最小生成树。
的输出kruskal_minimum_spanning_tree(g, std::back_inserter(spanning_tree));
来自 the BGL example是一个简单的边列表。但是,我想用深度优先算法 处理树,但不知道该怎么做。
有人可以给我一些提示吗?
最佳答案
更新:sehe 在这里提供了更新且更有效的解决方案: https://stackoverflow.com/a/49429372/85371
这是 Kruskal 和编写自定义 DFS 访问者的问题和良好示例的解决方案。它应该按原样运行。下面的代码中显示的示例输出是自包含的。正如我在评论中提到的,MST 算法的输出是一组边。这将向您展示如何使用该数据构建新图表。
示例取自 http://en.wikipedia.org/wiki/Kruskals_algorithm .
如有任何改进建议,我们将不胜感激。谢谢。
/**
Kruskal example from http://en.wikipedia.org/wiki/Kruskal's_algorithm
MST followed by DFS
Written by Paul W. Bible
*/
#include <iostream>
#include <vector>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
using namespace std;
using namespace boost;
typedef adjacency_list < vecS, vecS, undirectedS,
property< vertex_index_t, size_t> ,
property< edge_index_t, size_t, property<edge_weight_t,double> > > Graph;
typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef graph_traits<Graph>::edge_descriptor Edge;
typedef boost::property_map< Graph, boost::vertex_index_t>::type VertexIndexMap;
typedef boost::property_map< Graph, boost::edge_weight_t>::type WeightMap;
//DFS visitor, got help from http://stackoverflow.com/questions/14126/how-to-create-a-c-boost-undirected-graph-and-traverse-it-in-depth-first-search
// and http://www.boost.org/doc/libs/1_55_0/libs/graph/example/dfs-example.cpp
struct MyVis:default_dfs_visitor{
//Default dfs is templeted to work with any Edge or Graph class
// you will need to pass external graph info to the class
MyVis(vector<string> vNames):vertNames(vNames){}
template < typename Edge, typename Graph >
void tree_edge(Edge e, const Graph& g) const {
//This works since all graph verts will have an index
VertexIndexMap vMap = get(boost::vertex_index,g);
//print output message, source and target get the edge vertices
cout << "Edge " << vertNames.at(vMap[source(e,g)]) << " " << vertNames.at(vMap[target(e,g)]) << endl;
//cout << vertNames.size() << endl;
}
private:
vector<string> vertNames;
};
int main(int argc, char* argv[]){
Graph G;
vector<Vertex> verts;
vector<Edge> edges;
/* Vertices
0 A
1 B
2 C
3 D
4 E
5 F
6 G
*/
//add 7 vertices
for(size_t i = 0; i < 7; ++i){
Vertex v = add_vertex(G);
verts.push_back(v);
}
//vertex to index map, typdef above
VertexIndexMap vertexIndexMap = get(boost::vertex_index, G);
vector<string> vertexNames(num_vertices(G));
// Create the external property map, this map wraps the storage vector vertexNames
boost::iterator_property_map< std::vector< string >::iterator, VertexIndexMap >
vertexNameMap(vertexNames.begin(), vertexIndexMap);
//set names
vertexNames.at(0) = "A";
vertexNames.at(1) = "B";
vertexNames.at(2) = "C";
vertexNames.at(3) = "D";
vertexNames.at(4) = "E";
vertexNames.at(5) = "F";
vertexNames.at(6) = "G";
//get internal weight map
WeightMap weightMap = get(edge_weight,G);
//Edge 1 A -> B, weight 7
pair<Edge,bool> myPair = add_edge(verts.at(0),verts.at(1),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 7.0;
//Edge 2 A -> D, weight 5
myPair = add_edge(verts.at(0),verts.at(3),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 5.0;
//Edge 3 B -> C, weight 8
myPair = add_edge(verts.at(1),verts.at(2),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 8.0;
//Edge 4 B -> D, weight 9
myPair = add_edge(verts.at(1),verts.at(3),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 9.0;
//Edge 5 B -> E, weight 7
myPair = add_edge(verts.at(1),verts.at(4),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 7.0;
//Edge 6 C -> E, weight 5
myPair = add_edge(verts.at(2),verts.at(4),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 5.0;
//Edge 7 D -> E, weight 15
myPair = add_edge(verts.at(3),verts.at(4),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 15.0;
//Edge 8 D -> F, weight 6
myPair = add_edge(verts.at(3),verts.at(5),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 6.0;
//Edge 9 E -> F, weight 8
myPair = add_edge(verts.at(4),verts.at(5),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 8.0;
//Edge 10 E -> G, weight 9
myPair = add_edge(verts.at(4),verts.at(6),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 9.0;
//Edge 11 F -> G, weight 11
myPair = add_edge(verts.at(5),verts.at(6),G);
edges.push_back(myPair.first);
weightMap[myPair.first] = 11.0;
//output
cout << "vertices " << num_vertices(G) << endl;
cout << "edges " << num_edges(G) << endl;
//create a stoage vector for MST edges
vector<Edge> spanning_tree_edges;
kruskal_minimum_spanning_tree(G, std::back_inserter(spanning_tree_edges));
cout << "num MST edges " << spanning_tree_edges.size() << endl;
//create a graph for the MST
Graph MST;
//get a weight map for the MST, may be used later
WeightMap mstWeightMap = get(edge_weight,MST);
//create a list of original names for the MST graph.
vector<string> mstNames(num_vertices(G)); //the MST must span all verts in G
//Index map for verts in MST, all graphs use an indepenent index system.
VertexIndexMap mstIndexMap = get(boost::vertex_index, MST);
cout << "MST Edges" << endl;
for(size_t i = 0; i < spanning_tree_edges.size(); ++i){
//get the edge
Edge e = spanning_tree_edges.at(i);
//get the vertices
Vertex v1 = source(e,G);
Vertex v2 = target(e,G);
// output edge information
cout << "edge weight " << weightMap[e] << " v1 " << vertexNameMap[v1] << " v2 " << vertexNameMap[v2] << endl;
//insert the edge to the MST graph
// Both graphs will share the vertices in verts list.
myPair = add_edge(v1,v2,MST);
//set the correct weights
// may be needed at some point
Edge mstE = myPair.first;
mstWeightMap[mstE] = weightMap[e];
//get the vertex index in the MST and set the name to that of original graph
// mstNames will be used by the visitor
mstNames.at(mstIndexMap[v1]) = vertexNameMap[v1];
mstNames.at(mstIndexMap[v2]) = vertexNameMap[v2];
}
//Create your custom visitor and pass names to the visitor
MyVis vis(mstNames);
cout << "DFS on MST: start node E" << endl;
//call dfs, see visitor implimentation above.
boost::depth_first_search(MST, visitor(vis).root_vertex(verts.at(4)));
cout << "DFS on MST: start node B" << endl;
//call dfs, see visitor implimentation above.
boost::depth_first_search(MST, visitor(vis).root_vertex(verts.at(1)));
/* output
vertices 7
edges 11
num MST edges 6
MST Edges
edge weight 5 v1 A v2 D
edge weight 5 v1 C v2 E
edge weight 6 v1 D v2 F
edge weight 7 v1 B v2 E
edge weight 7 v1 A v2 B
edge weight 9 v1 E v2 G
DFS on MST: start node E
Edge E C
Edge E B
Edge B A
Edge A D
Edge D F
Edge E G
DFS on MST: start node B
Edge B E
Edge E C
Edge E G
Edge B A
Edge A D
Edge D F
*/
//hold for output
cin.get();
}
关于c++ - boost 最小生成树,如何做深度优先?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21867222/
关于 B 树与 B+ 树,网上有一个比较经典的问题:为什么 MongoDb 使用 B 树,而 MySQL 索引使用 B+ 树? 但实际上 MongoDb 真的用的是 B 树吗?
如何将 R* Tree 实现为持久(基于磁盘)树?保存 R* 树索引或保存叶值的文件的体系结构是什么? 注意:此外,如何在这种持久性 R* 树中执行插入、更新和删除操作? 注意事项二:我已经实现了一个
目前,我正在努力用 Java 表示我用 SML 编写的 AST 树,这样我就可以随时用 Java 遍历它。 我想知道是否应该在 Java 中创建一个 Node 类,其中包含我想要表示的数据,以及一个数
我之前用过这个库http://www.cs.umd.edu/~mount/ANN/ .但是,它们不提供范围查询实现。我猜是否有一个 C++ 范围查询实现(圆形或矩形),用于查询二维数据。 谢谢。 最佳
在进一步分析为什么MySQL数据库索引选择使用B+树之前,我相信很多小伙伴对数据结构中的树还是有些许模糊的,因此我们由浅入深一步步探讨树的演进过程,在一步步引出B树以及为什么MySQL数据库索引选择
操作系统的那棵“树” 今天从一颗 开始,我们看看如何从小树苗长成一颗苍天大树。 运转CPU CPU运转起来很简单,就是不断的从内存取值执行。 CPU没有好好运转 IO是个耗费时间的活,如果CPU在取值
我想为海洋生物学类(class)制作一个简单的系统发育树作为教育示例。我有一个具有分类等级的物种列表: Group <- c("Benthos","Benthos","Benthos","Be
我从这段代码中删除节点时遇到问题,如果我插入数字 12 并尝试删除它,它不会删除它,我尝试调试,似乎当它尝试删除时,它出错了树的。但是,如果我尝试删除它已经插入主节点的节点,它将删除它,或者我插入数字
B+ 树的叶节点链接在一起。将 B+ 树的指针结构视为有向图,它不是循环的。但是忽略指针的方向并将其视为链接在一起的无向叶节点会在图中创建循环。 在 Haskell 中,如何将叶子构造为父内部节点的子
我在 GWT 中使用树控件。我有一个自定义小部件,我将其添加为 TreeItem: Tree testTree = new Tree(); testTree.addItem(myWidget); 我想
它有点像混合树/链表结构。这是我定义结构的方式 struct node { nodeP sibling; nodeP child; nodeP parent; char
我编写了使用队列遍历树的代码,但是下面的出队函数生成错误,head = p->next 是否有问题?我不明白为什么这部分是错误的。 void Levelorder(void) { node *tmp,
例如,我想解析以下数组: var array1 = ["a.b.c.d", "a.e.f.g", "a.h", "a.i.j", "a.b.k"] 进入: var json1 = { "nod
问题 -> 给定一棵二叉树和一个和,确定该树是否具有从根到叶的路径,使得沿路径的所有值相加等于给定的和。 我的解决方案 -> public class Solution { public bo
我有一个创建 java 树的任务,它包含三列:运动名称、运动类别中的运动计数和上次更新。类似的东西显示在下面的图像上: 如您所见,有 4 种运动:水上运动、球类运动、跳伞运动和舞蹈运动。当我展开 sk
我想在 H2 数据库中实现 B+ Tree,但我想知道,B+ Tree 功能在 H2 数据库中可用吗? 最佳答案 H2 已经使用了 B+ 树(PageBtree 类)。 关于mysql - H2数据库
假设我们有 5 个字符串数组: String[] array1 = {"hello", "i", "cat"}; String[] array2 = {"hello", "i", "am"}; Str
我正在处理树。每个节点都有带有 Tree * 值的对象。我读取的数据如下所示: 1 2 2 ... 这意味着,将 1 作为 0 的子节点,将 2 作为 1 的子节点,将 3 作为 o 2 的子节点。在
我正在寻找一个好的 JavaScript 树/树网格包。现在——在你回答之前: 它需要能够在大量节点上正常运行。可能有 1,000 个兄弟节点。它需要能够在 2 或 3 秒内绘制到 1,000 个节点
下面的代码块究竟是如何工作的?更具体地说,程序如何知道返回哪个选项? return ancestor (node1->left(), node2) || ancestor
我是一名优秀的程序员,十分优秀!