R:修改图 [英] R: Modifying Graphs

查看:46
本文介绍了R:修改图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处发布了对另一个stackoverflow帖子的评论/回复:

  V(graph)$ type<-V(graph)$ name%in%film_data [,1]is.bipartite(图)[1]是 

不过,您可以故意破坏"通过在两个角色(actor_2和actor_3)之间添加链接来创建该图,从而使该图不再是二分法的:

  film_data<-data.frame(电影"= 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"或"actie"演员"= 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"或"act_8"))#创建有向图图表<-graph.data.frame(film_data,有向= F)图<-简化(图)情节(图) 

但是R仍然会说该图是二部图:

  V(graph)$ type<-V(graph)$ name%in%film_data [,1]is.bipartite(图)[1]是 

您可以通过在两部电影之间添加额外的链接来进一步破坏该图:

  film_data<-data.frame(电影"= 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"或"actie"movie_1"),演员"= 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"或"act_8"movie_2"))#创建有向图图表<-graph.data.frame(film_data,有向= F)图<-简化(图)情节(图) 

但是R仍将其称为二分的:

  V(graph)$ type<-V(graph)$ name%in%film_data [,1]is.bipartite(图)[1]是 

有人知道我做错了什么吗?最后两个图实际上是二部图吗?还是我错误地使用了代码?

只需澄清一下:所有无向图都是循环的吗?如果您有一个仅包含一种类型节点的无向​​图,它必然是二分的吗?

谢谢

解决方案

实际上,您创建的图形不是两部分的:角色"部分具有相邻的顶点.

函数 is.bipartite()(或其名称)极易引起误解.它仅告诉您图形是否具有称为type的必需顶点属性.它不会检查使图成为二分图的其他特征.来源:?is.bipartite

I posted a comment/reply to another stackoverflow post over here : R: Understanding Graph relating to graphs in R.

If you create some data corresponding to movies and actors (in which movies can not be connected to other movies directly, and actors can not be connected to other actors directly), you write some R code to check if your graph is bipartite:

library(igraph)
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)

V(graph)$type <- V(graph)$name %in% film_data[,1]
is.bipartite(graph)
[1] TRUE

However, you can "purposefully sabotage" this graph by adding a link between two actors (actor_2 and actor_3) so that the graph is no longer bipartite:

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_2"),
    "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", "actor_3")
)

#create directed graph 
graph <- graph.data.frame(film_data, directed=F)
graph <- simplify(graph)
plot(graph)

But R will still say that this graph is bipartite:

V(graph)$type <- V(graph)$name %in% film_data[,1]
 is.bipartite(graph)
[1] TRUE

You can further sabotage this graph by adding an extra link between two movies:

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_2", "movie_1"),
    "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", "actor_3", "movie_2")
)

#create directed graph 
graph <- graph.data.frame(film_data, directed=F)
graph <- simplify(graph)
plot(graph)

But R will still call it bipartite:

V(graph)$type <- V(graph)$name %in% film_data[,1]
is.bipartite(graph)
[1] TRUE

Does anyone know if I am doing something wrong? Are these last two graphs actually bipartite? Or am I applying the code incorrectly?

Just to clarify: Are all undirected graphs cyclic? If you have a undirected graph with just one type of node, it it necessarily bipartite?

Thanks

解决方案

Indeed, the graph you created is not bipartite: the part 'actors' has adjacent vertices.

The function is.bipartite() (or its name) is highly misleading. It only tells you if the graph has the required vertex attribute called type. It doesn't check the other characteristics of what makes a graph bipartite. Source: ?is.bipartite

这篇关于R:修改图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