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. ASP去除所有html标签

    ASP去除所有html标签 function nohtml(str) dim re Set re=new RegExp re.IgnoreCase =true re.Global=True re.Pa ...

  2. php实现redis消息发布订阅

    基础介绍 Pub/Sub功能(means Publish, Subscribe)即发布及订阅功能 基于事件的系统中,Pub/Sub是目前广泛使用的通信模型,它采用事件作为基本的通信机制,提供大规模系统 ...

  3. hdu1316 大数

    题意:      给你一个区间,问这个区间有多少个斐波那契数. 思路:      水的大数,可以直接模拟,要是懒可以用JAVA,我模拟的,打表打到1000个就足够用了... #include<s ...

  4. android 代码中使用textAppearance

    一开始在代码中我以为使用tvAge.setTextAppearance(context, resid);这样的的方式就能行, 运行之后发现这个设置并未生效,于是到处搜索在代码中设置系统样式的的解决方法 ...

  5. 修改wordpress版权信息

    修改页脚版权信息位置:找到C:\wamp64\www\wordpress\wp-content\themes\travelify\library\structure\footer-extensions ...

  6. VMware Workstation中安装Hyper-V

    1:在虚拟机设置中,CPU属性中勾选"Virtualize Intel VT-x/EPT or AMD-V/RVI"来启用虚拟机的CPU支持虚拟化. 2:2.在虚拟机文件所在目录中 ...

  7. 【odoo】[经验分享]数据迁移注意事项

    [odoo14]经典好书学习没有烂尾,主体已完成,可移步了解.https://www.cnblogs.com/xushuotec/p/14428210.html 背景 近期,有朋友打算上odoo系统. ...

  8. unapp一键登录

    一.整理思路 un-app官网提供多种实现[一键登录](https://uniapp.dcloud.net.cn/uniCloud/univerify "")的方法,这里的选择是 ...

  9. Mybatis学习之自定义持久层框架(七) 自定义持久层框架优化

    前言 接上文,这里只是出于强迫症,凭借着半年前的笔记来把之前没写完的文章写完,这里是最后一篇了. 前面自定义的持久层框架存在的问题 Dao层若使用实现类,会存在代码重复,整个操作的过程模版重复(加载配 ...

  10. VS中光标变成方块状,输入时会把光标覆盖的部分替换掉的解决方法

    按下键盘上的Insert键,切换为插入模式.