【问题标题】:igraph: How to convert a graph to a bipartite graph?igraph:如何将图转换为二分图?
【发布时间】:2021-11-01 07:12:37
【问题描述】:

我在 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()

【问题讨论】:

  • 除了显示 igraph 对象的打印表示之外,您还可以编辑您的问题以包含 dput(myGraph) 的结果。

标签: r graph-theory igraph bipartite


【解决方案1】:

二分图依赖于图对象myGraphtype 属性,而不是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)

【讨论】:

  • Dang,我不知道我做了什么,但我在我的(现已删除)答案中用type 重新创建了这一切,并且不知何故错过了我不必做任何进一步的事情。这显然是一个简单的修复!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
  • 2020-01-14
相关资源
最近更新 更多