R语言 创建一个3D点阵图,其中节点与摩尔邻域中的所有其他节点相连

64jmpszr  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(106)

我正在尝试创建一个3D晶格igraph对象,其中节点连接到3D中的相邻和对角邻居(摩尔邻居)。我可以创建一个所有节点都连接到其冯诺依曼邻居(仅相邻节点)的对象,在3D中使用默认函数。

library(rgl)
library(igraph)

g <- make_lattice( c(4, 4, 4))
coords <- layout_with_fr(g, dim = 3)
rglplot(g, layout = coords)

现在我想让它的每一个节点都连接到它的对角邻居以及它的邻居。任何建议都将不胜感激...我相当卡住了。
如下所示(但适用于所有节点):

vnzz0bqm

vnzz0bqm1#

我认为这是可行的,虽然这是蛮力-几乎可以肯定有一种更优雅的方法来做到这一点,我不会在大型网格上尝试这种方法...

library(Matrix)
library(igraph)
library(rgl)

n <- 4  ## grid dimension
dd <- do.call(expand.grid, replicate(6, 1:n, simplify = FALSE))
## determine adjacency
afun <- function(p) as.numeric(max(abs(p[1:3]-p[4:6])) <= 1)
adj <- apply(dd, 1, afun)

## set up node IDs, drop self-loops
nodes <- expand.grid(1:n^3, 1:n^3)
self <- nodes[,1] == nodes[,2]
nodes <- nodes[!self,]
adj <- adj[!self]

## construct adjacency matrix
m <- Matrix(0, nrow = n^3, ncol = n^3)
m[as.matrix(nodes)] <- adj
image(m)

## convert to graph, plot
g <- graph_from_adjacency_matrix(m)
coords <- layout_with_fr(g, dim = 3)
rglplot(g, layout = coords)

我认为布局算法把内部节点拉进比角多一点,因为它们有更多的连接...
(At有一点我写了一些很好的技巧来使用稀疏克罗内克积构造二维网格邻接矩阵-它们可能可以扩展到三维...)

相关问题