[Python]networkx Getting Started

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_installEasy_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 the ez_setup.py file online, execute the Python ez_setup.py installation Setuptools tool. Perform
Install a third-party package
Pip is similar to Easy_install, which is downloaded according to the name one-click and calls Setuptools installation. Pip is an upgraded version that can be viewed as a easy_install. How to use: Download the get-pip.py file by http://www.jsxubar.info/install-pip.html to run Python get-pip.py, install it automatically, including the Setuptools package use:
Pip Install Simplejson--Install package pip install--upgrade Simplejson--Upgrade package PIP Uninstall Simplejson--Uninstall Package

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.
The diagram Networkx can establish a simple graph graph, a direction graph digraph, a repeatable edge multi-graph.
Import Networkx as Nxg=nx. Graph () D=nx. DiGraph () M=nx. Multigraph ()
The point here can be any distinguishable object (hashable), such as numbers, strings, objects, etc.
G.add_node (1) g.add_node (' First_node ') #这里用一个对象多为key来唯一区别一个点 # We can also use a list to batch add dots g.add_nodes_from ([+]) # You can also use a graph object as a point and embed it in other diagrams G.add_node (d) #这里D作为一个点的key # or assign all points of a graph to another figure G.add_nodes_from (d) #这里返回D的所有点, giving g# the same delivery method as joining, We can also delete point G.remove_node (1) g.remove_nodes_from ([[+])
Side here can use two separate objects as input
G.add_edge #表示1, there is an edge between 2. #如果不存在点1, 2, the point collection is automatically added. #或者以元组的形式作为输入e = (G.add_edge) (*e) #这里的 * represents the bar tuple unpacking (unpack), as a value thrown into the function. #如果不解包, equivalent to #g.add_edge (e) =g.add_edge (()) is inconsistent with the way parameters are passed. #类似的, we can also use a list containing tuples to pass parameter G.add_edges_from ([(2,3)]) #我们还可以报一个图的边赋予另一个图G. Add_edges_from (H) #删除G. Remove_edge ( (G.remove_edges_from) ([2,3)])
Access
Node_list = G.nodes () edge_list = G.edges () #可以返回包含点与边的列表node = g.node[' First_node '] #如上根据key返回点edge = g.edge[' First_ Node ' [' Second_node '] #同样的方法, returns the edge between two keys
Properties we 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 ') #批量添加点是, keeping up with the unified properties G.add_nodes_from ([(3,{') Time ': ' 5pm '}), (4,{' time ': ' 4pm '})]) #根据字典key访问属性值 #或者写成元组列表 [(Key,dict), (key,dict)] in the form of g.node[1][' time ']. #给边添加属性也类似G. Add_edge (1,2,time= ' 3am ') g.add_edges_from ([(1,2,{' time ' = ' 5pm '}), (2,3,{' time ' =3am})]) #批量赋予属性G. edge[1 ][2][' time ' #访问 # We can also use a specific function to return properties in bulk, such as time = Nx.get_edge_attributes (G, ' time ') #返回得到以元组为key, the time property is worth a dictionary time[]
Graph algorithm
Nx.connected_components (G) #返回列表形式的G的全连通分量, one sub-list per component
Similarly, Networkx also offers a variety of graph algorithms, which are visible in official documents. Drawing
Nx.draw (G) #画出图Gmatplotlib. Show () #显示出来

We use the connected component algorithm and draw a simple diagram as follows:
The default property of Networkx is not good,we set the appropriate properties based on the excuses provided by the documentation 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 Getting Started

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.