nvGRAPH三角形计数和遍历示例

#include “ stdlib.h”

#include“ inttypes.h”

#include“ stdio.h”

#include“ nvgraph.h”

  1. #define check( a )\

{\

nvgraphStatus_t status =(a); \

if((status)!= NVGRAPH_STATUS_SUCCESS){\

  1.         printf(“ERROR :%s,%s:%d \ n”,status__ FILE ____ LINE __); \

exit(0); \

} \

}

int main(int argc,char ** argv)

{

// nvgraph变量

nvgraphHandle_t handle;

nvgraphGraphDescr_t graph;

nvgraphCSRTopology32I_t CSR_input;

//初始化主机数据

CSR_input =(nvgraphCSRTopology32I_t)malloc(sizeof(struct nvgraphCSRTopology32I_st));

//无向 graph:

// 3个三角形

//邻接矩阵下三角形的CSR:

const size_t n = 6,nnz = 8;

int source_offsets [] = {0,0,1,2,4,4,6,8};

int destination_indices [] = {0,1,1,2,2,2,3,3,4};

check(nvgraphCreate(&handle));

check(nvgraphCreateGraphDescr( handle&graph));

CSR_input-> nvertices = n;

CSR_input-> nedges = nnz;

CSR_input-> source_offsets = source_offsets;

CSR_input-> destination_indices = destination_indices;

//设置 graph连接性

check(nvgraphSetGraphStructure(handle,graph,(void *)CSR_input,NVGRAPH_CSR_32));

uint64_t trcount = 0;

check(nvgraphTriangleCount( handle, graph,&trcount));

printf(“三角形数:%” PRIu64 “ \ n”,trcount);

free(CSR_input);

check(nvgraphDestroyGraphDescr( handle, graph));

check(nvgraphDestroy( handle));

return 0;

}

nvGRAPH遍历示例

void check_status(nvgraphStatus_t status){

if((int)status!= 0){

printf(“error:%d \ n”,status);

exit(0);

}

}

int main(int argc,char ** argv){

// graph示例(CSR格式)

const size_t n = 7,nnz = 12,vertex_numsets = 2,edge_numset = 0;

int source_offsets_h [] = {0,1,3,4,6,6,8,10,12};

int destination_indices_h [] = {5,0,2,0,4,5,5,2,3,3,4,1,5};

//存储结果的位置(与源的距离)和存储结果的位置(搜索树中的前身)

int bfs_distances_h [n],bfs_predecessors_h [n];

// nvgraph变量

nvgraphStatus_tstatus;

nvgraphHandle_t handle;

nvgraphGraphDescr_t graph;

nvgraphCSRTopology32I_t CSR_input;

cudaDataType_t * vertex_dimT;

size_t distances_index = 0;

size_t predecessors_index = 1;

vertex_dimT =(cudaDataType_t *)malloc(vertex_numsets * sizeof(cudaDataType_t));

vertex_dimT [distances_index] = CUDA_R_32I;

vertex_dimT [predecessors_index] = CUDA_R_32I;

//创建nvgraph对象

check_status(nvgraphCreate(&handle));

check_status(nvgraphCreateGraphDescr( handle&graph));

//设置 graph的连通性和属性(转移)

CSR_input =(nvgraphCSRTopology32I_t)malloc(sizeof(struct nvgraphCSCTopology32I_st));

CSR_input-> nvertices = n;

CSR_input-> nedges = nnz;

CSR_input-> source_offsets = source_offsets_h;

CSR_input-> destination_indices = destination_indices_h;

check_status(nvgraphSetGraphStructure(handle,graph,(void *)CSR_input,NVGRAPH_CSR_32));;

check_status(nvgraphAllocateVertexData( handle, graph,vertex_numsets,vertex_dimT));

int source_vert = 1;

//设置遍历参数

nvgraphTraversalParameter_t traversal_param;

nvgraphTraversalParameterInit(&traversal_param);

nvgraphTraversalSetDistancesIndex(&traversal_param,distances_index);

nvgraphTraversalSetPredecessorsIndex(&traversal_param,predecessors_index);

nvgraphTraversalSetUndirectedFlag(&traversal_param,false);

//使用BFS算法进行遍历

check_status(nvgraphTraversal( handle, graph,NVGRAPH_TRAVERSAL_BFS,&source_vert,traversal_param));

//获取结果

check_status(nvgraphGetVertexData(handle, graph表,(void *)bfs_distances_h,distances_index));

check_status(nvgraphGetVertexData( handle, graph,(void *)bfs_predecessors_h,predecessors_index));

//

for(int i = 0; i <n; i ++),期望bfs distances_h =(1 0 1 3 3 2 2147483647) printf(“距顶点%d的距离:%i \ n”,i,bfs_distances_h [i]) ; printf(“ \ n”);

//

for(int i = 0; i <n; i ++),期望bfs前驱体=(1 -1 1 5 5 0 -1) printf(“顶点%d的前驱体:%i \ n”,i,bfs_predecessors_h [i ]); printf(“ \ n”);

free(vertex_dimT);

free(CSR_input);

check_status(nvgraphDestroyGraphDescr( handle, graph));

check_status(nvgraphDestroy(handle));

return 0;

}

