如何更改igraph中节点的id?

如何更改igraph中节点的id?,r,igraph,R,Igraph,我有一棵树,比如: library(igraph) tree<- graph.empty(15) tree <- tree + path(5,4,3,1,2) tree <- tree + path(8,7,6,5) tree <- tree + path(15,14,13,12,11,10,9,5) root <- V(tree)[igraph::degree(tree, mode="out")==0] plot(tree, vertex.size=6, edg

我有一棵树,比如:

library(igraph)
tree<- graph.empty(15)
tree <- tree + path(5,4,3,1,2)
tree <- tree + path(8,7,6,5)
tree <- tree + path(15,14,13,12,11,10,9,5)

root <- V(tree)[igraph::degree(tree, mode="out")==0]
plot(tree, vertex.size=6, edge.arrow.size=0.1, 
     layout=layout.reingold.tilford(tree, mode="in", root=root))

我想重新编号ID,以便根为1。使用
name
属性,我得到:

# label the root as 1, and the rest as 2...N
root <- V(tree)[igraph::degree(tree, mode="out")==0]
V(tree)$name[V(tree) != root] <- 2:vcount(tree)
V(tree)$name[V(tree) == root] <- 1
root <- V(tree)[igraph::degree(tree, mode="out")==0]
plot(tree, vertex.size=6, edge.arrow.size=0.1,
     layout=layout.reingold.tilford(tree, mode="in", root=root))
有没有办法重新分配ID,以便我可以直接使用它们而不是使用它们的名称?它应提供:

V(tree)
Vertex sequence:
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

下面是Gabor Csardi的想法的一个例子:假设我们想使用
permute
更改节点ID,以降序排列:

library(igraph)
set.seed(1)
g = erdos.renyi.game(20,0.2)
V(g)$name = letters[1:20]
g2 = permute(g, Matrix::invPerm(order(degree(g), decreasing = T)))

您可以使用
permute()
对其进行排列。谢谢Gabor。如果原始id不是1,2,该怎么办。。N但随机整数?这不可能发生,因为igraph顶点ID始终是R中[1;N]范围内的连续整数。因此,当V(g)向我显示诸如985之类的数字时,它会向我显示名称吗?然后通过V(g)序列中每个节点所处的位置可以看到id?
V(tree)
Vertex sequence:
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
library(igraph)
set.seed(1)
g = erdos.renyi.game(20,0.2)
V(g)$name = letters[1:20]
g2 = permute(g, Matrix::invPerm(order(degree(g), decreasing = T)))