NetworkX is the most popular Python package for manipulating and analyzing graphs. Several packages offer the same basic level of graph manipulation, and study of the structure, dynamics, and functions of complex networks. Pygraphviz is a Python interface to the Graphviz graph layout and visualization package.Python language data structures for graphs, digraphs, and multigraphs. Nodes can be "anything" (e.g. text, images, XML records)Edges can hold arbitrary data (e.g. weights, time-series)Generators for classic graphs, random graphs, and synthetic networksStandard graph algorithmsNetwork structure and analysis measuresBasic graph drawingOpen source BSD licenseWell tested: more than 1500 unit testsAdditional benefits from Python: fast prototyping, easy to teach, multi-platform.
If you've done any sort of data analysis in Python or have the Anaconda distribution, my guess is you probably have pandas and matplotlib. However, you might not have networkx.
pip install pandas pip install networkx pip install matplotlib
Next we will present simple example of network with 5 nodes. To fire up network, we have to import networkx.
import networkx as nx
import matplotlib.pyplot as plt
Now you use the edge list and the node list to create a graph object in networkx.
# Create empty graph G = nx.Graph()
"G" is the name of the graph you build. Next we add nodes. There are two ways to add nodes: one by one,
print(type(G)) #type=global keyword/class
##to find the first node
#first make a node list
nodelist = list(G.node)
print(nodelist) #python starts with 0
Add in a list of Nodes
nodelist = ['B','C','D','E'] G.add_nodes_from(nodelist)
Then we add edges. Also there are two ways, either one by one
Add in a batch
edgeList = [('A','B'),('B','C'),('C','D'),('A','D')] #list of tuples G.add_edges_from(edgeList)
Finally we will visualize our work and get basic information of network.
nx.draw(G, with_labels =True, node_color = 'pink', node_size = 900)
If you like Codersarts blog and looking for Assignment help,Project help, Programming tutors help and suggestion you can send mail at firstname.lastname@example.org.
Please write your suggestion in comment section below if you find anything incorrect in this blog post.