0

I have a directed graph, G. Some of the edges in G are reciprocal, and some are not. For reciprocal edges, edge attributes may be different. That is E(v1,v2)$att may not equal E(v2,v1)$att.

I need to fill in all missing reciprocal edges (that is, if E(v2,v1) does not exist while E(v1,v2) does, I want to create E(v2, v1) and copy all attribute information from E(v1, v2)).

If the reciprocal edge does exist, I need to keep the unique edge attribute information.

There are a lot of edges and a lot of attributes, so I am trying to avoid a loop here. Currently, where g1 is the directed but incomplete graph, I:

#make undirected with loops for reciprocal friendships
g2 <- as.undirected(g1, mode = c("each"))

#force everything to be directed, create new edges
g <- as.directed(g2, mode = c("mutual"))

#get rid of the double loops. 
 gnew <- simplify(g, remove.multiple = TRUE,
     edge.attr.comb = "random")   

The only problem with this is edge.attr.comb = "random" That is, I override the pre-existing mutual edge attribute information. I am thinking that I can flag missing mutual edges from g1and add the necessary edges (and copy their attribute information) using which_mutual but am having a difficult time with the indexing of edges. I must be missing an easy solution. An example:

g <- graph_from_literal(A+-+B, A-+C)
E(g)$att1 <- c(1,2,3)

#I want (note the default order of vertices for igraph)
g2 <- graph_from_literal(A+-+B, A+-+C)
E(g2)$att1 <- c(1, 2, 3, 2) 
EUps
  • 41
  • 6
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 19 '18 at 18:56

1 Answers1

2

Figured it out. Perhaps not the most eloquent solution, but it works. As an example,

g <- graph_from_literal(10-+40, 10-+30, 20+-+40)
E(g)$att1 <- c(1,2,3, 4)
E(g)$att2 <- c(10, 11, 12, 13)

######################################################################

test <- which((which_mutual(g) == FALSE))

head <- head_of(g,test)
tail <- tail_of(g,test)

combine <- matrix(c(head,tail), ncol = length(test), byrow = TRUE)
combineV <- as.vector(combine)

attributes <- list(att1 = E(g)$att1[test],att2 = E(g)$att2[test])

gnew <- add_edges(g,combineV, attr = attributes)
EUps
  • 41
  • 6