![]() |
VOOZH | about |
NetworkX is a Python library for creating, analyzing and visualizing complex networks. It models real-world systems as graphs, where nodes represent entities and edges represent relationships. The library supports loading, storing and generating various types of graphs, analyzing network structures, designing algorithms and drawing networks with ease and flexibility.
To install the latest version of NetworkX, simply run:
pip install networkx
NetworkX supports several types of graphs:
Letβs begin with a basic undirected graph:
You can add nodes one at a time or in batches:
Output:
π Creating NodesAfter this step, your graph G has 6 nodes.
Edges connect the nodes in your graph:
Output:
π Creating EdgesNote: Since it's an undirected graph, the edge (1, 2) is the same as (2, 1).
You can remove nodes and edges just as easily:
Output:
π Removing Nodes and EdgeNetworkX allows you to perform deep analysis of your graph:
Output
[1, 2, 4, 7, 9]
[(1, 4), (1, 9), (1, 7), (2, 4), (2, 9)]
5
5
2
[4, 9]
Explanation: