图论学习(二)在Boost Graph中返回连通子图序列
来源:互联网 发布:windows arping 编辑:程序博客网 时间:2024/06/05 06:30
参考
http://stackoverflow.com/questions/26763193/return-a-list-of-connected-component-subgraphs-in-boost-graph
本代码实现功能
1.计算连通子图
2.保存子图
3.映射子图和原图的顶点
示例输出如图
代码如下
#include <sstream>#include <iostream>#include <boost/graph/subgraph.hpp>#include <boost/graph/adjacency_list.hpp>#include <boost/config.hpp>#include <vector>#include <boost/graph/connected_components.hpp>using namespace std;using namespace boost;// Underlying graph representation and implementation//typedef adjacency_list_traits<vecS, vecS, directedS> Traits;// Graph representationtypedef subgraph< adjacency_list<vecS, vecS, directedS, property<vertex_color_t, int>, property<edge_index_t, int> > > Graph;// Iterating over vertices and edgestypedef graph_traits<Graph>::vertex_iterator vertex_iter;//typedef graph_traits<Graph>::edge_iterator edge_iter;int main(void){ Graph g; add_edge(0,1, g); add_edge(1,4, g); add_edge(4,0, g); add_edge(2,5, g); std::vector<int> comp(num_vertices(g)); int num = connected_components (g, comp.data()); std::cout << std::endl; std::vector < int >::iterator i; std::cout << "Total number of components: " << num << std::endl; for (i = comp.begin(); i != comp.end(); ++i) std::cout << "Vertex " << i - comp.begin() << " is in component " << *i << std::endl; std::vector<Graph* > comps(num); for(size_t i=0;i<num;++i) { comps[i] = & g.create_subgraph(); } for(size_t i=0;i<num_vertices(g);++i) { cout<<"add vetex "<<i<<"to sub graph "<<comp[i]<<endl; add_vertex(i, *comps[comp[i]]); } pair<vertex_iter, vertex_iter> vip; cout << "Vertices in g = [ "; vip = vertices(g); for(vertex_iter vi = vip.first; vi != vip.second; ++vi) { cout << *vi << " "; } cout << "]" << endl; for(size_t i=0;i<num;i++) { cout << "Vertices (local) in comps[i]' = [ "; pair<vertex_iter, vertex_iter> lvip; lvip = vertices(*comps[i]); for(vertex_iter vi = lvip.first; vi != lvip.second; ++vi) { cout << (*comps[i]).local_to_global(*vi) << " "; } cout << "]" << endl; } return 0;}
0 0
- 图论学习(二)在Boost Graph中返回连通子图序列
- 【LintCode】在无向图中找出各极大连通子图 Find the Connected Component in the Undirected Graph
- 图论学习(一)使用Boost Graph Library表示图
- 极大连通子图 + 极小连通子图 + 连通分量
- 强连通子图
- hdu5438 连通子图
- 【TensorFlow】计算图graph的使用学习笔记(二)
- Boost学习(二)
- 找出一个图中所有的强连通子图
- 极大连通子图与极小连通子图
- 极大连通子图和极小连通子图
- Boost Graph Library 学习笔记
- ccf 高速公路(连通子图)
- ccf 高速公路(连通子图)
- Boost Graph Library (BGL)学习:使用Bundled Properties
- boost graph read_graphml 要解析graph property(图的属性)要自己给boost graph加代码
- boost graph --- 有向图中两点间所有路径(可处理有环情况)
- CUGB图论专场2:The Bottom of a Graph 强连通Tarjan算法
- 深拷贝和浅拷贝的几个实例
- 利用java反射机制写的一个实体类属性拷贝的工具
- 学习定时任务(Quartz)
- Scala类
- python——接收处理外带的参数
- 图论学习(二)在Boost Graph中返回连通子图序列
- 【学习笔记07】java面向对象-继承
- mysqld --initialize 执行失败
- Java字符串去除中文
- httpclient 以及 urlConnection 发送请求
- 磁疗是真的吗?磁刺激技术在神经疾病方面的研究与应用
- Java方向如何准备BAT技术面试答案(汇总版)
- XMind 8中怎样设置自动保存/备份?
- intelj idea 创建 Servlet 类