NetworkX is a Python language software package for the creation, manipulation, and study of the structure, dynamics, and function of complex networks. It is used to study large complex networks represented in form of graphs with nodes and edges. Using networkx we can load and store complex networks. We can generate many types of random and classic networks, analyze network structure, build network models, design new network algorithms and draw networks.
Installation of the package:
pip install networkx
Add one node at a time:
Add a list of nodes:
Let us create nodes in the graph G. After adding nodes 1, 2, 3, 4, 7, 9
Adding one edge at a time:
G.add_edge(1,2) G.add_edge(3,1) G.add_edge(2,4) G.add_edge(4,1) G.add_edge(9,1)
Adding a list of edges:
After adding edges (1,2), (3,1), (2,4), (4,1), (9,1), (1,7), (2,9)
Removing Nodes and Edges
One can demolish the graph using any of these functions:
Graph.remove_node(), Graph.remove_nodes_from(), Graph.remove_edge() and Graph.remove_edges_from()
After removing node 3
After removing edge (1,2)
#1 [1, 2, 3, 4, 7, 9] #2 [(1, 9), (1, 2), (1, 3), (1, 4), (1, 7), (2, 4), (2, 9)] #3 [1, 2, 4, 7, 9] #4 [(1, 9), (1, 4), (1, 7), (2, 4), (2, 9)] #5 5 #6 5 #7 2 #8 [4, 9]
In the next post, we’ll be discussing how to create weighted graphs, directed graphs, multi graphs. How to draw graphs. In later posts we’ll see how to use inbuilt functions like Depth fist search aka dfs, breadth first search aka BFS, dijkstra’s shortest path algorithm.
Reference : Networxx at Github
This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Introduction to Social Networks using NetworkX in Python
- Python | Visualize graphs generated in NetworkX using Matplotlib
- Operations on Graph and Special Graphs using Networkx module | Python
- Python | Clustering, Connectivity and other Graph properties using Networkx
- Network Centrality Measures in a Graph using Networkx | Python
- Small World Model - Using Python Networkx
- Directed Graphs, Multigraphs and Visualization in Networkx
- Link Prediction - Predict edges in a network using Networkx
- Signed Networks in Social Networks
- Python | Plotting Google Map using gmplot package
- Create and Access a Python Package
- Python | Plotting Google Map using folium package
- Python | Adding markers to volcano locations using folium package
- Install Python package using Jupyter Notebook
- How to install MySQL connector package in Python?
- Humanize Package in Python
- What is the difference between Python's Module, Package and Library?
- Python - Database Manager (dbm) package
- Snakebite Python Package For Hadoop HDFS
- Plotting Data on Google Map using Python's pygmaps package