SlideShare a Scribd company logo
Python
NetworkX Library
Quick Start Guide
Mohammed Zuhair Al-Taie
Big Data Centre - Universiti Teknologi Malaysia - 2016
python networkx library – quick start guide
The fast developments in the Web and Internet in the last decade and the advancements in computing and
communication have drawn people in innovative ways
Sites, such as Twitter, Facebook, LinkedIn, and MySpace allow people to make new virtual relationships.
Sending and receiving emails, visiting a Webpage or posting a comment on a blog site leaves a digital
footprint that can be traced back to the person or group behind it.
Vast amounts of network data are being generated and collected.
How can we analyse these data?
New Age of Web Usage
python networkx library – quick start guide
Networks in real world fall into the following four major types
Social networks: Networks in which vertices represent people and edges represent some form of
social interaction between vertices including friendship, kinship, information flow, disease outbreak,
etc.
Information networks: Man-made networks in which data items are linked together in some way.
The best-known example is the World Wide Web (WWW)
Technological networks: Man-made networks that are designed to distribute commodity or
resources. One of the greatest examples is the Internet.
Biological networks: Networks that represent patterns of interaction between biological elements.
Real-World Networks
python networkx library – quick start guide
A graph is a set of points and lines that connect them.
It is a way of representing the relationships among a collection of objects.
Points are called 'vertices', and lines are called 'edges‘.
A graph G with a set X of vertices together with a set E of edges is written as G = (X, E)
Graph: Basic Concepts
python networkx library – quick start guide
There are several different types of graphs to represent the relationship between nodes:
Undirected graph, Directed graph, Weighted graph, Planar graph, Orthogonal graph, Grid-based
graph, etc.
The degree of a vertex is the number of edges incident to it.
For directed graphs, out-degree measure is the number of outgoing edges, while in-degree measure is the
number of incoming edges.
Types of Graphs
python networkx library – quick start guide
NetworkX is a Python language software package and an open-source tool for the creation, manipulation,
and study of the structure, dynamics, and functions of complex networks.
Complex networks are networks with non-trivial topological features—features that do not occur in simple networks
such as lattices or random graphs but often occur in real graphs. E.g.: circadian rhythms, electrochemical reactions,
laser arrays, neuron networks, Josephson junction arrays..., etc.
NetworkX can load, store and analyze networks, generate new networks, build network models, and draw
networks.
It is a computational network modelling tool and not a software tool development.
The first public release of the library, which is all based on Python, was in April 2005.
The library can engage with languages other than Python such as C, C++, and FORTRAN.
What is NetworkX?
python networkx library – quick start guide
Although NetworkX is not ideal for large-scale problems with fast processing requirements, it is a great option
for real-world network analysis:
Most of the core algorithms rely on extremely fast legacy code.
It uses standard graph algorithms
It has an extensive set of native readable and writable formats.
It is easy to install and use on major platforms with a strong online up-to-date documentation.
It is Ideal for representing networks of different types like e.g. classic graphs, random graphs, and
synthetic networks.
It takes advantage of Python’s ability to import data from outer sources.
NetworkX includes many graph generator functions and facilities to read and write graphs in many
formats such as .edgelist, .adjlist, .gml, .graphml, .pajek, etc.
Why NetworkX
python networkx library – quick start guide
The library is implemented as dictionary of dictionaries with a node-centric view of network
based on nodes and connections between them.
Nodes can be any hashable object such as a text string, an image, an XML object, another Graph, a
customized node object, etc.
Python’s none object should not be used as a node as it determines whether optional function
arguments have been assigned in many functions.
Each graph, node, and edge can hold key/value attribute pairs (e.g. weights, labels, and colours) in an
associated attribute dictionary (the keys must be hashable).
Edges are represented as tuples with optional edge data and can hold arbitrary data (e.g. weights, time
series, etc.)
NetworkX: Data Structure
python networkx library – quick start guide
Several packages and tools are available in Python for network analysis and visualization:
networkX: http://networkx.lanl.gov/
PyCX Project: http://pycx.sf.net/
ComplexNetworkSim
SimPy
graph-tool [http://graph-tool.skewed.de/]
pyGraphViz (GraphViz) [http://www.graphviz.org/]
igraph [http://igraph.org/]
python-graph
gephi [http://gephi.org/]
NetworkX was not designed as a graph drawing package.
However, it provides basic drawing capabilities through matplotlib.
NetworkX provides an interface to that package.
For more complex visualization techniques, it is preferred to use the open source GraphViz software package
Python: Visualization Packages
python networkx library – quick start guide
scipy: a Matlab-like library for Python.
lots of features: linear algebra, Fourier transform,
www.scipy.org
numpy: www.numpy.org
matplotlib: www.matplotlib.org
NetworkX: Required Packages
python networkx library – quick start guide
>>> Import networkx as nx # import library
>>> G = nx.Graph() # create new simple undirected graphs
>>> EG = nx.empty_graph(100) # create an empty graph
>>> DG = nx.DiGraph # create a simple directed graphs
>>> MG = nx.MultiGraph() # create undirected with parallel edges
>>> MDG = nx.MultiDiGraph() # create directed with parallel edges
>>> CG = nx.complete_graph(10) # create a complete graph
>>> PG = nx.path_graph(5) # create a chain of nodes
>>> CBG = nx.complete_bipartite_graph(n1, n2) # create bipartite
>>> GG = nx.grid_graph([10, 10, 10, 10]) # arbitrary dimensional lattice
To get graph attributes
>>> G.graph
To convert to undirected
>>> G.to_undirected()
To convert to directed
>>> G.to_directed()
To clear a graph from nodes and edges
>>> G.clear()
Graph Types
python networkx library – quick start guide
To add one node at a time
>>> G.add_node(1)
To add a list of nodes
>>> G.add_nodes_from([2, 3, 4, 5]) # takes any iterable collection
To add nbunch of nodes
>>> H = nx.path_graph(10)
>>> G.add_nodes_from(H)
To add a graph as a node
>>> G.add_node(H)
To print the number of nodes
>>> G.number_of_nodes()
To print graph nodes
>>> G.nodes()
To print type of nodes
>>> type(G.nodes()) # it will show class list
To relabel nodes
>>> nx.relabel_nodes(G, mapping, copy = True) # mapping  new labels
To check node membership
>>> G.has_node(1)
Network Nodes
python networkx library – quick start guide
To add an edge
>>> G.add_edge(1, 2)
To add a list of edges
>>> G.add_edges_from([5, 6]) # automatically add nodes if not exist
To add edges from a list
>>> edge = (3, 4)
>>> G.add_edge(*edge)
To check edge membership
>>> G.has_edge(3, 4)
To print the number of edges
>>> G.number_of_edges()
To remove an edge
>>> G.remove_edge(1, 2)
To remove a list of edges
>>> G.remove_edges_from([(1, 2), (3, 4)])
To print graph edges
>>> G.edges()
General read format
>>> nx.read_format(“path/to/file.txt”,...options...)
To read edge list from file
>>> el = nx.read_edgelist(“test.edges”, comments = “#”)
To read adjacency list from file
>>> al = nx.read_adjlist(“test2.adj”)
General write format
>>> nx.write_format(g,“path/to/file.txt”,...options...)
To write edge list
>>> nx.write_edgelist(G, “newFile.edges”, comments = “#”, data = True)
To print type of edges
>>> type(G.edges()) # it will show class list
Network Edges
python networkx library – quick start guide
To create an empty directed graph
>>> DG = nx.DiGraph() # creates simple directed graphs
To add weighted edges to DG
>>> DG.add_weighted_edges_from([(7, 8, 2.7), (9, 10, 0.5)])
To calculate outdegree
>>> DG.out_degree()
To calculate outdegree with attributes included
>>> DG.out_degree(with_labels = True) # Boolean should be capitalized
To calculate successors
>>> DG.successors(1)
To calculate predecessors
>>> DG.predecessors(1)
To calculate neighbors
>>> DG.neighbors(1)
To convert directed graphs to undirected
>>> DG.to_undirected()
Directed Graphs
python networkx library – quick start guide
To add attributes
>>> G = nx.Graph(day = “Wednesday”)
To update attributes
>>> G.graph[“day”] = “Thursday”
To add attributes to nodes
>>> G.add_node(1, time = “5am”) # attributes are optional
>>> G.add_nodes_from([3], time = “2am”)
To get node attributes
>>> G.node[1]
To add attributes to edges
>>> G.add_edges_from([(11, 12), (13, 14)], color = “blue”)
To get edge attributes
>>> G[1][2]
To get a particular attribute value
>>> G[1][2][“color”]
Attributed Graphs
python networkx library – quick start guide
To add weighted edges to a graph
>>> G.add_edge(15, 16, weight = 3.3)
>>> G.add_edge(17, 18, weight = 4.3)
To calculate node degree without weight
>>> G.degree(1)
To calculate node degree with weight included
>>> G.degree(1, weight = “weight”)
To calculate all node degrees
>>> G.degree(weight = “weight”)
Weighted Graphs
python networkx library – quick start guide
To build a multigraph
>>> MG = nx.MultiGraph()
To add edges to MG
>>> MG.add_weighted_edges_from([1, 2, 0.75), (1, 2, 1,25), (2, 3, 0.75)])
To calculate degrees
>>> MG.degree(weight = “weight”)
Multigraphs
python networkx library – quick start guide
>>> nx.subgraph(G, nbunch) # induce subgraph of G on nodes in nbunch
>>> nx.union(G1, G2) # graph union
>>> nx.disjoint_union(G1, G2) # graph union/all node are different
>>> nx.cartesian_product(G1, G2) # return Cartesian product graph
>>> nx.compose(G1, G2) # combine graphs identifying common nodes
>>> nx.complement(G) # graph complement
>>> nx.create_empty_graph(G) # return an empty copy of the same graph class
>>> nx.convert_to_undirected(G) # return an undirected copy of G
>>> nx.convert_to_directed(G) # return a directed copy of G
Classic Graph Operations
python networkx library – quick start guide
Using a call to one of the classic small graphs
>>> petersen = nx.petersen_graph()
>>> tutte = nx.tutte_graph()
>>> maze = nx.sedgewick_maze_graph()
>>> tet = nx.tetrahedral_graph()
Using a (constructive) generator for a classic graph
>>> k_5 = nx.complete_graphs(10)
>>> k_3_5 = nx.complete_bipartite_graph(3, 5)
>>> barbell = nx.barbell_graph(15, 15)
>>> lollipop = nx.lollipop_graph(5, 10)
Using a stochastic graph generator
>>> er = nx.erdos_renyi_graph(50, 0.5)
>>> ws = nx.watts_strogatz_graph(20, 2, 0.5)
>>> ba = nx.barabasi_albert_graph(50, 5)
>>> red = nx.random_lobster(100, 0.9, 0.9)
Graph Generators
python networkx library – quick start guide
Algorithms Package (networkx.algorithms)
bipartite
block
boundary
centrality (package)
clique
cluster
components (package)
core
cycles
dag
distance measures
ow (package)
isolates
isomorphism (package)
link analysis (package)
matching
mixing
mst
operators
shortest paths (package)
smetric
python networkx library – quick start guide
Reading and Writing
Adjacency List
Multiline Adjacency List
Edge List
GEXF
GML
Pickle
GraphML
LEDA
YAML
SparseGraph6
Pajek
GIS Shapefile
python networkx library – quick start guide
To find connected components
>>> nx.connected_components(G)
To sort nodes based on node degree
>>> sorted(nx.degree(G).values())
To calculate degree of a specific node
>>> G.degree(1)
To calculate all degrees
>>> G.degree()
To see if network is connected
>>> nx.is_connected(G)
To calculate network global clustering coefficient
>>> nx.clustering(G)
To calculate the clustering coefficient of each node
>>> nx.clustering(G, with_labels = True)
To calculate coefficient for a particular node
>>> nx.clustering(G, 1)
To find the shortest path between two nodes
>>> nx.shortest_path(G, 1, 3)
To find the length of the shortest path between two nodes
>>> nx.shortest_path_length(G, 3, 1)
Basic Network Analysis (1)
python networkx library – quick start guide
To find in-degree distribution of G
>>> G.in_degree()
To find out-degree distribution of G
>>> G.out_degree()
To calculate number of nodes
>>> G.order()
>>> nx.number_of_nodes(G)
>>> len(G)
To calculate number of edges
>>> G.size()
>>> nx.number_of_edges(G)
To find network diameter
>>> nx.diameter(G)
To find network radius
>>> nx.radius(G)
To find cores in network
>>> nx.find_cores(G)
Basic Network Analysis (2)
python networkx library – quick start guide
To calculate degree centrality
>>> nx.degree_centrality(G)
To calculate betweenness centrality
>>> nx.betweenness_centrality(G)
To calculate closeness centrality
>>> nx.closeness_centrality(G)
To calculate eigenvector centrality
>>> nx.eigenvector_centrality(G)
Centrality measures
python networkx library – quick start guide
>>> import matplotlib.pyplot as plt # Can use GraphViz
To clear the previous graph
>>> plt.clf()
To draw a graph
>>> nx.draw(G)
>>> nx.draw_random(G)
>>> nx.draw_circular(G)
>>> nx.draw_spectral(G)
To show the graph
>>> plt.show() # to show the file
To save the graph
>>> plt.savefig(“myFig.png”) # save as .png file
To close the file
>>> plt.close()
To extract the main connected component from G
>>> nx.connected_component_subgraphs(G) # graph should be undirected
Drawing Graphs
python networkx library – quick start guide

More Related Content

What's hot (20)

PPTX
Functional programming with Java 8
LivePerson
 
PDF
Graph Analyses with Python and NetworkX
Benjamin Bengfort
 
PPTX
Introduction to AngularJS
David Parsons
 
PDF
Python my sql database connection
Learnbay Datascience
 
PPT
05 architectural styles
Majong DevJfu
 
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPT
Files and Directories in PHP
Nicole Ryan
 
PDF
Python Class | Python Programming | Python Tutorial | Edureka
Edureka!
 
PPT
JAVA OOP
Sunil OS
 
PPTX
Unit 1 introduction to web programming
zahid7578
 
PPT
2.3 bayesian classification
Krish_ver2
 
PPTX
Data visualization using R
Ummiya Mohammedi
 
PPT
Introduction To C#
SAMIR BHOGAYTA
 
PPTX
Python ppt
AMIT VIRAMGAMI
 
PPTX
Loops in Python
Arockia Abins
 
PPT
Introduction to Python
amiable_indian
 
PPTX
React render props
Saikat Samanta
 
PDF
Network programming Using Python
Karim Sonbol
 
Functional programming with Java 8
LivePerson
 
Graph Analyses with Python and NetworkX
Benjamin Bengfort
 
Introduction to AngularJS
David Parsons
 
Python my sql database connection
Learnbay Datascience
 
05 architectural styles
Majong DevJfu
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Files and Directories in PHP
Nicole Ryan
 
Python Class | Python Programming | Python Tutorial | Edureka
Edureka!
 
JAVA OOP
Sunil OS
 
Unit 1 introduction to web programming
zahid7578
 
2.3 bayesian classification
Krish_ver2
 
Data visualization using R
Ummiya Mohammedi
 
Introduction To C#
SAMIR BHOGAYTA
 
Python ppt
AMIT VIRAMGAMI
 
Loops in Python
Arockia Abins
 
Introduction to Python
amiable_indian
 
React render props
Saikat Samanta
 
Network programming Using Python
Karim Sonbol
 

Similar to Python networkx library quick start guide (20)

PPTX
Session 09 learning relationships.pptx
Sara-Jayne Terp
 
PPTX
Session 09 learning relationships.pptx
bodaceacat
 
PDF
Advance Programming Slides lect.pptx.pdf
mohsinfareed780
 
PPTX
Eclipse Con Europe 2014 How to use DAWN Science Project
Matthew Gerring
 
ODP
Linux and Open Source in Math, Science and Engineering
PDE1D
 
PPTX
Towards an Incremental Schema-level Index for Distributed Linked Open Data G...
Till Blume
 
PDF
Lecture 4: Deep Learning Frameworks
Mohamed Loey
 
PDF
Microservices, containers, and machine learning
Paco Nathan
 
PPTX
Session 2
HarithaAshok3
 
PDF
Natural Language Processing with CNTK and Apache Spark with Ali Zaidi
Databricks
 
PPTX
libraries in python using different .pptx
urvashipundir04
 
PDF
Standardizing on a single N-dimensional array API for Python
Ralf Gommers
 
PDF
Strata NYC 2015 - What's coming for the Spark community
Databricks
 
DOCX
CLASSROOM activity in graph analytics course
NPriya5
 
PPTX
Graph Analytics: Graph Algorithms Inside Neo4j
Neo4j
 
PDF
The Future of Computing is Distributed
Alluxio, Inc.
 
PPTX
AI與大數據數據處理 Spark實戰(20171216)
Paul Chao
 
PDF
Spark what's new what's coming
Databricks
 
PDF
Data analysis in R
Andrew Lowe
 
Session 09 learning relationships.pptx
Sara-Jayne Terp
 
Session 09 learning relationships.pptx
bodaceacat
 
Advance Programming Slides lect.pptx.pdf
mohsinfareed780
 
Eclipse Con Europe 2014 How to use DAWN Science Project
Matthew Gerring
 
Linux and Open Source in Math, Science and Engineering
PDE1D
 
Towards an Incremental Schema-level Index for Distributed Linked Open Data G...
Till Blume
 
Lecture 4: Deep Learning Frameworks
Mohamed Loey
 
Microservices, containers, and machine learning
Paco Nathan
 
Session 2
HarithaAshok3
 
Natural Language Processing with CNTK and Apache Spark with Ali Zaidi
Databricks
 
libraries in python using different .pptx
urvashipundir04
 
Standardizing on a single N-dimensional array API for Python
Ralf Gommers
 
Strata NYC 2015 - What's coming for the Spark community
Databricks
 
CLASSROOM activity in graph analytics course
NPriya5
 
Graph Analytics: Graph Algorithms Inside Neo4j
Neo4j
 
The Future of Computing is Distributed
Alluxio, Inc.
 
AI與大數據數據處理 Spark實戰(20171216)
Paul Chao
 
Spark what's new what's coming
Databricks
 
Data analysis in R
Andrew Lowe
 
Ad

More from Universiti Technologi Malaysia (UTM) (11)

PDF
A self organizing communication model for disaster risk management
Universiti Technologi Malaysia (UTM)
 
PDF
Spark Working Environment in Windows OS
Universiti Technologi Malaysia (UTM)
 
PDF
Python 3.x quick syntax guide
Universiti Technologi Malaysia (UTM)
 
PDF
Social media with big data analytics
Universiti Technologi Malaysia (UTM)
 
PPTX
Predicting the relevance of search results for e-commerce systems
Universiti Technologi Malaysia (UTM)
 
PPT
Scientific theory of state and society parities and disparities between the p...
Universiti Technologi Malaysia (UTM)
 
PPTX
Nation building current trends of technology use in da’wah
Universiti Technologi Malaysia (UTM)
 
PPT
Flight MH370 community structure
Universiti Technologi Malaysia (UTM)
 
PPT
Visualization of explanations in recommender systems
Universiti Technologi Malaysia (UTM)
 
PPT
Explanations in Recommender Systems: Overview and Research Approaches
Universiti Technologi Malaysia (UTM)
 
PPT
Factors disrupting a successful implementation of e-commerce in iraq
Universiti Technologi Malaysia (UTM)
 
A self organizing communication model for disaster risk management
Universiti Technologi Malaysia (UTM)
 
Spark Working Environment in Windows OS
Universiti Technologi Malaysia (UTM)
 
Python 3.x quick syntax guide
Universiti Technologi Malaysia (UTM)
 
Social media with big data analytics
Universiti Technologi Malaysia (UTM)
 
Predicting the relevance of search results for e-commerce systems
Universiti Technologi Malaysia (UTM)
 
Scientific theory of state and society parities and disparities between the p...
Universiti Technologi Malaysia (UTM)
 
Nation building current trends of technology use in da’wah
Universiti Technologi Malaysia (UTM)
 
Flight MH370 community structure
Universiti Technologi Malaysia (UTM)
 
Visualization of explanations in recommender systems
Universiti Technologi Malaysia (UTM)
 
Explanations in Recommender Systems: Overview and Research Approaches
Universiti Technologi Malaysia (UTM)
 
Factors disrupting a successful implementation of e-commerce in iraq
Universiti Technologi Malaysia (UTM)
 
Ad

Recently uploaded (20)

DOC
MATRIX_AMAN IRAWAN_20227479046.docbbbnnb
vanitafiani1
 
PDF
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
PPT
Lecture 2-1.ppt at a higher learning institution such as the university of Za...
rachealhantukumane52
 
PPTX
TSM_08_0811111111111111111111111111111111111111111111111
csomonasteriomoscow
 
PDF
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
PPTX
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
PPTX
recruitment Presentation.pptxhdhshhshshhehh
devraj40467
 
PDF
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
PPTX
This PowerPoint presentation titled "Data Visualization: Turning Data into In...
HemaDivyaKantamaneni
 
PDF
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
DOCX
AI/ML Applications in Financial domain projects
Rituparna De
 
PDF
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
PPT
01 presentation finyyyal معهد معايره.ppt
eltohamym057
 
PPTX
fashion industry boom.pptx an economics project
TGMPandeyji
 
PPTX
Usage of Power BI for Pharmaceutical Data analysis.pptx
Anisha Herala
 
PPTX
GenAI-Introduction-to-Copilot-for-Bing-March-2025-FOR-HUB.pptx
cleydsonborges1
 
PDF
Context Engineering vs. Prompt Engineering, A Comprehensive Guide.pdf
Tamanna
 
PDF
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
PPTX
DATA-COLLECTION METHODS, TYPES AND SOURCES
biggdaad011
 
PDF
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 
MATRIX_AMAN IRAWAN_20227479046.docbbbnnb
vanitafiani1
 
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
Lecture 2-1.ppt at a higher learning institution such as the university of Za...
rachealhantukumane52
 
TSM_08_0811111111111111111111111111111111111111111111111
csomonasteriomoscow
 
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
recruitment Presentation.pptxhdhshhshshhehh
devraj40467
 
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
This PowerPoint presentation titled "Data Visualization: Turning Data into In...
HemaDivyaKantamaneni
 
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
AI/ML Applications in Financial domain projects
Rituparna De
 
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
01 presentation finyyyal معهد معايره.ppt
eltohamym057
 
fashion industry boom.pptx an economics project
TGMPandeyji
 
Usage of Power BI for Pharmaceutical Data analysis.pptx
Anisha Herala
 
GenAI-Introduction-to-Copilot-for-Bing-March-2025-FOR-HUB.pptx
cleydsonborges1
 
Context Engineering vs. Prompt Engineering, A Comprehensive Guide.pdf
Tamanna
 
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
DATA-COLLECTION METHODS, TYPES AND SOURCES
biggdaad011
 
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 

Python networkx library quick start guide

  • 1. Python NetworkX Library Quick Start Guide Mohammed Zuhair Al-Taie Big Data Centre - Universiti Teknologi Malaysia - 2016
  • 2. python networkx library – quick start guide The fast developments in the Web and Internet in the last decade and the advancements in computing and communication have drawn people in innovative ways Sites, such as Twitter, Facebook, LinkedIn, and MySpace allow people to make new virtual relationships. Sending and receiving emails, visiting a Webpage or posting a comment on a blog site leaves a digital footprint that can be traced back to the person or group behind it. Vast amounts of network data are being generated and collected. How can we analyse these data? New Age of Web Usage
  • 3. python networkx library – quick start guide Networks in real world fall into the following four major types Social networks: Networks in which vertices represent people and edges represent some form of social interaction between vertices including friendship, kinship, information flow, disease outbreak, etc. Information networks: Man-made networks in which data items are linked together in some way. The best-known example is the World Wide Web (WWW) Technological networks: Man-made networks that are designed to distribute commodity or resources. One of the greatest examples is the Internet. Biological networks: Networks that represent patterns of interaction between biological elements. Real-World Networks
  • 4. python networkx library – quick start guide A graph is a set of points and lines that connect them. It is a way of representing the relationships among a collection of objects. Points are called 'vertices', and lines are called 'edges‘. A graph G with a set X of vertices together with a set E of edges is written as G = (X, E) Graph: Basic Concepts
  • 5. python networkx library – quick start guide There are several different types of graphs to represent the relationship between nodes: Undirected graph, Directed graph, Weighted graph, Planar graph, Orthogonal graph, Grid-based graph, etc. The degree of a vertex is the number of edges incident to it. For directed graphs, out-degree measure is the number of outgoing edges, while in-degree measure is the number of incoming edges. Types of Graphs
  • 6. python networkx library – quick start guide NetworkX is a Python language software package and an open-source tool for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks. Complex networks are networks with non-trivial topological features—features that do not occur in simple networks such as lattices or random graphs but often occur in real graphs. E.g.: circadian rhythms, electrochemical reactions, laser arrays, neuron networks, Josephson junction arrays..., etc. NetworkX can load, store and analyze networks, generate new networks, build network models, and draw networks. It is a computational network modelling tool and not a software tool development. The first public release of the library, which is all based on Python, was in April 2005. The library can engage with languages other than Python such as C, C++, and FORTRAN. What is NetworkX?
  • 7. python networkx library – quick start guide Although NetworkX is not ideal for large-scale problems with fast processing requirements, it is a great option for real-world network analysis: Most of the core algorithms rely on extremely fast legacy code. It uses standard graph algorithms It has an extensive set of native readable and writable formats. It is easy to install and use on major platforms with a strong online up-to-date documentation. It is Ideal for representing networks of different types like e.g. classic graphs, random graphs, and synthetic networks. It takes advantage of Python’s ability to import data from outer sources. NetworkX includes many graph generator functions and facilities to read and write graphs in many formats such as .edgelist, .adjlist, .gml, .graphml, .pajek, etc. Why NetworkX
  • 8. python networkx library – quick start guide The library is implemented as dictionary of dictionaries with a node-centric view of network based on nodes and connections between them. Nodes can be any hashable object such as a text string, an image, an XML object, another Graph, a customized node object, etc. Python’s none object should not be used as a node as it determines whether optional function arguments have been assigned in many functions. Each graph, node, and edge can hold key/value attribute pairs (e.g. weights, labels, and colours) in an associated attribute dictionary (the keys must be hashable). Edges are represented as tuples with optional edge data and can hold arbitrary data (e.g. weights, time series, etc.) NetworkX: Data Structure
  • 9. python networkx library – quick start guide Several packages and tools are available in Python for network analysis and visualization: networkX: http://networkx.lanl.gov/ PyCX Project: http://pycx.sf.net/ ComplexNetworkSim SimPy graph-tool [http://graph-tool.skewed.de/] pyGraphViz (GraphViz) [http://www.graphviz.org/] igraph [http://igraph.org/] python-graph gephi [http://gephi.org/] NetworkX was not designed as a graph drawing package. However, it provides basic drawing capabilities through matplotlib. NetworkX provides an interface to that package. For more complex visualization techniques, it is preferred to use the open source GraphViz software package Python: Visualization Packages
  • 10. python networkx library – quick start guide scipy: a Matlab-like library for Python. lots of features: linear algebra, Fourier transform, www.scipy.org numpy: www.numpy.org matplotlib: www.matplotlib.org NetworkX: Required Packages
  • 11. python networkx library – quick start guide >>> Import networkx as nx # import library >>> G = nx.Graph() # create new simple undirected graphs >>> EG = nx.empty_graph(100) # create an empty graph >>> DG = nx.DiGraph # create a simple directed graphs >>> MG = nx.MultiGraph() # create undirected with parallel edges >>> MDG = nx.MultiDiGraph() # create directed with parallel edges >>> CG = nx.complete_graph(10) # create a complete graph >>> PG = nx.path_graph(5) # create a chain of nodes >>> CBG = nx.complete_bipartite_graph(n1, n2) # create bipartite >>> GG = nx.grid_graph([10, 10, 10, 10]) # arbitrary dimensional lattice To get graph attributes >>> G.graph To convert to undirected >>> G.to_undirected() To convert to directed >>> G.to_directed() To clear a graph from nodes and edges >>> G.clear() Graph Types
  • 12. python networkx library – quick start guide To add one node at a time >>> G.add_node(1) To add a list of nodes >>> G.add_nodes_from([2, 3, 4, 5]) # takes any iterable collection To add nbunch of nodes >>> H = nx.path_graph(10) >>> G.add_nodes_from(H) To add a graph as a node >>> G.add_node(H) To print the number of nodes >>> G.number_of_nodes() To print graph nodes >>> G.nodes() To print type of nodes >>> type(G.nodes()) # it will show class list To relabel nodes >>> nx.relabel_nodes(G, mapping, copy = True) # mapping  new labels To check node membership >>> G.has_node(1) Network Nodes
  • 13. python networkx library – quick start guide To add an edge >>> G.add_edge(1, 2) To add a list of edges >>> G.add_edges_from([5, 6]) # automatically add nodes if not exist To add edges from a list >>> edge = (3, 4) >>> G.add_edge(*edge) To check edge membership >>> G.has_edge(3, 4) To print the number of edges >>> G.number_of_edges() To remove an edge >>> G.remove_edge(1, 2) To remove a list of edges >>> G.remove_edges_from([(1, 2), (3, 4)]) To print graph edges >>> G.edges() General read format >>> nx.read_format(“path/to/file.txt”,...options...) To read edge list from file >>> el = nx.read_edgelist(“test.edges”, comments = “#”) To read adjacency list from file >>> al = nx.read_adjlist(“test2.adj”) General write format >>> nx.write_format(g,“path/to/file.txt”,...options...) To write edge list >>> nx.write_edgelist(G, “newFile.edges”, comments = “#”, data = True) To print type of edges >>> type(G.edges()) # it will show class list Network Edges
  • 14. python networkx library – quick start guide To create an empty directed graph >>> DG = nx.DiGraph() # creates simple directed graphs To add weighted edges to DG >>> DG.add_weighted_edges_from([(7, 8, 2.7), (9, 10, 0.5)]) To calculate outdegree >>> DG.out_degree() To calculate outdegree with attributes included >>> DG.out_degree(with_labels = True) # Boolean should be capitalized To calculate successors >>> DG.successors(1) To calculate predecessors >>> DG.predecessors(1) To calculate neighbors >>> DG.neighbors(1) To convert directed graphs to undirected >>> DG.to_undirected() Directed Graphs
  • 15. python networkx library – quick start guide To add attributes >>> G = nx.Graph(day = “Wednesday”) To update attributes >>> G.graph[“day”] = “Thursday” To add attributes to nodes >>> G.add_node(1, time = “5am”) # attributes are optional >>> G.add_nodes_from([3], time = “2am”) To get node attributes >>> G.node[1] To add attributes to edges >>> G.add_edges_from([(11, 12), (13, 14)], color = “blue”) To get edge attributes >>> G[1][2] To get a particular attribute value >>> G[1][2][“color”] Attributed Graphs
  • 16. python networkx library – quick start guide To add weighted edges to a graph >>> G.add_edge(15, 16, weight = 3.3) >>> G.add_edge(17, 18, weight = 4.3) To calculate node degree without weight >>> G.degree(1) To calculate node degree with weight included >>> G.degree(1, weight = “weight”) To calculate all node degrees >>> G.degree(weight = “weight”) Weighted Graphs
  • 17. python networkx library – quick start guide To build a multigraph >>> MG = nx.MultiGraph() To add edges to MG >>> MG.add_weighted_edges_from([1, 2, 0.75), (1, 2, 1,25), (2, 3, 0.75)]) To calculate degrees >>> MG.degree(weight = “weight”) Multigraphs
  • 18. python networkx library – quick start guide >>> nx.subgraph(G, nbunch) # induce subgraph of G on nodes in nbunch >>> nx.union(G1, G2) # graph union >>> nx.disjoint_union(G1, G2) # graph union/all node are different >>> nx.cartesian_product(G1, G2) # return Cartesian product graph >>> nx.compose(G1, G2) # combine graphs identifying common nodes >>> nx.complement(G) # graph complement >>> nx.create_empty_graph(G) # return an empty copy of the same graph class >>> nx.convert_to_undirected(G) # return an undirected copy of G >>> nx.convert_to_directed(G) # return a directed copy of G Classic Graph Operations
  • 19. python networkx library – quick start guide Using a call to one of the classic small graphs >>> petersen = nx.petersen_graph() >>> tutte = nx.tutte_graph() >>> maze = nx.sedgewick_maze_graph() >>> tet = nx.tetrahedral_graph() Using a (constructive) generator for a classic graph >>> k_5 = nx.complete_graphs(10) >>> k_3_5 = nx.complete_bipartite_graph(3, 5) >>> barbell = nx.barbell_graph(15, 15) >>> lollipop = nx.lollipop_graph(5, 10) Using a stochastic graph generator >>> er = nx.erdos_renyi_graph(50, 0.5) >>> ws = nx.watts_strogatz_graph(20, 2, 0.5) >>> ba = nx.barabasi_albert_graph(50, 5) >>> red = nx.random_lobster(100, 0.9, 0.9) Graph Generators
  • 20. python networkx library – quick start guide Algorithms Package (networkx.algorithms) bipartite block boundary centrality (package) clique cluster components (package) core cycles dag distance measures ow (package) isolates isomorphism (package) link analysis (package) matching mixing mst operators shortest paths (package) smetric
  • 21. python networkx library – quick start guide Reading and Writing Adjacency List Multiline Adjacency List Edge List GEXF GML Pickle GraphML LEDA YAML SparseGraph6 Pajek GIS Shapefile
  • 22. python networkx library – quick start guide To find connected components >>> nx.connected_components(G) To sort nodes based on node degree >>> sorted(nx.degree(G).values()) To calculate degree of a specific node >>> G.degree(1) To calculate all degrees >>> G.degree() To see if network is connected >>> nx.is_connected(G) To calculate network global clustering coefficient >>> nx.clustering(G) To calculate the clustering coefficient of each node >>> nx.clustering(G, with_labels = True) To calculate coefficient for a particular node >>> nx.clustering(G, 1) To find the shortest path between two nodes >>> nx.shortest_path(G, 1, 3) To find the length of the shortest path between two nodes >>> nx.shortest_path_length(G, 3, 1) Basic Network Analysis (1)
  • 23. python networkx library – quick start guide To find in-degree distribution of G >>> G.in_degree() To find out-degree distribution of G >>> G.out_degree() To calculate number of nodes >>> G.order() >>> nx.number_of_nodes(G) >>> len(G) To calculate number of edges >>> G.size() >>> nx.number_of_edges(G) To find network diameter >>> nx.diameter(G) To find network radius >>> nx.radius(G) To find cores in network >>> nx.find_cores(G) Basic Network Analysis (2)
  • 24. python networkx library – quick start guide To calculate degree centrality >>> nx.degree_centrality(G) To calculate betweenness centrality >>> nx.betweenness_centrality(G) To calculate closeness centrality >>> nx.closeness_centrality(G) To calculate eigenvector centrality >>> nx.eigenvector_centrality(G) Centrality measures
  • 25. python networkx library – quick start guide >>> import matplotlib.pyplot as plt # Can use GraphViz To clear the previous graph >>> plt.clf() To draw a graph >>> nx.draw(G) >>> nx.draw_random(G) >>> nx.draw_circular(G) >>> nx.draw_spectral(G) To show the graph >>> plt.show() # to show the file To save the graph >>> plt.savefig(“myFig.png”) # save as .png file To close the file >>> plt.close() To extract the main connected component from G >>> nx.connected_component_subgraphs(G) # graph should be undirected Drawing Graphs
  • 26. python networkx library – quick start guide