R:有向图与无向图(参数被忽略,潜在的警告信息)

R: Directed Graphs vs Undirected Graphs (arguments being ignored, potential warning message)

我正在使用 R 编程语言和“igraph”库。

假设我有一些数据,我将其制成无向图并执行社区检测(图聚类)

# undirected graph 

library(igraph)

my_data <- data.frame(

"node_a" = c("Store_A", "Store_A", "Store_A", "Store_B", "Store_B", "Store_C", "Store_C", "Store_C", "Store_C", "Store_C", "Store_B", "Store_C", "customer_4", "customer_9", "customer_1"),
"node_b" = c("customer_1", "customer_2", "customer_3", "customer_3", "customer_4", "customer_2", "customer_5", "customer_6", "customer_7", "Store_B", "customer_9","customer_9", "customer_5", "customer_4", "customer_1")
)

node_size <- data.frame(col = unique(unlist(my_data)))
node_size$size <- c("50","50","50","15","15","15","15","15","15","15","15")

graph <- graph.data.frame(my_data, directed=F)
graph <- simplify(graph)

cluster = cluster_edge_betweenness(graph)

plot(cluster,graph)

这似乎工作正常。

但是:当我使用相同的数据,制作有向图然后运行社区检测时,我得到以下警告消息(但代码仍然运行s):

#directed graph (warning messages)

图书馆(igraph)

my_data <- data.frame(

"node_a" = c("Store_A", "Store_A", "Store_A", "Store_B", "Store_B", "Store_C", "Store_C", "Store_C", "Store_C", "Store_C", "Store_B", "Store_C", "customer_4", "customer_9", "customer_1"),
"node_b" = c("customer_1", "customer_2", "customer_3", "customer_3", "customer_4", "customer_2", "customer_5", "customer_6", "customer_7", "Store_B", "customer_9","customer_9", "customer_5", "customer_4", "customer_1")
)

node_size <- data.frame(col = unique(unlist(my_data)))
node_size$size <- c("50","50","50","15","15","15","15","15","15","15","15")

graph <- graph.data.frame(my_data, directed=T)
graph <- simplify(graph)

cluster = cluster_edge_betweenness(graph)

#warning message produced by R:
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.
Modularity is implemented for undirected graphs only.

plot(cluster,graph)

由于两段代码产生不同的视觉输出 - 我不确定此语句 graph <- graph.data.frame(my_data, directed=T) 是否被计算机忽略。

有谁知道这条语句是否被忽略了?

谢谢

不,它没有被忽略。结果图是有向的,您可以从警告消息中看到这一点,如果您打印 graph 变量:

> graph
IGRAPH ad7270b DN-- 11 15 -- 

其中 DN 表示有向和命名图。最后,您可以在绘图时从箭头中看到它。您可以使用 is.directed 函数进行检查。

边介数算法计算出的社区结构是不同的,因为它对边的方向很敏感。如果你 运行 它带有 directed = FALSE 参数,那么你会得到与有向图相同的社区:

cluster <- cluster_edge_betweenness(graph, directed = FALSE)

警告消息抱怨的不是社区检测算法,而是计算具有给定社区结构的图的模块化的方法。 edge_betweenness 和其他一些社区检测方法给出了层次聚类结果。为了获得单个成员向量,igraph 计算聚类的每个级别的模块化,并选择模块化得分最高的级别。您可以在 cluster$merges 中找到层次聚类,在 cluster$modularity 中找到每个级别的模块化。如您所见 here in the code, after issuing the warning message, the calculation continues the same way for directed graphs, I guess just ignoring directions. Also this issue recently has been addressed and soon modularity calculation for directed graphs will be available in igraph.