当前位置:网站首页>邻接矩阵的COO格式
邻接矩阵的COO格式
2022-07-26 15:22:00 【aelum】
我们知道,邻接矩阵通常是稀疏矩阵,而COO格式(Coordinate Format)是稀疏矩阵的一种存储方式,本文将简要介绍如何将无权无向图的邻接矩阵转化为COO格式。
顾名思义,COO格式即坐标格式,我们只需考虑邻接矩阵中不为零的元素的坐标。对于无权无向图,其邻接矩阵是对称阵并且元素非 0 0 0 即 1 1 1,考虑下面的邻接矩阵:
[ 0 1 0 1 1 0 1 0 0 1 0 0 1 0 0 0 ] \begin{bmatrix} 0 & 1 & 0 & 1 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ \end{bmatrix} ⎣⎡0101101001001000⎦⎤
先考虑下三角部分,不为零的元素的坐标为 ( 1 , 0 ) , ( 2 , 1 ) , ( 3 , 0 ) (1,0), (2,1),(3,0) (1,0),(2,1),(3,0),因此所有不为零的元素的坐标为 ( 1 , 0 ) , ( 0 , 1 ) , ( 2 , 1 ) , ( 1 , 2 ) , ( 3 , 0 ) , ( 0 , 3 ) (1,0),(0,1),(2,1),(1,2),(3,0),(0,3) (1,0),(0,1),(2,1),(1,2),(3,0),(0,3)。将这六个坐标转置成列向量并沿列方向拼在一起即可得到此邻接矩阵的COO格式:
[ 1 0 2 1 3 0 0 1 1 2 0 3 ] \begin{bmatrix} 1 &0 & 2&1&3&0\\ 0&1&1&2&0&3 \\ \end{bmatrix} [100121123003]
容易看出,对于无权无向图,设它有 num_edges 条边,则邻接矩阵的COO格式的形状为 (2, num_edges * 2)。
️ 在 PyG 中,一条无向边被视为两条有向边的组合,COO格式中的
num_edges指的是有向边的个数,因此这种情况下无论是有向图还是无向图,形状均可统一为(2, num_edges)。
numpy 实现:
import numpy as np
def adj2coo(adj):
"""Convert the adjacency matrix to its COO format Args: adj (ndarray): Adjacency matrix Returns: ndarray: COO format """
return np.vstack(adj.nonzero())
例如:
a = np.array([[0, 1, 0, 1],
[1, 0, 1, 0],
[0, 1, 0, 0],
[1, 0, 0, 0]])
print(adj2coo(a))
# [[0 0 1 1 2 3]
# [1 3 0 2 1 0]]
边栏推荐
- University rankings in Beijing
- [5 minutes paper] Pointer network
- 数仓:数仓建设中的数据建模和日志体系
- 线程和进程
- TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(3)
- 采购实用技巧,5个瓶颈物料的采购方法
- NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
- 【EXPDP导出数据】expdp导出23行记录,且不包含lob字段的表,居然用时48分钟,请大家帮忙看看
- R语言ggplot2可视化:使用ggpubr包的ggballoonplot函数可视化气球图(可视化由两个分类变量组成的列联表)、配置guides函数中的size参数指定不显示数据点大小的图例
- QT is the most basic layout, creating a window interface
猜你喜欢

Glyphs V3 Font Icon query

# 工欲善其事必先利其器-C语言拓展--嵌入式C语言(十一)

Creation and traversal of binary tree

TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(4)

PS + PL heterogeneous multicore case development manual for Ti C6000 tms320c6678 DSP + zynq-7045 (4)

Google tries to introduce password strength indicator for chromeos to improve online security
![[leave some code] Apply transformer to target detection, and understand the model operation process of the model through debug](/img/71/068de34da9d1e6041237d936314e14.png)
[leave some code] Apply transformer to target detection, and understand the model operation process of the model through debug

C # set different text watermarks for each page of word

二叉树的创建以及遍历
![[expdp export data] expdp exports a table with 23 rows of records and no lob field. It takes 48 minutes. Please help us have a look](/img/ee/7bec8211dddc90e52c2bc0f56c8dd4.png)
[expdp export data] expdp exports a table with 23 rows of records and no lob field. It takes 48 minutes. Please help us have a look
随机推荐
拒绝噪声,耳机小白的入门之旅
企业数字化转型需要深入研究,不能为了转型而转型
FOC motor control foundation
OSPF综合实验
使用verdaccio搭建自己的npm私有库
VP video structured framework
使用两个栈实现一个队列
R语言ggplot2可视化:使用ggpubr包的ggballoonplot函数可视化气球图(可视化由两个分类变量组成的列联表)、配置guides函数中的size参数指定不显示数据点大小的图例
What is the transport layer protocol tcp/udp???
数据中台、BI业务访谈(四)—— 十个问题看本质
Abbkine EliKine人甲胎蛋白(AFP)ELISA定量试剂盒操作方法
[static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial
请问参数化视图可以根据传入参数的特点得到不同行数的SQL吗?比如这里我想根据传输参数@field中列
软测(七)性能测试(1)简要介绍
04 callable and common auxiliary classes
pytorch---进阶篇(函数使用技巧/注意事项)
认识JS基础与浏览器引擎
cs224w(图机器学习)2021冬季课程学习笔记5
Continuous integration (II) introduction to the basic use of Jenkins
数仓:爱奇艺数仓平台建设实践