将EdgeList存储为Boost图库

时间:2015-10-14 18:35:33

标签: boost-graph unordered-set

我正在复制科学实验,我需要将图形的边集存储为无序集。我试图使用BGL adjacency_graph,我认为最后一个参数是listS的hash_setS intead。但是当我这样做时,我的程序无法运行。我在这里看到一个类似的问题,其中一个人说要调整结构数据来模拟BGL的de EdgeListGraph,但我甚至不知道如何做到这一点。

有人可以帮忙吗?感谢。

2 个答案:

答案 0 :(得分:0)

using namespace std;

typedef boost::property<boost::edge_weight_t, double> EdgeWeightProperty;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, EdgeWeightProperty, boost::hash_setS> Graph;

int main()
{

ifstream arquivo_in ("shrd150-3.dis");
ofstream arquivo_out ("matriz.txt");

int numero, j, i;
Graph g(15);
EdgeWeightProperty e;
enum { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O };
const char name[] = "ABCDEFGHIJKLMNO";

boost::property_map<Graph, vertex_index_t>::type
vertex_id = get(vertex_index, g);
boost::property_map<Graph, edge_weight_t>::type
trans_delay = get(edge_weight, g);

j = 1;

for(i = 0; i < 15; ++i)
    Graph::vertex_descriptor i = boost::add_vertex(g);


for(j = 1; !arquivo_in.eof(); ++j)
{
    for(i = 0; i < j; ++i)
    {
        arquivo_in >> numero;
        e = numero;
        boost::add_edge(i, j, e, g);
    }
}

我不确定是否存储为hash_set,但我会尝试检查冲突。再次感谢!

答案 1 :(得分:0)

我不知道你为什么需要“unordered_map”存储(你从未提及过)。但既然你说:

  

我不确定是否存储为hash_set,但我会尝试检查冲突

我得到的印象是你只想防止重复的边缘。在这种情况下,只需对setS容器选择器使用OutEdgeList

此容器控制每个顶点的外边缘存储方式。如果它们是唯一的,那么根据定义,图形将不包含重复的边缘。

  

注意如果图表模式为undirectedS,那么Boost Graph Library会对源/目标顶点进行排序,以使(0,1)(1,0)边相当于此处/

样本

* Live On Coliru **

#include <boost/graph/adjacency_list.hpp>
#include <iostream>
#include <cassert>

typedef boost::property<boost::edge_weight_t, double> EdgeWeightProperty;
typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, boost::no_property, EdgeWeightProperty, boost::hash_setS> Graph;

int main()
{
    Graph g(15);

    assert( add_edge(0,1,g).second);
    assert( add_edge(1,2,g).second);
    assert( add_edge(0,2,g).second);
    assert( add_edge(0,3,g).second);
    assert(!add_edge(3,0,g).second);
}
相关问题