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;
}
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三角形计数和遍历示例的更多相关文章
- 基于mapreduce实现图的三角形计数
		
源代码放在我的github上,想细致了解的可以访问:TriangleCount on github 一.实验要求 1.1 实验背景 图的三角形计数问题是一个基本的图计算问题,是很多复杂 ...
 - Luogu   P2807 三角形计数
		
题目背景 三角形计数(triangle) 递推 题目描述 把大三角形的每条边n等分,将对应的等分点连接起来(连接线分别平行于三条边),这样一共会有多少三角形呢?编程来解决这个问题. 输入输出格式 输入 ...
 - Java实现三角形计数
		
题: 解: 这道题考的是穷举的算法. 一开始看到这道题的时候,本能的想到用递归实现.但使用递归的话数据少没问题,数据多了之后会抛栈溢出的异常.我查了一下,原因是使用递归创建了太多的变量, 每个变量创建 ...
 - 洛谷 P2807 三角形计数
		
P2807 三角形计数 题目背景 三角形计数(triangle) 递推 题目描述 把大三角形的每条边n等分,将对应的等分点连接起来(连接线分别平行于三条边),这样一共会有多少三角形呢?编程来解决这个问 ...
 - ContentProvider官方教程(3)ContentResolver查询、遍历 示例
		
Retrieving Data from the Provider This section describes how to retrieve data from a provider, using ...
 - luogu P2992 [USACO10OPEN]三角形计数Triangle Counting
		
https://www.luogu.org/problemnew/solution/P2992 考虑包含原点,不包含原点的三角形有什么特征. 包含原点的三角形:任意找一个顶点和原点连线,一定能把另外两 ...
 - 611. Valid Triangle Number三角形计数
		
[抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题 ...
 - 【题解】Luogu P2992 [USACO10OPEN]三角形计数Triangle Counting
		
原题传送门 我们考虑进行容斥 包含原点的三角形个数=所有可能三角形的个数-不包含原点三角形的个数 对于每个点,我们会发现:将它与原点连线,在直线左边任选两点或右边任选两点与这个点构成的三角形一定是不包 ...
 - 3828. 三角形计数 3829. ZCC loves Isaac 3830. 字符消除
		
3828 给定n个点的坐标(0<=xi,yi<=10000)求选出任意三个点能组成的三角形的总面积. 题解 太naive了 枚举三角形的y最小的点,把剩余的点按角度排序 然后随便算,可以用 ...
 
随机推荐
- 百度sitemap.xml
			
<?xml version="1.0" encoding="UTF-8" ?> <urlset xmlns="http://www. ...
 - POJ1094查分约束,判断关系是否唯一
			
题意: 给你一些a<b的关系,然后有三组询问. 1 当前这组之后如果能确定这n个数的大小关系,那么就输出关系 2 当前时候出现bug,就是和前面如果冲突,那么就不行 3 最后的答案是 ...
 - php 解析富文本编辑器中的hmtl内容,富文本样式正确输出
			
说明:富文本编辑器中的内容在直接获获取后需要解析以后才能在页面中正确显示 我在后端这样处理: $content = htmlspecialchars_decode($info['intro']); h ...
 - 面向对象编程OOP
			
这节讲一下,什么是面向对象(Object Oriented Programming).说面向对象之前,我们不得不提的是面向过程(Process Oriented Programming),C语言就是面 ...
 - Asp.NetCore Web开发之初始文件解析
			
在写代码之前,有必要了解一下.net帮我们生成的文件都是干什么用的,在开发过程中他们都负责那些地方(下面以MVC模板举例). 先简单介绍一下什么是MVC,MVC(model-view-controll ...
 - JavaWeb——JDBC连接池&JDBCTemplate
			
今日内容 1. 数据库连接池 2. Spring JDBC : JDBC Template 数据库连接池 1. 概念:其实就是一个容器(集合),存放数据库连接的容器. 当系统初始化好后,容器被创建,容 ...
 - IIS部署.Net5全流程
			
介绍 Internet Information Services (IIS) 是一种灵活.安全且可管理的 Web 服务器,用于托管 Web 应用(包括 ASP.NET Core).虽然我们的程序可以跨 ...
 - Linux VMware Tools详解
			
VMware Tools描述 VMware Tools 中包含一系列服务和模块,可在 VMware 产品中实现多种功能,从而使用户能够更好地管理客户机操作系统,以及与客户机系统进行无缝交互. 在Lin ...
 - [bug] C:warning: implicit declaration of function ‘getline’
			
参考 https://blog.csdn.net/loushuai/article/details/38983793
 - MySQL给某个用户给某个库表设置权限
			
-- 用root(最高权限的用户)进行以下操作-- 创建数据库:emc_power CREATE DATABASE emc_power DEFAULT CHARACTER SET utf8 COLLA ...