nvGRAPH三角形计数和遍历示例

#include “ stdlib.h”

#include“ inttypes.h”

#include“ stdio.h”

#include“ nvgraph.h”

#define check( a )\

{\

nvgraphStatus_t status =(a); \

if((status)!= NVGRAPH_STATUS_SUCCESS){\

        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. linux下export命令添加删除环境变量

    Linux export命令参数   功能说明:设置或显示环境变量. 语 法:export [-fnp][变量名称]=[变量设置值] 补充说明:在shell中执行程序时,shell会提供一组环境变量. ...

  2. 十步解决php utf-8编码

    以前说过如果JS文件不是UTF8会在IE有bug,所以JS代码也要用UTF-8.还有数据库也都要用UTF-8.php用UTF-8总结: php文件本身必须是UTF-8编码.不像Java会生成class ...

  3. 关于Number、parseInt、isNaN转化参数

    1.首先,关于NaN的相等判断 alert(NaN==NaN) //返回的是false: 2.isNaN 确定这个参数是否是数值或者是否可以被转化为数值:NaN是not a number 的缩写,所以 ...

  4. 播放视频插件swfobject.js与Video Html5

    播放视频的方法: 方法一. 使用HTML5播放 <video src="./files/Clip_480_5sec_6mbps_h264.mp4" width="1 ...

  5. Windows PE第6章 栈与重定位表

    第六章 栈与重定位表 本章主要介绍栈和代码重定位.站和重定位表两者并没有必然的联系,但都和代码有关.栈描述的是代码运行过程中,操作系统为调度程序之间相互调用关系,或临时存放操作数而设置的一种数据结构. ...

  6. HTTP自定义Header-(SOCKET-TCP)

      HTTP自定义Header-TCP 前几天弄一些东西,需要在发送http请求的时候自定义http头,找了几个库用着很不爽.有的把Cookie直接干掉了,还自己在头里加了版权,最后终于忍不了了.在网 ...

  7. SSRF_FastCGI

    SSRF_FastCGI 目录 SSRF_FastCGI FastCGI协议 SSRF ssrf + fastcgi 参考 FastCGI协议 简介 Fast CGI源自旧版本的CGI 路由/结构图 ...

  8. linux命令的使用 以及基本docker命令及docker镜像安装

    以linux CentOS-7 64位 系统为例 查看ip  ifconfig 固定ip 输入vim /etc/sysconfig/network-scripts/ifcfg-ens3 其中vim是修 ...

  9. PageHelper简单使用

    PageHelper的简单使用 先引入对应的依赖 <dependency> <groupId>com.github.pagehelper</groupId> < ...

  10. 自定义元类 __call__,__init__,__new__总结

    只要对象能被调用 产生对象的类里必然有__call__方法 在调用类时,必定先触发type里的__call__ __call__下有: 1.产生对象的object.__new__ 2..被调用的类自己 ...