SNAP複雜網路處理平臺
阿新 • • 發佈:2018-11-14
SNAP複雜網路處理平臺
簡介:這是斯坦福提供的,用於處理複雜網路的平臺庫,核心是使用C++編寫的,效率相對很高,提供了C++和Python的介面,原始碼可以在snap網站下載,也可以在github上下載。
C++
編譯,安裝與測試
需要安裝兩個相關的軟體,用於繪圖,Gnuplot
和Graphviz
這兩個的安裝都很簡單,在ubuntu上只需要一行安裝命令即可,具體的可百度。
在ubuntu環境下,在snap根目錄下執行make all
,執行完成之後,進入到example
資料夾下,可以使用已經提供的介面,例如,使用下列命令生成一個小世界網路。
cd examples/graphgen ./graphgen -g:w -n:1000 -k:4 -p:0.1 -o:smallworld.txt
生成指定度分佈的冪律圖
檢視檔案,發現只需要提供nodes和exponent即可生成,速度極快,可能採用的是文章《Efficient and Simple Generation of Random Simple Connected Graphs with Prescribed Degree Sequence》類似的方法,使用命令為:
./graphgen -g:p -n:100000 -p:1.5 -o:t1.5.txt
-n
表示nodes數
-p
表示exponent(指數)
-o
表示輸出檔案
檢視原始碼,在examples
中的graphgen.cpp
中,可以使用已經提供的生成圖方式。生成冪律圖對應以下函式。
G = TSnap::GenRndPowerLaw(N, P, true);
經測試,生成的圖符合分佈!
附graphgen.cpp檔案原始碼
#include "stdafx.h"
int main(int argc, char* argv[]) {
Env = TEnv(argc, argv, TNotify::StdNotify);
Env.PrepArgs(TStr::Fmt("Graph generators. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm()));
TExeTm ExeTm;
Try
const TStr OutFNm = Env.GetIfArgPrefixStr("-o:", "output.txt", "Output graph filename");
const TStr Plot = Env.GetIfArgPrefixStr("-g:", "e", "Which generator to use:"
"\n\tf: Complete graph. Required parameters: n (number of nodes)"
"\n\ts: Star graph. Required parameters: n (number of nodes)"
"\n\t2: 2D Grid. Required parameters: n (number of rows), m (number of columns)"
"\n\te: Erdos-Renyi (G_nm). Required parameters: n (number of nodes), m (number of edges)"
"\n\tk: Random k-regular graph. Required parameters: n (number of nodes), k (degree of every node)"
"\n\tb: Albert-Barabasi Preferential Attachment. Required parameters: n (number of nodes), k (edges created by each new node)"
"\n\tp: Random Power-Law graph. Required parameters: n (number of nodes), p (power-law degree exponent)"
"\n\tc: Copying model by Kleinberg et al. Required parameters: n (number of nodes), p (copying probability Beta)"
"\n\tw: Small-world model. Required parameters: n (number of nodes), k (each node is connected to k nearest neighbors in ring topology), p (rewiring probability)\n"
);
const int N = Env.GetIfArgPrefixInt("-n:", 1000, "Number of nodes");
const int M = Env.GetIfArgPrefixInt("-m:", 5000, "Number of edges");
const double P = Env.GetIfArgPrefixFlt("-p:", 0.1, "Probability/Degree-exponent");
const int K = Env.GetIfArgPrefixInt("-k:", 3, "Degree");
if (Env.IsEndOfRun()) { return 0; }
TExeTm ExeTm;
TInt::Rnd.PutSeed(0); // initialize random seed
printf("Generating...\n");
PUNGraph G;
TStr DescStr;
if (Plot == "f") {
G = TSnap::GenFull<PUNGraph>(N);
DescStr = TStr::Fmt("Undirected complete graph.");
} else
if (Plot == "s") {
G = TSnap::GenStar<PUNGraph>(N, false);
DescStr = TStr::Fmt("Undirected star graph (1 center node connected to all other nodes).");
} else
if (Plot == "2") {
G = TSnap::GenGrid<PUNGraph>(N, M, false);
DescStr = TStr::Fmt("Undirected 2D grid of %d rows and %d columns.", N, M);
} else
if (Plot == "e") {
G = TSnap::GenRndGnm<PUNGraph>(N, M, false);
DescStr = TStr::Fmt("Undirected Erdos-Renyi random graph.");
} else
if (Plot == "k") {
G = TSnap::GenRndDegK(N, K);
DescStr = TStr::Fmt("Undirected k-regular random graph (every node has degree K).");
} else
if (Plot == "b") {
G = TSnap::GenPrefAttach(N, K);
DescStr = TStr::Fmt("Undirected Albert-Barabasi Preferential Attachment graph (each new node creades k preferentially attached edges).");
} else
if (Plot == "p") {
G = TSnap::GenRndPowerLaw(N, P, true);
DescStr = TStr::Fmt("Random Graph with Power-Law degree distribution with exponent P.");
} else
if (Plot == "c") {
G = TSnap::ConvertGraph<PUNGraph>(TSnap::GenCopyModel(N, P));
DescStr = TStr::Fmt("Copying model by Kleinberg et al. Node u comes, selects a random v, and with prob P it links to v, with 1-P links u links to neighbor of v. Power-law degree slope is 1/(1-P).");
} else
if (Plot == "w") {
G = TSnap::GenSmallWorld(N, K, P);
DescStr = TStr::Fmt("Watts-Strogatz Small-world model. Every node links to K other nodes.");
}
printf("done.\n");
TSnap::SaveEdgeList(G, OutFNm, DescStr);
Catch
printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
return 0;
}
Python
與C++僅介面的呼叫形式不同,核心實現使用的是C++
重點總結
SNAP這個平臺一直以來沒有發現,這應該是非常優秀的一個平臺,速度極快,有時間可以好好研究一下。