NetworkX in Python
Here’s a example using $NetworkX$.
In this example, we’ll work with a directed graph (digraph) to model a transportation network, calculate advanced metrics, and visualize the network with different node and edge attributes.
Step 1: Install Necessary Libraries
If you don’t have $NetworkX$ and $Matplotlib$ installed, you can install them using pip:
1 | pip install networkx matplotlib |
Step 2: Create and Analyze the Transportation Network Graph
1 | import networkx as nx |
Explanation:
Graph Creation:
- The directed graph
Grepresents a transportation network, with nodes as cities and edges as routes between them. Each edge has attributesdistance(in kilometers) andcapacity(road capacity).
- The directed graph
Network Metrics:
- Shortest Path (Distance): The shortest path from city “A” to city “G” is calculated based on the distance attribute.
- Maximum Flow: The maximum flow value is calculated from “A” to “G” based on the road capacity.
- PageRank: A metric used to rank the importance of each city in the network.
- Eigenvector Centrality: Measures the influence of a city based on its connections.
Visualization:
- Node Sizes: Nodes are scaled by their Eigenvector Centrality.
- Edge Widths: Edges are scaled by their capacity.
- Edge Colors: Edges are colored by their distance, with a color bar representing the scale.
- Edge Labels: Distances are labeled on each edge.
Result:
Running this code will display a complex transportation network graph where node sizes represent their influence, edge widths represent road capacities, and edge colors represent distances.
You’ll also get the shortest path based on distance, the maximum flow, PageRank, and Eigenvector Centrality metrics printed out.

This graph provides a detailed and visually rich representation of the transportation network, useful for analyzing and optimizing routes.














