May 2019
Beginner
170 pages
4h 9m
English
In the previous examples, each edge is considered to have a unit weight. What that means is that the cost of traveling from one node to another through an edge is the same. This need not always be the case. In the case of a road network, each road segment is different from each other in terms of length and time (taken to traverse it). So, if we are going to represent these road segments as edges, we need to make sure that the edges have different costs or weights.
The networkx library allows us to add weight to an edge, which is demonstrated in the code, as follows:
import networkx as nx#Create a weighted graphG=nx.DiGraph()G.add_edge('A','B',weight=6)G.add_edge('A','C',weight=2)G.add_edge('C','D',weight=4.5)G.add_edge('C','E',weight=5) ...Read now
Unlock full access