我在 igraph 中有一个简单的图表,如下所示:
> myGraph
IGRAPH 9689c03 DN-- 11 8 --
+ attr: name (v/n), types (v/n), col (v/n), degree (v/n)
+ edges from 9689c03 (vertex names):
[1] 2-> 7 2-> 8 2-> 9 2->10 3->11 3->12 3->13 3->14
虽然它是一个简单的图,但它的拓扑结构就像一个二部图。这意味着节点的类型为 0 和 1,并且每个组中的节点没有连接在一起:
> V(myGraph)$types
[1] 1 1 0 0 0 0 0 0 0 0 0
我想知道是否有一种方法/函数/方法可以将这个简单的图转换为二分图以使用一些可用的函数,例如:bipartite.projection()
?
最佳答案
二分图依赖于图对象myGraph
的type
属性,而不是types
。在这种情况下,您可以尝试使用以下代码使 myGraph
成为二分图:
> myGraph <- myGraph %>%
+ set_vertex_attr(name = "type", value = as.logical(V(.)$types))
> is_bipartite(myGraph)
[1] TRUE
您可以看到 myGraph
现在是一个二部图,并且 bipartite.projection()
为您提供
> bipartite.projection(myGraph)
$proj1
IGRAPH 8f910c3 UNW- 8 12 --
+ attr: name (v/c), types (v/l), weight (e/n)
+ edges from 8f910c3 (vertex names):
[1] 7 --8 7 --9 7 --10 8 --9 8 --10 9 --10 11--12 11--13 11--14 12--13
[11] 12--14 13--14
$proj2
IGRAPH 8f910c3 UN-- 2 0 --
+ attr: name (v/c), types (v/l)
+ edges from 8f910c3 (vertex names):
虚拟数据
df <- data.frame(
from = c(2, 2, 2, 2, 3, 3, 3, 3),
to = c(7, 8, 9, 10, 11, 12, 13, 14)
)
myGraph <- graph_from_data_frame(df) %>%
set_vertex_attr(name = "types", value = names(V(.)) %in% df$from)
关于r - igraph:如何将图转换为二分图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69036539/