SNAP复杂网络处理平台

版权声明:本文为原创博客,未经允许,请勿转载。 https://blog.csdn.net/u013095333/article/details/83831906

SNAP复杂网络处理平台

简介:这是斯坦福提供的,用于处理复杂网络的平台库,核心是使用C++编写的,效率相对很高,提供了C++和Python的接口,源代码可以在snap网站下载,也可以在github上下载。

C++

编译,安装与测试

需要安装两个相关的软件,用于绘图,GnuplotGraphviz这两个的安装都很简单,在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这个平台一直以来没有发现,这应该是非常优秀的一个平台,速度极快,有时间可以好好研究一下。

猜你喜欢

转载自blog.csdn.net/u013095333/article/details/83831906