Python-networkx Learning (1)

Source: Internet
Author: User

Introduced:

Networkx is a library of Python that provides algorithms, generators, and drawing tools for the graph's data structure. Recently, using Ryu for shortest path acquisition, this library can be used to simplify the workload. The library uses a function to invoke the appropriate API, and its parameter types are usually graph objects.

Function API calls, follow these steps to create a build diagram:

1.networkx of loading

Calling Networkx in Python is typically only needed to import the library

import Networkx as NX

2. Creating a Diagram Object

NETWORKX provides four basic diagram objects: Graph,digraph,multigraph,multidigraph.

You can create an empty diagram of the above four diagram objects using the following call method.

G=NX. Graph () G=NX. DiGraph () G=NX. Multigraph () G=nx. Multidigraph ()

In Networkx, each node of the graph is allowed to be represented as a Hashtable object, and for each parameter of the edge in the graph, it can be identified by the way it is associated with the edge, in general, for weights, weight as keyword, and for other parameters, The user can use any keyword other than weight to name it.

3. In 2, the creation is just a pair of NULL, in order to get a node, have edges of the diagram, generally use the following function:

G.add_edge (#default Edge data=1 G.add_edge) #specify Edge data=0.9

The Add_edge () function, which needs to pass in two parameters U and V, and multiple optional parameters when calling

You and V are the two nodes in the diagram, if there is no node in the diagram, the two nodes are automatically added in the call, and the connection relationship between the two nodes is constructed, and the optional parameters usually refer to the relationship parameters such as the weight of the edge. It is important to note that if this edge is already present in the diagram, it will be followed by a new action (that is, overwriting the original information) when you re-add it.

For this function, there are several ways to create an edge in addition to the methods described above:

G.add_edge (*e)             # single Edge as tuple of both Nodesg.add_edge (1, 3, weight=7, capacity=15, length=342.7)  #using Man Y arguements to create Edgeg.add_edges_from ([(1, 2)]) # Add edges from Iterable container

Sometimes, when you create an edge in the default way, we may also add the relevant parameters of the edge to the side, and you can update the edge information in the following way:

#For non-string attribute keys, use subscript notation. G.add_edge (1, 2) g[1][2].update ({0:5})     #更新边的信息G. Edges[1, 2].update ({0:5})  #更新边的信息 # The above two methods of updating, choose one

The attentive friend may have noticed that I was writing about the Add_edges_from () function, which was also used to create the edge, which was slightly different from the add_edges (), and was created in a way that was add_edges () in a single node. It comes in more convenient. This function, when called, requires a node tuple as a parameter and more than one optional parameter as the edge information. You can pass it this way:

Create an edge between nodes by default:

G.add_edges_from ([(U,V)])

You can also write this, adding information at the same time you create it:

G.add_edges_from ([(3, 4), (1, 4)], label= ' WN2898 ')

In this way, the connection of a 3-4-1 diagram is constructed, and each edge is labeled.

You can then create your own graph model.

          

Python-networkx Learning (1)

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.