当节点具有圆形连接时,Igraph会保持权重最大的边缘

时间:2018-10-25 16:45:37

标签: r igraph

我有一个更大的图,如下面链接中的图所示,其中某些节点的入站和出站边具有不同的权重。恐怕我正在努力寻找一种方法来识别要去除的边缘。

任何帮助将不胜感激

library(igraph)
set.seed(100)

g <- graph_from_data_frame(data.frame(v_1 = c(1,2,1,3, 1, 3), v_2 = c(2,1,3,1, 4, 4)), directed = TRUE)

E(g)$weights <- c(1, 3, 3, 1, 1, 3)

plot.igraph(g, edge.width=E(g)$weights, edge.curved = 0.3)

Graph that I have

Graph that I want to get to

1 个答案:

答案 0 :(得分:1)

回答您自己的问题有点儿厚脸皮,但是非常感谢ANG的评论,这使我的大脑工作了。

曾经为此苦苦挣扎了一段时间,但这是一种适用于我的情况的解决方案。希望它可以帮助遇到类似问题的人。

library(igraph)
set.seed(100)

df <- data.frame(v_1 = c(1,2,1,3, 1, 3), v_2 = c(2,1,3,1, 4, 4))
df$weights <- c(1, 3, 3, 1, 1, 3)

#Order the vertex IDs so that an edge between 1-2 and 2-1 both become 1-2, then collapse them together so they're in 1 column that can be used to identify the duplicate.
df$ordered_verticies <- apply(df[1:2], 1, FUN = function(x) paste0(sort(x), collapse = ""))

#order by vertice, then negative weight for a decending order
df <- df[order(df$ordered_verticies, -df$weights),]

#removing duplications now gets rid of the the second (and lower weighted) instance of an edge
df <- df[!duplicated(df$ordered_verticies),]

#new graph
g <- graph_from_data_frame(df[1:2], directed = TRUE)
E(g)$weights <- df[,3]
plot.igraph(g, edge.width=E(g)$weights, edge.curved = 0.3)