0

我正在尝试使用 GraphLab 实现 BFS 算法。在我的 vertex_data 结构中,我使用一个向量来保存到目前为止已经遍历的路径。

struct vertex_data {

    std::vector<int> current_path;
    bool visited;
    int destination;
    vertex_data() {
        destination = -1;
        visited = false;
    }
}

然后在 apply() 函数中,我试图将当前顶点 id 推送到该特定顶点的 current_path:

class bfs: public graphlab::ivertex_program<graph_type, graphlab::empty,
        vertex_data> {
private:
    vertex_data msg;

public:


 void apply(icontext_type& context, vertex_type& vertex, const gather_type& gather_result) {
        int vid = vertex.id();
        vertex.data().destination = msg.destination;
        vertex.data().visited = true;
        msg.current_path.push_back(vid);
        vertex.data().current_path.push_back(vid);
}
}

文档中 data() 的定义也是:

vertex_data_type &  data ()
    Returns a reference to the data on the vertex.

GraphLab 负责将 vertex_data 对象包装为 vertex_data_type。当我运行它时,某些顶点的 push_back() 函数出现段错误。我检查了 vid,它始终是一个具有实际值的整数。运行 vertex.data().current_path.size(),我得到 0。

我尝试了几种方法,例如创建一个本地向量并在 vertex.data() 替换一个并在推送 id 之前调整大小。在第一种情况下,我在尝试替换时遇到 seg 错误,在第二种情况下,resize 可以毫无问题地执行,但推送一直给我 seg 错误。

您对可能出现的问题有任何想法吗?

谢谢

4

1 回答 1

0

Graphlab 中的类需要可序列化。因此,如果变量不是普通旧数据 (POD),则需要编写自己的加载和保存函数。

在你的情况下,它应该是这样的(注意加载和保存列表应该是相同的顺序):

结构顶点数据{

std::vector<int> current_path;
bool visited;
int destination;
vertex_data() {
    destination = -1;
    visited = false;
}
void save(graphlab::oarchive& oarc) const {
    oarc << current_path << visited << destination;
}

void load(graphlab::iarchive& iarc) {
    iarc >> current_path >> visited >> destination;
}

}

于 2015-10-28T21:35:00.967 回答