In a directed, weighted graph, when I have bidirectional edges, (I.e. A->B and B->A exist), I would like to subtract the two weights such that the resulting edge direction is in the direction of the larger weight. My example below accomplishes this for edges with only a 'weight' attribute, but when I have other edge attributes (multiple edges), I want to retain the attributes when aggregating. The issue with my example is that when I convert my graph to an adjacency matrix via as_adjacency_matrix
, the function combines the weights and I lose the attribute data. How can I combine bidirectional edge weights while retaining edge attribute data?
Example with a "group" attribute:
library(dplyr)
library(igraph)
library(visNetwork)
df<-data.frame(from = c('A','A','B','B','C','C','G','A','A','B','B','C','C','G'),
to = c('B','C','A','E','F','G','C','B','C','A','E','F','G','C'),
weight = c(50,30,20,15,45,34,60,40,100,20,25,35,72,50),
group = c('1','2','2','1','1','1','2','2','1','2','1','1','2','2'))
g<-igraph::graph_from_data_frame(df)
# visual - original graph
edges<-igraph::as_data_frame(g, what = 'edges')
nodes<-data.frame(id = append(edges$from, edges$to) %>% unique())
visNetwork(nodes=nodes, edges = edges)%>%
visEdges(arrows ="to")
# aggregate bidirectional edges
g_old<-g
mx_old<-as_adjacency_matrix(g_old, attr = "weight") %>% as.matrix()
mx_new<-mx_old
u = mx_old[row(mx_old) == (col(mx_old) - 1)] # upper off-diagonal
l = mx_old[row(mx_old) == (col(mx_old) + 1)] # lower off-diagonal
mx_new[row(mx_new) == (col(mx_new) - 1)]<-ifelse((u - l) > 0, (u - l), ifelse((u - l) < 0, 0, u))
mx_new[row(mx_new) == (col(mx_new) + 1)]<-ifelse((l - u) > 0, (l - u), ifelse((l - u) < 0, 0, l))
# new graph with bidrectional edges removes
g_new = graph_from_adjacency_matrix(mx_new, weighted = TRUE)
# visual - new graph
edges<-igraph::as_data_frame(g_new, what = 'edges')
nodes<-data.frame(id = append(edges$from, edges$to) %>% unique())
visNetwork(nodes=nodes, edges = edges)%>%
visEdges(arrows ="to")