Welcome to the world of Graphs. If you have connected data then you might need one of the types of graphs to model those patterns. There are several things that can be done with Graphs like mapping traffic patterns, managing water distribution networks, social media analysis, etc… At it’s heart we need to be able to create Nodes and Edges with data associated about each.
NetworkX is a library dealing with Graph Database creation/import/export/manipulation/algorithms/plotting. You can start using several types of network graphs. For example: Facebook might put their users in a plain Graph()
import networkx as nx facebook = nx.Graph() facebook.add_node('you') facebook.add_node('me') if both_agree_to_be_friends('you','me'): facebook.add_edge('you','me') #order isn't important here.
Facebook would use a regular Graph() because there isn’t anything special about the edge between nodes. This Facebook example can only have one edge (friendship) between nodes.
Another type of Graph would be a Directed Graph. Twitter would use a Directed Graph because the nodes have a direction. In Twitter I can follow you but you don’t have to follow me. So we could represent that with this code:
import networkx as nx twitter = nx.DiGraph() twitter.add_node('you') twitter.add_node('me') twitter.add_edge('me','you') #order is important here.
This is how Twitter might set up ‘me’ to follow ‘you’, but not the other way around.
There are MultiGraphs() and MultiDiGraphs() as well just in case you want more than one edge between two nodes. These four types cover a wide variety of problems that can be represented with Graphs. If you want you can add a dictionary of data to both nodes and edges. The documentation on this module is incredible. Every algorithm is well researched and well implemented. I hope you enjoy working with it.
The detailed instructions on installing NetworkX is available here.
As with any other python package, NetworkX can be installed using pip, Miniconda/Anaconda and from source code.
Installing with pip
pip install networkx
An attempt will be made to find and install an appropriate version of NetworkX that matches your operating system and Python version.
To use pip, you need to have setuptools installed.
If you want to install the development version from GitHub, use the command
pip install git://github.com/networkx/networkx.git#egg=networkx
Miniconda and Anaconda use
conda for software installation/updates.
You can update/install NetworkX to the latest version with:
conda install networkx
or if you want to update NetworkX installation then
conda update networkx
Installing from source
Source file archive
- Download the source from
https://pypi.python.org/pypi/networkx/ or get the latest version.
- Unpack and change
directory to the source directory (it should have the files
README.txt and setup.py).
python setup.py installto build and
- (Optional) Run nosetests to execute the tests if you have
Installing from GitHub
Clone the NetworkX repository (see https://github.com/networkx/networkx/ for options)
git clone https://github.com/networkx/networkx.git
Change directory to NetworkX
python setup.py installto build and install
(Optional) Run nosetests to execute the tests if you have nose installed.
If you don’t have permission to install software on your system, you can install into another directory using the
--home flags to setup.py.
To use NetworkX you need Python 2.7, 3.3 or later
- NumPy: Provides matrix representation of graphs and is used in some graph algorithms for high-performance matrix computations. (http://scipy.org/Download)
- SciPy: Provides sparse matrix representation of graphs and many numerical scientific tools. (http://scipy.org/Download)
- Matplotlib: Provides flexible drawing of graphs. (http://matplotlib.sourceforge.net/)
- GraphViz in conjunction with either PyGraphviz (http://pygraphviz.github.io) or pydotplus (https://github.com/carlos-jenkins/pydotplus): provides graph drawing and graph layout algorithms. (http://graphviz.org)
- PyYAML: Required for YAML format reading and writing. (http://pyyaml.org)
if you want to reproduce, please indicate the source:
Getting started with networkx – Installation or Setup - CodeDay