[Python] networkx entry

networkx is a third-party package for python, which can easily call the calculation of various graph algorithms.
The visualization of the graph can be realized by calling the python drawing package matplotlib.

1. Installation


Just sort out the installation method of python third-party packages.

setuptools

Using the setuptools package, we were able to manually download and install third-party packages.
How to use:
Download the third-party package source file and execute it in the setup.py directory
python XX install


easy_install


The easy_install tool automatically searches, downloads, and invokes setuptools to install based on the name of the third-party package.

How to use:

Download the ez_setup.py file from the Internet and execute

python ez_setup.py installs the setuptools tool.

implement

easy_install SQLObject

Install third-party packages


pip


Similar to easy_install, one-click download according to the name, call setuptools to install. pip can be seen as an upgraded version of easy_install.

How to use:


Download the get-pip.py file first

Run python get-pip.py to automatically install packages including setuptools

use:

pip install simplejson --install package 
pip install --upgrade simplejson --upgrade package
pip uninstall simplejson --uninstall package



Using setuptools to download and install by yourself, you often encounter various problems such as package authenticity, no compiler, etc., which cause the installation to fail. So using easy_install or pip is a more convenient choice.



install networkx


安装networkx之前要安装画图工具matplotlib,以及矩阵运算工具numpy,于是我们执行:

pip install numpy
pip install matplotlib
pip install networkx

安装完成。





2.使用



networkx的英文版说明可以在其官方网站上下载得到,写的简单易懂。




这里记录下一些简单的建图方法。

在networkx中,图以对象的形式提供借口。

在图对象中,还有点,边等也是以对象形式提供,画图则调用matplotlib的函数完成。



建图


networkx可以建立简单无向图graph,有向图digraph,可重复边的multi-graph。

import networkx as nx
G=nx.Graph()
D=nx.DiGraph()
M=nx.MultiGraph()


这里的点可以是任意可区分的对象(hashable),比如数字,字符串,对象等。

G.add_node(1)
G.add_node('first_node')

这里用一个对象多为key来唯一区别一个点

我们还能够用一个列表来批量加入点

G.add_nodes_from([1,2,3])

还可以用一个图对象作为点,嵌入到其他图中

G.add_node(D) #这里D作为一个点的key

或者把一个图的所有点赋予另一个图

G.add_nodes_from(D) #这里返回D的所有点,赋予G

与加入相同的传递方法,我们也可以删除点

G.remove_node(1)
G.remove_nodes_from([1,2,3])



这里的边可以使用两个单独的对象作为输入

G.add_edge(1,2) #表示1,2之间有一条边。

如果不存在点1,2,则会自动加入点集合。

或者以元组的形式作为输入

e=(1,2)
G.add_edge(*e)

这里的*代表吧元组解包(unpack),当作一个个的值扔到函数中去。

如果不解包,等价于

G.add_edge(e)=G.add_edge((1,2))与参数传递的方式不符。

类似的,我们还可以使用包含元组的列表来传递参数

G.add_edges_from([(1,2),(2,3)])

我们还可以报一个图的边赋予另一个图

G.add_edges_from(H)

删除

G.remove_edge(1,2)
G.remove_edges_from([(1,2),(2,3)])


访问


node_list = G.nodes()
edge_list = G.edges()

可以返回包含点与边的列表

node = G.node[‘first_node’]

如上根据key返回点

edge = G.edge['first_node']['second_node']

同样的方法,返回两个key之间的边

属性

我们可以给图,点,边赋予各种属性,最简单的就是权值属性
G.add_node(1,time='5pm')#在添加时跟上属性G.add_nodes_from([1,2,3],time='5pm')#批量添加点是,跟上统一的属性G.add_nodes_from([(3,{'time':'5pm'}), (4,{'time':'4pm'})])#或者写成元组列表[(key,dict),(key,dict)]的形式G.node[1]['time']#根据字典key访问属性值。#给边添加属性也类似G.add_edge(1,2,time='3am')G.add_edges_from([(1,2,{'time'='5pm'}),(2,3,{'time'=3am})])#批量赋予属性G.edge[1][2][‘time’]#访问#我们还可以使用特定的函数批量返回属性,如time = nx.get_edge_attributes(G,'time')#返回得到以元组为key,time属性为值得一个字典time[(1,2)]

图算法

nx.connected_components(G)#返回列表形式的G的全连通分量,每个分量一个子列表
类似的,networkx还提供各种图算法,具体可见官方文档。

画图

nx.draw(G) #画出图Gmatplotlib.show() #显示出来

我们使用连通分量算法,并画出一个简单的图如下:

networkx的默认属性效果并不好, 我们根据文档提供的借口设置合适的属性,才能得到合适的图。
networkx提供图的点位置,大小,颜色,形状,边的颜色,类型等属性可设置。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325025784&siteId=291194637