资源简介
Graphchi环境下的BFS算法实现,实现的具体描述请参考本人博客http://blog.csdn.net/codeera/article/details/46385711
代码片段和文件信息
/**
* @file
* @author Aapo Kyrola
* @version 1.0
*
* @section LICENSE
*
* Copyright [2012] [Aapo Kyrola Guy Blelloch Carlos Guestrin / Carnegie Mellon University]
*
* Licensed under the Apache License Version 2.0 (the “License“);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing software
* distributed under the License is distributed on an “AS IS“ BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @section DEscriptION
*
* Template for GraphChi applications. To create a new application duplicate
* this template.
*/
#include
#include “graphchi_basic_includes.hpp“
#include “util/labelanalysis.hpp“
using namespace graphchi;
/**
* Type definitions. Remember to create suitable graph shards using the
* Sharder-program.
*/
typedef unsigned VertexDataType;
typedef unsigned EdgeDataType;
unsigned single_source = 1;
bool converged = false;
unsigned maxlevel = 10000000;
bool scheduler = true;
/**
* GraphChi programs need to subclass GraphChiProgram
* class. The main logic is usually in the update function.
*/
struct bfs : public GraphChiProgram {
/**
* Vertex update function.
*/
void update(graphchi_vertex &vertex graphchi_context &gcontext) {
if (gcontext.iteration == 0) {
/* On first iteration initialize vertex (and its edges). This is usually required because
on each run GraphChi will modify the data files. To start from scratch it is easiest
do initialize the program in code. Alternatively you can keep a copy of initial data files. */
// vertex.set_data(init_value);
// if(vertex.num_inedges() == 0)
if(vertex.id() == single_source)
{
vertex.set_data(single_source);
for(int id = 0; id < vertex.num_outedges(); id++)
{
if(scheduler)
gcontext.scheduler->add_task(vertex.outedge(id)->vertexid);
vertex.outedge(id)->set_data(vertex.get_data());
}
}
else
{
vertex.set_data(maxlevel);
for(int id = 0; id < vertex.num_outedges(); id++)
{
vertex.outedge(id)->set_data(vertex.get_data());
if(scheduler)
{
gcontext.scheduler->add_task(vertex.outedge(id)->vertex_id());
}
}
}
} else {
/* Do computation */
/* Loop over in-edges (example) */
for(int i=0; i < vertex.num_inedges(); i++) {
// Do something
// value += vertex.ine
评论
共有 条评论