Python 图论工具

networkx:

一个用Python语言开发的图论与复杂网络建模工具,

内置了经常使用的图与复杂网络分析算法,

能够方便的进行复杂网络数据分析、仿真建模等工作。


依赖工具

numpy 

pyparsing 

datautil 

matplotlib 

networkx 


採用随机图做个实验:

from random import random, choice
import networkx as nx
import matplotlib.pyplot as plt

def dist(a, b):
    (x1, y1) = a
    (x2, y2) = b
    return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5

G = nx.Graph()

points = [(random(), random()) for _ in range( 8 )]

for p1, p2 in zip(points[:-1], points[1:]):
    G.add_edge(p1, p2, weight=dist(p1, p2))


for _ in range( 8 ):
    p1, p2 = choice(points), choice(points)
    G.add_edge(p1, p2, weight=dist(p1, p2))
    
nx.draw(G)
plt.savefig( 'asd.png' )
plt.show()

技术分享



全然图做个试验:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.complete_graph(6)

nx.draw(G)
plt.savefig("asd.png")
plt.show()

技术分享


文章来自:http://www.cnblogs.com/mengfanrong/p/4350017.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3