Network C++表示网络结构
2015-09-16 14:41
447 查看
对于社交网络分析, 有时需要保存一个完整的网络结构。
需要利用数据结构 图
这里提供一个数据结构范例,可以较快速进行图的操作。
需要利用数据结构 图
这里提供一个数据结构范例,可以较快速进行图的操作。
class Vertex { public: Vertex(void){ num_in_edges = 0; //这个点的入度 num_out_edges = 0; //点的出度 }; ~Vertex(void){}; vector< pair<int, double> > in_edges; //进入点的index和权重 vector< pair<int, double> > out_edges; int num_in_edges, num_out_edges; };
class Graph { public: Graph(void); ~Graph(void); // Number of vertices int vertex_num; // Number of edges int edge_num; <span style="white-space:pre"> unordered_map<int, int> vertex_map;</span> // Adjacent List vector<class Vertex> vertices;
void add_vertex(int id){ vertex_map[id] = vertex_num; vertices.push_back(class Vertex()); vertex_num++; }
void add_edge(int from, int to, double weight){ if (vertex_map.find(from) == vertex_map.end()){ add_vertex(from); } if (vertex_map.find(to) == vertex_map.end()){ add_vertex(to); } int from_id = vertex_map.find(from)->second; int to_id = vertex_map.find(to)->second; // if this edge already exists, return for (unsigned int i = 0; i < vertices[from_id].out_edges.size(); i++){ if (vertices[from_id].out_edges[i].first == to_id){ return; } } vertices[from_id].out_edges.push_back(make_pair(to_id, weight)); vertices[from_id].num_out_edges++; vertices[to_id].in_edges.push_back(make_pair(from_id, weight)); vertices[to_id].num_in_edges++; edge_num++; }
相关文章推荐
- Android基础入门教程——7.6.1 Socket学习网络基础准备
- http请求头及响应头等
- 组策略映射网络驱动器
- Android利用HTTP请求加载网络图片
- Https(SSL/TLS)原理详解
- http与https的区别
- Android 用httpUtils绑定setion和cookin
- Java发送HttpRequest
- HTTP协议详解
- HTTP协议详解
- python抓取网络内容
- iOS开发网络篇—数据缓存
- 网络游戏中使用促销码
- AFNetworking从指定网址下载文件的两种方式总结 - 家柱
- HTTP 协议详解
- android 第二更(后台判断网络状态,反馈到页面)
- HTTPS原理详解
- 【网络】(十二)UDP简介
- WebHttpRequest Post请求
- Python网络爬虫 - 2. Beautiful Soup小试牛刀