0

I'm trying to visualize a preferential network of products using R. I already have a graph of the product network using igraph, but I want to see what would happen if I were to remove one product. I found that I can delete a node using

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

but it would also delete all the edges connected to that specific node.

Is there any way to delete just the node and to see how the other nodes reconnect to each other after the deletion of that one node? Any help in this matter is appreciated.

P.S.

Hopefully this will make it clearer:

For example, if we generate the random graph:

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)

If you notice, when you delete the node '3' from the network, node '9' remains unconnected. Is there a way to see the new edge of node '9' after node '3' is connected? Still following the same plot, we would expect that it would connect to node '2'. Is there a function that does this in igraph? Or should i make a code for it?

Snowy
  • 59
  • 6
  • Could you please post a [more representative example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) ? Or at least, show the problem more clearly (e.g. by adding an example with images of the initial graph and the final desired graph) – digEmAll Oct 14 '14 at 12:23
  • *"how the other nodes reconnect to each other after the deletion of that one node"* how are they suppose to reconnect? – m0nhawk Oct 14 '14 at 12:30
  • @digEmAll Oh okay, I'll put one up in a bit. – Snowy Oct 14 '14 at 13:19
  • @m0nhawk Putting an example up so it's a bit clearer – Snowy Oct 14 '14 at 13:19
  • What if you have A -> B <- C and you delete B? What will be the direction of the new A - C edge? Or there will be no edge? The same with A <- B -> C. It is easier for undirected graphs, I guess. – Gabor Csardi Oct 14 '14 at 15:22
  • @GaborCsardi Yes I agree with you there, I suppose if there is a direction, it should be taken into account when deleting a node. I guess another way around it would be to have a probability function and redistribute the edges randomly to A and C. – Snowy Oct 15 '14 at 04:17

1 Answers1

1

Maybe not the most efficient way, but it should work :

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))

Original :

enter image description here

Modified :

enter image description here

digEmAll
  • 56,430
  • 9
  • 115
  • 140