2

我有一个名称从 1 到 10 的图表

library(igraph)
library(Cairo)

graphics.off()  
g<- graph(c(0,1,0,4,0,9,1,7,1,9,2,9,2,3,2,5,3,6,3,9,4,5,4,8,5,8,6,7,6,8,7,8),n=10,dir=FALSE)
V(g)$name<-c(1:10)
V(g)$label<-V(g)$name
coords <- c(0,0,13.0000,0,5.9982,5.9991,7.9973,7.0009,-1.0008,11.9999,0.9993,11.0002,7.9989,13.0009,10.9989,14.0009,5.9989,14.0009,7.0000,4.0000)
coords <- matrix(coords, 10,2,byrow=T)
plot(g,layout=coords)

从每个顶点的邻居中,我做了以下事情,

listMn<-neighborhood(g,1,0:9)

m1<-V(g)[listMn[[7]]]$name # here I'm trying to name this array to don't confuse me 

g<-add.edges(g, c(listMn[[7]][2],listMn[[7]][3])) # with 6 3 7 8 I take ids 2 and 3 from this array to add between 3 and 7 an edge,
                                                   # but it's really 4 and 8 if i want to continue with the names id....  :s 

plot(g , layout = coords[V(g)$name,]) 

现在我进行下一步“寻找同构”,消除顶点名称(7)并在名称“9”和“4”之间添加边

vc<-c(0,1,1,2,2,3,3,0,0,2)
gp<-graph(vc,dir=FALSE);
listSub<-graph.get.subisomorphisms.vf2(g,gp)

m<-matrix(c(unlist(listSub)),length(listSub),length(unique(vc)),byrow=T)
m<-m[!duplicated(m[,1]),]

在这种情况下,使用第一行 i=1 我很困惑,因为我想在 m[1,] 中添加 9 到 4 个名称之间的边,但在这种情况下,id 已更改

i<-1

if(length(neighbors(g,c(m[i,1])))==3){
    found<-1
    g<-delete.vertices(g,c(m[i,1]))

    if(are.connected(g, c(m[i,2]),c(m[i,4]))==TRUE){

    }else{
        g<-add.edges(g, c(m[i,2],m[i,4]))
    }

}else if(length(neighbors(g,c(m[i,3])))==3){
    found<-1
    g<-delete.vertices(g,c(m[i,3]))

    if(are.connected(g, c(m[i,2]),c(m[i,4]))==TRUE){

    }else{
        g<-add.edges(g, c(m[i,2],m[i,4]))
    }
}

plot(g , layout = coords[V(g)$name,]) 

总之,处理该算法的 id 的最佳方法是什么,我需要消除顶点并添加新边?

4

0 回答 0