简体   繁体   English

图中的一组指定节点是否连接?

[英]Are a specified set of nodes in a graph connected?

I'm new to using graphs in R, and haven't been able to find a solution to this problem.我是在 R 中使用图表的新手,并且无法找到解决此问题的方法。 Take a simple graph做一个简单的图表

library(igraph)
df <- data.frame(a = c("a","a","a","b","c","f"),
                 b = c("b","c","e","d","d","e"))
my.graph <- graph.data.frame(df, directed = FALSE)
plot(my.graph)

What I want is to be able to define a function which takes the graph and a set of nodes as arguments and for a logical output as to whether those nodes are connected.我想要的是能够定义一个函数,该函数将图形和一组节点作为参数,以及关于这些节点是否连接的逻辑输出。 For example例如

my.function(my.graph, c("b", "a", "c"))
# TRUE
my.function(my.graph, c("b", "a", "e"))
# TRUE
my.function(my.graph, c("b", "a", "f"))
# FALSE

Any help appreciated任何帮助表示赞赏

You are just asking if the induced subgraph is connected, so compute the subgraph and test if it is connected.您只是在询问诱导子图是否已连接,因此计算子图并测试它是否已连接。

my.function = function(g, nodes) {
    is_connected(subgraph(g, nodes)) }

my.function(my.graph, c("b", "a", "c"))
[1] TRUE
my.function(my.graph, c("b", "a", "e"))
[1] TRUE
my.function(my.graph, c("b", "a", "f"))
[1] FALSE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM