【发布时间】:2020-12-10 22:24:07
【问题描述】:
我正在使用 R 编程语言和“igraph”库。我试图更好地理解“两种模式”图(其中有两种类型的节点的图)的图结构。特别是,我试图了解如何“投影”两种模式”(据我所知,这些通常是“二分”)图。(https://rpubs.com/pjmurphy/317838)
例如,我创建了一个“男人”和“女人”之间的关系图。虽然这个图有两种模式(男性和女性),但我不认为这个图是二分的(因为“边”可以存在于相同类型的节点之间:
library(igraph)
# I don't think this is a bipartite graph
gender_data <- data.frame(
"men" = c("john", "kevin", "mark", "kevin", "kevin", "mark", "henry", "mark", "susan", "john", "henry", "susan", "susan", "janet", "janet", "henry", "henry", "john"),
"women" = c("janet", "janet", "sarah", "lucy", "lucy", "susan", "janet", "susan", "lucy", "kevin", "lucy", "janet", "kevin", "mark", "lucy", "sarah", "mark", "mark")
)
#create directed graph
graph <- graph.data.frame(gender_data, directed=F)
graph <- simplify(graph)
V(graph)["john"]$color<-"red"
V(graph)["kevin"]$color<-"red"
V(graph)["mark"]$color<-"red"
V(graph)["janet"]$color<-"blue"
V(graph)["sarah"]$color<-"blue"
V(graph)["lucy"]$color<-"blue"
V(graph)["henry"]$color<-"red"
V(graph)["susan"]$color<-"blue"
plot(graph)
我读到理解二分图的更好方法是通过“演员和电影”。不同的演员可以在同一部电影中,而一个演员可以在不同的电影中——但这样的演员不能与自己共享优势,而电影也不能与自己共享优势。这是我对这种网络的解释:
film_data <- data.frame(
"movie" = c("movie_1", "movie_1", "movie_1", "movie_2", "movie_2", "movie_2", "movie_3", "movie_3", "movie_3", "movie_4", "movie_4", "movie_4", "movie_4", "movie_5", "movie_5", "movie_5", "movie_6", "movie_6"),
"actor" = c("actor_1", "actor_2", "actor_3", "actor_2", "actor_3", "actor_4", "actor_1", "actor_5", "actor_6", "actor_2", "actor_7", "actor_1", "actor_8", "actor_5", "actor_9", "actor_3", "actor_2", "actor_8")
)
#create directed graph
graph <- graph.data.frame(film_data, directed=F)
graph <- simplify(graph)
plot(graph)
但是,(根据此处的此 stackoverflow 帖子:valued bipartite projection using R igraph),此演员图仍然不是二分的(我不明白为什么):
is.bipartite(graph)
[1] FALSE
根据同样的stackoverflow帖子,actor图仍然可以转换为二分图(我不明白刚刚发生了什么):
V(graph)$type <- V(graph)$name %in% film_data[,1]
is.bipartite(graph)
[1] TRUE
从这里,可以进行“投影”两个独立图形的投影:
proj<-bipartite.projection(graph, V(graph)$type,multiplicity = TRUE)
proj
$proj1
IGRAPH b5bc5ca UNW- 9 16 --
+ attr: name (v/c), weight (e/n)
+ edges from b5bc5ca (vertex names):
[1] actor_1--actor_2 actor_1--actor_3 actor_1--actor_5 actor_1--actor_6 actor_1--actor_7 actor_1--actor_8 actor_2--actor_3 actor_2--actor_4
[9] actor_2--actor_7 actor_2--actor_8 actor_3--actor_4 actor_3--actor_5 actor_3--actor_9 actor_5--actor_6 actor_5--actor_9 actor_7--actor_8
$proj2
IGRAPH b5bc5ca UNW- 6 11 --
+ attr: name (v/c), weight (e/n)
+ edges from b5bc5ca (vertex names):
[1] movie_1--movie_3 movie_1--movie_4 movie_1--movie_2 movie_1--movie_6 movie_1--movie_5 movie_2--movie_4 movie_2--movie_6 movie_2--movie_5
[9] movie_3--movie_4 movie_3--movie_5 movie_4--movie_6
最后,可以绘制两个投影:
plot(proj$proj1)
plot(proj$proj2)
我的问题:
-
为什么原来的演员-电影图表不是“二分”的?毕竟,它是无向和循环的。
-
为什么
V(graph)$type <- V(graph)$name %in% film_data[,1]线会将演员-电影图转换为二分图? -
有什么原因
is.bipartite(proj$proj1) 1 错误
is.bipartite(proj$proj2) 1 错误
-
这条线
proj<-bipartite.projection(graph, V(graph)$type,multiplicity = TRUE)如何“工作”?在原始演员-电影图中,我专门输入了数据,这样两部电影或两个演员之间就没有直接关系。例如,在“proj2”中,“movie_1”和“movie_2”之间有一条边——这是怎么发生的,为什么会这样?在我的原始数据中,movie_1和movie_2之间没有这种直接关系? -
假设 actor_1、actor_2、actor_3、actor_4 是男性,actor_5、actor_6、actor_7、actor_8、actor_9 是女性。现在有没有办法进行3个预测?男演员投影,女演员投影,电影投影?
谢谢
【问题讨论】:
标签: r graph data-visualization nodes