2014-10-14 2 views
0

R을 사용하여 제품의 우선 네트워크를 시각화하려고합니다. 이미 igraph를 사용하여 제품 네트워크의 그래프를 가지고 있지만, 하나의 제품을 제거하는 경우 어떻게 될지보고 싶습니다. . 나는단일 노드 삭제 R

g2 <- g - V(g)[15] 

을 사용하여 노드를 삭제할 수 있지만, 그것은 또한 특정 노드에 연결된 모든 가장자리를 삭제할 것을 발견했다.

노드를 삭제하고 해당 노드를 삭제 한 후 다른 노드가 서로 어떻게 다시 연결되는지 보는 방법이 있습니까? 이 문제에 도움이 되었으면 좋겠습니다.

P.

희망이 그것을 명확하게됩니다 예를 들어

, 우리는 무작위로 그래프를 생성하는 경우 : 네트워크에서 노드를 '3'삭제

set.seed(10) 
Data <- data.frame(
    X = sample(1:10), 
    Y = sample(3, 10, replace=T) 
) 
d <- graph.data.frame(Data) 
plot(d) 
d2 <- d-V(d)[2] #deleting '3' from the network 
plot(d2) 

당신이 통지하는 경우를, 노드 ' 9 '는 연결되지 않은 채로 있습니다. 노드 '3'이 연결된 후에 노드 '9'의 새 가장자리를 볼 수있는 방법이 있습니까? 여전히 동일한 플롯을 따르면 노드 2에 연결될 것으로 예상됩니다. igraph에서 이것을 수행하는 함수가 있습니까? 아니면 코드를 작성해야합니까? 어쩌면

+0

는 [A를 게시하시기 바랍니다 수 더 대표적인 예] (http://stackoverflow.com/q/5963269/how-to-make-a-great-r-reproducible-example)? 또는 적어도 문제를보다 명확하게 표시하십시오 (예 : 초기 그래프 및 최종 원하는 그래프의 이미지가 포함 된 예제 추가) – digEmAll

+0

* "다른 노드가 해당 노드를 삭제 한 후 서로 어떻게 다시 연결되는지"* 어떻게 표시됩니까? 다시 연결하라고? – m0nhawk

+0

@digEmAll 오, 좋아, 내가 좀 올려 줄께. – basecracker

답변

1

없는 가장 효율적인 방법,하지만 작동합니다 :

library(igraph) 

set.seed(10) # for plot images reproducibility 

# create a graph 
df <- data.frame(
    X = c('A','A','B','B','D','E'), 
    Y = c('B','C','C','F','B','B') 
) 
d <- graph.data.frame(df) 

# plot the original graph 
plot(d) 

# function to remove the vertex 
removeVertexAndKeepConnections <- function(g,v){ 

    # we does not support multiple vertices 
    stopifnot(length(v) == 1) 

    vert2rem <- V(g)[v] 

    if(is.directed(g) == FALSE){ 
    # get the neigbors of the vertex to remove 
    vx <- as.integer(V(g)[nei(vert2rem)]) 
    # create edges to add before vertex removal 
    newEdges <- as.matrix(unique(expand.grid(vx,vx))) 
    # remove the cycles 
    newEdges <- newEdges[newEdges[,1] != newEdges[,2],] 
    # sort each row index to remove all the duplicates 
    newEdges <- t(apply(newEdges,1,sort)) 
    newEdges <- unique(newEdges) 
    }else{ 
    # get the ingoing/outgoing neigbors of the vertex to remove 
    vx <- as.integer(V(g)[nei(vert2rem,mode='in')]) 
    vy <- as.integer(V(g)[nei(vert2rem,mode='out')]) 
    # create edges to add before vertex removal 
    newEdges <- as.matrix(unique(expand.grid(vx,vy))) 
    } 

    # remove already existing edges 
    newEdges <- newEdges[!apply(newEdges,MARGIN=1,FUN=function(x)are.connected(g,x[1],x[2])),] 

    # add the edges 
    g <- g + edges(as.integer(t(newEdges))) 
    # remove the vertex 
    g <- g - vertex(vert2rem) 

    return(g) 
} 

# let's remove B (you can also use the index 
v <- 'B' 
plot(removeVertexAndKeepConnections(d,v)) 

원본 :

enter image description here

수정 :

enter image description here

+0

편집 : 무향 그래프에 대한 지원이 추가되었습니다. – digEmAll

+0

놀라워요! 이것은 많은 도움이됩니다! 감사! – basecracker

+0

@basecracker : 무향 그래프에 작은 버그 수정 추가 – digEmAll