SNAP学习笔记

SNAP学习

Stanford Network Analysis Project(SNAP)
官网地址
Snap.py - SNAP for Python

是什么:

去官网看

怎么用

安装

pip install snap-stanford

官网给出了许多样例, 最好都读一下, 看看怎么使用

介绍

snap.py supports graphs and networks

  • Graphs describe topologies. That is nodes with unique integer ids and directed/undirected/multiple edges between the nodes of the graph.
  • Networks are graphs with data on nodes and/or edges of the network.
  • Data types that reside on nodes and edges are simply passed as template parameters which provides a very fast and convenient way to implement various kinds of networks with rich data on nodes and edges.
    Graph types in SNAP
TUNGraph: undirected graph (single edge between an unordered pair of nodes)        无向图
TNGraph: directed graph (single directed edge between an ordered pair of nodes)    有向图

Network types in SNAP

TNEANet: directed multigraph with attributes for nodes and edges

创建使用有向图的样例

# create a graph PNGraph
G1 = snap.TNGraph.New()
G1.AddNode(1)
G1.AddNode(5)
G1.AddNode(32)
G1.AddEdge(1,5)
G1.AddEdge(5,1)
G1.AddEdge(5,32)

迭代器的使用

# create a directed random graph on 100 nodes and 1k edges
G2 = snap.GenRndGnm(snap.PNGraph, 100, 1000)
# traverse the nodes
for NI in G2.Nodes():
    print("node id %d with out-degree %d and in-degree %d" % (
        NI.GetId(), NI.GetOutDeg(), NI.GetInDeg()))
# traverse the edges
for EI in G2.Edges():
    print("edge (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId()))
# traverse the edges by nodes
for NI in G2.Nodes():
    for Id in NI.GetOutEdges():
        print("edge (%d %d)" % (NI.GetId(), Id))

node提供的函数

GetId(): return node id
GetOutDeg(): return out-degree of a node
GetInDeg(): return in-degree of a node
GetOutNId(e): return node id of the endpoint of e-th out-edge
GetInNId(e): return node id of the endpoint of e-th in-edge
IsOutNId(int NId): do we point to node id n
IsInNId(n): does node id n point to us
IsNbrNId(n): is node n our neighbor

更多关于Graph and Network类的详细介绍

保存和加载网络

# generate a network using Forest Fire model
G3 = snap.GenForestFire(1000, 0.35, 0.35)
# save and load binary
FOut = snap.TFOut("test.graph")
G3.Save(FOut)
FOut.Flush()
FIn = snap.TFIn("test.graph")
G4 = snap.TNGraph.Load(FIn)
# save and load from a text file
snap.SaveEdgeList(G4, "test.txt", "Save as tab-separated list of edges")
G5 = snap.LoadEdgeList(snap.PNGraph, "test.txt", 0, 1)

操纵Graph and Network

# generate a network using Forest Fire model
G6 = snap.GenForestFire(1000, 0.35, 0.35)
# convert to undirected graph
G7 = snap.ConvertGraph(snap.PUNGraph,G6)
WccG = snap.GetMxWcc(G6)
# get a subgraph induced on nodes {0,1,2,3,4,5}
SubG = snap.GetSubGraph(G6, snap.TIntV.GetV(0,1,2,3,4))
# get 3-core of G
Core3 = snap.GetKCore(G6, 3)
# delete nodes of out degree 10 and in degree 5
snap.DelDegKNodes(G6, 10, 5)

计算网络结构特性

# generate a Preferential Attachment graph on 1000 nodes and node out degree of 3
G8 = snap.GenPrefAttach(1000, 3)
# vector of pairs of integers (size, count)
CntV = snap.TIntPrV()
# get distribution of connected components (component size, count)
snap.GetWccSzCnt(G8, CntV)
# get degree distribution pairs (degree, count)
snap.GetOutDegCnt(G8, CntV)
# vector of floats
EigV = snap.TFltV()
# get first eigenvector of graph adjacency matrix
snap.GetEigVec(G8, EigV)
# get diameter of G8
snap.GetBfsFullDiam(G8, 100)
# count the number of triads in G8, get the clustering coefficient of G8
snap.GetTriads(G8)
snap.GetClustCf(G8)

Snap.py操作指南

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值