nvGRAPH三角形计数和遍历示例的更多相关文章

  1. 基于mapreduce实现图的三角形计数

    源代码放在我的github上,想细致了解的可以访问:TriangleCount on github 一.实验要求 1.1 实验背景         图的三角形计数问题是一个基本的图计算问题,是很多复杂 ...

  2. Luogu P2807 三角形计数

    题目背景 三角形计数(triangle) 递推 题目描述 把大三角形的每条边n等分,将对应的等分点连接起来(连接线分别平行于三条边),这样一共会有多少三角形呢?编程来解决这个问题. 输入输出格式 输入 ...

  3. Java实现三角形计数

    题: 解: 这道题考的是穷举的算法. 一开始看到这道题的时候,本能的想到用递归实现.但使用递归的话数据少没问题,数据多了之后会抛栈溢出的异常.我查了一下,原因是使用递归创建了太多的变量, 每个变量创建 ...

  4. 洛谷 P2807 三角形计数

    P2807 三角形计数 题目背景 三角形计数(triangle) 递推 题目描述 把大三角形的每条边n等分,将对应的等分点连接起来(连接线分别平行于三条边),这样一共会有多少三角形呢?编程来解决这个问 ...

  5. ContentProvider官方教程(3)ContentResolver查询、遍历 示例

    Retrieving Data from the Provider This section describes how to retrieve data from a provider, using ...

  6. luogu P2992 [USACO10OPEN]三角形计数Triangle Counting

    https://www.luogu.org/problemnew/solution/P2992 考虑包含原点,不包含原点的三角形有什么特征. 包含原点的三角形:任意找一个顶点和原点连线,一定能把另外两 ...

  7. 611. Valid Triangle Number三角形计数

    [抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题 ...

  8. 【题解】Luogu P2992 [USACO10OPEN]三角形计数Triangle Counting

    原题传送门 我们考虑进行容斥 包含原点的三角形个数=所有可能三角形的个数-不包含原点三角形的个数 对于每个点,我们会发现:将它与原点连线,在直线左边任选两点或右边任选两点与这个点构成的三角形一定是不包 ...

  9. 3828. 三角形计数 3829. ZCC loves Isaac 3830. 字符消除

    3828 给定n个点的坐标(0<=xi,yi<=10000)求选出任意三个点能组成的三角形的总面积. 题解 太naive了 枚举三角形的y最小的点,把剩余的点按角度排序 然后随便算,可以用 ...

随机推荐

  1. hdu4400 BFS+STL

    题意:        煤矿爆炸,每个煤矿有自己的x,y,d,d是他爆炸后会是d距离内的爆炸,每次输入一个爆炸的煤矿,问你这个煤矿爆炸会有多少个煤矿爆炸. 思路:       爆炸的过程就是搜索的过程, ...

  2. 手机改 user模式为debug模式

    logcat 是Android中一个命令行工具,可用于监控手机应用程序的log信息.网上相关的教学很多,这里只想把自己折腾 2 部手机(一个是三星S4 I9500 港水,Android 5.01,一个 ...

  3. CVE-2014-7911学习笔记

    工作日分析的差不多了,写个标题周末搞

  4. 逆向 stdio.h 函数库 fopen 函数(调试版本)

    0x01 fopen 函数 函数原型:FILE *fopen(const char *filename, const char *mode) 返回值为 FILE 类型 函数功能:使用给定的模式 mod ...

  5. Day002 Hello,World!!!

    Hello,World! 随便新建一个文件,存放代码 新建一个java文件 文件后缀名为.java Hello.java [注意点] 系统可能没有显示文件后缀名,我们需要手动打开 编写代码 publi ...

  6. 【maven】You may use+to add a project ro to let the plugin find all pom.xml files...

    错误显示 解决方法 点击pom.xml,再Add as Maven Project 如果还不能解决,点击idea的log 复制报错(技巧:可以先将idea.log删除,比较好定位) Caused by ...

  7. PHP Excel文件导入数据到数据库

    1.php部分(本例thinkphp5.1): 下载PHPExcel了扩展http://phpexcel.codeplex.com/ <?phpnamespace app\admin\contr ...

  8. webpack 快速入门 系列 —— 初步认识 webpack

    初步认识 webpack webpack 是一种构建工具 webpack 是构建工具中的一种. 所谓构建,就是将资源转成浏览器可以识别的.比如我们用 less.es6 写代码,浏览器不能识别 less ...

  9. 微信小程序中的常见弹框

    显示加载中的提示框 wx.showLoading() 当我们正在在进行网络请求时,常常就需要这个提示框 手动调用wx.hideLoading()方法才能够关闭这个提示框,通常在数据请求完毕时就应该关闭 ...

  10. MFC的六大机制

    MFC的六大机制 程序的初始化过程 运行时类型识别 动态创建 永久保存 消息映射 命令传递 运行时类型识别 MFC的运行时类型识别就是在程序运行过程中判断某个对象是否属于某个类,MFC通过为需要进行运 ...