,可以通过以下步骤实现:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()
G.add_nodes_from([1, 2, 3, 4, 5])
G.add_edges_from([(1, 2), (2, 3), (3, 4), (4, 5)])
G.add_edges_from([(1, 3), (2, 4), (3, 5)])
edge_labels = {e: str(i) for i, e in enumerate(G.edges())}
node_colors = ['lightblue' if node in [1, 5] else 'lightgray' for node in G.nodes()]
edge_colors = ['red' if edge in [(1, 2), (2, 3), (3, 4), (4, 5)] else 'blue' for edge in G.edges()]
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, node_color=node_colors)
nx.draw_networkx_edges(G, pos, edge_color=edge_colors)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
plt.axis('off')
plt.show()
这样就可以在NetworkX中显示多个长度相等的路径。其中,节点颜色用于区分起点和终点,边的颜色用于区分不同的路径,边的标签用于显示路径的编号。
关于NetworkX的更多信息和使用方法,可以参考腾讯云的相关产品介绍链接地址:NetworkX产品介绍。
领取专属 10元无门槛券
手把手带您无忧上云