创建一个 3D 点阵图,其中节点连接到其 Moore 邻域中的所有其他节点

问题描述 投票:0回答:1

我正在尝试创建一个 3D 格子

igraph
对象,其中节点在 3D 中连接到相邻和对角线邻居(摩尔邻域)。我可以使用默认函数在 3D 中创建一个所有节点都连接到它们的 von Neumann 邻居(仅相邻节点)的节点。例如

library(rgl)
library(igraph)

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

现在我想让每个节点都连接到它的对角线邻居以及它的相邻邻居。任何建议将不胜感激......我很困惑。

像这样(但对于所有节点):

r graph-theory igraph
1个回答
0
投票

我认为这样做是可行的,尽管它是蛮力-几乎可以肯定有一种更优雅的方法,我不会在大网格上尝试...

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)

我认为布局算法将内部节点拉得比角落多一点,因为它们有更多的连接......

© www.soinside.com 2019 - 2024. All rights reserved.