[Python] Networkx Introductory turn

Source: Internet
Author: User

Networkx is a third-party package in Python that makes it easy to invoke the calculations of various graph algorithms. The visualization of graphs can be realized by calling Python drawing package matplotlib.
1. Installation
Just sort it out. How to install Python third-party packages. Setuptools using the Setuptools package, we are able to manually download and install third party packages. Usage: Download the third-party package source file and execute it in the setup.py directory

python XX install

Easy_install
Easy_install tool, automatically according to the name of the third-party package, one-click Internet search, download and call Setuptools installation.
How to use:
Download ez_setup.py file online, execute
Python ez_setup.py installs the Setuptools tool.
Perform
easy_install SQLObject
Install a third-party package

Pip
Similar to Easy_install, download by name one-click, call Setuptools install. Pip is an upgraded version that can be viewed as a easy_install.
How to use:
by http://www.jsxubar.info/install-pip.html
Download get-pip.py file First
Run Python get-pip.py, automatic installation, including Setuptools package
Use:

pip install simplejson --安装包pip install --upgrade simplejson --升级包pip uninstall simplejson --卸载包

Using the Setuptools self-service download installation, you will often encounter packages that do not have a compiler, and so on, causing the installation to fail. So using Easy_install or PIP is a convenient option.
Before installing Networkx install Networkx to install the Paint tool matplotlib, and the matrix arithmetic tool NumPy, we execute:
pip install numpypip install matplotlibpip install networkx
Installation is complete. 2. The English version of the NETWORKX can be downloaded on its official website and is easy to read and understand. Http://networkx.github.io/documentation/latest/reference/index.html

Here are some simple ways to build a map. In Networkx, the figure provides an excuse in the form of an object. In the graph object, also a bit, edge and so on is also provided as an object, drawing calls Matplotlib function complete.
Building MapNetworkx can be used to establish simple graph graph, digraph, repeatable edge multi-graph.
import networkx as nxG=nx.Graph()D=nx.DiGraph()M=nx.MultiGraph()
PointThe points here can be any distinguishable object (hashable), such as numbers, strings, objects, etc.
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])
sideThe sides here can use two separate objects as input
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)])</pre></div>
Access
node_list = G.nodes()edge_list = G.edges()#可以返回包含点与边的列表node = G.node[‘first_node’]#如上根据key返回点edge = G.edge['first_node']['second_node']#同样的方法,返回两个key之间的边</pre></div>
PropertiesWe can give graphs, points, edges to various properties, the simplest is the weight property
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)]</pre></div>####  

Nx.connected_components (G)

Returns the fully connected component of G in the form of a list, one sub-list per component
<div style="font-family:Tahoma;font-size:14px;">类似的,networkx还提供各种图算法,具体可见官方文档。</div>#### **画图**

Nx.draw (G) #画出图G
Matplotlib.show () #显示出来
```
We use the connected component algorithm and draw a simple diagram as follows:



The default properties of the Networkx are not good, and we can set the appropriate properties based on the excuses provided by the document to get the appropriate graphs.

NETWORKX provides the point location, size, color, shape, edge color, type and other properties of the graph to be set.

[Python] Networkx Introductory turn

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.