Networkx First Acquaintance

Source: Internet
Author: User

I heard that networkx is a very useful tool for complex network research, just try it.

ImportNetworkx as NxG= NX. Graph ()#Create a blank diagramG.add_node ("Node1")#Add a node called Node1G.add_node (1) G.add_node (2)#add two nodes called "x "G.add_edge ()#increase the edge of a connection nodePrint(G.nodes ())#Print the node of Figure GPrint(G.edges ())#print the edges of the figure GNx.draw (G)#drawing the image of Figure G

To get an image like this, the dot does not show the name of the node. How can I display the node name?

Refer to the Nx.draw () method.

Nx.draw (g,with_lables = True) #节点是否带标签

There are other details, such as label color, font size, transparency, and more, for more information: http://www.cnblogs.com/forstudy/archive/2012/03/20/2408125.html

NETWORKX provides 4 common network modeling methods, namely: Rule diagram, er random graph, WS Small World Network and BA scale scale-free network.

This paper first introduces the method of generating these network models in Networkx, then takes the modeling of BA scale-free network as an example, analyzes the basic idea of the design of complex network evolution model by using NETWORKX, so as to develop our own model in the future.

Reference URL: http://blog.sina.com.cn/s/blog_720448d301018px7.html

Http://www.cnblogs.com/forstudy/archive/2012/03/20/2407954.html

1. Rules Chart

In Networkx, the random_graphs.random_regular_graph (d, N) method can be used to generate a rule diagram with n nodes, each node having a D neighbor node. Here is a sample code that generates a rule diagram with 20 nodes and 3 neighbors per node:


Import Networkx as NX#import= nx.random_graphs.random_regular_graph (3,20) # generate a rule diagram with 20 nodes, 3 neighbors per node RGpos = nx.spectral_layout (RG) # defines a layout where the spectral layout is used, The later changes will also introduce other layout methods, note the differences on the graphics nx.draw (rg,pos,with_labels=false,node_size =) # Draw the graph of the rule graph, with_ The labels determines that the node is non-tagged (numbered) and that the node_size is the diameter of the node #plt. Show () # display Graphics

The results of the operation are as follows:

2.ER Random graph

Er random graph is a kind of "complex" network which is studied more early, and the basic idea of this model is to connect each pair of nodes in N nodes with probability p. In Networkx, you can use the Random_graphs.erdos_renyi_graph (n,p) method to generate an ER random graph with n nodes connected with the probability p:

First run Ipython-Pylab, or remove the comments from the second and last lines import  networkx as NX#Import Matplotlib.pyplot as pltER = nx.random_graphs.erdos_renyi_graph (20,0.2)  # generates a 20 node, Random graphs connected with probability 0.2 pos = nx.shell_layout (ER)          # defines a layout where the shell layout is Nx.draw (er,pos,with_ labels=#plt.show ()

Operation Result:

3.WS Small World Model

Import= nx.random_graphs.watts_strogatz_graph (20,4,0.3)  # build contains 20 nodes, 4 neighbors per node, The Small World Network pos = Nx.circular_layout (WS) # that randomly re-connects the probability to 0.3           defines a layout where the circular layout is used Nx.draw (ws,pos , with_labels=false,node_size = +)  # drawing Graphics

4.BA Scale-Free network

In Networkx, you can use the Random_graphs.barabasi_albert_graph (n, m) method to generate a BA scale-free network with n nodes, each with M-bars, here is an example:

Import Networkx as Nxba= Nx.random_graphs.barabasi_albert_graph (20,1)  # generate n=20, m=1 BA scale-free network pos = nx.spring_layout (BA)          # defines a layout that uses the spring layout Nx.draw (ba,pos,with_labels=false,node_ Size = $)  # draw a graphic

Networkx First Acquaintance

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.