Python学习igraph

来源:互联网 发布:剑网三男捏脸数据 编辑:程序博客网 时间:2024/05/18 01:10
总结:学习一种全新的语言看官方的文档是真的很有帮助,这次我的大部分python代码的完成都是靠着igraph官方的API文档。


官方API:http://pythonhosted.org/python-igraph/igraph.Graph-class.html


首先导入igraph工具包 from igraph import *(python语法)
创建图 g = Graph.Tree(127, 2)
运行fastunfloding算法 g.community_multilevel("weight",True)
绘制graph plot(g)
代码参考 fastunfloding.txt


给图添加边并且添加权值:g.add_edges([(0,1)])
g.es[1]["weight"]=2
其中g.es[1]代表的是第一条边


最开始我的图是从数据库导出,即(点,点,权)这种模式所以就需要从txt文档中读边并且建立图
打开文件:f = open('D:\\test1\\test.txt','r')
读文件中一行:line =  f.readline() 注:line是字符串的形式
取出文件一行的整数并且加入到数组尾部 result.append(map(int,line.split(',')))




读图完毕后就需要将图放到fastunfolding算法中去了
a = g.community_multilevel("weight",True)
官方API上说 当g.community_multilevel最后一个参数为True的时候
a返回的是一组list,list的存放位置在(a[0])处,a[0][s]代表着
community为s的所有顶点


注:a list of VertexClustering objects, one corresponding to each level (if return_levels is True)


取list长度:len(a[0])


将输入的图存成gml格式:g.write_graphmlz(self, f, compresslevel=9)
读gml格式的图:g = Graph.Read_GraphMLz("D:\\test1\\final_graph.gml")
1 0
原创粉丝点击