3

I have a symmetric matrix that represent degree of connections among actors. I would like to cancel out the vertex unconnected.

The functions included in igraph (as delete_edges or delete_vertices) do not work for my case. I share my code

#import of matrix
matrix3<-import("matrix2a.xlsx")
r.name <- matrix2a [,1]
rownames(matrix2a) <- r.name
matrix2a <- matrix2a %>% select(-X__1)
View(matrix2a)
m=as.matrix(matrix2a)
#I compute the maximum spanning tree graph
g <- graph_from_adjacency_matrix(m, mode = "upper", weighted = T, diag = F)
max_spann_tree <- mst(g, weights = -E(g)$weight)
#I obtain a network with some unconneted vertex that I would like to erase

thanks in advance for your help!

G5W
  • 36,531
  • 10
  • 47
  • 80
Leonardo
  • 71
  • 1
  • 1
  • 10

1 Answers1

14

I am not sure what you mean "The functions included in igraph (as delete_edges or delete_vertices) do not work for my case." delete.vertices is made for precisely this purpose.

Since you do not provide data, I will show a small example from random data. I am adding labels to the graph so that the numbering will not change when I delete the isolated vertices. I am also using an explicit layout so I can lay out the reduced graph in the same way for comparison.

library(igraph)
set.seed(1234)
G = erdos.renyi.game(40, 0.055)
V(G)$label=1:40     
LO = layout_with_fr(G)
plot(G, layout = LO)

Graph 01

Now identify the isolated vertices and use delete.vertices to remove them.

Isolated = which(degree(G)==0)
G2 = delete.vertices(G, Isolated)
LO2 = LO[-Isolated,]
plot(G2, layout=LO2)

Graph 02

The same graph but without the isolated vertices.

If this is not what you want, please be more explicit about why it does not work for your graph.

G5W
  • 36,531
  • 10
  • 47
  • 80
  • thank you for your reply. If I refer the function delete.vertices to my "g", that I indicated in my script, I got the answer "Error in delete.vertices(g, Isolated) : delete.vertices requires an argument of class network". How can I cope with this? – Leonardo Jun 04 '18 at 13:25
  • I suspect that you have more than one function `delete.vertices` in your environment. Please try `igraph::delete.vertices` – G5W Jun 04 '18 at 13:29
  • I would think more to a function like this: "isolated=V(max_spann_tree, which(degree(max_spann_tree)==0) " But It seems that does not work like this – Leonardo Jun 04 '18 at 13:40
  • I tried also: delete.vertices (max_spann_tree, V(max_spann_tree)[degree(max_spann_tree) == 0]) without success – Leonardo Jun 04 '18 at 14:06
  • @G5W, Hello, I'm running this code on my igraph object and get the error `(list) object cannot be created to type 'double'`. Can you please help? or simplify the code using `dplyr` and `degree()` – Alex Feb 11 '21 at 21:31
  • 1
    @Alex I am not sure what you mean "simplify". It is only a few lines of code. What line of code is giving you that error? – G5W Feb 11 '21 at 21:37
  • @G5W, `Isolated = which(degree(G)==0)` its where I get the error. I successfully created the object and need to remove the isolated nodes. I was thinking of something like `my_igrapgh_object %>% degree() %>% filter` but can't write it – Alex Feb 11 '21 at 21:40
  • Sorry, I am not much of a `dplyr` user. My way worked for me, but I am not sure what to do with `filter` – G5W Feb 11 '21 at 23:26
  • 1
    Notice that for a directed graph one needs to use the `mode` parameter in `degree()`, i.e, `degree(G, mode='out')==0` to remove nodes with 0 outflow connections. – Emiliano A. Carlevaro Nov 08 '22 at 22:51