2

我想使用 R 提取图的巨大组件的邻接矩阵。

例如,我可以创建 Erdos-Renyi g(n,p)

n = 100
p = 1.5/n
g = erdos.renyi.game(n, p)
coords = layout.fruchterman.reingold(g)
plot(g, layout=coords, vertex.size = 3, vertex.label=NA)

# Get the components of an undirected graph
cl = clusters(g)

# How many components?
cl$no           

# How big are these (the first row is size, the second is the number of components of that size)?
table(cl$csize) 

cl$membership
# Get the giant component
nodes = which(cl$membership == which.max(cl$csize))

# Color in red the nodes in the giant component and in sky blue the rest
V(g)$color  = "SkyBlue2"
V(g)[nodes]$color = "red"
plot(g, layout=coords, vertex.size = 3, vertex.label=NA)

在这里,我只想提取那些红色节点的邻接矩阵。

在此处输入图像描述

4

1 回答 1

0

很容易将巨型组件获取为如下所示的新图,然后获取邻接矩阵。

g  <- erdos.renyi.game(100, .015, directed = TRUE)
# if you have directed graph, decide if you want
# strongly or weakly connected components
co <- components(g, mode = 'STRONG')
gi <- induced.subgraph(g, which(co$membership == which.max(co$csize)))
# if you want here you can decide if you want values only
# in the upper or lower triangle or both
ad <- get.adjacency(gi)

但是您可能希望保留原始图形的顶点 ID。在这种情况下,只需对邻接矩阵进行子集化:

g  <- erdos.renyi.game(100, .015)
co <- components(g)
gi_vids <- which(co$membership == which.max(co$csize))
gi_ad <- get.adjacency(g)[gi_vids, gi_vids]
# you can even add the names of the nodes
# as row and column names.
# generating dummy node names:
V(g)$name <- sapply(
    seq(vcount(g)),
    function(i){
        paste(letters[ceiling(runif(5) * 26)], collapse = '')
    }
)
rownames(gi_ad) <- V(g)$name[gi_vids]
colnames(gi_ad) <- V(g)$name[gi_vids]
于 2018-06-09T13:44:01.750 回答