轉:R igraph社團發現演算法測試(全)
存下來,實踐下。
R相關的介紹:http://igraph.wikidot.com/community-detection-in-r
原文地址:http://blog.sina.com.cn/s/blog_153999aac0102vzk5.html
igraph是複雜網路分析的一個強有力的工具,純C語言寫的開源工具庫,也提供了關於R和python的一些介面。裡面也包含了許多模組,其中社團發現的演算法就包括以下幾個:
(1) edge.betweenness.community [Newman and Girvan, 2004]
(2) fastgreedy.community [Clauset et al., 2004] (modularity optimization method)
(3) label.propagation.community [Raghavan et al., 2007]
(4) leading.eigenvector.community [Newman, 2006]
(5) multilevel.community [Blondel et al., 2008] (the Louvain method)
(6) optimal.community [Brandes et al., 2008]
(7) spinglass.community [Reichardt and Bornholdt, 2006]
(8) walktrap.community [Pons and Latapy, 2005]
(9) infomap.community [Rosvall and Bergstrom, 2008]
這些演算法的具體介紹請參照論文和相關資料,此次不進行介紹。
鑑於社團發現的一些重要特性,於是對該工具的如上演算法進行測試,測試工具為R語言 + RStudio。測試資料為公開的karate資料。
其測試截圖如下:
edge betweenness的社團發現結果fast greedy社團發現演算法執行結果leading.eigenvector的社團發現結果標籤傳播演算法(LPA)的社團發現演算法結果multi level的社團發現演算法結果optimal.community社團發現演算法結果spinglass.community的社團發現演算法結果library(igraph)
layout(matrix(c(1,2,3,
4,5,6),nr = 2, byrow = T))
##########################################################
#經典的“Zachary 空手道俱樂部”(Zachary‟s Karate Club)社
#會網路[26]。20 世紀70 年代,Zachary 用了兩年的時間觀察美國一所大學的空手道
#俱樂部內部成員間關係網路。在Zachary 調查的過程中,該俱樂部的主管與校長因
#為是否提高俱樂部收費的問題產生了爭執,導致該俱樂部最終分裂成了兩個分別
#以主管與校長為核心的小俱樂部。就模組度優化而言,當前學者們已經廣泛認為,
#該網路的最優劃分是模組度Q=0.419 的4 個社團劃分。
##########################################################
g <- graph.famous("Zachary")
##
#• Community structure in social and biological networks
# M. Girvan and M. E. J. Newman
#• New to version 0.6: FALSE
#• Directed edges: TRUE
#• Weighted edges: TRUE
#• Handles multiple components: TRUE
#• Runtime: |V||E|^2 ~稀疏:O(N^3)
##
system.time(ec <- edge.betweenness.community(g))
print(modularity(ec))
plot(ec, g)
#• Computing communities in large networks using random walks
# Pascal Pons, Matthieu Latapy
#• New to version 0.6: FALSE
#• Directed edges: FALSE
#• Weighted edges: TRUE
#• Handles multiple components: FALSE
#• Runtime: |E||V|^2
system.time(wc <- walktrap.community(g))
print(modularity(wc))
#membership(wc)
plot(wc , g)
#• Finding community structure in networks using the eigenvectors of matrices
# MEJ Newman
# Phys Rev E 74:036104 (2006)
#• New to version 0.6: FALSE
#• Directed edges: FALSE
#• Weighted edges: FALSE
#• Handles multiple components: TRUE
#• Runtime: c|V|^2 + |E| ~N(N^2)
system.time(lec <-leading.eigenvector.community(g))
print(modularity(lec))
plot(lec,g)
#• Finding community structure in very large networks
# Aaron Clauset, M. E. J. Newman, Cristopher Moore
#• Finding Community Structure in Mega-scale Social Networks
# Ken Wakita, Toshiyuki Tsurumi
#• New to version 0.6: FALSE
#• Directed edges: FALSE
#• Weighted edges: TRUE
#• Handles multiple components: TRUE
#• Runtime: |V||E| log |V|
system.time(fc <- fastgreedy.community(g))
print(modularity(fc))
plot(fc, g)
#• Fast unfolding of communities in large networks
# Vincent D. Blondel, Jean-Loup Guillaume, Renaud Lambiotte, Etienne Lefebvre
#• New to version 0.6: TRUE
#• Directed edges: FALSE
#• Weighted edges: TRUE
#• Handles multiple components: TRUE
# Runtime: “linear” when |V| \approx |E| ~ sparse; (a quick glance at the algorithm \
# suggests this would be quadratic for fully-connected graphs)
system.time(mc <- multilevel.community(g, weights=NA))
print(modularity(mc))
plot(mc, g)
#• Near linear time algorithm to detect community structures in large-scale networks.
# Raghavan, U.N. and Albert, R. and Kumara, S.
# Phys Rev E 76, 036106. (2007)
#• New to version 0.6: TRUE
#• Directed edges: FALSE
#• Weighted edges: TRUE
#• Handles multiple components: FALSE
# Runtime: |V| + |E|
system.time(lc <- label.propagation.community(g))
print(modularity(lc))
plot(lc , g)