A common problem where networks make an appearance is in the problem of finding the shortest – or perhaps more precisely, the highest reward – route between two nodes in a network. For instance, this could be the shortest distance between two cities, where the nodes represent the cities and the edges are roads connecting pairs of cities. In this case, the weights of the edges would be their lengths.
In this recipe, we will find the shortest path between two nodes in a network with weights.
Getting ready
For this recipe, we will need the NetworkX package imported, as usual, under the name nx, the Matplotlib pyplot module imported as plt, and a random number generator object from NumPy:
from numpy.random import default_rng
rng = default_rng(12345) # seed for reproducibility
How to do it...
Follow these steps to find the shortest path between...