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 email@example.com. 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Small World Model - Using Python Networkx
- Python | Clustering, Connectivity and other Graph properties using Networkx
- Python | Visualize graphs generated in NetworkX using Matplotlib
- Network Centrality Measures in a Graph using Networkx | Python
- Operations on Graph and Special Graphs using Networkx module | Python
- Humanize Package in Python
- Create and Access a Python Package
- How to install MySQL connector package in Python?
- Install Python package using Jupyter Notebook
- Python | Plotting Google Map using gmplot package
- Python | Plotting Google Map using folium package
- Python | Plotting Data on Google Map using pygmaps package
- Python | Adding markers to volcano locations using folium package
- Directed Graphs, Multigraphs and Visualization in Networkx
- Complex Numbers in Python | Set 1 (Introduction)