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()

Logo

GitCode AI社区是一款由 GitCode 团队打造的智能助手,AI大模型社区、提供国内外头部大模型及数据集服务。

更多推荐