Diameter and mean shortest path

The size of a network can be quantified in several ways. In Chapter 5, The Small Scale – Nodes and Centrality, the distance between two nodes was defined as the length of the shortest path between them. With a way to measure distance, it becomes possible to define size based on that distance.

NetworkX provides several convenient functions for finding distances and shortest paths. The shortest paths between two particular nodes can be found using the all_shortest_paths() function:

list(nx.all_shortest_paths(G_karate, mr_hi, john_a))[[0, 8, 33], [0, 13, 33], [0, 19, 33], [0, 31, 33]]

If you only need to know the distance, the shortest_path_length() function will provide that:

nx.shortest_path_length(G_karate, ...

Get Network Science with Python and NetworkX Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.