igraph 的联合功能问题
Issue with union function of igraph
我尝试在 2 个图上使用 union 函数,其中引入了同一图 G 的子图(因此它们之间没有冲突)。
我的问题是原始图(和子图)是加权的,另一方面这些子图的并集不是。
这是我创建子图的代码(权重可以采用 {1, 2, 3, 4, 5} 中的值:
weights <- E(g)$weight
graphs <- list()
for (r in seq(5)) {
graphs[[r]] <- delete.edges(g, E(g)[weights!=r])
}
reduced_graphs <- list()
x <- numeric(5)
y <- numeric(5)
for (r in seq(5)) {
reduced_graphs[[r]] <- delete.vertices(graphs[[r]], V(graphs[[r]])[degree(graphs[[r]])<=-1])
y[r] <- length(get_users(reduced_graphs[[r]]))
reduced_graphs[[r]] <- induced_subgraph(reduced_graphs[[r]], c(sample(get_users(reduced_graphs[[r]]), y[r]^sqrt(4.3/10)), get_movies(reduced_graphs[[r]])))
x[r] <- length(get_movies(reduced_graphs[[r]]))
reduced_graphs[[r]] <- induced_subgraph(reduced_graphs[[r]], c(sample(get_movies(reduced_graphs[[r]]), x[r]^(4.3/10)), get_users(reduced_graphs[[r]])))
reduced_graphs[[r]] <- delete.vertices(reduced_graphs[[r]], V(reduced_graphs[[r]])[degree(reduced_graphs[[r]])==0])
}
这是给我带来问题的代码:
reduced_graph <- igraph::union(reduced_graphs[[1]], reduced_graphs[[2]])
is_weighted(reduced_graph)
[1] FALSE
is_weighted(reduced_graphs[[1]])
[1] TRUE
is_weighted(reduced_graphs[[2]])
[1] TRUE
事实证明,生成的图形的顶点也丢失了它们的其他属性(如类型和颜色):
V(reduced_graph)$type
NULL
V(reduced_graph)$color
NULL
有谁知道如何解决这个问题?谢谢!
OS: Windows 10 亲
工作室
igraph 版本:1.2.6
也许您可以尝试以下解决方法
graph_from_data_frame(
unique(
do.call(
rbind,
lapply(reduced_graphs, get.data.frame)
)
)
)
我尝试在 2 个图上使用 union 函数,其中引入了同一图 G 的子图(因此它们之间没有冲突)。 我的问题是原始图(和子图)是加权的,另一方面这些子图的并集不是。
这是我创建子图的代码(权重可以采用 {1, 2, 3, 4, 5} 中的值:
weights <- E(g)$weight
graphs <- list()
for (r in seq(5)) {
graphs[[r]] <- delete.edges(g, E(g)[weights!=r])
}
reduced_graphs <- list()
x <- numeric(5)
y <- numeric(5)
for (r in seq(5)) {
reduced_graphs[[r]] <- delete.vertices(graphs[[r]], V(graphs[[r]])[degree(graphs[[r]])<=-1])
y[r] <- length(get_users(reduced_graphs[[r]]))
reduced_graphs[[r]] <- induced_subgraph(reduced_graphs[[r]], c(sample(get_users(reduced_graphs[[r]]), y[r]^sqrt(4.3/10)), get_movies(reduced_graphs[[r]])))
x[r] <- length(get_movies(reduced_graphs[[r]]))
reduced_graphs[[r]] <- induced_subgraph(reduced_graphs[[r]], c(sample(get_movies(reduced_graphs[[r]]), x[r]^(4.3/10)), get_users(reduced_graphs[[r]])))
reduced_graphs[[r]] <- delete.vertices(reduced_graphs[[r]], V(reduced_graphs[[r]])[degree(reduced_graphs[[r]])==0])
}
这是给我带来问题的代码:
reduced_graph <- igraph::union(reduced_graphs[[1]], reduced_graphs[[2]])
is_weighted(reduced_graph)
[1] FALSE
is_weighted(reduced_graphs[[1]])
[1] TRUE
is_weighted(reduced_graphs[[2]])
[1] TRUE
事实证明,生成的图形的顶点也丢失了它们的其他属性(如类型和颜色):
V(reduced_graph)$type
NULL
V(reduced_graph)$color
NULL
有谁知道如何解决这个问题?谢谢!
OS: Windows 10 亲 工作室 igraph 版本:1.2.6
也许您可以尝试以下解决方法
graph_from_data_frame(
unique(
do.call(
rbind,
lapply(reduced_graphs, get.data.frame)
)
)
)