按照边列表的形式读入文件,生成无向图。
# -*- coding: utf-8 -*- #设置中文注释
import igraph as ig
#按照边列表的形式读入文件,生成无向图
g = ig.Graph.Read_Edgelist("com-youtube.ungraph.txt", directed=False)
ecount = g.ecount()#统计边的数目
vcount = g.vcount()#统计节点数目
maxdegree = g.maxdegree()#最大度值
degree()函数生成度序列
import igraph as ig
#按照边列表的形式读入文件,生成无向图
g = ig.Graph.Read_Edgelist("com-youtube.ungraph.txt", directed=False)
degrees = g.degree()#列表
#序列化度序列
# f = open("youtube_degree.txt", 'w+')
# for d in degrees:
# print >> f,"%d" % d
类似效果:
29
256
12
2753
192
get_edgelist()函数生成边列表文件,该函数可以将内存中的网络整理成边列表形式
#序列化边信息
edgelist = g.get_edgelist()
f = open("youtube_edgelist.txt", 'w+')
for edge in edgelist:
print >> f,"%d\t%d" % edge
类似效果:
1 2
1 3
1 4
1 5
1 6
1 7
1 8
给定节点编号,求得其所有的邻居节点
neighbors = g.neighbors(vertex=1)
f = open("youtube_data.txt", 'w+')
try:
for neighbor in neighbors:
print >> f,"%d" % neighbor
finally:
f.close()
类似结果:
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 376, 1219, 268635, 317880, 665255, 665256, 665257, 665258]
按照节点编号顺序依次列出所有的邻居节点,其中这列节点的标号顺序可以看做网络中边的编号。
import igraph as ig
g = ig.Graph.Read_Edgelist("com-youtube.ungraph.txt", directed=False)
# ig.Graph.neighborhood()
neighbors = g.neighborhood()
# print type(neighbors)
f = open("youtube_data.txt", 'a+')
try:
for list in neighbors:
for neighbor in list[1:]:#第一个元素是当前节点,其余元素是该节点的全部邻居节点
print >> f,"%d" % neighbor
finally:
f.close()
数据连接:
http://snap.stanford.edu/data/bigdata/communities/com-youtube.ungraph.txt.gz