September 2018
Intermediate to advanced
288 pages
7h 38m
English
In the previous section, Graphs using the NetworkX package, we have used the networkx package to represent graphs in Python. Using this tool, the search for the shortest paths becomes extremely simple and equally rapid. Let's analyze the following code:
import networkx as nximport matplotlib.pyplot as pltG = nx.Graph()G.add_node(1)G.add_node(2)G.add_node(3)G.add_node(4)G.add_edge(1, 2, weight=2)G.add_edge(2, 3, weight=2)G.add_edge(3, 4, weight=3)G.add_edge(1, 3, weight=5)G.add_edge(2, 4, weight=6)
pos = nx.spring_layout(G, scale=3)nx.draw(G, pos,with_labels=True, font_weight='bold')edge_labels = nx.get_edge_attributes(G,'r')nx.draw_networkx_edge_labels(G, pos, labels = edge_labels)plt.show() ...
Read now
Unlock full access