Path Analysis in Networks: Understanding Connectivity with Python

Path Analysis Networks Understanding Connectivity Python
Image by Author
 

When you analyze a network, understanding how nodes connect reveals patterns about information flow, influence, and network efficiency. Path analysis shows you the routes that connect different parts of your network, helping you identify how far apart nodes are and how many alternative pathways exist between them. This tutorial builds on the Network Analysis Fundamentals by exploring path metrics that reveal how your network connects. We’ll use the same Zachary’s Karate Club dataset from our community detection tutorial, which documents friendships among 34 members of a university karate club.

Prerequisites and Setup

You’ll need NetworkX installed. This tutorial assumes you’re familiar with basic network concepts like nodes and edges covered in our fundamentals guide.

Start by loading the karate club network and confirming the basic structure.

 
import networkx as nx

# Load Zachary's Karate Club network
G = nx.karate_club_graph()

print(f"Nodes: {G.number_of_nodes()}")
print(f"Edges: {G.number_of_edges()}")

Output:

 
Nodes: 34
Edges: 78

This confirms the network has 34 members connected by 78 friendships, giving you the foundation for path analysis.

Finding the Shortest Path

A shortest path is the route between two nodes that uses the fewest edges. In social networks, this represents the minimum number of introductions needed to connect two people. In the karate club, nodes 0 and 33 represent the administrator and instructor who eventually led opposing factions when the club split.

 
source, target = 0, 33
shortest_path = nx.shortest_path(G, source=source, target=target)
path_length = nx.shortest_path_length(G, source=source, target=target)
print(f"Path: {shortest_path}")
print(f"Length: {path_length} steps")

Output:

 
Path: [0, 8, 33]
Length: 2 steps

The shortest path between the two leaders requires only 2 steps, passing through node 8. Despite being rival factions, they remain remarkably close in the social network, separated by just one intermediary.

Discovering All Shortest Paths

Networks often have multiple shortest paths of equal length between two nodes. Finding all of them reveals redundancy in your network, showing alternative routes for information flow if one path becomes unavailable.

 
source, target = 0, 33
all_paths = list(nx.all_shortest_paths(G, source=source, target=target))
for i, path in enumerate(all_paths, 1):
    print(f"  Path {i}: {path}")
print(f"Total paths: {len(all_paths)}")

Output:

 
Path 1: [0, 8, 33]
Path 2: [0, 13, 33]
Path 3: [0, 19, 33]
Path 4: [0, 31, 33]
Total paths: 4

Four different 2-step paths connect the two leaders, each passing through a different intermediary (nodes 8, 13, 19, or 31). When you call nx.shortest_path(), NetworkX returns one of these paths arbitrarily, typically the first one it finds during its search algorithm. The choice of [0, 8, 33] in the previous example doesn’t indicate it’s more important than the others. All four paths are equally short and equally valid. This redundancy means that even if one intermediary becomes unavailable, three alternative pathways remain for connecting these two key nodes.

path analysis networks

Visual showing the network with nodes 0 (green) and 33 (orange) connected by four red-highlighted paths through intermediary nodes 8, 13, 19, and 31 (yellow).

The visualization above makes this redundancy clear. The four intermediary nodes (highlighted in yellow) each provide an independent 2-step route between the administrator and instructor. This shows why the network maintains connectivity even if individual relationships change.

Measuring Network Diameter and Reach

Network diameter measures the maximum distance between any two nodes, revealing how many steps are needed to traverse the entire network. Average path length shows typical separation between nodes.

 
diameter = nx.diameter(G)
avg_path_length = nx.average_shortest_path_length(G)
print(f"Diameter: {diameter}")
print(f"Average path length: {avg_path_length:.3f}")

Output:

 
Diameter: 5
Average path length: 2.408

The diameter of 5 means the two most distant members in the club are separated by 5 friendship links. The average path length of 2.408 means most members can reach each other through about 2 to 3 intermediaries. This relatively small average suggests a well-connected social network where information can spread quickly.

Identifying Central and Peripheral Positions

Eccentricity measures the maximum distance from a node to any other node in the network. Nodes with low eccentricity are centrally positioned and can reach all other nodes quickly, while high eccentricity indicates peripheral positions.

 
eccentricity = nx.eccentricity(G)
center_nodes = nx.center(G)
periphery_nodes = nx.periphery(G)
print(f"Center nodes: {sorted(center_nodes)}")
print(f"Peripheral nodes: {sorted(periphery_nodes)}")
print(f"Node 0 eccentricity: {eccentricity[0]}")
print(f"Node 33 eccentricity: {eccentricity[33]}")

Output:

 
Center nodes: [0, 1, 2, 3, 8, 13, 19, 31]
Peripheral nodes: [14, 15, 16, 18, 20, 22, 23, 26, 29]
Node 0 eccentricity: 3
Node 33 eccentricity: 4

The administrator (node 0) has an eccentricity of 3, meaning they can reach any club member within 3 steps. The instructor (node 33) has an eccentricity of 4, placing them slightly further from some members. This positional advantage may have influenced their ability to mobilize their respective factions during the club’s split. Eight nodes form the network center, while nine occupy peripheral positions at the network’s edges.

Conclusion

Path analysis reveals how efficiently networks connect their members. Short path lengths indicate rapid information flow, while multiple shortest paths provide redundancy and resilience. The karate club’s small diameter and low average path length show a tightly connected social structure where most members are within 2 to 3 degrees of separation. These connectivity patterns help you spot strategically positioned nodes and assess how quickly changes might propagate through your network. For analyzing the groups these paths connect, check out our community detection guide.

Leave a Reply

Your email address will not be published. Required fields are marked *