Python Complex network Analysis library Networkx

Source: Internet
Author: User

Networkx is a graph theory and complex network modeling tool developed in Python language, which is built with commonly used graphs and complex network analysis algorithms , which can facilitate complex network data analysis and simulation modeling. NETWORKX supports the creation of simple, forward-and multi-graph (multigraph), built-in many standard graph theory algorithms, nodes can be arbitrary data, support arbitrary boundary value dimension, feature-rich, easy to use.

Introducing Modules

Import Networkx as NX Print NX
Graph without direction

Example 1:

#!-*-coding:utf8-*- ImportNetworkx as NXImportMatplotlib.pyplot as PLTG= NX. Graph ()#Create an empty graph GG.add_node (1)#Add a Node 1G.add_edge (2,3)#Add an edge 2-3 (implicitly adding two nodes 2, 3)G.add_edge (3,2)#for a graph without direction, Edge 3-2 and Edge 2-3 are considered to be an edgePrint "nodes:", G.nodes ()#Output ALL nodes: [1, 2, 3]Print "edges:", G.edges ()#output All edges: [(2, 3)]Print "Number of edges:", G.number_of_edges ()#number of output edges: 1Nx.draw (G) plt.savefig ("Wuxiangtu.png") plt.show ()

Output

Nodes: [1, 2, 3]edges: [(2, 3)]number of edges:1

Example 2:

#-*-coding:utf8-*- ImportNetworkx as NXImportMatplotlib.pyplot as PLTG=NX. DiGraph () G.add_node (1) G.add_node (2)#AddG.add_nodes_from ([3,4,5,6])#Add a collectionG.add_cycle ([1,2,3,4])#HoopG.add_edge (1,3) G.add_edges_from ([(3,5), (3,6), (6,7)])#Add Edge CollectionNx.draw (G) plt.savefig ("Youxiangtu.png") plt.show ()

A map of the direction

Example 1:

# !-*-coding:utf8-*- Import Networkx as NX Import  = NX. DiGraph () G.add_node (1) G.add_node (2) g.add_nodes_from ([3,4,5,6]) g.add_cycle ([ 1,2,3,4]) G.add_edge (1,3) g.add_edges_from ([(3,5), (3,6), (6,7)]) Nx.draw (G) Plt.savefig (  "youxiangtu.png") plt.show ()

Note : The graph and the graph can be converted to each other, using the function:

    • Graph.to_undirected ()
    • Graph.to_directed ()

For example, in the example, the graph is converted to an image without a direction:

# !-*-coding:utf8-*- Import Networkx as NX Import  = NX. DiGraph () G.add_node (1) G.add_node (2) g.add_nodes_from ([3,4,5,6]) g.add_cycle ([ 1,2,3,4]) G.add_edge (1,3) g.add_edges_from ([(3,5), (3,6), (6,7= g.to_ undirected () Nx.draw (G) plt.savefig ("wuxiangtu.png") plt.show ()

Weighted graph

Both the graph and the graph can give the edge weight, the method used is Add_weighted_edges_from, it accepts 1 or more triples [u,v,w] as parameters, where U is the starting point, V is the end point, W is the weight.

Example 1:

#!-*-coding:utf8-*- ImportNetworkx as NXImportMatplotlib.pyplot as PLTG= NX. Graph ()#Create an empty graph GG.add_edge (2,3)#Add an edge 2-3 (implicitly adding two nodes 2, 3)G.add_weighted_edges_from ([(3, 4, 3.5), (3, 5, 7.0)])#for a graph without direction, Edge 3-2 and Edge 2-3 are considered to be an edgePrintG.get_edge_data (2, 3)PrintG.get_edge_data (3, 4)PrintG.get_edge_data (3, 5) Nx.draw (G) plt.savefig ("Wuxiangtu.png") plt.show ()

Output

{}{'weight': 3.5}{'weight': 7.0}

Algorithm calculation of classical graph theory

Calculation 1: The shortest path between any two points of a non-graph

# -*-coding:cp936-*- Import Networkx as NX Import  # calculation 1: The shortest path between any two points of the non-graph is G = NX. Graph () G.add_edges_from ([(), (1,3), (1,4), (1,5), (4,5), (4,6), (5,6= nx.all_pairs_shortest_ Path (G)print path[1]

Python Complex network Analysis library Networkx

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.