Algorithm 相关网络实现
我一直在研究我的图形/网络问题,我想我终于知道我想做什么了。现在我已经开始实现了,我在决定使用什么库时遇到了一些问题。图本身非常简单,每个节点都用一个字符串标记,每个节点都是两个节点(变量)之间的概率/相关系数,并且是无向的。我要在图形上执行的操作有:Algorithm 相关网络实现,algorithm,boost,graph,graph-algorithm,minimum-spanning-tree,Algorithm,Boost,Graph,Graph Algorithm,Minimum Spanning Tree,我一直在研究我的图形/网络问题,我想我终于知道我想做什么了。现在我已经开始实现了,我在决定使用什么库时遇到了一些问题。图本身非常简单,每个节点都用一个字符串标记,每个节点都是两个节点(变量)之间的概率/相关系数,并且是无向的。我要在图形上执行的操作有: 插入新节点/边(快速) 查找所有对的最短(1/概率)路径,并记住路径中的节点-可能是Johnson算法 构造k个特定顶点的最小权Steiner树 使用Johnson算法构建最短路径 迭代路径p中的当前节点,找到k中剩余节点的最短路径 看看图
- 插入新节点/边(快速)
- 查找所有对的最短(1/概率)路径,并记住路径中的节点-可能是Johnson算法
- 构造k个特定顶点的最小权Steiner树
- 使用Johnson算法构建最短路径
- 迭代路径p中的当前节点,找到k中剩余节点的最短路径
- 看看图的平均度数
- 评估节点的介数
- 获得聚类系数
- 求图的模性
- 柠檬
- 易于使用和安装
- 直观的文档
- 已经有一些我想要的功能了
- 通过读取文本文件动态创建图形,并确保没有重复的节点,这是一个我一直无法理解的噩梦
- Boost图形库
- 难以理解的对象定义,以及如何使用它们
- 文档与代码不匹配,这是必然的
- 它有许多我想要的算法,以及从文本文件创建图形的非常简单的方法
- 多线程图形库
- 已合并并行性
- 比BGL更容易阅读
- 没有那么多功能
- 仍然神秘
using namespace boost;
int main()
{
//Read in the file
std::ifstream datafile("stuff");
if (!datafile)
{
std::cerr << "No Stuff file" << std::endl;
return EXIT_FAILURE;
}
//Build the graph
typedef adjacency_list < vecS, vecS, undirectedS, property < vertex_name_t,
std::string >, property < edge_weight_t, double > > Graph;
Graph g;
//Build the two properties we want, string and double
//Note, you have to nest properties for more
typedef property_map< Graph, vertex_index_t >::type vertex_index_map_t;
vertex_index_map_t vertex_index_map = get(vertex_index, g);
typedef property_map < Graph, vertex_name_t >::type name_map_t;
name_map_t name_map = get(vertex_name, g);
typedef property_map < Graph, edge_weight_t >::type probability_map_t;
probability_map_t probability = get(edge_weight, g);
//Map of of the vertices by string
typedef graph_traits < Graph >::vertex_descriptor Vertex;
typedef std::map < std::string, Vertex > NameVertexMap;
NameVertexMap AllNodes;
//Load the file into the graph
for (std::string line; std::getline(datafile, line);)
{
char_delimiters_separator < char >sep(false, "", ";");
tokenizer <> line_toks(line, sep);
tokenizer <>::iterator i = line_toks.begin();
std::string conditionA = *i++;
NameVertexMap::iterator pos;
bool inserted;
Vertex u, v;
boost::tie(pos, inserted) = AllNodes.insert(std::make_pair(conditionA, Vertex()));
if (inserted)
{
u = add_vertex(g);
name_map[u] = conditionA;
pos->second = u;
}
else
{
u = pos->second;
}
std::string correlation = *i++;
std::istringstream incorrelation(correlation);
double correlate;
incorrelation >> correlate;
boost::tie(pos, inserted) = AllNodes.insert(std::make_pair(*i, Vertex()));
if (inserted) {
v = add_vertex(g);
name_map[v] = *i;
pos->second = v;
}
else
{
v = pos->second;
}
graph_traits < Graph >::edge_descriptor e;
boost::tie(e, inserted) = add_edge(u, v, g);
if (inserted)
probability[e] = 1.0/correlate;
}
typedef boost::graph_traits<Graph>::edge_iterator edge_iter;
std::pair<edge_iter, edge_iter> edgePair;
Vertex u, v;
for(edgePair = edges(g); edgePair.first != edgePair.second; ++edgePair.first)
{
u = source(*edgePair.first, g);
v = target(*edgePair.first, g);
std::cout << "( " << vertex_index_map[u] << ":" << name_map[u] << ", ";
std::cout << probability[*edgePair.first] << ", ";
std::cout << vertex_index_map[v] << ":" << name_map[v] << " )" << std::endl;
}
}
使用名称空间boost;
int main()
{
//读入文件
std::ifstream数据文件(“stuff”);
如果(!数据文件)
{
标准:cerr>图形;
图g;
//构建我们想要的两个属性:string和double
//注意,您必须嵌套属性以获取更多信息
typedef属性映射<图形,顶点索引映射>::键入顶点索引映射;
顶点索引映射顶点索引映射=get(顶点索引,g);
typedef属性映射<图形,顶点名称映射>::类型名称映射;
name\u map\u t name\u map=get(顶点名称,g);
typedef property_map<图,边_-weight_-t>::type probability_-map\t;
概率图概率=get(边权,g);
//通过字符串映射顶点
typedef graph_traits:顶点描述符顶点;
typedef std::mapNameVertexMap;
NameVertexMap所有节点;
//将文件加载到图形中
for(std::string行;std::getline(数据文件,行);)
{
字符分隔符分隔符sep(false,“,”;”);
令牌发生器线路(线路,sep);
标记器::迭代器i=line_toks.begin();
std::string conditionA=*i++;
NameVertexMap::迭代器pos;
布尔插入;
顶点u,v;
boost::tie(pos,inserted)=所有节点。insert(std::make_pair(conditionA,Vertex());
如果(插入)
{
u=添加_顶点(g);
name_map[u]=conditionA;
pos->second=u;
}
其他的
{
u=位置->秒;
}
std::string correlation=*i++;
std::istringstream不相关(相关);
双重关联;
不相关>>相关;
boost::tie(pos,inserted)=所有节点。insert(std::make_pair(*i,Vertex());
如果(插入){
v=添加顶点(g);
name_map[v]=*i;
pos->second=v;
}
其他的
{
v=位置->秒;
}
图形特征<图形>::边描述符e;
boost::tie(e,inserted)=添加_边(u,v,g);
如果(插入)
概率[e]=1.0/相关;
}
typedef boost::graph_traits::edge_迭代器edge_iter;
标准:对边pair;
顶点u,v;
对于(edgePair=边(g);edgePair.first!=edgePair.second;++edgePair.first)
{
u=来源(*边空气优先,g);
v=目标(*边缘第一,g);
标准::cout